Unit - 3 Linked List: Linear Data Structure
Unit - 3 Linked List: Linear Data Structure
NULL
FIRST
Unit - 3
Linked List
Linear Data Structure
Prepared By:
Ms. Purvi Tandel, Chandni Naik
Topics of Unit 3
Queue:
Representation Of Queue
Operations On Queue
Circular Queue
Priority Queue
Array representation of Priority Queue
Double Ended Queue
Applications of Queue
Linked List:
Singly Linked List
Doubly Linked list
Circular linked list
Linked implementation of Stack
Linked implementation of Queue
Applications of linked list
Classification of Data Structure
Data
Structure
Non-Linear
Linear Data
Data
Structure
Structure
Sequential Linked
Hierarchical Network
Organizatio Organizatio
Relationship Relationship
n n
Advantages: Disadvantages:
• Array is very simple to use and easy • Array is static in nature. Memory is
to create. allocated at compile time.
Advantages: Disadvantages:
• It is a dynamic data structure therefore • Lots of overhead in code (lots of
the size of the linked list can grow or malloc calls & assigning pointers).
shrink in size during execution of the
programme.
• Must traverse entire list to go to the
nth node.
• Programmer need not to worry about
how many element will be stored in the
linked list. • It consume extra space to contain
the address of the next item in the
list, it is time consuming also.
• Each element is allocated with memory
as it is added to the list.
Pros & Cons of Linked Allocation (operation-wise)
Pros
Insertion Operation
• we have an n elements in list and it is required to insert a new element between the
first and second element, what to do with sequential allocation & linked allocation?
• Insertion operation is more efficient in Linked allocation.
X 1000
2100
Continue..
Deletion Operation
• Deletion operation is more efficient in Linked Allocation
FIRST S8 NULL
Example: Suppose, I want to store S8 then memory address Memory Representation 400
with 400 is used and AVAIL points to next free block cell. Address Roll No Next
• The OS does this task of adding the free memory to the
FIRST 100 S1 200
free pool.
200 S2 300
• This task performed by OS when it finds the CPU idle or
the program are falling short of memory space. 300 S3 500
• The OS scans through all the memory cells & marks those AVAIL 400 S8 -1(NULL)
600
cells that are being used by some other program. 500 S4 700
• Then, it collects all the cells which are not being used & AVAIL 600 900
adds their address to free pool, this process is called 700 S5 800
garbage collection. 800 S6 1000
900 -1(NULL)
1000 S7 400
-1(NULL)
Operations & Type of Linked List
Operations on Linked List Types of Linked List
Insert Singly Linked List
• Insert at first position Circular Linked List
• Insert at last position Doubly Linked List
• Insert into ordered list
Delete
Traverse list (Print list)
Copy linked list
Singly Linked List
Node
of Node
Node Info (Node)
Link (Node)
Data Pointer to
Next Node
struct node
C Structure {
to represent int info;
struct node *link;
a node };
Algorithms for singly linked list
1. Insert at first position
2. Insert at last position
3. Insert in Ordered Linked list
4. Delete Element
5. Copy Linked List
Availability Stack
• A pool or list of free nodes, which we refer to as the availability stack is maintained in
conjunction with linked allocation.
• Whenever a node is to be inserted in a list, a free node is taken from the availability
stack and linked to the new list.
• On other end, the deleted node from the list is added to the availability stack.
FIRST
22
NEW
Function: INSORD(X, FIRST)
1. [Underflow?] 6. [Does the new node precede
IF AVAIL = NULL all other node in the list?]
THEN Write (“Availability IF INFO(NEW) ≤ INFO (FIRST)
Stack Underflow”) THEN LINK (NEW) FIRST
Return(FIRST) Return (NEW)
2. [Obtain address of 7. [Initialize temporary pointer]
next free Node] SAVE FIRST
NEW AVAIL 8. [Search for predecessor of
3. [Remove free node from new node]
availability Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
4. [Initialize fields of SAVE LINK (SAVE)
new node] 9. [Set link field of NEW node
INFO(NEW) X and its predecessor]
5. [Is the list empty?] LINK (NEW) LINK (SAVE)
IF FIRST = NULL LINK (SAVE) NEW
THEN LINK(NEW) NULL 10. [Return first node pointer]
Return (NEW) Return (FIRST)
Function: INSORD(X, FIRST) 1. [Underflow?]
IF AVAIL = NULL
6. [Does the new node precede
all other node in the list?]
FIRST = INSORD(X, FIRST) THEN Write (“Availability IF INFO(NEW) ≤ INFO (FIRST)
FIRST=INSORD(3, 100) Availability Stack Stack Underflow”) THEN LINK (NEW) FIRST
AVAIL Return(FIRST) Return (NEW)
200 800 2. [Obtain address of 7. [Initialize temporary pointer]
NEW
next free Node] SAVE FIRST
AVAIL NEW AVAIL 8. [Search for predecessor of
300 200 3. [Remove free node from new node]
availability Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
Null 300 4. [Initialize fields of SAVE LINK (SAVE)
new node] 9. [Set link field of NEW node
INFO(NEW) X and its predecessor]
5. [Is the list empty?] LINK (NEW) LINK (SAVE)
IF FIRST = NULL LINK (SAVE) NEW
THEN LINK(NEW) NULL 10. [Return first node pointer]
Return (NEW) Return (FIRST)
NEW
5 10 15 20 25 30
FIRST
Procedure: DELETE( X, FIRST)
1. [Is Empty list?] 6. [End of the list?]
IF FIRST = NULL If SAVE ≠ X
THEN write (‘Underflow’) THEN write (‘Node not found’)
Return Return
SAVE
5 400 10 600
500 15 600 20 200 22 700 25 900 30
100 400 500 600 200 700 900
FIRST
Function: TRAVERSE(FIRST) 1. [Empty List?]
IF FIRST = NULL
Then Write (“LINKED
LIST EMPTY”)
Return
10 500 15 600 20 700 25 900 30 2. [store the address of
400 500 600 700 900 first Node]
FIRST temp temp temp temp FIRST
temp temp
3. [Repeat while loop ]
Repeat while temp!=NULL
print(INFO(temp))
10 15 20 25 30
temp LINK (temp)
NULL
Function: COUNT_NODES(FIRST)
• This function counts number of nodes of the linked list and returns
COUNT.
• FIRST is a pointer to the first element of a Singly linked linear list.
• Typical node contains INFO and LINK fields.
• SAVE is a Temporary pointer variable.
Function: COUNT_NODES(FIRST)
1. [Is list Empty?]
IF FIRST = NULL
Then COUNT 0
Return(COUNT)
4. [Return Count]
Return (COUNT)
Function: COPY (FIRST)
• This function Copy a Link List and creates new Linked List
• This function returns address of first node of newly created linked list.
• 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
• FIRST is a pointer to the first element of a Singly linked linear list.
• Typical node contains INFO and LINK fields.
• AVAIL is a pointer to the top element of the availability stack.
• NEW, SAVE and PRED are temporary pointer variables.
Function: COPY (FIRST) 6. [Copy Node]
IF AVAIL = NULL
1. [Is Empty list?]
IF FIRST = NULL
FIRST SAVE SAVE SAVE SAVE THEN write THEN Return(NULL)
(‘Underflow’) 2. [Copy first node]
5 600 10 700 15 800 30 Return (NULL) IF AVAIL = NULL
ELSE NEW AVAIL THEN write (‘Underflow’)
500 600 700 800
AVAIL Return (NULL)
BEGIN PRED LINK(AVAIL) ELSE NEWAVAIL
PRED PRED
AVAILLINK(AVAIL)
FIELD(NEW)INFO(SAVE)
5 300 15 500 30 NULL PTR(PRED)NEW FIELD(NEW)INFO(FIRST)
10 400
BEGIN NEW
NEW 200 NEW 300 NEW 400 NEW 500 7. [Set link of last3. [Initialize Traversal]
node and SAVE FIRST
return] 4. [Move the next node if
PTR(NEW) NULL not at
AVAIL Return(BEGIN) the end if list]
300 200
NEW Repeat thru step 6
while LINK(SAVE) ≠ NULL
AVAIL 300 5. [Update predecessor and
400 save pointer]
NEW
Availability Stack PREDNEW
AVAIL 400 SAVELINK(SAVE)
500
NEW
5 10 15 20 25 30
FIRST
5 10 15 20 25 30
FIRST
Circularly Linked Linear List Cont…
• Applications of Circular List
• When we are surfing the net, we can use the back button & the forward button
to move to the previous pages that we have already visited. A circular linked list
is used to maintain the sequence of web page visited. Traversing this circular
linked list either in forward or backward direction helps to revisit the pages again
using back & forward buttons.
Operations on Circular List
• Insert at First
• Insert at Last
• Delete a node from front
Procedure: CINSERT( X,FIRST)
• This procedure inserts a new node at the first position of Circular linked
list.
• X is a new element to be inserted.
• FIRST is a pointer to the first elements of a Circular linked linear list.
• Typical node contains INFO and LINK fields.
• NEW and PTR is a temporary pointer variable.
1. [UnderFlow?]
CINSERT( X,FIRST) IF AVAIL = NULL
THEN write (‘ Availability stack
FIRST=INSERT(X, FIRST) AVAIL 300 200 Underflow’)
NEW Return(FIRST)
INSERT(10, NULL) 2. [Obtain address of next free node ]
INSERT(20, 200) AVAIL 300 NEWAVAIL
400
INSERT(30, 300) NEW 3. [Remove free node from availability
Availability Stack
stack]
AVAIL 400 AVAILLINK(AVAIL)
500
NEW 4. [Initialize Fields of new node]
INFO(NEW) X
AVAIL Null 500 5. [Check for the empty list]
if FIRST = NULL
THEN
FIRST PTR FIRST PTR FIRST NEW
FIRST
LINK(NEW) FIRST
Return NEW
6. [Set PTR]
30 300 20 200 10 300
200
400 PTRFIRST
7. [Check for the last node]
400 300 200 While LINK(PTR)!= FIRST
NEW NEW NEW PTR LINK(PTR)
8. [Add new node at front]
LINK(NEW) FIRST
LINK(PTR) NEW
FIRST NEW
return FIRST
Procedure: C_END_INSERT( X, FIRST)
• This procedure inserts a new node at the last position of Circular
linked list.
• X is a new element to be inserted.
• FIRST is a pointer to the first element of a Circular linked linear list.
• Typical node contains INFO and LINK fields.
• NEW & PTR are temporary pointer variable.
1. [Underflow?]
C_END_INSERT( X, FIRST) Availability Stack
If AVAIL = NULL
Then write(“Availability stack
FIRST=C_END_INSERT(X, FIRST) AVAIL 300 200 underflow”)
INSERT(10, NULL) NEW Return(FIRST)
INSERT(20, 200) 2. [Obtain address of next free node]
AVAIL NEW AVAIL
INSERT(30, 300) 400 300
NEW 3. [Remove free node from availability
stack]
AVAIL 400 AVAIL LINK(AVAIL)
500
NEW 4. [Initialize fields of new node]
INFO(NEW) X
AVAIL Null 500 5. [Check for the empty list]
If FIRST = NULL
Typical node of L R
Doubly Linked List Doubly linked linear list
Doubly Linked Linear List
• Doubly linked list is a sequence of elements in which every element has
links to its previous and next element in the sequence.
Advantages:
• We can traverse in both direction i.e. from starting to end & from end to
starting as well.
• It is easy to reverse the linked list
• If we are at a node, then we can go to any node (which is not possible to
reach the previous node in singly linked list)
Disadvantages:
• It requires more space per node because one extra field is require for
pointer for previous node.
• Still elements are accessed sequentially
Doubly Linked Linear List
Applications:
• To implement tree
• Browser cache which allows you to hit the back button
• Undo/redo functionality in word
• A music player which has next & previous buttons
• Represent deck of cards in a game
• In many OS, the thread scheduler maintains a doubly linked list
Procedure: DOUBINS (L,R,M,X)
• This algorithm inserts a new node in doubly linked linear list.
• The insertion is to be performed to the left of a specific node with
its address given by the pointer variable M
• Typical node of doubly linked list contains following fields LPTR,
RPTR and INFO
• LPTR is pointer variable pointing to Predecessor of a node
• RPTR is pointer variable pointing to Successor of a node
• L & R are pointer variables pointing for Leftmost and Rightmost
node of Linked List.
• NEW is the address of New Node
• X is value to be inserted
Procedure: DOUBINS (L,R,M,X)
4. [Left-most insertion?]
1. [Obtain new node from availability
If M = L
stack]
Then LPTR(NEW) NULL
NEW NODE
RPTR(NEW) M
LPTR(M) NEW
2. [Copy information field]
L NEW
INFO(NEW) X
Return
3. [Insertion into an empty list?]
5. [Insert in middle]
If R = NULL
LPTR(NEW) LPTR(M)
then LPTR(NEW) RPTR(NEW) NULL
RPTR(NEW) M
L R NEW
LPTR(M) NEW
Return
RPTR(LPTR(NEW)) NEW
Return
1. [Obtain new node from availability
DOUBINS (L,R,M,X) stack]
NEW NODE
Insertion into an empty list: Function call
2. [Copy information field]
DOUBINS(NULL,NULL,NULL,10) INFO(NEW) X
5. [Insert in middle]
LPTR(NEW) LPTR(M)
RPTR(NEW) M
LPTR(M) NEW
RPTR(LPTR(NEW)) NEW
Return
PROCEDURE: DOUBDEL (L, R,
OLD)
• This algorithm deletes the node whose address is contained in the
variable OLD
• Typical node of doubly linked list contains following fields LPTR, RPTR
and INFO
• LPTR is pointer variable pointing to Predecessor of a node
• RPTR is pointer variable pointing to Successor of a node
• L & R are pointer variables pointing for Leftmost and Rightmost node
of Linked List.
Procedure: DOUBDEL (L, R,
OLD)
1. [Underflow ?]
IF R = NULL
THEN write (‘UNDERFLOW’)
return
2. [Delete node]
IF L = R (single node in list)
THEN L R NULL
ELSE IF OLD = L (left most node)
THEN L RPTR(L)
LPTR (L) NULL
ELSE IF OLD = R (right most)
THEN R LPTR (R)
RPTR (R) NULL
ELSE RPTR(LPTR (OLD)) RPTR (OLD)
LPTR(RPTR (OLD)) LPTR (OLD)
3. [Return deleted node]
Restore(OLD)
Return
1. [Underflow ?]
DOUBDEL (L, R, OLD) IF R = NULL
THEN write (‘UNDERFLOW’)
DOUBDEL (L, R, OLD) return
Case 1: Single node in the list 2. [Delete node]
IF L = R (single node in list)
DOUBDEL(500,500,500) THEN L R NULL
ELSE IF OLD = L (left most node)
THEN L RPTR(L)
LPTR (L) NULL
NULL 10 NULL ELSE IF OLD = R (right most)
L = R = 500 THEN R LPTR (R)
500
RPTR (R) NULL
L R L = R = NULL ELSE RPTR(LPTR (OLD)) RPTR (OLD)
LPTR(RPTR (OLD)) LPTR (OLD)
Case 2: Leftmost node in the list 3. [Return deleted node]
Restore(OLD)
DOUBDEL(200,700,200) Return