2. 2
Need of Dynamic Data Structures
• Linked Lists are dynamic data structures: They can grow
or shrink during the execution of a program
• Efficient memory utilization: Memory is allocated
whenever it is required and deallocated when it is no
longer needed
• Insertion and Deletion are easier & efficient: It provides
flexibility in inserting a data item at a specified position
and deletion of a data item from the given position
• Many complex applications can be easily carried out with
linked list
3. 3
Continuous Implementation of Lists
• It can be done with help of continuous memory
allocation data structure i.e. Arrays
• For the purpose of implementing lists using arrays we
are required to create a structure of two elements.
1. Info
2. Next
And then create the array (variable) of structure.
e.g. struct node
{ int info,next;};
struct node n[100];
4. 4
A linked list node
• Each node contains a data item and a pointer to the next
node
• The last node has no next node
• An external pointer gives access to the first node
data next next
first
5. 5
Linked list notation
. . . . . .
p
node(p) Predecessor node(p) node(p) Successor
info(p) next(p)
6. 6
Accessing nodes in a linked list
• No direct access to individual nodes
• First node accessed via external pointer
• Other nodes accessed via pointer in previous
node
• It is a sequential access data structure
first
8. 8
TRAVERSING A LINKED LIST
1. Set PTR = START
2. Repeat steps 3 and 4 while PTR!=NULL
3. Apply PROCESS to INFO[PTR]
4. Set PTR=LINK[PTR]
5. Exit
9. 9
Types of Linked Lists
1. Singly linked list
• Begins with a pointer to the first node
• Terminates with a null pointer
• Only traversed in one direction
2. Circular, singly linked
• Pointer in the last node points back to the first node
3. Doubly linked list
• Two “start pointers” – first element and last element
• Each node has a forward pointer and a backward pointer
• Allows traversals both forwards and backwards
4. Circular, doubly linked list
• Forward pointer of the last node points to the first node and
backward pointer of the first node points to the last node
10. 10
Singly Linked List
• A singly linked list is a concrete
data structure consisting of a
sequence of nodes
• Each node stores
– element
– link to the next node
next
elem node
A B C D
11. 11
Adding an item
• Three steps:
– find node after which to add
– Allocate space for a node and store data in it
– Link the new node into the existing linked list
• Two cases for linking the node into the existing
linked list
– Adding an item at the beginning of the list
– Adding an item after an existing node
12. 12
Inserting at the Beginning
NULLfirst 'A' 'B'
p
Initial List
1. Create a new node.
'C'p
2. Fill the new node with data.
'A' 'B'
3. Make the new node point to the first node.
4. Make first point to the new node .
'A' 'B'
first
'C'p
'C'p
NULL
NULL
first
13. 13
Inserting at the Beginning (Algo.)
INSERT(INFO,LINK,START,AVAIL,ITEM)
1. IF AVAIL=NULL then Write OVERFLOW and EXIT.
2. NEW=AVAIL and AVAIL=LINK[AVAIL]
3. INFO[NEW]=ITEM
4. LINK[NEW]=START
5. Set START=NEW
6. EXIT.
14. 14
Inserting after a given NodeINSERTLOC(INFO,LINK,START,AVAIL,ITEM)
1. IF AVAIL=NULL then Write OVERFLOW and Exit
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. If LOC=NULL then
Set LINK[NEW]= START and START=NEW
Else
Set LINK[NEW]=LINK[LOC] and LINK[LOC]=NEW
5. Exit.
15. 15
Inserting into Sorted List
FIND(INFO, LINK, START, ITEM, LOC)
1. If START =NULL then Set LOC=NULL and Return.
2. If ITEM<INFO[START] then Set LOC=NULL and Return
3. Set SAVE=START and PTR=LINK[START].
4. Repeat Step 5 AND 6 While PTR!= NULL.
5. If ITEM<INFO[PTR] then Set LOC=SAVE and Return.
6. Set SAVE=PTR and PTR = LINK[PTR]
7. Set LOC= SAVE.
8. Return.
INSERT(INFO,LINK,START,AVAIL,ITEM)
1. Call FIND(INFO, LINK, START. ITEM, LOC)
2. Call INSERTLOC(INFO,LINK,START,AVAIL,ITEM)
16. 16
Deleting the Node Following a given Node
DEL(INFO,LINK,START,AVAIL,LOC,LOCP)
1. IF LOCP=NULL, THEN, Set START=LINK[START]
ELSE Set LINK[LOCP]=LINK[LOC]
2. Set LINK[LOC]=AVAIL and AVAIL=LOC
3. Exit.
Here, LOCP is Predecessor of Location.
17. 17
Header linked lists
• Header linked list is a linked list which always
contains a special node called the Header Node, at
the beginning of the list.
Header linked list
Grounded Header linked list Circular Header linked list
18. 18
• Grounded Header linked lists
– The last node contains null pointer.
A
Head
B C X
19. 19
• Circular linked lists
– The last node points to the first node of the
list
– How do we know when we have finished
traversing the list?
A
Head
B C
20. 20
Comparison between Array & Dynamic
Implementation of Linked List
ARRAY LINKED LIST
1. Linear collection of data
elements
1. Linear collection of nodes
2. Consecutive memory
allocation
2. Not Consecutive memory
allocation
3. Allow Sequential & Random
Access of data
3. Allow only Sequential Access
of data
4. Insertions / Deletions are not
efficient
4. Insertions / Deletions are
efficient with a constant time
5. Can’t add any number of
elements at run time
5. Can add any number of
elements in the list at run time
6. No extra cost of saving
address of next element
6. Extra cost of saving address
of next element in each node
21. 21
Circular Lists
• The last node contains pointer to the first
node of the list
• We can begin at any node and traverse the list
until we reach the same node where we
started
A
START
B C
22. 22
Inserting an element in the beginning of a
Circular List
1. If AVAIL==NULL then Write Overflow and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. INFO[NEW]=ITEM
4. Set PTR=START
5. Repeat Step 6 while LINK[PTR]!=START
6. PTR=LINK[PTR]
7. Set LINK[NEW]=START
8. Set LINK[PTR]=NEW
9. Set START=NEW
10. Exit
23. 23
Inserting an element at the end of a Circular
List
1. If AVAIL==NULL then Write Overflow and Return
2. Set NEW=AVAIL and AVAIL=LINK[AVAIL]
3. Set INFO[NEW]=ITEM
4. Set LINK[NEW]=START
5. Set PTR=START
6. Repeat Step 7 while LINK[PTR]!=START
7. PTR=LINK[PTR]
8. Set LINK[PTR]=NEW
9. Exit
24. 24
Deleting the First node from Circular List
1. If START==NULL then Write Underflow and
Return
2. Set PTR=START
3. Repeat STEP 4 while LINK[PTR]!=START
4. Set PTR=LINK[PTR]
5. Set LINK[PTR]=LINK[START]
6. LINK[START]=AVAIL and AVAIL=START
7. START=LINK[PTR]
8. Exit
25. 25
Deleting the Last node from Circular List
1. If START==NULL then Write Underflow and
Return
2. Set PTR=START
3. Repeat STEP 4 while LINK[PTR]!=START
4. Set SAVE=PTR and PTR=LINK[PTR]
5. Set LINK[SAVE]=START
6. LINK[PTR]=AVAIL and AVAIL=PTR
7. Exit
26. 26
Doubly Linked List
• A Doubly linked list is a two way linked list, where
each node N is divided into three parts:
1. An information field INFO which contains the data
of N
2. A pointer FORW which contains the location of the
next node in the list
3. A pointer field BACK which conatins the location of
the preceding node in the list
4. The BACK pointer of first node and the FORW
pointer of the last node will contain NULL