DS Lecture Week 5
DS Lecture Week 5
(Linked List)
Linked Storage Representation
There are many applications where sequential allocation method is unacceptable because of
following characteristics
Unpredictable storage requirement
Extensive manipulation of stored data
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.
2
Linked Storage Representation
NULL
Last Node Value
A 1000 B 2050 C 3335 D
5000 1000 2050 3335
A linked List
The linked allocation method of storage can result in both efficient use of computer storage
and computer time.
A linked list is a non-sequential collection of data items.
Each node is divided into two parts, the first part represents the information of the element and the second
part contains the address of the next mode.
The last node of the list does not have successor node, so null value is stored as the address.
It is possible for a list to have no nodes at all, such a list is called empty list.
3
Pros & Cons of Linked Allocation
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
4
Pros & Cons of Linked Allocation
Deletion Operation
Deletion operation is more efficient in Linked Allocation
5
Pros & Cons of Linked Allocation
Search Operation
If particular node in the list is required, it is necessary to follow links from the first node onwards until the
desired node is found, in this situation it is more time consuming to go through linked list than a sequential
list.
Search operation is more time consuming in Linked Allocation.
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…
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
8
Singly Linked List
9
Node Structure of Singly List
Info Link
Accessing Part
Node
of Node
Typical Node Info (Node)
Data Pointer to Link (Node)
Next Node
struct node
{
C/C++ Structure to int info;
represent a node struct node *link;
};
10
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
11
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.
12
Function: INSERT(X, First)
This function inserts a new node at the first position of Singly linked list.
This function returns address of FIRST node.
X is a new element to be inserted.
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 is a temporary pointer variable.
13
Function: INSERT(X,FIRST) Cont…
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. [Initialize fields of new node and its link to the list]
INFO(NEW) X
LINK (NEW) FIRST
5. [Return address of new node]
Return (NEW)
14
Example: INSERT(50, FIRST)
15
Function: INSEND(X, FIRST)
This function inserts a new node at the last position of linked list.
This function returns address of FIRST node.
X is a new element to be inserted.
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 is a temporary pointer variable.
16
Function: INSEND(X, First) Cont…
1. [Underflow?] 5. [Is the list empty?]
If AVAIL = NULL If FIRST = NULL
Then Write (“Availability Then Return (NEW)
Stack Underflow”)
Return(FIRST) 6. [Initialize search for a last node]
SAVE FIRST
2. [Obtain address of next free Node]
NEW AVAIL 7. [Search for end of list]
Repeat while LINK (SAVE) ≠ NULL
3. [Remove free node from availability SAVE LINK (SAVE)
Stack]
AVAIL LINK(AVAIL) 8. [Set link field of last node to NEW]
LINK (SAVE) NEW
4. [Initialize fields of new node]
INFO(NEW) X 9. [Return first node pointer]
LINK (NEW) NULL Return (FIRST)
17
Function: INSEND(50, FIRST)
4. [Initialize fields of new node] 7. [Search for end of list]
INFO(NEW) X Repeat while LINK (SAVE) ≠ NULL
LINK (NEW) NULL SAVE LINK (SAVE)
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
18
Function: INSORD(X, FIRST)
This function inserts a new node such that linked list preserves the ordering of the terms in
increasing order of their INFO field.
This function returns address of FIRST node.
X is a new element to be inserted.
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 is a temporary pointer variable. Predecessor
5 10 15 20 25 30
FIRST
22
NEW
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)
20
Function: INSORD(3, 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)
21
Function: INSORD(22, FIRST)
7. [Initialize temporary pointer] 9. [Set link field of NEW node and its
SAVE FIRST predecessor]
8. [Search for predecessor of new node] LINK (NEW) LINK (SAVE)
Repeat while LINK (SAVE) ≠ NULL LINK (SAVE) NEW
& INFO(NEW) ≥ INFO(LINK(SAVE)) 10. [Return first node pointer]
SAVE LINK (SAVE) Return (FIRST)
SAVE
5 10 15 20 25 30
FIRST
22
NEW
22
Procedure: DELETE(X, FIRST)
This algorithm delete a node whose address is given by variable X.
FIRST is a pointer to the first element of a Singly linked linear list.
Typical node contains INFO and LINK fields.
SAVE & PRED are temporary pointer variable.
PRED SAVE
5 10 15 20 25 30
FIRST
23
Procedure: DELETE( X, FIRST)
1. [Is Empty list?] 5. [Move to next node]
IF FIRST = NULL SAVE LINK(SAVE)
THEN write (‘Underflow’) 6. [End of the list?]
Return If SAVE ≠ X
2. [Initialize search for X] THEN write (‘Node not found’)
SAVE FIRST Return
3. [Find X] 7. [Delete X]
Repeat thru step-5 If X = FIRST
while SAVE ≠ X and THEN FIRST LINK(FIRST)
LINK (SAVE) ≠ NULL ELSE LINK (PRED) LINK (X)
4. [Update predecessor marker] 8. [Free Deleted Node]
PRED SAVE Free (X)
24
Procedure: DELETE(7541, FIRST)
2. [Initialize search for X] 6. [End of the list?]
SAVE FIRST If SAVE ≠ X
3. [Find X] THEN Write (‘Node not found’)
Repeat thru step-5 Return
while SAVE ≠ X and 7. [Delete X]
LINK (SAVE) ≠ NULL If X = FIRST
4. [Update predecessor marker] THEN FIRST LINK(FIRST)
PRED SAVE ELSE LINK (PRED) LINK (X)
5. [Move to next node] 8. [Free Deleted Node]
SAVE LINK(SAVE) Free (X)
SAVE
PRED
5 10 15 20 25 30
4455 8564 7541 1254 3254
5000
FIRST
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.
26