Linked List
Linked List
LINKED LISTS
Linked lists and array s are simila r since they both stor e collection s of
dat a. One way to think abou t linked lists is to look at how arr ay s work
and think abou t alter n a t e app ro a c h e s .
Array is the most common data structure used to store collections of elements. Arrays are convenient
to declare and provide the easy syntax to access any element by its index number. Once the array is
set up, access to any element is convenient and fast.
The size of the arr ay is fixed. Most often this size is specified at
compile time. This make s the prog r a m m e r s to allocat e arr ays ,
which see m s "larg e enou g h" than requi r e d .
Linked lists have their own strengths and weaknesses, but they happen to be strong where arrays are
weak. Generally array's allocates the memory for all its elements in one block whereas linked lists use
an entirely different strategy.
Linked lists allocat e mem o ry for each elem e n t sep a r a t e ly and only when
nec es s a r y.
Her e is a quick review of the ter mi nology and rules of point e r s . The
linked list code will dep e n d on the following functions :
Since a void * is returned the C standard states that this pointer can be converted to any type. For
example,
char *cp;
cp = (char *) malloc (100);
Attempts to get 100 bytes and assigns the starting address to cp. We can also use the sizeof()
function to specify the number of bytes. For example,
int *ip;
ip = (int *) malloc (100*sizeof(int));
59
free() is the opposite of malloc(), which de-allocates memory. The argument to free() is a pointer to a
block of memory in the heap — a pointer which was obtained by a malloc() function. The syntax is:
free (ptr);
The advantage of free() is simply memory management when we no longer need a block.
6. 1. Link e d List:
A linked list is a non-sequential collection of data items. It is a dynamic data structure. For every data
item in a linked list, there is an associated pointer that would give the memory location of the next
data item in the linked list.
The data items in the linked list are not in consecutive memory locations. They may be anywhere, but
the accessing of these data items is easier as each data item contains the address of the next data
item.
1. Linked lists are dyna mic dat a struc t u r e s . i.e., they can grow or
shrink durin g the executio n of a prog r a m .
3. Inse r tio n and Deletion s are easie r and efficient. Linked lists
provide flexibility in inse rtin g a dat a item at a specified position
and deletion of the dat a item from the given position.
4. Many complex applica tio n s can be easily carri e d out with linked
lists.
Basically we can put linked lists into the following four item s:
60
3. Circul a r Linke d List.
4. Circul a r Double Linked List.
A single linked list is one in which all node s are linked toget h e r in som e
sequ e n ti al man n e r. Henc e, it is also called as linea r linked list.
A doubl e linked list is one in which all node s are linked toget h e r by
multiple links which helps in acce s si n g both the succ e s s o r node (next
node) and pred e c e s s o r node (previous node) from any arbit r a r y node
within the list. Ther efo r e eac h node in a double linked list has two link
fields (point e r s ) to point to the left node (previou s) and the right node
(next). This helps to trave r s e in forwa r d direc tio n and backw a r d
direc tio n.
A circula r linke d list is one, which has no beginni n g and no end. A single
linked list can be mad e a circul a r linked list by simply storin g add r e s s of
the very first node in the link field of the last node.
A circula r double linked list is one, which has both the succ e s s o r point e r
and pred e c e s s o r pointe r in the circula r man n e r .
P(x) = a 0 Xn + a 1 Xn- 1 + …… + a n- 1 X + a n
61
2. Repr e s e n t very larg e num b e r s and oper a tio n s of the larg e num b e r
such as addition, multiplica tio n and division.
A linked list allocates space for each element separately in its own block of memory called a "node".
The list gets an overall structure by using pointers to connect all its nodes together like the links in a
chain.
Each node cont ai n s two fields; a "dat a" field to stor e what ev e r elem e n t ,
and a "next" field which is a point e r used to link to the next node.
Each node is alloca t e d in the hea p using malloc(), so the node mem o ry
contin u e s to exist until it is explicitly de- allocat e d using free(). The front
of the list is a point e r to the “star t ” node. A single linked list is show n in
figure 6.2.1.
S TAC K H EAP
100
st a rt
10 200 20 300 30 400 40 X
T h e st art 100 200 300 400
p o int er
h o ld s t h e Ea c h n o d e St o r e s t h e n e xt T h e n e xt f i e l d of
a d dr e s s of st or e s t h e d at a. n o d e a d dr e s s. t h e l a st n o d e i s
t h e f ir s t N ULL.
n o d e of t h e
l i st .
Fi g u r e 6 . 2 . 1 . S i n g l e L i n k e d L i s t
The beginni n g of the linked list is store d in a " start " point e r which points
to the first node. The first node cont ai n s a point e r to the secon d node.
The secon d node cont ai n s a point e r to the third node, ... and so on. The
last node in the list has its next field set to NULL to mark the end of the
list. Code can acce s s any node in the list by sta rti n g at the start and
following the next point e r s .
62
Before writing the code to build the above list, we nee d to crea t e a start
node , used to crea t e and acce s s othe r node s in the linked list. The
following struc t u r e definition will do (see figur e 6.2.2):
st r u ct s l i n k l ist
{
int d at a; node: d at a n e xt
st r u ct s l i n k l ist * n e x t ;
};
t y p e d e f st r u ct s l i n k l ist n o d e ; st a rt
n o d e * st a r t = N U L L; E m p t y li s t : NULL
Fi g u r e 6 . 2 . 2 . S t r u c t u r e d e f i n it i o n , s i n g l e l i n k n o d e a n d e m p t y l i s t
Crea tio n.
Inse r tio n.
Deletion.
Traver si n g.
Creating a singly linked list starts with creating a node. Sufficient memory has to be allocated for
creating a node. The information is stored in the memory, allocated by using the malloc() function.
The function getnode(), is used for creating a node, after allocating memory for the structure of type
node, the information for the item (i.e., data) has to be read from the user, set next field to NULL and
finally returns the address of the node. Figure 6.2.3 illustrates the creation of a node for single linked
list.
no d e* g et no d e( )
{ new node
no d e* n e w no d e;
n e w n o d e = ( n o d e * ) m a l l o c ( s iz e o f ( n o d e ) ) ; 10 X
p r i n t f ( " \ n En t e r d a t a : " ) ; 100
s ca n f ( " % d " , & n e w n o d e - > d a t a ) ;
n e w n o d e - > n e x t = N U L L;
r et u r n n e w no d e;
}
Fi g u r e 6 . 2 . 3 . n e w n o d e w it h a v a l u e o f 1 0
63
Get the new node using get no d e ().
start = newnode;
If the list is not empty, follow the steps given below:
The next field of the new node is mad e to point the first
node (i.e. sta rt node) in the list by assignin g the addr e s s of
the first node.
st a rt
100
Fi g u r e 6 . 2 . 4 . S i n g l e L i n k e d L i s t w it h 4 n o d e s
The function cre a t elis t(), is used to cre a t e ‘n’ num b e r of node s:
v o i d c r e a t e list ( i n t n )
{
i n t i;
no d e * n e w no d e;
no d e * t e m p ;
fo r( i = 0 ; i < n ; i+ + )
{
n e w no d e = g et no d e( ) ;
if ( st a r t = = N U L L)
{
st a rt = n e w no d e;
}
e ls e
{
t e m p = st a r t ;
w h i l e ( t e m p - > n e x t ! = N U L L)
tem p = t em p - > next;
t e m p - > n e x t = n e w no d e;
}
}
}
64
Ins e r t i o n of a Nod e :
One of the most primitive operations that can be done in a singly linked list is the insertion of a node.
Memory is to be allocated for the new node (in a similar way that is done while creating a list) before
reading the data. The new node will contain empty data field and empty next field. The data field of
the new node is then stored with the information read from the user. The next field of the new node is
assigned to NULL. The new node can then be inserted at three different places namely:
Ins e r t i n g a nod e at th e be g i n n i n g :
The function inser t_at_b e g(), is used for insertin g a node at the
beginni n g
Figur e 6.2.5 shows inser ti n g a node into the single linked list at the
beginni n g.
65
st a rt
500
5 100
500
Fi g u r e 6 . 2 . 5 . I n s e r t i n g a n o d e a t t h e b e g i n n i n g
Ins e r t i n g a nod e at th e en d:
The following steps are followe d to inse r t a new node at the end of the
list:
newnode = getnode();
tem p = start;
while(te m p -> next != NULL)
tem p = tem p -> next;
temp -> next = newnode;
The function inser t_at_e n d(), is use d for inser ti n g a node at the end.
Figur e 6.2.6 shows inse r ti n g a node into the single linked list at the end.
st a rt
100
50 X
500
Fi g u r e 6 . 2 . 6 . I n s e r t i n g a n o d e a t t h e e n d .
The following steps are followed, to insert a new node in an intermediate position in the list:
66
Ens u r e that the specified position is in betw e e n first node and
last node. If not, specified position is invalid. This is done by
count n o d e () function.
The function inser t_at_mi d(), is used for inser ti n g a node in the
inter m e d i a t e position.
Figure 6.2.7 shows inserting a node into the single linked list at a specified intermediate position other
than beginning and end.
st a rt pr ev t e mp
100
50 300
500 new node
Fi g u r e 6 . 2 . 7 . I n s e r t i n g a n o d e a t a n i n t e r m e d i a t e p o s it i o n .
Del e t i o n of a nod e:
Anothe r primitive oper a tio n that can be done in a singly linked list is the
deletion of a node. Memo ry is to be rele a s e d for the node to be delet e d. A
node can be delet e d from the list from thre e differe n t place s nam ely.
Del e t i n g a nod e at th e be g i n n i n g :
67
The following steps are followe d, to delet e a node at the beginni n g of the
list:
tem p = start;
star t = star t -> next;
free(t e m p );
The function delete_at_beg(), is used for deleting the first node in the list.
Figure 6.2.8 shows deleting a node at the beginning of a single linked list.
st a rt
200
Fi g ur e 6. 2. 8. De l e t i n g a n o d e at t h e b e g i n n i n g.
Del e t i n g a nod e at th e en d:
The following steps are followe d to delet e a node at the end of the list:
The function delet e_a t_las t(), is used for deletin g the last node in the list.
Figur e 6.2.9 shows deletin g a node at the end of a single linked list.
68
st a rt
100
10 200 20 300 30 X 40 X
100 200 300 400
Fi g u r e 6 . 2 . 9 . D e l e t i n g a n o d e a t t h e e n d .
Figure 6.2.10 shows deleting a node at a specified intermediate position other than beginning and
end from a single linked list.
st a rt
100
Fi g u r e 6 . 2 . 1 0 . De l e t i n g a n o d e a t a n i n t e r m e d i a t e p o s it i o n .
69
To display the inform a tio n, you have to trave r s e (move) a linked list,
node by node from the first node, until the end of the list is reac h e d .
Traver si n g a list involves the following steps:
v o id t r a v e rs e( )
{
no d e * t e m p ;
t e m p = st a r t ;
p r i n t f ( " \ n T h e co n t e n t s o f L ist ( L e f t t o Ri g h t ) : \ n " ) ;
if ( st a r t = = N U L L )
p r i n t f ( " \ n Em p t y L ist " ) ;
e ls e
w h i l e ( t e m p ! = N U L L)
{
p r int f( " % d - > " , t e m p - > d at a) ;
tem p = t em p - > next;
}
p r i n t f ( " X" ) ;
}
v o i d r e v _ t r a v e r s e ( n o d e * st )
{
if ( st = = N U L L)
{
r et u r n;
}
e ls e
{
r e v _ t r a v e r s e ( st - > n e x t ) ;
p r i n t f ( " % d - > " , st - > d a t a ) ;
}
}
Cou n t i n g th e Nu m b e r of Nod e s :
The following code will count the num b e r of node s exist in the list using
recursio n .
70
i n t co u n t n o d e ( n o d e * st )
{
if ( st = = N U L L)
r et u r n 0 ;
e ls e
r e t u r n ( 1 + co u n t n o d e ( st - > n e x t ) ) ;
}
6.3. A Complete Source Code for the Implementation of Single Linked List:
stru ct slinklist
{
int data;
stru ct slinklist *next;
};
int menu()
{
int ch;
clrscr();
printf("\n 1.Cre a t e a list ");
printf("\n- -------------------------");
printf("\n 2.Inse r t a node at beginnin g ");
printf("\n 3.Inse r t a node at end");
printf("\n 4.Inse r t a node at middle");
printf("\n- -------------------------");
printf("\n 5.Delet e a node from begin nin g");
printf("\n 6.Delet e a node from Last");
printf("\n 7.Delet e a node from Middle");
printf("\n- -------------------------");
printf("\n 8.Trave r s e the list (Left to Right)");
printf("\n 9.Trave r s e the list (Right to Left)");
printf("\n- -------------------------");
printf("\n 10. Count node s ");
printf("\n 11. Exit ");
printf("\n\n Enter your choice: ");
scanf("%d",&ch);
retu r n ch;
}
71
}
retu r n (count);
}
72
star t = newno d e;
}
}
void insert_at_mid()
{
node *newnod e, *temp, *prev;
int pos, nodec t r , ctr = 1;
newno d e = getno d e();
printf("\n Ente r the position: ");
scanf("%d", &pos);
nodect r = count n o d e ( s t a r t );
if(pos > 1 && pos < nodect r)
{
tem p = prev = start;
while(ctr < pos)
{
prev = tem p;
tem p = temp -> next;
ctr + + ;
}
prev -> next = newno d e ;
newno d e -> next = tem p;
}
else
{
printf("position %d is not a middle position", pos);
}
}
73
printf("\n Empty List..");
retu r n ;
}
else
{
tem p = start;
prev = star t;
while(te m p -> next != NULL)
{
prev = tem p;
tem p = temp -> next;
}
prev -> next = NULL;
free(t e m p );
printf("\n Node delet e d ");
}
}
}
if(pos > 1 && pos < nodect r)
{
tem p = prev = start;
while(ctr < pos)
{
prev = tem p;
tem p = temp -> next;
ctr + + ;
}
prev -> next = tem p -> next;
free(t e m p );
printf("\n Node delet e d ..");
}
else
{
printf("\n Invalid position..");
getch();
}
}
}
void main(void)
{
int ch, n;
clrscr();
while(1)
{
ch = men u();
switch(c h)
{
case 1:
74
if(star t = = NULL)
{
printf("\n Num b e r of nodes you want to crea t e : ");
scanf("%d", &n);
creat elist(n);
printf("\n List crea t e d . .");
}
else
printf("\n List is alre a dy cre at e d . .");
brea k;
case 2:
insert_at_be g();
brea k;
case 3:
insert_at_en d();
brea k;
case 4:
insert_at_mid();
brea k;
case 5:
delet e_at_be g();
brea k;
case 6:
delet e_at_las t();
brea k;
case 7:
delet e_at_mid();
brea k;
case 8:
trave r s e();
brea k;
case 9:
printf("\n The conte n t s of List (Right to Left): \n");
rev_tr av e r s e ( s t a r t );
printf(" X ");
brea k;
case 10:
printf("\n No of nodes : %d ", count no d e( s t a r t ));
brea k;
case 11 :
exit(0);
}
getch();
}
}
A double linked list is a two-way list in which all nodes will have two links. This helps in accessing
both successor node and predecessor node from the given node position. It provides bi-directional
traversing. Each node contains three fields:
Left link.
Data.
Right link.
The left link point s to the pred e c e s s o r node and the right link points to
the succ e s s o r node. The data field store s the requir e d dat a.
Many applica tion s requir e sea rc hi n g forwa r d and backw a r d thru node s
of a list. For exam pl e sea rc hi n g for a nam e in a telep h o n e direct o ry
75
would nee d forw a r d and backw a r d scan ni n g thru a region of the whole
list.
Crea tio n.
Inse r tio n.
Deletion.
Traver si n g.
S TAC K St o r e s t h e H EAP
pr ev io u s n o d e
100 a d dr e s s.
st a rt
X 10 200 100 20 300 200 30 X
T h e st art 100 200 300
p o int er
h o ld s t h e St o r e s t h e d a t a . St o r e s t h e n e xt Th e r ig ht f ie ld of
a d dr e s s of n o d e a d dr e s s. t h e l a st n o d e i s
t h e f ir s t N ULL.
n o d e of t h e
l i st .
Fi g u r e 6 . 3 . 1 . D o u b l e L i n k e d L i s t
The begin ni n g of the double linked list is store d in a " start " point e r
which points to the first node. The first node’s left link and last node’s
right link is set to NULL.
st r u ct d l i n k l ist
{ node: lef t d at a r ig ht
st r u ct d l i n k l ist * l e f t ;
int d at a;
st r u ct d l i n k l ist * r i g h t ;
}; st a rt
E m p t y li s t : NULL
t y p e d e f st r u ct d l i n k l ist n o d e ;
n o d e * st a r t = N U L L;
Fi g u r e 6 . 4 . 1 . S t r u c t u r e d e f i n it i o n , d o u b l e l i n k n o d e a n d e m p t y l i s t
76
no d e* g et no d e( )
{
no d e* n e w no d e;
n e w n o d e = ( n o d e * ) m a l l o c( s iz e o f ( n o d e ) ) ; new node
p r in t f ( " \ n En t e r d a t a : " ) ; X 10 X
sca n f ( " % d " , & n e w n o d e - > d a t a ) ;
n e w n o d e - > l e f t = N U L L; 100
n e w n o d e - > r i g h t = N U L L;
r et u r n n e w no d e;
}
Fi g u r e 6 . 4 . 2 . n e w n o d e w it h a v a l u e o f 1 0
new no d e = g e t n o d e ();
The function cre a t elis t(), is used to cre a t e ‘n’ num b e r of node s:
v o i d c r e a t e list ( i n t n )
{
i n t i;
no d e * n e w no d e;
no d e * t e m p ;
fo r( i = 0 ; i < n; i+ + )
{
n e w no d e = g et no d e( ) ;
if ( st a r t = = N U L L)
{
st a r t = n e w n o d e ;
}
e ls e
{
t e m p = st a r t ;
w h ile( t e m p - > r ig ht )
t e m p = t e m p - > r ig ht ;
t e m p - > r ig ht = n e w no d e;
n e w no d e - > left = t e m p ;
}
}
}
77
Figur e 6.4.3 shows 3 item s in a double linked list stor e d at differ e n t
locations.
st a rt
100
Fi g u r e 6 . 4 . 3 . D o u b l e L i n k e d L i s t w it h 3 n o d e s
Ins e r t i n g a nod e at th e be g i n n i n g :
new no d e = g e t n o d e ( );
The function dbl_ins e r t_b e g(), is use d for inser ti n g a node at the
beginni n g. Figur e 6.4.4 shows inser ti n g a node into the double linked list
at the begin ni n g.
st a rt
400
X 40 100
400
Fi g u r e 6 . 4 . 4 . I n s e r t i n g a n o d e a t t h e b e g i n n i n g
Ins e r t i n g a nod e at th e en d:
The following steps are followe d to inse r t a new node at the end of the
list:
78
newnode=getnode();
tem p = start;
while(te m p -> right != NULL)
tem p = tem p -> right;
tem p -> right = new no d e ;
new no d e -> left = temp;
The function dbl_ins e r t_e n d (), is used for inse rti n g a node at the end.
Figur e 6.4.5 shows inse r ti n g a node into the double linked list at the end.
st a rt
100
300 40 X
400
Fi g u r e 6 . 4 . 5 . I n s e r t i n g a n o d e a t t h e e n d
new no d e = g e t n o d e ( );
79
The function dbl_ins e r t_ mi d(), is used for inser ti n g a node in the
inter m e d i a t e position. Figur e 6.4.6 shows inser ti n g a node into the
double linke d list at a specifie d inter m e d i a t e position othe r than
beginni n g and end.
st a rt
100 40 200
100
400
400 20 300
X 10 400
200
100
200 30 X
300
Fi g u r e 6 . 4 . 6 . I n s e r t i n g a n o d e a t a n i n t e r m e d i a t e p o s it i o n
Del e t i n g a nod e at th e be g i n n i n g :
The following steps are followe d, to delet e a node at the beginni n g of the
list:
tem p = start;
star t = star t -> right;
star t -> left = NULL;
free(t e m p );
The function dbl_delet e_b e g(), is used for deletin g the first node in the
list. Figur e 6.4.6 shows deletin g a node at the begin ni n g of a doubl e
linked list.
st a rt
200
Fi g u r e 6 . 4 . 6 . D e l e t i n g a n o d e a t b e g i n n i n g
Del e t i n g a nod e at th e en d:
The following steps are followe d to delet e a node at the end of the list:
If list is empty then display ‘Empty List’ mess a g e
tem p = start;
80
while(te m p -> right != NULL)
{
tem p = tem p -> right;
}
tem p -> left -> right = NULL;
free(t e m p );
The function dbl_delet e_la s t(), is use d for deletin g the last node in the
list. Figur e 6.4.7 shows deletin g a node at the end of a double linked list.
st a rt
100
Fi g u r e 6 . 4 . 7 . D e l e t i n g a n o d e a t t h e e n d
81
The function delete_at_mid(), is used for deleting the intermediate node in the list. Figure 6.4.8 shows
deleting a node at a specified intermediate position other than beginning and end from a double
linked list.
st a rt
100
Fi g u r e 6 . 4 . 8 D e l e t i n g a n o d e a t a n i n t e r m e d i a t e p o s it i o n
To display the inform a ti o n, you have to trave r s e the list, node by node
from the first node, until the end of the list is reac h e d . The function
traver s e_left_righ t () is used for trave r si n g and displaying the inform a ti o n
stor e d in the list from left to right.
The following steps are followe d, to trave r s e a list from left to right:
tem p = start;
while(te m p != NULL)
{
print tem p -> data;
tem p = tem p -> right;
}
To display the inform a tio n from right to left, you have to trave r s e the list,
node by node from the first node, until the end of the list is reac h e d . The
function travers e_rig h t_lef t () is used for trave r si n g and displayin g the
inform a tio n store d in the list from right to left.
The following steps are followe d, to trave r s e a list from right to left:
tem p = start;
while(te m p -> right != NULL)
tem p = tem p -> right;
while(te m p != NULL)
82
{
print tem p -> data;
tem p = tem p -> left;
}
Cou n t i n g th e Nu m b e r of Nod e s :
The following code will count the num b e r of node s exist in the list (using
rec u r sio n).
i n t co u n t n o d e ( n o d e * st a r t )
{
if ( st a r t = = N U L L)
r et u r n 0 ;
e ls e
r e t u r n ( 1 + co u n t n o d e ( st a r t - > r i g h t ) ) ;
}
6.5. A Complete Source Code for the Implementation of Double Linked List:
stru ct dlinklist
{
stru ct dlinklist *left;
int data;
stru ct dlinklist *right;
};
83
retu r n 0;
else
retu r n 1 + count n o d e ( s t a r t -> right);
}
int menu()
{
int ch;
clrscr();
printf("\n 1.Cre a t e ");
printf("\n- -----------------------------");
printf("\n 2. Inser t a node at begin nin g ");
printf("\n 3. Inser t a node at end");
printf("\n 4. Inser t a node at middle");
printf("\n- -----------------------------");
printf("\n 5. Delete a node from beginni n g");
printf("\n 6. Delete a node from Last");
printf("\n 7. Delete a node from Middle");
printf("\n- -----------------------------");
printf("\n 8. Traver s e the list from Left to Right ");
printf("\n 9. Traver s e the list from Right to Left ");
printf("\n- -----------------------------");
printf("\n 10.Cou n t the Num b e r of node s in the list");
printf("\n 11.Exit ");
printf("\n\n Enter your choice: ");
scanf("%d", &ch);
retu r n ch;
}
84
node *temp;
tem p = start;
printf("\n The conte n t s of List: ");
if(star t = = NULL)
printf("\n Empty List");
else
while(te m p -> right != NULL)
tem p = temp -> right;
while(te m p != NULL)
{
printf("\t%d", temp -> data);
tem p = temp -> left;
}
}
85
tem p -> right = newno d e ;
}
else
printf("position %d of list is not a middle position ", pos);
}
86
while(i < pos)
{
tem p = temp -> right;
i+ + ;
}
tem p -> right -> left = temp -> left;
tem p -> left -> right = temp -> right;
free(t e m p );
printf("\n node delet e d ..");
}
else
{
printf("\n It is not a middle position..");
getch();
}
}
}
void main(void)
{
int ch, n;
clrscr();
while(1)
{
ch = men u();
switch( ch)
{
case 1 :
printf("\n Ente r Numb e r of nodes to creat e: ");
scanf("%d", &n);
creat elist(n);
printf("\n List crea t e d . .");
brea k;
case 2 :
dll_inser t_b e g();
brea k;
case 3 :
dll_inser t_en d ();
brea k;
case 4 :
dll_inser t_mid();
brea k;
case 5 :
dll_delet e_b e g();
brea k;
case 6 :
dll_delet e_last();
brea k;
case 7 :
dll_delet e_mi d();
brea k;
case 8 :
trave r s e_left_to_righ t();
brea k;
case 9 :
trave r s e_rig h t_to_left();
brea k;
case 10 :
printf("\n Num b e r of nodes: %d", count n o d e( s t a r t ));
brea k;
case 11:
exit(0);
}
getch();
}
}
87
6. 6. Circ u l a r Sin g l e Link e d List:
It is just a single linke d list in which the link field of the last node points
back to the add r e s s of the first node. A circula r linke d list has no
beginni n g and no end. It is nece s s a r y to esta blis h a special point e r called
start point e r always pointin g to the first node of the list. Circula r linked
lists are frequ e n tly use d inste a d of ordina ry linke d list beca u s e many
oper a tio n s are much easie r to imple m e n t . In circul a r linked list no null
point e r s are used, henc e all point e r s cont ai n valid addr e s s .
st a rt
100
Fi g u r e 6 . 6 . 1 . C ir c u l a r S i n g l e L i n k e d L i s t
Crea tio n.
Inse r tio n.
Deletion.
Traver si n g.
start = newnode;
tem p = start;
while(te m p -> next != NULL)
tem p = tem p -> next;
tem p -> next = newno d e ;
88
new no d e -> next = star t;
The function cre a t elis t(), is used to cre a t e ‘n’ num b e r of node s:
Ins e r t i n g a nod e at th e be g i n n i n g :
star t = new no d e ;
new no d e -> next = star t;
last = star t;
while(las t -> next != sta rt)
last = last -> next;
new no d e -> next = star t;
star t = new no d e ;
last -> next = start;
The function cll_inse r t_b e g(), is used for inser ti n g a node at the
beginni n g. Figur e 6.6.2 show s inser ti n g a node into the circula r single
linked list at the begin ni n g.
st a rt
500
5 100
500
Fi g u r e 6 . 6 . 2 . I n s e r t i n g a n o d e a t t h e b e g i n n i n g
Ins e r t i n g a nod e at th e en d:
The following steps are followe d to inse r t a new node at the end of the
list:
89
If the list is empty, assign new node as start.
star t = new no d e ;
new no d e -> next = star t;
The function cll_inse r t_e n d (), is used for inser ti n g a node at the end.
Figur e 6.6.3 shows inser ti n g a node into the circula r single linked list at
the end.
st a rt
100
50 100
500
Fi g u r e 6 . 6 . 3 I n s e r t i n g a n o d e a t t h e e n d .
Del e t i n g a nod e at th e be g i n n i n g :
The following steps are followe d, to delet e a node at the beginni n g of the
list:
After deletin g the node, if the list is empty the n start = NULL.
The function cll_delet e_b e g (), is used for deletin g the first node in the
list. Figur e 6.6.4 shows deletin g a node at the begin ni n g of a circul a r
90
single linked list.
st a rt
200
Fi g u r e 6 . 6 . 4 . D e l e t i n g a n o d e a t b e g i n n i n g .
Del e t i n g a nod e at th e en d:
The following steps are followe d to delet e a node at the end of the list:
tem p = start;
prev = star t;
while(te m p -> next != star t)
{
prev = temp;
tem p = tem p -> next;
}
prev -> next = start;
After deletin g the node, if the list is empty the n start = NULL.
The function cll_delet e_la s t(), is used for deletin g the last node in the
list.
Figur e 6.6.5 shows deletin g a node at the end of a circula r single linked
list.
st a rt
100
Fi g u r e 6 . 6 . 5 . D e l e t i n g a n o d e a t t h e e n d .
The following steps are followe d, to trave r s e a list from left to right:
91
If the list is not empty, follow the step s given below:
tem p = start;
do
{
printf("%d ", temp -> data);
tem p = tem p -> next;
} while(t e m p != sta rt);
stru ct cslinklist
{
int data;
stru ct cslinklist *next;
};
int menu()
{
int ch;
clrscr();
printf("\n 1. Crea t e a list ");
printf("\n\n- -------------------------");
printf("\n 2. Inser t a node at begin nin g ");
printf("\n 3. Inser t a node at end");
printf("\n 4. Inser t a node at middle");
printf("\n\n- -------------------------");
printf("\n 5. Delete a node from beginni n g");
printf("\n 6. Delete a node from Last");
printf("\n 7. Delete a node from Middle");
printf("\n\n- -------------------------");
printf("\n 8. Display the list");
printf("\n 9. Exit");
printf("\n\n- -------------------------");
printf("\n Ente r your choice: ");
scanf("%d", &ch);
retu r n ch;
92
}
void display()
{
node *temp;
tem p = start;
printf("\n The conte n t s of List (Left to Right): ");
if(star t = = NULL )
printf("\n Empty List");
else
do
{
printf("\t %d ", temp -> data);
tem p = temp -> next;
}while(te m p != start);
printf(" X ");
}
93
{
star t = newno d e;
newno d e -> next = start;
}
else
{
tem p = start;
while(te m p -> next != start)
tem p = temp -> next;
tem p -> next = newno d e ;
newno d e -> next = start;
}
printf("\n Node insert e d at end..");
nodect r + + ;
}
94
void cll_delet e_last()
{
node *temp,*pr ev;
if(star t = = NULL)
{
printf("\n No nodes exist..");
getch();
retu r n ;
}
else
{
tem p = start;
prev = star t;
while(te m p -> next != start)
{
prev = tem p;
tem p = temp -> next;
}
prev -> next = star t;
free(t e m p );
nodect r- -;
if(nodec t r = = 0)
star t = NULL;
printf("\n Node delet e d ..");
}
}
if(star t = = NULL)
{
printf("\n No nodes exist..");
getch();
retu r n ;
}
else
{
printf("\n Which node to delete: ");
scanf("%d", &pos);
if(pos > nodec t r)
{
printf("\nThis node does not exist");
getch();
retu r n ;
}
if(pos > 1 && pos < nodect r)
{
tem p = s t a r t ;
prev = star t;
i = 0;
while(i < pos - 1)
{
prev = tem p;
tem p = temp -> next ;
i+ + ;
}
prev -> next = tem p -> next;
free(t e m p );
nodect r- -;
printf("\n Node Delete d..");
}
else
{
printf("\n It is not a middle position..");
getch();
95
}
}
}
void main(void)
{
int result;
int ch, n;
clrscr();
while(1)
{
ch = men u();
switch(c h)
{
case 1 :
if(star t = = NULL)
{
printf("\n Ente r Numb e r of nodes to creat e: ");
scanf("%d", &n);
creat elist(n);
printf("\nList creat e d ..");
}
else
printf("\n List is alre a dy Exist..");
brea k;
case 2 :
cll_inser t_b e g();
brea k;
case 3 :
cll_inser t_e n d();
brea k;
case 4 :
cll_inser t_mid();
brea k;
case 5 :
cll_delet e_b e g();
brea k;
case 6 :
cll_delet e_last();
brea k;
case 7 :
cll_delet e_ mid();
brea k;
case 8 :
display();
brea k;
case 9 :
exit(0);
}
getch();
}
}
A circula r double linke d list has both succ e s s o r point e r and pred e c e s s o r
point e r in circul a r man n e r . The objective behin d consid e ri n g circula r
double linke d list is to simplify the inser tion and deletion oper a tio n s
perfor m e d on doubl e linked list. In circula r double linked list the right
link of the right most node points back to the start node and left link of
the first node points to the last node.
96
A circula r double linke d list is show n in figur e 6.8.1.
100
st a rt
300 10 200 100 20 300 200 30 100
Fi g u r e 6 . 8 . 1 . C ir c u l a r D o u b l e L i n k e d L i s t
Crea tio n.
Inse r tio n.
Deletion.
Traver si n g.
star t = new no d e ;
new no d e -> left = star t;
new no d e -> ri g h t = start;
The function cdll_cre a t e lis t(), is use d to cre a t e ‘n’ num b e r of node s:
Ins e r t i n g a nod e at th e be g i n n i n g :
97
new no d e = g e t n o d e ( );
star t = new no d e ;
new no d e -> left = star t;
new no d e -> right = start;
The function cdll_ins e r t_b e g (), is used for inse r ti n g a node at the
beginni n g. Figur e 6.8.2 show s inser ti n g a node into the circula r doubl e
linked list at the begin ni n g.
st a rt
400
300 40 100
400
Fi g u r e 6 . 8 . 2 . I n s e r t i n g a n o d e a t t h e b e g i n n i n g
Ins e r t i n g a nod e at th e en d:
The following steps are followe d to inse r t a new node at the end of the
list:
newnode=getnode();
star t = new no d e ;
new no d e -> left = star t;
new no d e -> right = start;
98
The function cdll_ins e r t_e n d(), is used for inser ti n g a node at the end.
Figur e 6.8.3 shows inser ti n g a node into the circula r linke d list at the
end.
st a rt
100
300 40 100
400
Fi g u r e 6 . 8 . 3 . I n s e r t i n g a n o d e a t t h e e n d
new no d e = g e t n o d e ( );
The function cdll_insert_mid(), is used for inserting a node in the intermediate position. Figure 6.8.4
shows inserting a node into the circular double linked list at a specified intermediate position other
than beginning and end.
99
st a rt
100 40 200
100
400
300 10 400 400 20 300
100 200
200 30 100
300
Fi g u r e 6 . 8 . 4 . I n s e r t i n g a n o d e a t a n i n t e r m e d i a t e p o s it i o n
Del e t i n g a nod e at th e be g i n n i n g :
The following steps are followe d, to delet e a node at the beginni n g of the
list:
tem p = start;
star t = star t -> right;
tem p -> left -> right = star t;
star t -> left = tem p -> left;
The function cdll_delet e_b e g (), is used for deletin g the first node in the
list. Figur e 6.8.5 shows deletin g a node at the begin ni n g of a circul a r
double linke d list.
st a rt
200
Fi g u r e 6 . 8 . 5 . D e l e t i n g a n o d e a t b e g i n n i n g
Del e t i n g a nod e at th e en d:
The following steps are followe d to delet e a node at the end of the list:
100
tem p = start;
while(te m p -> right != star t)
{
tem p = tem p -> right;
}
tem p -> left -> right = tem p -> right;
tem p -> right -> left = tem p -> left;
The function cdll_delet e_la s t(), is used for deletin g the last node in the
list. Figur e 6.8.6 shows deletin g a node at the end of a circul a r double
linked list.
st a rt
100
Fi g u r e 6 . 8 . 6 . D e l e t i n g a n o d e a t t h e e n d
tem p = start;
i = 1;
while(i < pos)
{
tem p = tem p -> right ;
i+ + ;
}
tem p -> right -> left = tem p -> left;
tem p -> left -> right = tem p -> right;
free(t e m p );
101
printf("\n node delet e d..");
nodec t r- -;
}
The function cdll_delet e_ mid(), is used for deletin g the inter m e d i a t e node
in the list.
Figure 6.8.7 shows deleting a node at a specified intermediate position other than beginning and end
from a circular double linked list.
st a rt
100
Fi g u r e 6 . 8 . 7 . D e l e t i n g a n o d e a t a n i n t e r m e d i a t e p o s it i o n
The following steps are followe d, to trave r s e a list from left to right:
tem p = start;
Print tem p -> dat a;
tem p = tem p -> right;
while(te m p != star t)
{
print tem p -> data;
tem p = tem p -> right;
}
The following steps are followe d, to trave r s e a list from right to left:
102
tem p = tem p -> left;
print tem p -> data;
} while(t e m p != sta rt);
stru ct cdlinklist
{
stru ct cdlinklist *left;
int data;
stru ct cdlinklist *right;
};
int menu()
{
int ch;
clrscr();
printf("\n 1. Crea t e ");
printf("\n\n- -------------------------");
printf("\n 2. Inser t a node at Beginnin g");
printf("\n 3. Inser t a node at End");
printf("\n 4. Inser t a node at Middle");
printf("\n\n- -------------------------");
printf("\n 5. Delete a node from Beginni n g");
printf("\n 6. Delete a node from End");
printf("\n 7. Delete a node from Middle");
printf("\n\n- -------------------------");
printf("\n 8. Display the list from Left to Right");
printf("\n 9. Display the list from Right to Left");
printf("\n 10.Exit");
printf("\n\n Enter your choice: ");
scanf("%d", &ch);
retu r n ch;
}
103
{
nodect r = n;
for(i = 0; i < n; i+ + )
{
newno d e = getno d e();
if(star t = = NULL)
{
star t = newno d e;
newno d e -> left = star t;
newno d e -> rig h t = start;
}
else
{
newno d e -> left = start -> left;
newno d e -> right = star t;
star t -> left- > rig h t = newno d e;
star t -> left = newno d e ;
}
}
}
else
{
printf("\n List alrea dy exists..");
}
}
104
star t = newno d e;
newno d e -> left = star t;
newno d e -> right = star t;
}
else
{
newno d e -> left = star t -> left;
newno d e -> right = star t;
star t -> left -> right = newno d e;
star t -> left = newno d e ;
star t = newno d e;
}
}
}
}
105
void cdll_delet e_b e g()
{
node *temp;
if(star t = = NULL)
{
printf("\n No nodes exist..");
getch();
retu r n ;
}
else
{
nodect r- -;
if(nodec t r = = 0)
{
free(st a r t );
star t = NULL;
}
else
{
tem p = start;
star t = start -> right;
tem p -> left -> right = start;
star t -> left = tem p -> left;
free(t e m p );
}
printf("\n Node delet e d at Beginnin g..");
}
}
106
else
{
printf("\n Which node to delete: ");
scanf("%d", &pos);
if(pos > nodec t r)
{
printf("\nThis node does not exist");
getch();
retu r n ;
}
if(pos > 1 && pos < nodect r)
{
tem p = start;
while(ctr < pos)
{
tem p = temp -> right ;
ctr + + ;
}
tem p -> right -> left = temp -> left;
tem p -> left -> right = temp -> right;
free(t e m p );
printf("\n node delet e d ..");
nodect r- -;
}
else
{
printf("\n It is not a middle position..");
getch();
}
}
}
void main(void)
{
int ch,n;
clrscr();
while(1)
{
ch = men u();
switch( ch)
{
case 1 :
printf("\n Ente r Numb e r of nodes to creat e: ");
scanf("%d", &n);
cdll_cre a t elist( n);
printf("\n List crea t e d . .");
brea k;
case 2 :
cdll_inser t_b e g ();
brea k;
case 3 :
cdll_inser t_e n d ();
brea k;
case 4 :
cdll_inser t_mid();
brea k;
case 5 :
cdll_delet e_b e g ();
brea k;
case 6 :
cdll_delet e_last();
brea k;
case 7 :
cdll_delet e_mi d();
brea k;
case 8 :
cdll_display_left_rig h t();
brea k;
case 9 :
107
cdll_display_rig h t_left();
brea k;
case 10:
exit(0);
}
getch();
}
}
top
400
d at a n e xt
40 X
400
30 400
300
20 300
200
st art
100 10 200
100
Fi g u r e 6 . 9 . 1 . L i n k e d s t a c k
re pr e s e nt at io n
stru ct stack
{
int data;
stru ct stack *next;
};
void push();
void pop();
void display();
typed ef struct stack node;
node *start = N U L L;
node *top = NULL;
108
scanf("%d", &tem p -> dat a);
tem p -> next = NULL;
retu r n tem p;
}
void pop()
{
node *temp;
if(top = = NULL)
{
printf("\n\n\t Stack unde rflow");
retu r n ;
}
tem p = start;
if( start -> next = = NULL)
{
printf("\n\n\t Popp e d elem e n t is %d ", top -> data);
star t = NULL;
free(top);
top = NULL;
}
else
{
while(te m p -> next != top)
{
tem p = temp -> next;
}
tem p -> next = NULL;
printf("\n\n\t Popp e d elem e n t is %d ", top -> data);
free(top);
top = temp;
}
}
void display()
{
node *temp;
if(top = = NULL)
{
printf("\n\n\t\t Stack is empty ");
}
else
{
tem p = start;
printf("\n\n\n\t\t Elem e n t s in the stack: \n");
109
printf("%5d ", tem p -> data);
while(te m p != top)
{
tem p = temp -> next;
printf("%5d ", tem p -> data);
}
}
}
char menu()
{
char ch;
clrscr();
printf("\n \tStack oper a tio n s using point e r s .. ");
printf("\n -----------**********-------------\n");
printf("\n 1. Push ");
printf("\n 2. Pop ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Ente r your choice: ");
ch = getch e();
retu r n ch;
}
void main()
{
char ch;
node *newnod e;
do
{
ch = men u();
switch(c h)
{
case '1' :
newno d e = getno d e();
push(n e w n o d e );
brea k;
case '2' :
pop();
brea k;
case '3' :
display();
brea k;
case '4':
retu r n ;
}
getch();
}while( ch != '4' );
}
110
fr o nt rear
100 400
Fi g u r e 6 . 1 0 . 1 . L i n k e d Q u e u e r e p r e s e n t a t i o n
stru ct que u e
{
int data;
stru ct que u e *next;
};
111
}
tem p = front;
front = front -> next;
printf("\n\n\t Delete d elem e n t from queu e is %d ", temp -> data);
free(t e m p );
}
void displayQ()
{
node *temp;
if(front = = NULL)
{
printf("\n\n\t\t Empty Queu e ");
}
else
{
tem p = front;
printf("\n\n\n\t\t Elem e n t s in the Queu e are: ");
while(te m p != NULL )
{
printf("%5d ", tem p -> data);
tem p = temp -> next;
}
}
}
char menu()
{
char ch;
clrscr();
printf("\n \t..Que u e oper a tio n s using point e r s .. ");
printf("\n\t -----------**********-------------\n");
printf("\n 1. Inser t ");
printf("\n 2. Delete ");
printf("\n 3. Display");
printf("\n 4. Quit ");
printf("\n Ente r your choice: ");
ch = getch e();
retu r n ch;
}
void main()
{
char ch;
do
{
ch = men u();
switch(c h)
{
case '1' :
insert Q();
brea k;
case '2' :
delet e Q();
brea k;
case '3' :
displayQ();
brea k;
case '4':
retu r n ;
}
getch();
} while(ch != '4');
}
112
113