linkedlist-notes
linkedlist-notes
Circular
Linked List
Outlines
Introduction
Advantage of a Link List over Array
Link List Declaration
Basic Link List Operation
Circular Link List
Application
Disadvantage
Introduction
Node
data link
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
Circular linked list
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
Creation
new->data=item
new->link=NULL
head=new
head
New Node
A
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
Inserting at the beginning
head new->link=head
head=new
B C
A
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
Inserting at the last
head
B C A
ptr->link=new
new->link=NULL
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
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
Inserting after given a value
X
ptr
D
A B New Node
new->link=ptr->link
ptr->link=new
C
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
Deleting the first node
head srt=head
head=srt->link
A B C
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
Deleting the last node
head
A B C
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
Deleting after a given value
head
data link data link data link
srt
A B C
C
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
Deleting before a given value
head
data link data link data link data link
A B C D
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
Deleting after a given value
head
data link data link data link data link
temp srt
A B C D
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
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!
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
Creation
head
New Node New Node
head head
new->data=item B
A
new->link=head
Deleting the first node
head last
head=srt->link A B C
last->link=head
Doubly Linked List
2.) which points has a reference to both the next point and
pervious point of node in list.
prev next
Info
NODE
Operations on a Doubly linked list
1) Create list.
2) Insert element at beginning in list.
3) Insert element at end in list.
4) Insert element at any place in list.
5) Delete element from the beginning of list.
6) Delete element from the end of list.
7) Delete element from any place from list.
Insert an element at beginning
doubly linked list
Delete an element at any place
doubly linked list
Doubly Linked list
Advantages Disadvantages
1. We can traverse in both 1. It requires more space per space
directions i.e. from starting to end per node because one extra field
and as well as from end to is required for pointer to previous
starting. node.
2. It is easy to reverse the linked 2. Insertion and deletion take
list. more time than linear linked list
3. If we are at a node, then we because more pointer operations
can go to any node. But in linear are required than linear linked
linked list, it is not possible to list.
reach the previous node.
Application