Professional Documents
Culture Documents
Singly & Circular Linked List
Singly & Circular Linked List
Circular
Linked List
Presented by
Md. Zabirul Islam
Md. Sharzul Mostafa
Manon Rahman
Aparajita Dola and
Shahriar Parvej
Department of CSE, KUET
Outlines
Introduction
Advantage of a Link List over Array
Link List Declaration
Basic Link List Operation
Circular Link List
Application
Disadvantage
Node
data link
Computer Science & Engineering , KUET 04/43
Structure of singly linked list
The head always points to the first node .
All nodes are connected to each other through Link fields.
Null pointer indicated end of the list.
head
data link data link data link
A B C X
Algorithm
Allocate a new node create a new node
Insert new element new->data=item
Make new node point to null new->link=NULL
Create head to point to new node head=new
A
Computer Science & Engineering , KUET 11/43
What Is Insertion
Algorithm
Allocate a new node create a new node
Insert new element new->data=item
Make new node to point to old head new->link=head
Update head to point to new node head=new
head new->link=head
head=new
B C
A
Computer Science & Engineering , KUET 15/43
Inserting at the last
Algorithm
Allocate a new node
create a new node
Insert new element new->data=item
Searching the last node ptr=head while(ptr->link!=null) ptr=ptr>link
Have old last node point to new node ptr->link=new
Update new node that point to null new->link=NULL
B C A
ptr->link=new
new->link=NULL
Algorithm
Allocate a new node create a new node
Insert new element new->data=item
Searching the given node ptr=head while(ptr->info!=data) temp=ptr
Store the previous node ptr = ptr>link
Update new to point to previous node link new->link=temp->link
Update previous node to point to new temp->link=new
node
Computer Science & Engineering , KUET 18/43
Inserting before given a value
head
data link data link data link
X
temp ptr
B D
A
New Node
new->link=temp->link
temp->link=new
C
Computer Science & Engineering , KUET 19/43
Inserting after given a value
X
ptr
D
A B New Node
new->link=ptr->link
ptr->link=new
C
Computer Science & Engineering , KUET 21/43
What Is Deletion?
Algorithm
Declare a node srt=head
Assign head to the new node head=srt->link
Update head with the link of the
new node
head srt=head
head=srt->link
A B C
Algorithm
Declare nodes srt, temp declare srt, temp
Traverse the list and update temp for(srt=head;;temp=srt,
srt=srt->link)
with the value of srt
if(srt->link==NULL) temp-
If the link of srt is NULL, update the link >link=NULL; Exit;
of temp with NULL and break the loop
head
A B C
Algorithm
Declare a node srt Node *srt
Search the element to delete after srt=head while(srt->info!=data) srt=srt>link
Update link of the node with the link of the srt->link=srt->link->link
next node ptr->link=new
head
data link data link data link
srt
A B C
C
Algorithm
Declare nodes srt, temp and prev Node *srt, *temp, *prev
Search the element to delete before srt=head while(srt->info!=data) srt=srt>link
prev = temp; temp = srt;
Update link of the previous node with the Prev->link = srt
link of the next node
A B C D
Algorithm
Declare nodes srt and temp Node *srt, *temp
Search the element to delete Srt =head while(srt->info!=data) srt=srt->link
Update link of the previous node with temp = srt;
the link of the selected node temp->link = srt->link
temp srt
A B C D
The operation insert last, is almost the same as that of the singly list, difference: head
instead of NULL in the link of new node
The operation insert after, is same as that of the singly list. If the node is the last node
then we can perform the operation just by calling the insert last’s function
The operation insert before, is same as that of the singly list
The operation delete after and before, is same as that of the singly list. Deleting last
element is also almost the same, only have to update the link of previous node with
head. But the process of deleting the first element is different, it’ll be discussed in this
presentation
The operation delete element, is same as that of the singly list
Computer Science & Engineering , KUET 36/43
Operations on a singly circular linked list
Most of the operations are almost the same as singly linked list incase of a singly
circular linked list, only a little variation in list creation and deleting the first element,
next contents contain these operations!
head
New Node New Node
head head
new->data=item B
A
new->link=head
Computer Science & Engineering , KUET 39/43
Deleting the first node
head last
head=srt->link A B C
last->link=head
Computer Science & Engineering , KUET 41/43
Application