0% found this document useful (0 votes)
3 views23 pages

DS Lecture Week 6

The document discusses linear data structures, specifically circularly linked lists and doubly linked lists. It outlines the advantages and disadvantages of circular lists, as well as procedures for inserting and deleting nodes in both circular and doubly linked lists. Additionally, it explains the structure of nodes in these lists and provides algorithms for various operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views23 pages

DS Lecture Week 6

The document discusses linear data structures, specifically circularly linked lists and doubly linked lists. It outlines the advantages and disadvantages of circular lists, as well as procedures for inserting and deleting nodes in both circular and doubly linked lists. Additionally, it explains the structure of nodes in these lists and provides algorithms for various operations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Linear Data Structure

(Linked List)
Circularly Linked Linear List
 If we replace NULL pointer of the last node of Singly Linked Linear List with the address of its
first node, that list becomes circularly linked linear list or Circular List.
 FIRST is the address of first node of Circular List
 LAST is the address of the last node of Circular List
 Advantages of Circular List
 In circular list, every node is accessible from given node
 It saves time when we have to go to the first node from the last node. It can be done in single step because
there is no need to traverse the in between nodes. But in double linked list, we will have to go through in
between nodes LAST

5 10 15 20 25 30

FIRST
2
Circularly Linked Linear List Cont…
 Disadvantages of Circular List
 It is not easy to reverse the linked list.
 If proper care is not taken, then the problem of infinite loop can occur.
 If we at a node and go back to the previous node, then we can not do it in single step. Instead we have to
complete the entire circle by going through the in between nodes and then we will reach the required node.

 Operations on Circular List


 Insert at First
 Insert at Last
 Insert in Ordered List
 Delete a node

3
Procedure: CIR_INS_FIRST(X,FIRST,LAST)
 This procedure inserts a new node at the first position of Circular linked list.
 X is a new element to be inserted.
 FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
 Typical node contains INFO and LINK fields.
 NEW is a temporary pointer variable.

4
Procedure: CIR_INS_FIRST(X,FIRST,LAST)
1. [Creates a new empty node] IF FIRST = NULL
NEW NODE THEN LINK (NEW)  NEW
2. [Initialize fields of new node and FIRST  LAST  NEW
its link] ELSE LINK (NEW)  FIRST
INFO (NEW)  X LINK (LAST)  NEW
FIRST  NEW
FIRST
Return
NEW
FIRST LAST
50
LAST

50
NEW 5 10 1 -5

FIRST

5
Procedure: CIR_INS_LAST(X,FIRST,LAST)
 This procedure inserts a new node at the last position of Circular linked list.
 X is a new element to be inserted.
 FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
 Typical node contains INFO and LINK fields.
 NEW is a temporary pointer variable.

6
Procedure: CIR_INS_LAST( X,FIRST,LAST)
1. [Creates a new empty node] IF FIRST = NULL
NEW NODE THEN LINK (NEW)  NEW
2. [Initialize fields of new node FIRST  LAST  NEW
and its link] ELSE LINK (NEW)  FIRST
INFO (NEW)  X LINK (LAST)  NEW
LAST  NEW
Return
LAST
NEW
FIRST LAST
50
LAST
50
NEW
5 10 1 -5

FIRST

7
Procedure: CIR_INS_ORD(X,FIRST,LAST)
 This function inserts a new node such that linked list preserves the ordering of the terms in
increasing order of their INFO field.
 X is a new element to be inserted.
 FIRST and LAST are a pointer to the first & last elements of a Circular linked linear list,
respectively.
 Typical node contains INFO and LINK fields.
 NEW is a temporary pointer variable.

8
Procedure: CIR_INS_ORD(X,FIRST,LAST)
1. [Create New Empty Node] 5. [Initialize Temporary Pointer]
NEW NODE SAVE  FIRST
2. [Copy information content into new 6. [Search for Predecessor of new node]
node] Repeat while SAVE ≠ LAST &
INFO(NEW)  X INFO(NEW) ≥ INFO(LINK(SAVE))
3. [Is Linked List Empty?] SAVELINK(SAVE)
IF FIRST = NULL 7. [Set link field of NEW node and its
THEN LINK(NEW)  NEW Predecessor]
FIRST  LAST  NEW LINK(NEW)  LINK(SAVE)
Return LINK(SAVE)  NEW
4. [Does new node precedes all other IF SAVE = LAST
nodes in List?] THEN LAST  NEW
IF INFO(NEW)≤ INFO(FIRST) 8. [Finished]
THEN LINK(NEW)  FIRST Return
LINK(LAST)  NEW
FIRST  NEW
Return

9
Procedure: CIR_INS_ORD(3,FIRST,LAST)
1. [Create New Empty Node] 4. [Does new node precedes all other
NEW NODE nodes in List?]
2. [Copy information content into new IF INFO(NEW)≤ INFO(FIRST)
node] THEN LINK(NEW)  FIRST
INFO(NEW)  X LINK(LAST)  NEW
3. [Is Linked List Empty?] FIRST  NEW
IF FIRST = NULL Return
THEN LINK(NEW)  NEW
FIRST  LAST  NEW
Return FIRST
NEW

FIRST LAST 3
LAST

3
5 10 15 20
NEW
FIRST
10
Procedure: CIR_INS_ORD(18,FIRST,LAST)
5. [Initialize Temporary Pointer] 7. [Set link field of NEW node and its
SAVE  FIRST Predecessor]
6. [Search for Predecessor of new node] LINK(NEW)  LINK(SAVE)
Repeat while SAVE ≠ LAST & LINK(SAVE)  NEW
INFO(NEW) ≥ INFO(LINK(SAVE)) IF SAVE = LAST
SAVELINK(SAVE) THEN LAST  NEW
8. [Finished]
NEW Return

18
LAST
SAVE

5 10 15 20

FIRST

11
Procedure: CIR_DELETE(X,FIRST,LAST)
 This algorithm delete a node whose address is given by variable X.
 FIRST & LAST are pointers to the First & Last elements of a Circular linked list, respectively.
 Typical node contains INFO and LINK fields.
 SAVE & PRED are temporary pointer variable.

12
Procedure: CIR_DELETE(X,FIRST,LAST)
1. [Is Empty List?] 6. [End of Linked List?]
IF FIRST = NULL IF SAVE ≠ X
THEN write(‘Linked List is THEN write(‘Node not found’)
Empty’) Return
Return 7. [Delete X]
2. [Initialize Search for X] IF X = FIRST
SAVE  FIRST THEN FIRSTLINK(FIRST)
3. [Find X] LINK(LAST)FIRST
Repeat thru step 5 ELSE LINK(PRED)LINK(X)
while SAVE≠X & SAVE≠LAST IF X = LAST
4. [Update predecessor marker] THEN LAST  PRED
PRED  SAVE 8. [Free Deleted Node]
5. [Move to next node] Free (X)
SAVE  LINK(SAVE)

13
Procedure: CIR_DELETE(7541,FIRST,LAST)
1. [Is Empty List?] 6. [End of Linked List?]
IF FIRST = NULL IF SAVE ≠ X
THEN write(‘Linked List is Empty’) THEN write(‘Node not found’)
Return Return
2. [Initialize Search for X] 7. [Delete X]
SAVE  FIRST IF X = FIRST
3. [Find X] THEN FIRSTLINK(FIRST)
Repeat thru step5 while SAVE≠X & SAVE≠LAST LINK(LAST)FIRST
4. [Update predecessor marker] ELSE LINK(PRED)LINK(X)
PRED  SAVE IF X = LAST
5. [Move to next node] THEN LAST  PRED
SAVE  LINK(SAVE) 8. [Free Deleted Node]
Free (X)

SAVE
PRED

5 10 15 20 25 30
4455 8564 7541 1254 3254
5000
FIRST LAST
14
Doubly Linked Linear List
 In certain Applications, it is very desirable that a list be traversed in either forward or reverse
direction.
 This property implies that each node must contain two link fields instead of usual one.
 The links are used to denote Predecessor and Successor of node.
 The link denoting its predecessor is called Left Link.
 The link denoting its successor is called Right Link.
 A list containing this type of node is called doubly linked list or two way chain.

15
Doubly Linked Linear List
 Typical node of doubly linked linear list contains INFO, LPTR RPTR Fields
 LPTR is pointer variable pointing to Predecessor of a node
 RPTR is pointer variable pointing to Successor of a node
 Left most node of doubly linked linear list is called L, LPTR of node L is always NULL
 Right most node of doubly linked linear list is called R, RPTR of node R is always NULL

LPTR INFO RPTR

Typical node of
Doubly Linked List L Doubly linked linear list R

16
Insert node in Doubly Linked List
Insertion in the middle of Doubly Linked Linear List
Before Insertion M

L R
LPTR(NEW)  LPTR(M)
NEW RPTR(NEW)  M
LPTR(M)  NEW
RPTR(LPTR(NEW))  NEW
After Insertion
M

L R

NEW

17
Insert node in Doubly Linked List
Left most insertion in Doubly Linked Linear List
Before Insertion
M

L R
LPTR(NEW)  NULL
RPTR(NEW)  M
NEW LPTR(M)  NEW
L  NEW

M After Insertion

L
L R

NEW
18
Procedure: DOU_INS (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.

19
Procedure: DOU_INS (L,R,M,X)
1. [Create New Empty Node] 4. [Is left most insertion ?]
NEW NODE IF M = L
2. [Copy information field] THEN LPTR(NEW)  NULL
INFO(NEW)  X RPTR(NEW)  M
3. [Insert into an empty list] LPTR(M) NEW
IF R = NULL L  NEW
THEN LPTR(NEW)  NULL Return
RPTR(NEW)  NULL 5. [Insert in middle]
L  R  NEW LPTR(NEW)  LPTR(M)
Return RPTR(NEW)  M
LPTR(M)  NEW
RPTR(LPTR(NEW))  NEW
Return

20
PROCEDURE: DOU _DEL (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.

21
Delete from Doubly Linked List

10 L  R  NULL
OLD
L R

OLD OLD OLD

L L R R

L  RPTR(L) RPTR(LTRP(OLD))  RPTR(OLD) R  LPTR(R)


LPTR (L)  NULL LPTR(RTRP(OLD))  LPTR(OLD) RPTR (R)  NULL

22
PROCEDURE: DOU _DEL (L, R, OLD)
1. [Is 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. [FREE deleted node ?]
FREE(OLD)

23

You might also like