© 2004 Goodrich, Tamassia Linked Lists 1

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 48

Linked Lists

2004 Goodrich, Tamassia

Linked Lists

Arrays: pluses and minuses


+ Fast element access. -- Impossible to resize.

Many applications require resizing! Required size not always immediately


available.

2004 Goodrich, Tamassia

Linked Lists

Why Linked List?


Disadvantages of arrays as storage data structures:

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

2004 Goodrich, Tamassia

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

2004 Goodrich, Tamassia

Linked Lists

Linked List - Definition


A LL is an ordered collection of finite, homogeneous data elements called nodes where the linear order is maintained by means of links or pointers.
In a LL representation, data is stored at random locations and the current data location provides the information regarding the location of the next data.
2004 Goodrich, Tamassia

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

2004 Goodrich, Tamassia

Linked Lists

Static Representation of singly LL using Array

2004 Goodrich, Tamassia

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

Types of linked lists


Singly Linked List Circular Singly Linked List Doubly Linked List Circular Doubly Linked List

2004 Goodrich, Tamassia

Linked Lists

13

Singly Linked Lists


A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores

next

element link to the next node

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

TASK FOR OBTAINING THE NODE


The address of the next availability node is to be stored in the variable NEW. The availability stack contains only a finite number of nodes; therefore it must check for underflow condition. This condition is signaled by the value of AVAIL being NULL.

2004 Goodrich, Tamassia

Linked Lists

16

TASK FOR OBTAINING THE NODE


If a node is available, then the new topmost element of the stack is denoted by LINK(AVAIL). The fields of the node corresponding to the pointer value of NEW can now be filled in and the field LINK(NEW) is set to a value which designates the successor node of this new node.
2004 Goodrich, Tamassia

Linked Lists

17

AVAILABILITY STACK obtaining a node from it

2004 Goodrich, Tamassia

Linked Lists

18

AVAILABILITY STACK a free node

2004 Goodrich, Tamassia

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

2004 Goodrich, Tamassia

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).

2004 Goodrich, Tamassia

Linked Lists

22

Inserting at the Head


1. Allocate a new
node 2. Insert new element 3. Make new node point to old head 4. Update head to point to new node

2004 Goodrich, Tamassia

Linked Lists

23

Insert at the End


INSEND(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

Insert at the End


4.
[Initialize field of new node] INFO (NEW) <- X LINK (NEW) <- NULL 5. [Is the list is empty?] If FIRST = NULL Then Return (NEW) 6. [Initiate search for the last node] SAVE <- FIRST 7. [Search for end of list] Repeat while LINK (SAVE) NULL SAVE <- LINK (SAVE)
2004 Goodrich, Tamassia

Insert at the End


8. [Set LINK field of last node to NEW]
LINK (SAVE) <- NEW 9. [Return first node pointer] Return (FIRST)

2004 Goodrich, Tamassia

Inserting at the Tail


1. Allocate a new 2. 3. 4.
node Insert new element Have new node point to null Have old last node point to new node Update tail to point to new node
Linked Lists 27

5.

2004 Goodrich, Tamassia

Insert in sorted List


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) 4. [Copy information contents into new node] INFO (NEW) <- X
2004 Goodrich, Tamassia

Insert in sorted List


5. [Is the list empty?] If FIRST = NULL then LINK (NEW) <- NULL Return (NEW) 6. [Does the new node precede all others in the list?] If INFO (NEW) <= INFO (FIRST) Then LINK (NEW) <- FIRST Return (NEW) 7. [Initiate search for the last node] SAVE <- FIRST

2004 Goodrich, Tamassia

Insert in sorted List


8. [Search for predecessor of new node] Repeat while LINK (SAVE) NULL and INFO (LINK (SAVE)) <= INFO (NEW) SAVE <- LINK (SAVE) 9. [Set link fields of new node and its predecessor] LINK (NEW) <- LINK (SAVE) 10.[Return first node pointer] Return (FIRST)

2004 Goodrich, Tamassia

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.

2004 Goodrich, Tamassia

Delete a node - DELETE (X, FIRST)


1. [Empty List?] If FIRST = NULL Then Write (Underflow) 2. [Initialize search for X] TEMP<- FIRST 3. [Find X] Repeat thru step 5 while Temp X and LINK (TEMP) NULL 4. [Update predecessor marker] PRED <- TEMP 5. [Move to next node] TEMP <- LINK (TEMP)
2004 Goodrich, Tamassia

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.

2004 Goodrich, Tamassia

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)

2004 Goodrich, Tamassia

Circularly Linked Linear Lists


Replacing the null pointer in the last node of a list with address of its first node. Such a list is called a circularly liked linear list or simply circular list. Advantage over singly linked list Every node in a linked list is accessible form a given node. That is from this given node, all nodes can be reached by chaining through the list. Disadvantage in using circular list Without some care in processing, it is possible to get into an infinite loop.

2004 Goodrich, Tamassia

Circularly Linked Linear Lists


A circularly linked linear list

A circularly linked linear list with a head node

An empty list is represented by having LINK(HEAD) = HEAD


2004 Goodrich, Tamassia

Circularly Linked Linear Lists


Inserting a node at the head of a circular list with a list head consists of the following steps: NEW<- NODE INFO (NEW) <- Y LINK(NEW) <- LINK(HEAD) LINK(HEAD) <- NEW

2004 Goodrich, Tamassia

Doubly Linked List


A doubly linked list is often more convenient! Nodes store:

prev

next

Special trailer and header nodes


header nodes/positions trailer

element link to the previous node link to the next node

Node

node

elements
2004 Goodrich, Tamassia

Linked Lists

41

DOUBLY- INSERTION

2004 Goodrich, Tamassia

DOUBLY- INSERTION (LEFT)

2004 Goodrich, Tamassia

DOUBLY- INSERTION (MIDDLE)

2004 Goodrich, Tamassia

DOUBLY- INSERTION

2004 Goodrich, Tamassia

DOUBLY- INSERTION

2004 Goodrich, Tamassia

DOUBLY- DELETION

2004 Goodrich, Tamassia

DOUBLY- DELETION

2004 Goodrich, Tamassia

You might also like