Unit-2: Linear Data Structure (Linked List)
Unit-2: Linear Data Structure (Linked List)
Unit-2: Linear Data Structure (Linked List)
GTU # 3130702
Unit-2
Linear Data Structure
(Linked List)
One method of obtaining the address of node is to store address in computer’s main memory,
we refer this addressing mode as pointer of link addressing.
A simple way to represent a linear list is to expand each node to contain a link or pointer to the
next node. This representation is called one-way chain or Singly Linked Linear List.
X 1000
2100
Join Operation
Join operation is more efficient in Linked Allocation.
Linked list require more memory compared to array because along with value it stores pointer
to next node.
Linked lists are among the simplest and most common data structures. They can be used to
implement other data structures like stacks, queues, and symbolic expressions, etc…
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 7
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
Info Link
Accessing Part
Node
of Node
Typical Node Info (Node)
Data Pointer to Link (Node)
Next Node
struct node
{
C Structure to int info;
represent a node struct node *link;
};
5. [Is the list empty?] 8. [Set link field of last node to NEW]
If FIRST = NULL LINK (SAVE) NEW
Then Return (NEW)
9. [Return first node pointer]
6. [Initialize search for a last node] Return (FIRST)
SAVE FIRST
SAVE
10 50 -1 8 35 44 50
NEW
FIRST
5 10 15 20 25 30
FIRST
22
NEW
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 19
Function: INSORD(X, FIRST)
1. [Underflow?] 6. [Does the new node precede all other
IF AVAIL = NULL 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 next free Node] 7. [Initialize temporary pointer]
NEW AVAIL SAVE FIRST
3. [Remove free node from availability 8. [Search for predecessor of new node]
Stack] Repeat while LINK (SAVE) ≠ NULL
AVAIL LINK(AVAIL) & INFO(NEW) ≥ INFO(LINK(SAVE))
4. [Initialize fields of new node] SAVE LINK (SAVE)
INFO(NEW) X 9. [Set link field of NEW node and its
5. [Is the list empty?] predecessor]
IF FIRST = NULL LINK (NEW) LINK (SAVE)
THEN LINK(NEW) NULL LINK (SAVE) NEW
Return (NEW) 10. [Return first node pointer]
Return (FIRST)
5 10 15 20 25 30
FIRST
3
NEW
6. [Does the new node precede all other node in the list?]
IF INFO(NEW) ≤ INFO (FIRST)
THEN LINK (NEW) FIRST
Return (NEW)
SAVE
5 10 15 20 25 30
FIRST
22
NEW
PRED SAVE
5 10 15 20 25 30
FIRST
5 10 15 20 25 30
4455 8564 7541 1254 3254
5000
FIRST
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 25
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.
5
NEW
5 10 15 30
BEGIN
PRED
5 10 15 30
NEW NEW NEW NEW
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 30
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
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 31
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
50
NEW 5 10 1 -5
FIRST
FIRST
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 36
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.
FIRST LAST 3
LAST
3
5 10 15 20
NEW
FIRST
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 39
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
SAVELINK(SAVE) THEN LAST NEW
8. [Finished]
NEW Return
18
LAST
SAVE
5 10 15 20
FIRST
SAVE
PRED
5 10 15 20 25 30
4455 8564 7541 1254 3254
5000
FIRST LAST
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 43
Circularly Linked List with Header Node
We can have special node, often referred to as Head node of Circular Linked List.
Head node does not have any value.
Head node is always pointing to the first node if any of the linked list.
One advantage of this technique is Linked list is never be empty.
Pointer variable HEAD contains the address of head node.
HEAD
10 15 20 25 30
HEAD
Empty List
LINK(HEAD) HEAD
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 44
Procedure: CIR_HEAD_INS_FIRST(X,FIRST,LAST)
This procedure inserts a new node at the first position of Circular linked list with Head node.
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.
HEAD is pointer variable pointing to Head node of Linked List.
NEW is a temporary pointer variable.
NEW
50
HEAD
10 15 20 25 30
NEW
50
HEAD
10 15 20 25
FIRST LAST
50
HEAD P
10 15 20 25
FIRST LAST
Typical node of
Doubly Linked List L Doubly linked linear list R
L R
LPTR(NEW) LPTR(M)
NEW RPTR(NEW) M
LPTR(M) NEW
RPTR(LPTR(NEW)) NEW
After Insertion
M
L R
NEW
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 53
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
#3130702 (DS) Unit 2 – Linear Data Structure (Linked
Dr. Pradyumansinh U. Jadeja 54
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.
10 L R NULL
OLD
L R
L L R R
Thank
You