2 Linklist
2 Linklist
Chapter 2
Safia Sadruddin
Linked List
● A linked list is a linear data structure, in which elements are not stored at
contiguous memory location.
● The elements in a linked list are linked using pointers (entity that points
to the next element).
● In simple words, a linked list contains a nodes where each node contains
a data field and a reference (link) to the next node in the list.
Linked List
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main() Output :
{
1442
int *ptr;
ptr=(int*)malloc(sizeof(int)); 20
*ptr=20;
printf("%u\n",ptr);
printf("%d\n",*ptr);
getch();
}
free() Function
struct
struct node
node {{
data_type
data_type var;
var;
struct
struct node
node *next;
*next;
};
};
Linked List Node Creation
Creation of Node at Runtime
struct
struct node
node *new_node
*new_node ;;
new_node
new_node =(struct
=(struct node*)malloc(sizeof(struct
node*)malloc(sizeof(struct node));
node));
Memory Allocation is at runtime and thus Non- Memory allocation is contiguous as size of array
contiguous need to be predefined
Requires extra storage space for storing address As Memory Allocation is contiguous, all elements
of next node can be accessed with index
A B C D
Linked List Operations
struct Node
{ next
int data;
struct Node *next; data
};
node
Creating a Linked List
Once you have done these 2 steps (i.e. declared a node data structure, and created a
NULL start pointer, you have an empty linked list.
hea null
d
Operations on Singly
Linked List
19
Singly Linked List: Creation
20
Singly Linked List : Insertion at Beginning
If Initial List is empty
head= NULL
13
13
21
Singly Linked List : Insertion at Beginning
If Initial List is : head 10 20
new_node
Create new_node
new_node
Set its value : new_node -> data=13
13
new_node 13 10 20
Set head = new_node
head 13 10 20
22
Algorithm: Singly Linked List : Insertion at Beginning
1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 new_node -> next = NULL
4. Else
4.1 new_node -> next = head
4.2 head = new_node
5. End
23
Singly Linked List : Insertion at End
30
30
24
Singly Linked List : Insertion at End
Create new_node with required value and set node →next = null e.g : say 30
new_node 30
To insert at end Traverse the list
Set temp=head
While temp→next!=null
temp=temp→next
25
Algorithm: Singly Linked List : Insertion at End
1. Create a new_node
2. Set new_node -> data= 30 and new_node -> next = NULL
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = NULL
4. Else
4.1 Set temp=head
4.2 while temp -> next != NULL
4.2.1 temp= temp ->next
4.3 temp -> next = new_node
5. End
26
Singly Linked List : Inserting after any node
Create new_node with required value and set node →next = null e.g : say 25
new_node 25
While temp→data != 20
temp=temp→next
27
Algorithm: Singly Linked List : Insertion after any node
1. Create a new_node
2. Set new_node -> data= 25
3. Set temp=head
4. while temp -> data != 20
4.1 temp = temp ->next
5. new_node -> next = temp -> next
6. temp -> next = new_node
7. End
28
Singly Linked List : Inserting before any node
Create new_node with required value and set node →next = null e.g : say 25
new_node 25
While temp→data != 35
pre= temp
temp=temp→next
29
head
15 35 45
pre= null
temp
head
15 35 45
While temp→data != 35
temp pre= temp
pre
head temp=temp→next
15 35 45
temp
pre
25
Algorithm: Singly Linked List : Insertion before any
node
1. Create a new_node
2. Set new_node -> data= 25
3. Set temp=head
4. pre= NULL
5. while temp -> data != 20
4.1 pre =temp
4.2 temp= temp ->next
6. new_node -> next = temp
7. pre -> next = new_node
8. End
Deletion
Operations on
Singly Linked List
32
Singly Linked List
Caution:
On Deleting node, deallocate memory using free()
• General syntax:
free (ptr);
• Here, ptr is a pointer to a memory block which has been
previously created using malloc
33
Singly Linked List: Deletion at Beginning
Set temp=head
Now
1) head=head→next
2) free(temp)
34
Singly Linked List: Deletion at Beginning
1. If head = NULL
Print Underflow
End
2. Else
2.1 Set temp=head
2.2 head=head→next
2.3 free(temp)
3. End
35
Singly Linked List : Deletion at End
Initial List
head 10 20 30
36
Singly Linked List : Deletion at End
1 2
3
4
37
Singly Linked List : Deletion at End
Now set
1)p→next=null
2)free(q)
38
Singly Linked List: Deletion at End
39
Singly Linked List : Deletion of specific Node
head 10 20 25 30
40
Singly Linked List : Deletion of specific Node
Traverse List: repeat while q→data!=25
1) p=q
2) q=q→next
1 2
3
4
41
Singly Linked List : Deletion of specific Node
Now set
1)p→next=q→next
2)free(q)
42
Singly Linked List : Deletion of specific Node
43
Insertion
Operations on
Doubly Linked List
44
Doubly Linked List
struct node{
struct node *prev;
int data;
struct node *next;
};
45
Doubly Linked List: Creation
head null
– Create head pointer and set to null
temp
– Create node with initial value 20
– Set temp = head and traverse while temp→next !=null head
10
46
Doubly Linked List: Insertion at Beginning
Initial List
Now set,
1)head→prev=new_node
2)new_node→next=head
3)head=new_node
47
Doubly Linked List: Insertion at Beginning
1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 new_node -> next = NULL
3.3 new_node -> prev = NULL
4. Else
4.1 new_node -> next = head
4.2 new_node -> prev= NULL
4.3 head-> prev= new_node
4.4 head = new_node
5. End
48
Doubly Linked List: Insertion at End:
Initial List
49
Doubly Linked List: Insertion at End:
Set temp=head, start traversing
While temp→next!=null
temp=temp→next
1
2
3
4
50
Doubly Linked List: Insertion at End:
To insert:
1) temp→next=new_node
2) new_node→prev=temp
51
Doubly Linked List: Insertion at End
1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = NULL
3.3 head -> prev = NULL
4. Else
4.1Set temp=head
4.2 while temp -> next != NULL
4.2.1 temp= temp ->next
4.3 temp -> next = new_node
4.4 new_node -> prev = temp
4.5 new_node -> next= NULL
5. End
52
Doubly Linked List : Insertion after specific Node
53
Doubly Linked List
temp=temp→next
54
Doubly Linked List
To insert:
1)new_node→next=temp→next
2)temp→next→prev=new_node
3)new_node→prev=temp
4)temp→next=new_node
55
Doubly Linked List: Insertion after specific node
1. Create a new_node
2. new_node -> data = 13
3. Set temp=head
4. while temp -> data != 20
4.1 temp = temp-> next
5. new_node -> prev= temp
6. new_node -> next = temp -> next
7. temp-> next -> prev = new_node
8. temp -> next = new_node
9. End
56
Doubly Linked List : Insertion before specific Node
57
Doubly Linked List : Insertion before specific Node
temp=temp→next
1 2
To insert:
1)new_node→prev=temp→prev
2)temp→prev→next=new_node
3)new_node→next=temp
4)temp→prev=new_node
59
Doubly Linked List: Insertion after specific node
1. Create a new_node
2. new_node -> data = 13
3. temp = head
4. while temp -> data ! = 25
temp = temp -> next
5, new_node -> next = temp
6. new_node -> prev= temp -> prev
7. temp -> prev -> next = new_node
8. temp -> prev= new_node
9. End
60
Deletion
Operations on
Doubly Linked List
61
Doubly Linked List: Deletion at Beginning
Initial List
Set temp=head
Now
1) head=head→next
2)head->prev=null
3) free(temp)
62
Doubly Linked List: Deletion at Beginning
1. head = NULL
Print underflow
End
2. Else
2.1 temp = head
2.2 head = head -> next
2.3 head -> prev = NULL
2.4 free (temp)
3. End
63
Doubly Linked List : Deletion at End
Initial List
3 4
64
Doubly Linked List : Deletion at End
Now set
1)temp→prev→next=null
2)free(temp)
65
Doubly Linked List: Deletion at End
1. If head = NULL
Print Underflow
End
2. Else
2.1 temp = head
2.2 while temp -> next ! = NULL
temp = temp -> next
2.3 temp -> prev -> next = NULL
2.4 free(temp)
3. End
66
Doubly Linked List: Deletion After a specific node
Initial List
67
Doubly Linked List: Deletion After a specific node
Now set
temp1
68
Doubly Linked List: Deletion After a specific node
1. If head= NULL
Print Underflow
End
2. Else
2.1 temp = head
2.2 while temp -> data != 20
temp = temp->next
2.3 temp1= temp -> next
2.4 temp1-> next -> prev = temp
2.5 temp -> next = temp1-> next
2.6 free(temp1)
3. End
69
Doubly Linked List: Deletion Before a specific node
Initial List
Traverse List:
temp1=NULL
temp2=head
while temp→ data !=25
temp1= temp2
temp2= temp2-> next
70
Doubly Linked List: Deletion Before a specific node
Now set
temp1 temp2
71
Doubly Linked List: Deletion Before a specific node
1. If head = NULL
Print Underflow
End
2. Else
2.1 temp1= NULL
2.2 temp2= head
2.3 while temp2->data !=25
temp1= temp2
temp2= temp2-> next
2.4 temp1-> prev-> next = temp2
2.5 temp2-> prev = temp1 -> prev
2.6 free(temp1)
3. End
72
73
Doubly Link List
74
Circular Linked List
Representation:
head 10 20 30
Characteristics:
– Unidirectional
– Last Node points back to First Node
75
Circular Linked List
struct
structnode
node
{{
data_type
data_typevar;
var;
struct
structnode
node*next;
*next;
};
};
76
Circular Linked List
struct
structnode
node*new_node
*new_node;;
new_node
new_node=(struct
=(structnode*)
node*)malloc(sizeof(struct
malloc(sizeof(structnode));
node));
77
Circular Linked List : Insertion at Beginning
If Initial List is empty
head= NULL
13
13
78
Circular Linked List : Insertion at Beginning
If Initial List is : head 10 20
new_node
Create new_node
new_node head
13 10 20
79
Circular Linked List : Insertion at Beginning
To insert at beginning requires to traverse the list to find last node. So
that the next pointer of last node can be updated to point to newly
inserted node.
new_node head temp
Set temp=head
While temp→next!=head
13 10 20
temp=temp→next
13 10 20
80
Algorithm: Circular Linked List : Insertion at Beginning
1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = new_node
4. Else
4.1new_node-> next =head
4. 2 while temp -> next != head
4.2.1 temp= temp ->next
4.3 temp -> next = new_node
4.4 head = new_node
5. End
81
Circular Linked List : Insertion at End
If Initial List is empty
head= NULL
13
13
82
Circular Linked List : Insertion at End
To insert at end requires to traverse the list to find last node.
Set temp=head
While temp→next!=head
head temp
temp=temp→next
10 20
10 20 13
83
Algorithm: Circular Linked List : Insertion at End
1. Create a new_node
2. Set new_node -> data= 13
3. If head = = NULL
3.1 head = new_node
3.2 head -> next = new_node
4. Else
4.1 while temp -> next != head
4.2.1 temp= temp ->next
4.2 temp -> next = new_node
4.2 new_node -> next = head
5. End
84
Circular Linked List: Deletion at Beginning
Set p=head
p
While p→next!=head
p=p -> next
head 10 20 30
p
p
head 10 20 30
head 10 20 30
85
Circular Linked List: Deletion at Beginning
q p
Set q=head
Set head = head-> next head 10 20 30
Set p -> next =head
free(q)
head p
10 20 30
head
20 30
86
Circular Linked List: Deletion at Beginning
1. If head = NULL
Print Underflow
End
2. Else
2.1 Set p=head
2.2 while p-> next != head
2.2.1 p= p ->next
2.3 Set q= head
2.4 head= head- > next
2.5 p-> next = head
2.6 free(q)
3. End
87
Circular Linked List: Deletion at End
q
q p
q p
head 10 20 30
head 10 20 30
88
Circular Linked List: Deletion at End
q p
Set q=head
Set q -> next = head head 10 20 30
free(p)
q p
head 10 20 30
89
Circular Linked List: Deletion at End
1. If head = NULL
Print Underflow
End
2. Else
2.1 Set q= NULL , p=head
2.2 while p-> next != head
2.2.1 q= p
2.2.2 p= p ->next
2.3 Set q -> next = head
2.4 free(p)
3. End
90
Stack and Queue
using Linked List
91
Stack using Linked List
Stack Revision
1) Stack is linear data structure?
92
Stack using Linked List
93
Stack using Linked List
Which combination of insert and delete for push & pop, won’t be able to apply
LIFO order.
a) Insert and Delete at Beginning
b) Insert and Delete at End
c) Insert after/before specific node and Delete specific node
Which combination of insert and delete for Push and pop operation result in
best case complexity
a) Insert and Delete at Beginning
94
Stack using Linked List
95
Queue using Linked List
96
Queue using Linked List
97
Queue using Linked List
Which combination of insert and delete for Enqueue & Dequeue, won’t be able
to apply FIFO order.
a) Insert and Delete at Beginning
b) Insert and Delete at End
c) Insert at Beginning and Delete at End
d) Insert at End and Delete at Beginning
Which combination of insert and delete for Enqueue & Dequeue operation
result in best case complexity
a) Insert at end and Delete at Beginning
b) Insert at Beginning and Delete at End
98
Queue using Linked List
99
Queue using Linked List
10
0
Demo of Stack and Queue Using Linked List
https://visualgo.net/en/list
10
1
Application of Linked
List
102
Polynomial List
10
3
Polynomial List
Polynomial representation
10
4
Polynomial List
3x + 4x + 2x + 5
9 3
Can we have
Sorted linked list?..
10
5
Polynomial List
Can we have
Sorted linked list?..
10
6
Addition of Polynomial List
List 1
Copy remaining
1<
4
9 =7
> 2
4
nodes of list 1
List 2
List 3
10
7
Sorted Linked List
Insertion in Queue:
Initial List
head 10 20 30
Create new_node , with data 40
Set p=null, q=head
10
8
Sorted Linked List
Case 3: q→data<new_node→data
1) new_node→next=p→next For case 3 try with new_node→data=25
2) p→next=new_node
10
9
VLab Demo
http://ds1-iiith.vlabs.ac.in/data-structures-1/exp/poly-arithmetic/exp.ht
ml#Demo
http://ds1-iiith.vlabs.ac.in/data-structures-1/exp/poly-arithmetic/exp.ht
ml#Practice
11
0