Singly &
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
Computer Science & Engineering , KUET 01/43
Introduction
What Is Linked List?
The most commonly used data structure to store data in memory.
A linear collection of data elements, called nodes.
Unlike arrays, linked list elements are not stored at adjacent location.
The nodes connect by pointers.
Computer Science & Engineering , KUET 02/43
What’s wrong with Array and Why linked lists?
Disadvantages of arrays as storage data structures:
Insertion and deletion operations are slow.
Slow searching in unordered array.
Wastage of memory.
Fixed size.
Linked lists solve some of these problems :
Insertions and deletions are simple and faster.
Linked list is able to grow in size.
Linked List reduces the access time.
No wastage of memory.
Computer Science & Engineering , KUET 03/43
Singly Linked List
Singly linked list is a collection of nodes.
Each node contains two parts.
The data contains elements .
Link contains address of next node .
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
Computer Science & Engineering , KUET 05/43
Circular linked list
A circular linked list is one which has
No ending.
The null pointer in the last node of a linked list is replaced with the
address of its first node .
Computer Science & Engineering , KUET 06/43
Structure of circular linked list
CIRCULAR LINKED LIST:-
A 1000 B 2000 C 4000
4000 1000 2000
Computer Science & Engineering , KUET 07/43
Operations on linked list
The basic operations on linked lists are :
1. Creation
2. Insertion
3. Deletion
4. Traversing
5. Searching
Computer Science & Engineering , KUET 08/43
What Is creation
The creation operation is used to create a linked list.
Declare Node struct for nodes.
Data - any type
Link - a pointer to the next node.
Struct node{
int data;
Struct node *link;
}
Computer Science & Engineering , KUET 09/43
What Is creation
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
Computer Science & Engineering , KUET 10/43
Creation
new->data=item
new->link=NULL
head=new
head
New Node
A
Computer Science & Engineering , KUET 11/43
What Is Insertion
Insertion operation is used to insert a new node in the
linked list at the specified position.
When the list itself is empty , the new node is inserted
as a first node.
Computer Science & Engineering , KUET 12/43
Types of Insertion
There are many ways to insert a new node
into a list :
As the new first element.
As the new last element.
Before a given value.
After a given value.
Computer Science & Engineering , KUET 13/43
Inserting at the beginng
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
Computer Science & Engineering , KUET 14/43
Inserting at the beginning
head new->link=head
head=new
data link data link
New Node
X
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
Computer Science & Engineering , KUET 16/43
Inserting at the last
head
data link data link New Node
B C A
ptr->link=new
new->link=NULL
Computer Science & Engineering , KUET 17/43
Inserting before given a value
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
Allocate a new node, new
Algorithm
Insert new element create a new node
Searching the node new->data=item
Update new to point to the link of the ptr=head while(ptr->info!=data) ptr=ptr>link
given node new->link=ptr->link
Update given node to point to new ptr->link=new
node
Computer Science & Engineering , KUET 20/43
Inserting after given a value
head
data link data link data link
X
ptr
D
A B New Node
new->link=ptr->link
ptr->link=new
C
Computer Science & Engineering , KUET 21/43
What Is Deletion?
Deletion operation is used to delete a particular node in
the linked list.
we simply have to perform operation on the link of the
nodes(which contains the address of the next node) to
delete a particular element.
Computer Science & Engineering , KUET 22/43
Types of Deletion
Deletion operation may be performed in
the following conditions:
Delete first element.
Delete last element.
Delete before a given value.
Delete after a given value.
Delete a particular element
Computer Science & Engineering , KUET 23/43
Deleting the first node
Algorithm
Declare a node srt=head
Assign head to the new node head=srt->link
Update head with the link of the
new node
Computer Science & Engineering , KUET 24/43
Deleting the first node
head srt=head
head=srt->link
data link data link data link
X
srt
A B C
Computer Science & Engineering , KUET 25/43
Deleting the last node
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
Computer Science & Engineering , KUET 26/43
Deleting the last node
head
data link data link data link
X X
A B C
Computer Science & Engineering , KUET 27/43
Deleting after a given value
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
Computer Science & Engineering , KUET 28/43
Deleting after a given value
head
data link data link data link
srt
A B C
C
Computer Science & Engineering , KUET 29/43
Deleting before a given value
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
Computer Science & Engineering , KUET 30/43
Deleting before a given value
head
data link data link data link data link
prev temp srt
A B C D
Computer Science & Engineering , KUET 31/43
Deleting a given value
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
Computer Science & Engineering , KUET 32/43
Deleting after a given value
head
data link data link data link data link
temp srt
A B C D
Computer Science & Engineering , KUET 33/43
Operations on a singly circular linked list
The basic operations on singly circular linked lists are :
1. Creation
2. Insertion
3. Deletion
4. Traversing
5. Searching
Computer Science & Engineering , KUET 34/43
Operations on a singly circular linked list
The operations on a singly circular linked is almost same as that of a singly
linked list. The only difference between them is that the link of the last node of
a singly linked list contains NULL, whereas that of a circular list contains head
Creation of a circular linked list is same as singly list except that we will have to
use head instead of NULL
To traverse the list we have to start from head and end until the link of a node is
head
To insert a node at the beginning of the list we have to store the value of head
in a temporary node and operate accordingly
Computer Science & Engineering , KUET 35/43
Operations on a singly circular linked list
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!
Computer Science & Engineering , KUET 37/43
Creation of a circular list
Algorithm
Allocate a new node
create a new node
Insert new element
new->data=item
Make new node point to head
new->link=head
Update head to point to new node of first
element head=new, temp=new, if
head==NULL
If not first element update link of previous
element with current node address else temp->link=new,
temp=ptr
Computer Science & Engineering , KUET 38/43
Creation
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
Declare a new node, srt Algorithm
Assign head to the new node srt=head
Update head with the link of the new head=srt->link
node last->link=head
Update the link of last node with the link
of the new node
Computer Science & Engineering , KUET 40/43
Deleting the first node
head last
data link data link data link
head
head=srt->link A B C
last->link=head
Computer Science & Engineering , KUET 41/43
Application
Linked lists are used in many other data structures.
Linked lists are used in polynomial manipulation.
Representation of trees, stacks, queues. etc.
Computer Science & Engineering , KUET 42/43
Disadvantages of Linked Lists
The memory is wasted as pointers require extra memory for storage.
No element can be accessed randomly.
It has to access each node sequentially.
Reverse Traversing is difficult in linked list.
No binary search.
Computer Science & Engineering , KUET 43/43
Questions?
Thank you