1. Trang chủ
  2. » Công Nghệ Thông Tin

Insert Node to a Linked List

42 487 1
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 42
Dung lượng 408,82 KB

Nội dung

Insert Node to a Linked List Allocate memory for the new node and set up data Locate the pointer p in the list, which will point to the new node:  If the new node becomes the first element in the List: p is head head … pNew x  Otherwise: p is pPre->link, where pPre points to the predecessor of the new node pNew x head pPre … 30 Insert Node to a Linked List (cont.) Update pointers: – – Point the new node to its successor Point the pointer p to the new node head X pNew->link = head head= pNew (1) (2) … (2) pNew (1) X (2) head pNew X (1) pNew->link = pPre->link (1) pPre->link = pNew (2) pPre X … 31 Insert Node to a Linked List (cont.)  Insertion is successful when allocation memory for the new node is successful 32 Insert Node to a Linked List (cont.)  There is no difference between  insertion in the middle (a) and insertion at the end of the list (b) (2) (a) head pNew X pNew->link = pPre->link (1) pPre->link = pNew (2) pPre X (b) (1) … pNew x head pPre … 33 Insert Node to a Linked List (cont.)  There is no difference between  insertion at the beginning of the list (a) and insertion to an empty list (b) head X … (a) (2) pNew (1) X (b) pNew->link = head (1) head= pNew (2) head pNew head pNew 34 Insert Algorithm Insert (val DataIn ) // For ordered list Inserts a new node in a singly linked list Pre DataIn contains data to be inserted Post If list is not full, DataIn has been inserted; otherwise, list remains unchanged Return success or overflow 35 InsertNode Algorithm (cont.) Insert (val DataIn ) Allocate pNew if (memory overflow) return overflow else pNew->data = DataIn Locate pPre // pPre remains NULL if Insertion at the beginning or to an empty list if (pPre = NULL) // Adding at the beginning or to an empty list pNew->link = head head = pNew else // Adding in the middle or at the end of the list pNew->link = pPre->link pPre->link = pNew return success end Insert 36 Remove Node from a Linked List Locate the pointer p in the list which points to the node to be deleted (pDel will hold the node to be deleted)  If that node is the first element in the List: p is head head pDel …  Otherwise: p is pPre->link, where pPre points to the predecessor of the node to be deleted head pPre pDel … 37 Remove Node from a Linked List (cont.) Update pointers: p points to the successor of the node to be deleted head = pDel->link head X Recycle pDel pDel … pPre->link = pDel->link head pDel pPre Recycle pDel X … Recycle the memory of the deleted node 38 Remove Node from a Linked List (cont.)  Removal is successful when the node to be deleted is found 39 ... head (1) head= pNew (2) head pNew head pNew 34 Insert Algorithm Insert (val DataIn ) // For ordered list Inserts a new node in a singly linked list Pre DataIn contains data... (ref DataOut ) Removes a node from a singly linked list Pre DataOut contains the key need to be removed Post If the key is found, DataOut will contain the data corresponding to it, and... field 44 Search Algorithm for Auxiliary Function in Class • Public method Search of List ADT: Search (ref DataOut ) Can not return a pointer to a node if found • Auxiliary function

Ngày đăng: 05/10/2013, 08:20

HÌNH ẢNH LIÊN QUAN

• Sẽ bổ sung các hình minh họa các complex linked list (chỉ hình ảnh minh họa mà thôi) - Insert Node to a Linked List
b ổ sung các hình minh họa các complex linked list (chỉ hình ảnh minh họa mà thôi) (Trang 33)

TỪ KHÓA LIÊN QUAN

w