CH4 1
CH4 1
CH4 1
LINKED LIST
BIBHA STHAPIT
ASST. PROFESSOR
IoE, PULCHOWK CAMPUS
Array Vs Linked List
Chapter 4
2
Linked List : Introduction
A linked list is a linear data structure.
Chapter 4
Nodes are structures made up of data and a pointer to
another node called “next”.
3
Linked List : Introduction
• I t is a list or collection of data items that can be
stored in scattered locations (positions) in memory by
establishing link between the items.
Chapter 4
• To store data in scattered locations in memory we
have to make link between one data item to another.
4
Linked List : Introduction
• Ea c h data item of a linked list is called a node.
Chapter 4
• To locate the list an external pointer is used that points
the first node of the list.
• T h e link part of the last node will not pointany node. That
means it will be null.
Chapter 4
Singly Circular linked list
Chapter 4
7
Singly Linked List
Each node has only one link part
Each link part contains the address of the next node in the
list
Chapter 4
Link part of the last node contains NULL value which
signifies the end of the node
8
Schematic representation of SLL
• Each node contains a value(data) and a pointer to the next
node in the list
Chapter 4
node in the list
9
Basic Operations on a list
• Creating a List
• Inserting an element in a list
• At the beginning / end
• Before / After specific node
Chapter 4
• In the sorted list
• Deleting anelement from a list
• From the beginning / end
• After specific node
• From the sorted list
• Searching a list
10
• Reversing a list
Creating a node
Chapter 4
11
Insertion at the beginning
Chapter 4
12
Insertion at the beginning
Chapter 4
13
Insertion at the end
Chapter 4
14
Insertion at the end
Chapter 4
15
Insertion after specific node
Chapter 4
16
Insertion after specific node
Chapter 4
17
Insertion before specific node
Chapter 4
18
Insertion before specific node
Chapter 4
19
Insertion in a sorted list
• Here, we shall consider insertion of a node after the
first node or before the last node and the data of the
list are arranged in Ascending Order.
Chapter 4
• Here we have to perform two major tasks:
1. Locate (find out) the node after which the new node
will be inserted.
Chapter 4
21
Insertion in a sorted list
• The steps to locate the position:
Chapter 4
2. Compare the value of the next node with the value of
the new node.
Chapter 4
2. Point the new node by the previous node.
• (preptr->next = new_node).
23
Insertion in a sorted list
• Insert the node into a list (Graphical view)
Chapter 4
24
Insertion in a sorted list (pseudocode)
1. Input linked list (we have to use an existing list)
2. Declare pointers (start, ptr, preptr, new_node)
3. Create a new node:
new_node = new (node);
new_node →data = item;
Chapter 4
new_node →next = NULL;
4. Locate the appropriate position for the new node
while (ptr→data < new_node →data)
{
preptr= ptr; ptr = ptr→next;
}
5. Insert the new node at appropriate position (by linking previous
and next node);
new_node →next = preptr→next; 25
preptr→next = new_node;
6. Output :updated linked list
Deletion in linear SLL
• Deleting a node from the linked list has the following
instances:
• 1. Deletion from beginning
• 2. Deletion from end
Chapter 4
• 3. Deletion after specific node
• 4. Deletion in sorted list
26
Deletion from beginning
Chapter 4
27
Deletion from end
Chapter 4
28
Deleting after specific node
Chapter 4
29
Deleting after specific node
Chapter 4
30
Deletion in sorted list
Chapter 4
31
Deletion in sorted list
Chapter 4
32
Complexity of array Vs SLL
Chapter 4
33
Circular linked list
• A circular linked list is a singly- linked list, in which the
link field of the last node contains the address of the first
node of the list instead of pointing to NULL.
Chapter 4
• A circular linked list has no end, so we can use two
pointers first and last to point to the first and the last
nodes respectively (but not necessarily).
34
Inserting at beginning - CLL
Chapter 4
35
Inserting at beginning - CLL
Chapter 4
36
Inserting at end- CLL
Chapter 4
37
Inserting at end- CLL
Chapter 4
38
Deletion from beginning - CLL
Chapter 4
39
Deletion from beginning - CLL
Chapter 4
40
Deletion from end- CLL
Chapter 4
41
Deletion from end- CLL
Chapter 4
42
Stack as linked list
• The storage requirement of linked representation of the stack
with n elements is O(n), and the typical time requirement for
the operations is O(1).
Chapter2
data and another that stores the address of the next node.
Chapter2
Step 1: Allocate memory for the NEW_NODE
Step 2: SET NEW_NODE-> DATA = VAL
Step 3: IF TOP = NULL
SET NEW_NODE-> NEXT = NULL
SET TOP = NEW_NODE
ELSE
SET NEW_NODE ->NEXT = TOP
SET TOP = NEW_NODE
[END OF IF] 44
Step 4: END
Operations on linked stack
• Pop operation
Chapter2
Step 1: IF TOP = NULL
PRINT UNDERFLOW
Goto Step 5
[END OF IF]
Step 2: SET PTR = TOP
Step 3: SET TOP = TOP ->NEXT
Step 4: FREE PTR 45
Step 5: END
Queue as linked list
• The storage requirement of linked representation of a queue with
n elements is O(n) and the typical time requirement for operations is
O(1).
• In a linked queue, every element has two parts, one that stores the
data and another that stores the address of the next element.
Chapter2
• The START pointer of the linked list is used as FRONT. Here, we will
also use another pointer called REAR, which will store the address of
the last element in the queue.
• All insertions will be done at the rear end and all the deletions will
be done at the front end.
Chapter2
Step 1: Allocate memory for the new node ‘PTR’
Step 2: SET PTR-> DATA = VAL
Step 3: IF FRONT = NULL
SET FRONT = REAR = PTR
SET FRONT-> NEXT = REAR-> NEXT = NULL
ELSE
SET REAR-> NEXT = PTR
SET REAR = PTR
SET REAR-> NEXT = NULL 47
[END OF IF]
Step 4: END
Operations on linked queue
• Dequeue operation
Chapter2
Step 1: IF FRONT = NULL
Write Underflow
Go to Step 5
[END OF IF]
Step 2: SET PTR = FRONT
Step 3: SET FRONT = FRONT -> NEXT
Step 4: FREE PTR 48
Step 5: END
Application of linked list
Addition of two polynomials:
• Linked lists are widely used to represent and manipulate
polynomials. Polynomials are the expressions containing
number of terms with nonzero coefficients and exponents.
• p(X) = anxne + an-1xn-1e + ………. + a1x1e + a
Chapter2
• In the linked list representation of polynomials, each term is
considered as a node. Such a node contains three fields.
• Coefficient field
• Exponent field
• Link field
Chapter2
6. Compare the exponents of the two polynomials starting from the first nodes.
a. If both the exponents are equal then add the coefficients and store it in
the resultant linked list R. Move both pointers to the next nodes.
b. If the exponent of the current term of P is less than the exponent of the
current term of Q, then the current term of Q is added to the resultant
linked list R and the pointer q is moved to the next node.
c. If the exponent of the current term of P is greater than the exponent of
the current term of Q, then the current term of P is added to the resultant
linked list R and the pointer p is moved to the next node.
50
d. Append the remaining nodes of either of the polynomials to the resultant
linked list R.
• In summary:
• To do this, we have to break the process down to
cases:
• Case 1: exponent of p > exponent of q
• Copy node of p to end of r .
• p = p->next
• Case 2: exponent of p < exponent of q
• Copy node of q to end of r .
• q= q->next
Chapter2
• Case 3: exponent of p = exponent of q
• Create a new node in r with the same exponent and
with the sum of the coefficients of p and q.
• p = p->next and q = q->next
51
Chapter2
52
Doubly linked list
• Doubly linked list is a linked data structure that consists
of a set of sequentially linked records called nodes.
Chapter2
• two other field is links part that are pointer references
to the previous and to the next node in the sequence
of nodes.
Chapter2
node*previous;
};
54
DLL vs. SLL
Advantages of DLL:
Can be traversed in either direction (may be essential
for some programs)
Some operations, such as deletion and inserting
Chapter2
before a node, become easier
Disadvantages of DLL:
Requires more space
List manipulations are slower (because more links
must be changed)
55
Greater chance of having bugs (because more links
must be manipulated)
Operations in DLL
• Insertion operations
• At the beginning
• At the end
• After specific node
Chapter2
• Before specific node
• Deletion operations
• From the beginning
• From the end
• After specific node 56
• Before specific node
Insertion at beginning
Chapter2
57
Insertion at beginning
Step 1: Allocate memory for NEW_NODE
Step 2: IF NEW_NODE= NULL
Write OVERFLOW
Go to Step 9
[END OF IF]
Chapter2
Step 3: SET NEW_NODE ->DATA=VAL
Step 4: SET NEW_NODE ->PREV=NEW_NODE ->NEXT=NULL
Step 5: IF START = NULL
SET START = NEW_NODE
Go to step 9
[END OF IF]
Step 6: SET NEW_NODE->NEXT = START
Step 7: SET START->PREV = NEW_NODE 58
Step 8: SET START = NEW_NODE
Step 9: EXIT
Insertion at end
Chapter2
59
Insertion at end
Step 1: Allocate memory for NEW_NODE
Step 2: IF NEW_NODE= NULL
Write OVERFLOW
Go to Step 11
[END OF IF]
Step 3: SET NEW_NODE ->DATA=VAL
Step 4: SET NEW_NODE ->PREV=NEW_NODE ->NEXT=NULL
Chapter2
Step 5: IF START = NULL
SET START = NEW_NODE
Go to step 11
[END OF IF]
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR-> NEXT != NULL
Step 8: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 9: SET NEW_NODE-> PREV=PTR 60
Step 10: SET PTR-> NEXT = NEW_NODE
Step 11: EXIT
Insertion after specific node
Chapter2
61
Insertion after specific node
Step 1: Allocate memory for NEW_NODE
Step 2: IF NEW_NODE= NULL
Write OVERFLOW
Go to Step 13
[END OF IF]
Step 3: SET NEW_NODE ->DATA=VAL
Step 4: SET NEW_NODE ->PREV=NEW_NODE ->NEXT=NULL
Step 5: IF START = NULL
Chapter2
SET START = NEW_NODE
Go to step 13
[END OF IF]
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR-> DATA!= NUM
Step 8: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 9: SET NEW_NODE ->NEXT = PTR-> NEXT
Step 10:SET NEW_NODE-> PREV=PTR
Step 11: SET PTR-> NEXT -> PREV = NEW_NODE 62
Step 12 :SET PTR-> NEXT = NEW_NODE
Step 13: EXIT
Insertion before specific node
Chapter2
63
Insertion before specific node
Step 1: Allocate memory for NEW_NODE
Step 2: IF NEW_NODE= NULL
Write OVERFLOW
Go to Step 13
[END OF IF]
Step 3: SET NEW_NODE ->DATA=VAL
Step 4: SET NEW_NODE ->PREV=NEW_NODE ->NEXT=NULL
Step 5: IF START = NULL
Chapter2
SET START = NEW_NODE
Go to step 13
[END OF IF]
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR-> DATA!= NUM
Step 8: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 9: SET NEW_NODE ->NEXT = PTR
Step 10:SET NEW_NODE-> PREV=PTR->PREV
Step 11: SET PTR-> PREV -> NEXT = NEW_NODE 64
Step 12 :SET PTR-> PREV = NEW_NODE
Step 13: EXIT
Deletion from beginning
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 6
[END OF IF]
Step 2: SET PTR = START
Chapter2
Step 3: SET START = START-> NEXT
Step 4: SET START->PREV=NULL
Step 5: FREE PTR
Step 6: EXIT
65
Deletion from end
Chapter2
66
Deletion from end
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 7
[END OF IF]
Step 2: SET PTR = START
Chapter2
Step 3: Repeat step 4 while PTR ->NEXT!=NULL
Step 4: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT=NULL
Step 6: FREE PTR
Step 7: EXIT
67
Deletion after specific node
Chapter2
68
Deletion after specific node
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Chapter2
Step 3: Repeat step 4 while PTR ->DATA!=NUM
Step 4: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 5: SET TEMP= PTR->NEXT
Step 6: SET PTR->NEXT = TEMP->NEXT
Step 7: SET TEMP->NEXT->PREV = PTR
Step 8: FREE TEMP 69
Step 9: EXIT
Deletion before specific node
Chapter2
70
Deletion before specific node
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Chapter2
Step 3: Repeat step 4 while PTR ->DATA!=NUM
Step 4: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 5: SET TEMP= PTR->PREV
Step 6: SET TEMP ->PREV->NEXT= PTR
Step 7: SET PTR->PREV = TEMP ->PREV
Step 8: FREE TEMP
Step 9: EXIT 71
Circular doubly linked list
• A circular doubly linked list or a circular two-way linked list
contains a pointer to the next as well as the previous node in
the sequence.
• The circular doubly linked list does not contain NULL in the
previous field of the first node and the next field of the last
Chapter2
node.
• Rather, the next field of the last node stores the address of
the first node of the list, i.e., START. Similarly, the previous
field of the first field stores the address of the last node.
72
Insertion at beginning in circular DLL
Chapter2
73
Insertion at beginning in circular DLL
Step 1: Allocate memory for NEW_NODE
Step 2: IF NEW_NODE= NULL
Write OVERFLOW
Go to Step 14
[END OF IF]
Step 3: SET NEW_NODE ->DATA=VAL
Step 4: SET NEW_NODE ->PREV=NEW_NODE ->NEXT=NEW_NODE
Step 5: IF START = NULL
Chapter2
SET START = NEW_NODE
Go to step 14
[END OF IF]
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR-> NEXT!= START
Step 8: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 9: SET NEW_NODE-> PREV=PTR
Step 10: SET NEW_NODE -> NEXT= START
Step 11: SET PTR->NEXT = NEW_NODE 74
Step 12: SET START-> PREV= NEW_NODE
Step 13: SET START= NEW_NODE
Step 14: EXIT
Insertion at end in circular DLL
Chapter2
75
Insertion at end in circular DLL
Step 1: Allocate memory for NEW_NODE
Step 2: IF NEW_NODE= NULL
Write OVERFLOW
Go to Step 13
[END OF IF]
Step 3: SET NEW_NODE ->DATA=VAL
Step 4: SET NEW_NODE ->PREV=NEW_NODE ->NEXT=NEW_NODE
Step 5: IF START = NULL
Chapter2
SET START = NEW_NODE
Go to step 13
[END OF IF]
Step 6: SET PTR = START
Step 7: Repeat Step 8 while PTR-> NEXT!= START
Step 8: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 9: SET NEW_NODE-> PREV=PTR
Step 10: SET NEW_NODE-> NEXT =START
Step 11: SET PTR->NEXT = NEW_NODE 76
Step 12: SET START-> PREV= NEW_NODE
Step 13: EXIT
Deletion from beginning in circular DLL
Chapter2
77
Deletion from beginning in circular DLL
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Chapter2
Step 3: Repeat step 4 while PTR ->DATA!=NUM
Step 4: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 5: SET PTR->NEXT = START ->NEXT
Step 6: SET START->NEXT->PREV= PTR
Step 7: FREE START
Step 8: SET START= PTR->NEXT
Step 9: EXIT 78
Deletion from end in circular DLL
Chapter2
79
Deletion from end in circular DLL
Step 1: IF START = NULL
Write UNDERFLOW
Go to Step 9
[END OF IF]
Step 2: SET PTR = START
Chapter2
Step 3: Repeat step 4 while PTR ->DATA!=NUM
Step 4: SET PTR = PTR-> NEXT
[END OF LOOP]
Step 5: SET PTR->PREV->NEXT= START
Step 6: SET START->PREV= PTR->PREV
Step 7: FREE PTR
Step 8: EXIT
80
Chapter 4
THANK YOU!
81