LINKED LISTS
SINGLE/ONE WAY/LINEAR LINKED LIST
It is the very common data structure often used to store similar data in memory. Elements
of the linked list are called nodes. Each node consists of a data part and a link part. A link
is a pointer or an address that indicates the location of the node containing the successor
of the list element. N stands for NULL which indicates that this is the last node in the list.
Why Linked Lists are preferred over arrays?
For storing similar data in memory, we can use either an array or a linked list. Arrays are
simple to understand and elements of an array are accessible but arrays suffer from the
following limitations.
Arrays have a fixed dimension. Once the size of an array is decided it cannot be
increased or decreased during execution.
Arrays elements are always stored in contiguous memory locations. At time it
might so happen that enough contiguous location might not be available for the
array that we are trying to create.
Operation like insertion of a new element or deletion of existing element from the
array is pretty tedious. This is because during insertion or deletion each element
has to be shifted one position to the right or to the left.
Linked list overcome all these limitations.
A linked list can grow and shrink in size during its life time that is there is no
maximum size of linked list.
As nodes of linked list are stored at different memory locations it hardly happened
that we fall short of memory when required.
Unlike arrays while inserting or deleting the nodes of the linked list, shifting of
nodes is not required.
BUILDING A LINKED LIST
Allocate the memory for the first node.
200
Set a value in the data part and NULL in the link part. 70 N
200
Allocate the memory for the second node.
100
Set a value in the data part and NULL in the link part. 50 N
100
Set the link part of the first node with the address of the second.
70 100 50 N
200 100
OPEARATIONS ON THE LINKED LIST
To understand different operations on the linked list, we have a structure “node” to
represent a node. The structure “node” contains a data part and a link part.
struct node
{
struct node *link;
int data;
} *p;
The variable “p” has been declared as pointer to a node. We have used this pointer as
pointer to the 1st node in the linked list.
How many nodes get added to the linked list? P would continue to point t the 1 st node in
the list. When no node has added to the list, p has been set to NULL to indicate that the
list is empty.
Addition of a node in a linked list
To an empty list.
At the end.
At the beginning.
In the middle (anywhere)
Append function
This function deals with two situations:
The node is being added to an empty list.
The node is being added at the end of the existing list.
In the 1st case the condition
if(p = = NULL) gets satisfied then space is allocated for the node using new operator.
temp = new node;
Data and link part of this node are setup using the statements.
temp→data = num;
temp→link = NULL;
Lastly p is made to point this node.
In the 2nd case when the linked list is not empty, the condition
if(p = = NULL) would fail. Now temp is made to point to the first node in the list.
temp=p;
Then using temp we have traversed through the entire linked list using a statement
while(temp→link !=NULL)
temp= temp→link;
Every time through the loop the statement
temp= temp→link;
makes temp point to the next node in the list. When temp reaches the last node, the
condition
temp→link !=NULL
would fail.
Allocate the memory for the new node and set pointer “r” to point this node.
r = new node;
Set a value in the data part and NULL in the link part of the new node. All that now
remains to be done is connecting the previous last node to the new last node. This is done
through statement.
temp→link = r;
p temp
70 200 50 400 10 N
100 200 400
p temp r
70 200 50 400 10 300 10 N
100 200 400 300
Append function
p=NULL;
append(int num)
{
node *temp, *r;
if(p = = NULL)
{
temp = new node;
temp→data = num;
temp→link = NULL;
p = temp;
}
else
{
temp=p;
while(temp→link !=NULL)
temp= temp→link;
r = new node;
r→data = num;
r→link = NULL;
temp→link = r;
}
}
Add a node at the beginning
For adding a new node at the beginning, first space is allocated for the node and data is
stored in it.
temp = new node;
temp→data = num;
Now we need to make a link part of this node points to the existing 1st node.
temp→link = p;
Lastly this new node must be made the 1st node in the list. This is done by the statement.
p = temp;
temp p
70 200 50 400 10 300 40 N
100 200 400 300
p temp
70 200 50 400 10 300 40 N
100 200 400 300
Add a node at the beginning
Add_at_beg(int num)
{
node *temp;
temp = new node;
temp→data = num;
temp→link = p;
p = temp;
}
Adding a node after specific node
To begin with, “temp” is set to point the 1st node through statement
temp = p;
Then through loop, we skip number of nodes after which a new node is to be added.
Suppose we wish to add a new node after the 3 rd node in the list. Allocate memory for the
new node and set pointer “r” to point this node.
r = new node;
Using “r” set a value in the data part of the node. All that remains to be done is
readjustment of links such that the new node adjusts in between the 3 rd and 4th node and
this is achieved through the statement.
r→link = temp→link;
temp→link = r;
r
50 N
p temp 300
70 200 50 400 10 N
100 200 400
r
50 400
p temp 300
70 200 50 300 10 N
100 200 400
Adding a node after specific node
Add_after(int loc, int num)
{
node *temp, *r;
temp = p;
for(int i = 0; i<loc ; i++)
{
if(temp = = NULL)
{
printf(“There are less nodes in the list”);
return;
}
temp = temp→link;
}
r = new node;
r→data = num;
r→link = temp→link;
temp→link = r;
}
Deletion of a specific node from the linked list
Case (first element)
In this function, through the while loop we have traversed through the entire linked list,
checking at each node whether it is the node to be deleted. If so, we have checked, if the
node being deleted is the first node in the list. If so, we have simply shifted “p” to the
next node and then deleted the earlier node.
Case (Intermediate node)
If the node to be deleted is the intermediate node, then the position of various pointers
and the links before and after deletion is shown below.
P old temp
70 200 50 400 10 300 40 N
100 200 400 300
p old
70 200 50 300 40 N
100 200 300
Deletion of a specific node from the linked list
del(int num)
{
node *temp, *old;
temp = p;
while(temp!=NULL)
{
if(temp→data = = num)
{
if(temp= =p)
p = temp→link;
else
old→link = temp→link;
delete temp;
return;
}
else
{
old = temp;
temp = temp→link;
}
}
printf(“Element not found”);
}
Display function
display()
{
node *temp = p;
while(temp != NULL)
{
printf(“%d”, temp→data);
temp = temp→link;}}
Count function
count()
{
node *temp = p;
int c = 0;
while(temp != NULL)
{
temp = temp→link;
c ++;
}
return c;
}
CIRCULAR LINKED LIST
The linked list in which link field of he last node contain a pointer back to the first node
rather than a NULL is called circular linked list.
2 200 3 400 4 300 8 100
100 200 400 300
Although a linear linked list is a useful data structure, it has several short comings. For
example, given a pointer p to a node in a linear list, we cannot reach any of the nodes that
precede the node to which p is pointing. This disadvantage can be overcome by circular
linked list.
From any point in such a list it is possible to reach any other point in the list. If we begin
at a given node and traverse the entire list, we ultimately end up at the starting point. A
circular linked list does not have a first or last node. We must, therefore, establish a first
and last node by convention. A circular linked list can be used to represent a stack and a
queue.
Addition in the circular linked list
Add_cirq (int num)
{
node *q;
q = new node;
q→data = num;
if( front == NULL)
front = q;
else
rear→link = q;
rear = q;
rear→link = front;
}
front rear q
50 200 70 400 80 100 30
100 200 400 500
New Node
front rear q
50 200 70 400 80 500 30 100
100 200 400 500
Deletion of a specific node from circular linked list
Del_cirq (int num)
{
node *q;
int num;
if(front = =NULL)
printf(“Queue is empty”);
else
{
if(front == rear)
{
num = front→data;
delete front;
front = NULL;
rear = NULL;
}
else
{
q = front;
num = q→data;
front = front→link;
rear→link = front;
delete q;
}
}
Front Rear
50 200 70 400 80 500 30 100
100 200 400 500
Front Rear
50 200 70 400 80 500 30 200
100 200 400 500
Display function
cirq_display()
{
node *q, *p;
q = front;
p = front;
do
{
printf(“%d”, q→data);
q = q→link;
}
while(q != p)
}
DOUBLE LINKED LIST
The linked list in which each node stores not only the address of next node but also the
address of previous node is called double linked list or two way linked list.
Node Prev data next
N 11 100 200 2 400 100 14 N
200 100 400
OPERATIONS ON THE DOUBLE LINKED LIST
To understand different operations on double linked list, we have a structure “dnode” to
represent a node. The structure “dnode” contains a data part and two link parts to point to
next and previous nodes. We have declared two pointers “next” and “prev” of dnode
type. The variable p has been declared as pointer to the 1st node.
struct dnode
{
dnode *prev;
int data;
dnode *next;
} *p;
Append function
d_append (int num)
{ dnode *q, *r;
if(q = = NULL)
{ q = new dnode;
q→data = num;
q→prev = NULL;
q→next = NULL;
p = q;
}else
{ while(q→next !=NULL)
q = q→next;
r = new dnode;
r→data = num;
r→next = NULL;
r→prev = q;
q→next = r;
}
}
Add a node at the beginning
Add_at_beg(int num)
{ dnode *q;
q = new dnode;
q→prev = NULL;
q→next = p;
q→data = num;
p→prev = q;
p = q;
}
Adding a node after specific node in double linked list
Add_after(int loc, int num)
{
dnode *q, *r;
q = p;
for(int i = 0; i<loc ; i++)
{ q = q→next;
if(q == NULL)
{
printf(“There are less nodes in the list”);
return;
}
q = q→prev;
r = new dnode;
r→data = num;
r→prev = q;
r→next = q→next;
r→next→prev = r;
q→next = r;
}
temp
p q 300
N 90 N
N 11 100 200 2 400 100 14 N
200 100 400
p q
N 11 100 200 2 300 100 90 400 300 14 N
200 100 300 400
Deletion of a specific node from the double linked list
d_del(int num)
{
dnode *q = p;
while(q!=NULL)
{
if(q→data = =num)
{
if(q = = p)
{
p = p→next;
p→prev = NULL;
}
else
{
if(q→next == NULL)
q→prev→next = NULL;
else
{
q→prev→next = q→next;
q→next→prev = q→prev;
}
delete q;
}
return;
}
q = q→next;
printf(“Element not found”);
}
CIRCULAR DOUBLE LINKED LIST
The double linked list in which “next” pointer field of last node contains address of 1 st
node instead of NULL and “prev” pointer field of 1 st node contains address of last node
instead of NULL is called circular double linked list.
400 11 100 200 2 400 100 14 200
200 100 400