CPDS Unit 4
CPDS Unit 4
• Till now, we were using array data structure to organize the group of
elements that are to be stored individually in the memory.
• However, Array has several advantages and disadvantages which must be
known in order to decide the data structure which will be used throughout
the program.
o The size of array must be known in advance before using it in the program.
o Increasing size of the array is a time taking process.
o It is almost impossible to expand the size of the array at run time.
o All the elements in the array need to be contiguously stored in the
memory.
o Inserting any element in the array needs shifting of all its predecessors.
Linked list is the data structure which can overcome all the limitations of an array.
Consider an example where the marks obtained by the student in three subjects
are stored in a linked list as shown in the figure.
4.8M
117
Exception Handling in Java -
In the above figure, the arrow represents the links.
• The data part of every node contains the marks obtained by the student in
the different subject.
• The last node in the list is identified by the null pointer which is present in
the address part of the last node.
• We can have as many elements we require, in the data part of the list.
There are various operations which can be performed on singly linked list.
Insertion
SN Operation Description
SN Operation Description
#include<stdlib.h>
#include <stdio.h>
void create();
void display();
void insert_begin();
void insert_end();
void insert_pos();
void delete_begin();
void delete_end();
void delete_pos();
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
int main()
{
int choice;
while(1)
{
printf("\n MENU ");
printf("\n 1.Create ");
printf("\n 2.Display");
printf("\n 3.Insert at the beginning ");
printf("\n 4.Insert at the end");
printf("\n 5.Insert at specified position ");
printf("\n 6.Delete from beginning ");
printf("\n 7.Delete from the end");
printf("\n 8.Delete from specified position");
printf("\n 9.Exit ");
printf("\n--------------------------------------");
printf("\n Enter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display();
break;
case 3:
insert_begin();
break;
case 4:
insert_end();
break;
case 5:
insert_pos();
break;
case 6:
delete_begin();
break;
case 7:
delete_end();
break;
case 8:
delete_pos();
break;
case 9:
exit(0);
break;
default:
printf("n Wrong Choice:");
break;
}
}
return 0;
}
void create()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Out of Memory Space:");
exit(0);
}
printf("\n Enter the data value for the node:");
scanf("%d",&temp->info);
temp->next=NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
ptr=ptr->next;
}
ptr->next=temp;
}
}
void display()
{
struct node *ptr;
if(start==NULL)
{
printf("\n List is empty:");
return;
}
else
{
ptr=start;
printf("\n The List elements are:");
while(ptr!=NULL)
{
printf("%d\t",ptr->info );
ptr=ptr->next ;
}
}
}
void insert_begin()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Out of Memory Space:n");
return;
}
printf("\n Enter the data value for the node:" );
scanf("%d",&temp->info);
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
temp->next=start;
start=temp;
}
}
void insert_end()
{
struct node *temp,*ptr;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Out of Memory Space:");
return;
}
printf("\n Enter the data value for the node:" );
scanf("%d",&temp->info );
temp->next =NULL;
if(start==NULL)
{
start=temp;
}
else
{
ptr=start;
while(ptr->next !=NULL)
{
ptr=ptr->next ;
}
ptr->next =temp;
}
}
void insert_pos()
{
struct node *ptr,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\n Out of Memory Space:");
return;
}
printf("\n the position for the new node to be inserted:");
scanf("%d",&pos);
printf("\n Enter the data value of the node:");
scanf("%d",&temp->info) ;
temp->next=NULL;
if(pos==0)
{
temp->next=start;
start=temp;
}
else
{
for(i=0,ptr=start;i<pos-1;i++)
{
ptr=ptr->next;
if(ptr==NULL)
{
printf("\n Position not found:[Handle with care]");
return;
}
}
temp->next =ptr->next ;
ptr->next=temp;
}
}
void delete_begin()
{
struct node *ptr;
if(start==NULL)
{
printf("\n List is Empty:");
return;
}
else
{
ptr=start;
start=start->next ;
printf("\n The deleted element is :%d",ptr->info);
free(ptr);
}
}
void delete_end()
{
struct node *temp,*ptr;
if(start==NULL)
{
printf("\n List is Empty:");
exit(0);
}
else if(start->next ==NULL)
{
ptr=start;
start=NULL;
printf("\n The deleted element is:%d",ptr->info);
free(ptr);
}
else
{
ptr=start;
while(ptr->next!=NULL)
{
temp=ptr;
ptr=ptr->next;
}
temp->next=NULL;
printf("\n The deleted element is:%d",ptr->info);
free(ptr);
}
}
void delete_pos()
{
int i,pos;
struct node *temp,*ptr;
if(start==NULL)
{
printf("\n The List is Empty:");
exit(0);
}
else
{
printf("\n Enter the position of the node to be deleted:");
scanf("%d",&pos);
if(pos==0)
{
ptr=start;
start=start->next ;
printf("\n The deleted element is:%d",ptr->info );
free(ptr);
}
else
{
ptr=start;
for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;
if(ptr==NULL)
{
printf("\n Position not Found:");
return;
}
}
temp->next =ptr->next ;
printf("\n The deleted element is:%d",ptr->info );
free(ptr);
}
}
}
Output:
MENU
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1
MENU
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:1
MENU
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
MENU
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
MENU
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
MENU
1.Create
2.Display
3.Insert at the beginning
4.Insert at the end
5.Insert at specified position
6.Delete from beginning
7.Delete from the end
8.Delete from specified position
9.Exit
--------------------------------------
Enter your choice:2
A doubly linked list containing three nodes having numbers from 1 to 3 in their
data part, is shown in the following image.
Structure of a node in doubly linked list can be given as :
struct node
{
struct node *prev;
int data;
struct node *next;
}
▪ The prev part of the first node and the next part of the last node will always
contain null indicating end in each direction.
▪ In a singly linked list, we could traverse only in one direction, because each
node contains address of the next node and it doesn't have any record of
its previous nodes.
▪ However, doubly linked list overcome this limitation of singly linked list.
▪ Due to the fact that, each node of the list contains the address of its
previous node, we can find all the details about the previous node as well
by using the previous address stored inside the previous part of each node.
SN Operation Description
1 Insertion at beginning Adding the node into the linked list at beginning.
2 Insertion at end Adding the node into the linked list to the end.
3 Insertion after specified node Adding the node into the linked list after the spe
node.
5 Deletion at the end Removing the node from end of the list.
6 Deletion of the node at given data Removing the node which is present just after the
containing the given data.
Insertion
In a double linked list, the insertion operation can be performed in three ways as
follows...
The representation of three nodes as a linked list is shown in the below figure:
• We can observe in the above figure that there are three different nodes
having address 100, 200 and 300 respectively.
• The first node contains the address of the next node, i.e., 200, the second
node contains the address of the last node, i.e., 300, and the third node
contains the NULL value in its address part as it does not point to any node.
• The pointer that holds the address of the initial node is known as a head
pointer.
• The linked list, which is shown in the above diagram, is known as a singly
linked list as it contains only a single link.
• In this list, only forward traversal is possible; we cannot traverse in the
backward direction as it has only one link in the list.
struct node
{
int data;
struct node *next;
}
• As the name suggests, the doubly linked list contains two pointers.
• We can define the doubly linked list as a linear data structure with three
parts: the data part and the other two address part.
• In other words, a doubly linked list is a list that has three parts in a single
node, includes one data part, a pointer to its previous node, and a pointer
to the next node.
• Suppose we have three nodes, and the address of these nodes are 100,
200 and 300, respectively.
• The representation of these nodes in a doubly-linked list is shown below:
• As we can observe in the above figure, the node in a doubly-linked list has
two address parts;
• one part stores the address of the next while the other part of the node
stores the previous node's address.
• The initial node in the doubly linked list has the NULL value in the address
part, which provides the address of the previous node.
struct node
{
int data;
struct node *next;
struct node *prev;
}
The next pointer variable holds the address of the next node, and the prev
pointer holds the address of the previous node. The type of both the pointers,
i.e., next and prev is struct node as both the pointers are storing the address of
the node of the struct node type.
Circular linked list
struct node
{
int data;
struct node *next;
}
A circular linked list is a sequence of elements in which each node has a link to
the next node, and the last node is having a link to the first node.
The representation of the circular linked list will be similar to the singly linked list,
as shown below:
The doubly circular linked list has the features of both the circular linked
list and doubly linked list.
• The above figure shows the representation of the doubly circular linked list
in which the last node is attached to the first node and thus creates a circle.
• It is a doubly linked list also because each node holds the address of the
previous node also.
• The main difference between the doubly linked list and doubly circular
linked list is that the doubly circular linked list does not contain the NULL
value in the previous field of the node.
• As the doubly circular linked contains three parts, i.e., two address parts
and one data part so its representation is similar to the doubly linked list.