Professional Documents
Culture Documents
© 2004 Goodrich, Tamassia Linked Lists 1
© 2004 Goodrich, Tamassia Linked Lists 1
© 2004 Goodrich, Tamassia Linked Lists 1
Linked Lists
Linked Lists
slow searching in unordered array slow insertion in ordered array Fixed size
Linked lists solve some of these problems. Linked lists are general purpose storage data structures.
Linked Lists 3
Cont
Array Static Data Structure Once the memory is allocated, it cannot be extended any more.
Linked List Dynamic Data Structure The amount of memory required can be varied during its use.
2004 Goodrich, Tamassia
Linked Lists
Linked List
The adjacency between the elements is maintained by means of links and pointers.
A link or pointer actually us the address of the subsequent element. Thus, in the LL, data (actual content) and link (to point to the next data) both are required to be maintained.
2004 Goodrich, Tamassia
Linked Lists
Node
An element in a linked list is a specially termed as node. A node consists of two fields. DATA to store the actual information LINK to point to the next node
Linked Lists
Linked Lists
Memory Representation of LL
Two ways 1) Static representation using array 2) Dynamic representation using free pool of storage. In Static representation of a single linked list, two arrays are maintained: 1) Array of Data 2) Array of Links
2004 Goodrich, Tamassia
Linked Lists
Static Representation of LL
Linked Lists
Linked Lists
10
Dynamic Representation
The efficient way is using free pool of storage. There is a memory bank (free memory spaces) and memory manager (program). During the creation of a linked list, whenever a node is required the request is placed to the memory manager. Then it searches the memory bank for the block requested and if found, grants the desired block to the caller.
2004 Goodrich, Tamassia
Linked Lists
11
Dynamic Representation
Garbage collector: It plays whenever a node is no more in use; it returns the unused node to the memory bank. Note: Memory bank is basically a list of memory spaces which is available to a programmer. Such a memory management is known as dynamic memory management.
2004 Goodrich, Tamassia
Linked Lists
12
Linked Lists
13
next
elem
node
A
2004 Goodrich, Tamassia
C
Linked Lists
D
14
Operations on a Single LL
Traversing the list Inserting a node into the list Deleting a node from the list Copying the list to make a duplicate of it Searching for an element in the list Merging the linked list with another one to make a larger list.
2004 Goodrich, Tamassia
Linked Lists
15
Linked Lists
16
Linked Lists
17
Linked Lists
18
Linked Lists
19
INSERT(X,FIRST)
Given X New element FIRST a pointer to the first element of a linked linear list whose typical node contains INFO and LINK fields. AVAIL pointer to the first element of the availability stack. NEW is a temporary variable
Linked Lists 20
INSERT(X,FIRST)
1. [Underflow?]
if AVAIL = NULL then Write(AVAILABILITY STACK UNDERFLOW) Return(FIRST) 2. [Obtain address of next free node] NEW <- AVAIL 3. [Remove free node from availability stack] AVAIL <- LINK(AVAIL)
2004 Goodrich, Tamassia
Linked Lists
21
INSERT(X,FIRST)
4. [Initiate fields of new node and its link to the list] INFO(NEW) <- X LINK(NEW) <- FIRST 5. [Return address of new node] Return(NEW).
Linked Lists
22
Linked Lists
23
5.
Delete a node
Given X and FIRST, pointer variables whose values denote the address of a node in a linked list and the address of the first node in the linked list, respectively, this procedures deletes the node whose address is given by X. Temp is used to find the desired node PRED - keeps track of the predecessor of TEMP.
Delete a node
6. [End of the list?] If TEMP X Then Write (Node Not Found) Return 7. [Delete X] If X = FIRST (Is X the first node?) Then FIRST <- LINK (FIRST) Else LINK (PRED) <- LINK(X) 8. [Return node to availability area] LINK (X) <- AVAIL AVAIL <- X Return
2004 Goodrich, Tamassia
COPY (FIRST)
A typical node in the given list contains of INFO and LINK fields; the new list is to contain nodes whose information and pointer fields are denoted by FIELD and PTR, respectively.
The address of the first node in the newly created list is to be placed in BEGIN. NEW, SAVE, and PRED are pointer variables.
COPY (FIRST)
1. [Empty list?] If FIRST = NULL Then Return (NULL) 2. [Copy first node] If AVAIL = NULL Then Write (Availability Stack Underflow) Return (0) Else NEW <- AVAIL AVAIL <- LINK (AVAIL) FIELD (NEW) <- INFO (FIRST) BEGIN <- NEW
2004 Goodrich, Tamassia
COPY (FIRST)
3. [Initialize traversal] SAVE <- FIRST 4. [Move to next node if not at end of list] Repeat thru step 6 while LINK (SAVE) NULL 5. [Update predecessor and save pointers] PRED <- NEW SAVE <- LINK (SAVE) 6. [Copy node] If AVAIL = NULL Then Write (Availability Stack Underflow) Return (0) Else NEW <- AVAIL AVAIL <- LINK (AVAIL)
2004 Goodrich, Tamassia
COPY (FIRST)
FIELD (NEW) <- INFO (SAVE) PTR (PRED) <- NEW 7. [Set link of last node and return] PTR (NEW) <- NULL Return (BEGIN)
prev
next
Node
node
elements
2004 Goodrich, Tamassia
Linked Lists
41
DOUBLY- INSERTION
DOUBLY- INSERTION
DOUBLY- INSERTION
DOUBLY- DELETION
DOUBLY- DELETION