Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Dsa Mod3 Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 54

Data Structures and Applications (CS204) Module 3

Module 3
Linked Lists: Definition, Representation of linked lists in Memory, Linked list operations: Traversing,
Searching, Insertion, and Deletion, Doubly Linked lists Circular linked lists and header linked
operations: Traversing, Searching, Insertion, and Deletion, polynomial using header linked list.

Text book 1: 4.1, 4.2, 4.3, 4.4, 4.5, 4.8

Why use linked list over array?


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.

Array contains following limitations:

1. The size of array must be known in advance before using it in the program.
2. All the elements in the array need to be contiguously stored in the memory.
3. Inserting any element in the array needs shifting of all its predecessors. Also, delete operations is
tedious.
4. Expanding or shrinking the size of the array is difficult.

Linked list is the data structure which can overcome all the limitations of an array. Using linked list is
useful because,

1. It allocates the memory dynamically.


2. All the nodes of linked list are non-contiguously stored in the memory and linked together with
the help of pointers.
3. Sizing is no longer a problem since we do not need to define its size at the time of declaration.
4. List grows as per the program's demand and limited to the available memory space.

Note: No particular data structure is the best. The choice of the data structure depends on the kind of
application that needs to be implemented. While for some applications linked lists may be useful, for
others, arrays may be useful.

 Use Arrays when we need quick access to elements, better memory efficiency, and when the data size
is static.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 1


Data Structures and Applications (CS204) Module 3

 Use Linked Lists when the data size is dynamic, frequent insertions and deletions are needed, or when
implementing certain complex data structures.

3.1 Definition : Linked List

Linked list is a linear data structure that consists of a sequence of elements where each element (usually
called a node) comprises of two items - the data and a reference (link) to the next node. The last node
has a reference to null.
The linked list contains a list variable called START or FIRST or HEAD, which contains the address of the first
node in the list. We need only this address in START to trace through the list.

The entry point into a linked list is called the head (start or first) of the list. It should be noted that
head is not a separate node, but the reference to the first node. If the list is empty then the start is a
null reference. The list with no nodes –empty list or null list.
Types of Linked List
1. Singly Linked List
2. Doubly Linked List
3. Circular Linked List
4. Header Linked List

3.2 Representation of linked lists in Memory


We need the following capabilities to make linked representations possible:
1) A mechanism for defining a node’s structure, that is, the fields it contains. We use self-referential
structure for this.
struct node
{
int data;
struct node *next;
};
struct node *head;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 2


Data Structures and Applications (CS204) Module 3

2) A way to create new nodes when we need them. We use malloc() function for this.
3) A way to remove nodes that we no longer need. We use free() function for this.

head

3.3 Linked list operations: Traversing, Searching, Insertion, and Deletion


Singly linked list or One way chain
Singly linked list can be defined as the collection of ordered set of elements. The number of elements
may vary according to need of the program. A node in the singly linked list consist of two parts: data part
and link part. Data part of the node stores actual information that is to be represented by the node while
the link part of the node stores the address of its immediate successor.

One way chain or singly linked list can be traversed only in one direction. In other words, we can say
that each node contains only next pointer, therefore we cannot traverse the list in the reverse direction.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 3


Data Structures and Applications (CS204) Module 3

Consider an example where the marks obtained by the student in three subjects are stored in a linked list
as shown in the figure.

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.

Operations on Singly Linked List


There are various operations which can be performed on singly linked list. A list of all such operations is
given below.

Node Creation
struct node
{
int data;
struct node *next;
};
struct node *head, *ptr;
ptr = (struct node *)malloc(sizeof(struct node));

SN Operation Description

Insertion at beginning

It involves inserting any


element at the front of the list.
1 We just need to a few link
adjustments to make the new
node as the head of the list.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 4


Data Structures and Applications (CS204) Module 3

Insertion at end of the list

It involves insertion at the last


2
of the linked list.

Insertion after specified node

It involves insertion after the


specified node of the linked list.
We need to skip the desired
3
number of nodes in order to
reach the node after which the
new node will be inserted.

Deletion at beginning
It involves deletion of a node
from the beginning of the list. It
4
just need a few adjustments in
the node pointers.

Deletion at the end of the list


It involves deleting the last node
5
of the list.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 5


Data Structures and Applications (CS204) Module 3

Deletion after specified node It involves deleting the node


after the specified node in the
list. We need to skip the desired
6 number of nodes to reach the
node after which the node will
be deleted. This requires
traversing through the list.

Traversing
In traversing, we simply visit
each node of the list at least
once in order to perform some
7
specific operation on it, for
example, printing data part of
each node present in the list.

Searching In searching, we match each


element of the list with the
given element. If the element is
8 found on any of the location
then location of that element is
returned otherwise null is
returned.

Linked List in C: Menu Driven Program


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 6


Data Structures and Applications (CS204) Module 3

struct node *head=NULL;


void begin_insert ();
void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at random location\n4.Delete from Beginning");
printf("\n5.Delete from last\n6.Delete node after specified location\n7.Search for an
element\n8.Show\n9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
begin_insert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 7


Data Structures and Applications (CS204) Module 3

case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void begin_insert()
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head;
head = ptr;
printf("\nNode inserted");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node*)malloc(sizeof(struct node));

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 8


Data Structures and Applications (CS204) Module 3

if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
ptr -> next = NULL;
head = ptr;
printf("\nNode inserted");
}
else
{
temp = head;
while (temp -> next != NULL)
{
temp = temp -> next;
}
temp->next = ptr;
ptr->next = NULL;
printf("\nNode inserted");

}
}
}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 9


Data Structures and Applications (CS204) Module 3

{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=1;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head;
head = ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
}
}
void last_delete()
{
struct node *ptr,*ptr1;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 10


Data Structures and Applications (CS204) Module 3

if(head == NULL)
{
printf("\nlist is empty");
}
else if(head -> next == NULL)
{
ptr=head;
head = NULL;
free(ptr);
printf("\nOnly node of the list deleted ...\n");
}

else
{
ptr = head;
while(ptr->next != NULL)
{
ptr1 = ptr;
ptr = ptr ->next;
}
ptr1->next = NULL;
free(ptr);
printf("\nDeleted Node from the last ...\n");
}
}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=1;i<=loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete");

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 11


Data Structures and Applications (CS204) Module 3

return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void search()
{
struct node *ptr;
int item,i=1,flag=0;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
if(flag==0)
{
printf("Item not found\n");
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 12


Data Structures and Applications (CS204) Module 3

void display()
{
int count=0;
struct node *ptr;
ptr = head;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
count++;
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
printf("\n The number of nodes %d",count);
}
}
Ex : 5
Title: Design, Develop and Implement a menu driven Program in C for handling change of branch.

Program:
#include<stdio.h>
#include<stdlib.h>
struct SLL
{
int usn;
char name[20];
char mode[20];
struct SLL *next;
};
typedef struct SLL node;
node *start=NULL;
node* createNode()
{
node *newnode;
newnode=(node*)malloc(sizeof(node));

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 13


Data Structures and Applications (CS204) Module 3

printf("Enter the usn: \n");


scanf("%d",&newnode->usn);
printf("Enter the Name: \n");
scanf("%s",newnode->name);
printf("Enter the mode (Regular/Lateral/COB/COC): \n");
scanf("%s",newnode->mode);
newnode->next=NULL;
return newnode;
}
void insertend()
{
node *newnode,*temp;
newnode=createNode();
if(start==NULL)
{
start=newnode;
return;
}
temp=start;
while(temp->next!=NULL)
temp=temp->next;
temp->next=newnode;
}
void display()
{
node* temp=start;
if(start==NULL)
{
printf("list is empty\n");
return;
}
printf("Students in the class are:\n");
printf("USN\tNAME\tMode\n");
while(temp!=NULL)
{
printf("%d\t%s\t%s\n",temp->usn,temp->name,temp->mode);
temp=temp->next;
}
printf("\n");
}
void deletekey()
{

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 14


Data Structures and Applications (CS204) Module 3

node *temp=start,*prev;
int key;
if(start==NULL)
{
printf("List is empty. Deletion is not possible.\n");
return;
}
printf("Enter the USN to be deleted: \n");
scanf("%d",&key);
if (start->usn == key)
{
start = start->next;
printf("Deleted USN: %d\n",temp->usn);
free(temp);
}
else
{
while(temp!=NULL && temp->usn!=key)
{
prev=temp;
temp=temp->next;
}
if(temp==NULL)
{
printf("USN is not found, so can't delete.\n");
return;
}
prev->next=temp->next;
printf("Deleted USN: %d\n",temp->usn);
free(temp);
}
}
int main()
{
int choice,n,i;
while(1)
{
printf("\nEnter your choice:\n");
printf("1 for insert\n");
printf("2 for display\n");
printf("3 for delete students based on COC/COB\n");
printf("4 for adding students from lateral entry/COC/COB\n");

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 15


Data Structures and Applications (CS204) Module 3

printf("5 for exit\n");


scanf("%d",&choice);
switch(choice)
{
case 1:
case 4: printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
insertend();
break;
case 2: display();
break;
case 3: deletekey();
break;
case 5: exit(0);
default:printf("Invalid chioce\n");
}
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 16


Data Structures and Applications (CS204) Module 3

3.4 Circular Singly Linked List


In a circular Singly linked list, the last node of the list contains a pointer to the first node of the list. We
can have circular singly linked list as well as circular doubly linked list.

We traverse a circular singly linked list until we reach the same node where we started. The circular
singly linked list has no beginning and no ending. There is no null value present in the next part of any of
the nodes.

The following image shows a circular singly linked list.

Circular linked list are mostly used in task maintenance in operating systems. There are many examples
where circular linked list are being used in computer science including browser surfing where a record of
pages visited in the past by the user, is maintained in the form of circular linked lists and can be accessed
again on clicking the previous button.

Memory Representation of circular linked list:


In the following image, memory representation of a circular linked list containing marks of a student in 4
subjects. However, the image shows a glimpse of how the circular list is being stored in the memory. The
start or head of the list is pointing to the element with the index 1 and containing 13 marks in the data
part and 4 in the next part. Which means that it is linked with the node that is being stored at 4th index of
the list.

However, due to the fact that we are considering circular linked list in the memory therefore the last node
of the list contains the address of the first node of the list.

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 17


Data Structures and Applications (CS204) Module 3

We must be able to identify the last node of any linked list so that we can find out the number of
iterations which need to be performed while traversing the list.
Operations on Circular Singly linked list:
SN Operation Description

Insertion at
beginning It involves inserting any element at the front of the list. We just need to a few
1
link adjustments to make the new node as the head of the list.

Insertion at
2 It involves insertion at the last of the linked list.
end of the list

Insertion after It involves insertion after the specified node of the linked list. We need to skip
3 specified the desired number of nodes in order to reach the node after which the new node
node will be inserted.

Deletion at It involves deletion of a node from the beginning of the list. It just need a few
4 beginning adjustments in the node pointers.

Deletion at the
5 It involves deleting the last node of the list.
end of the list

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 18


Data Structures and Applications (CS204) Module 3

Deletion after
It involves deleting the node after the specified node in the list. We need to skip
specified node
6 the desired number of nodes to reach the node after which the node will be
deleted. This requires traversing through the list.

Traversing In traversing, we simply visit each node of the list at least once in order to
7 perform some specific operation on it, for example, printing data part of each
node present in the list.

Searching In searching, we match each element of the list with the given element. If the
8 element is found on any of the location then location of that element is returned
otherwise null is returned.

Menu-driven program in C implementing all operations on circular singly linked list


#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;

void begin_insert ();


void lastinsert ();
void randominsert();
void begin_delete();
void last_delete();
void random_delete();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 19


Data Structures and Applications (CS204) Module 3

{
printf("\n*********Main Menu*********\n");
printf("\nChoose one option from the following list ...\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Delete from Beginning\n4.Delete from last\n5.Search f
or an element\n6.Show\n7.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
begin_insert();
break;
case 2:
lastinsert();
break;
case 3:
randominsert();
break;
case 4:
begin_delete();
break;
case 5:
last_delete();
break;
case 6:
random_delete();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 20


Data Structures and Applications (CS204) Module 3

}
}
void begin_insert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter the node data?");
scanf("%d",&item);
ptr -> data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
}
else
{
temp = head;
while(temp->next != head)
temp = temp->next;
ptr->next = head;
temp -> next = ptr;
head = ptr;
}
printf("\nnode inserted\n");
}

}
void lastinsert()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *)malloc(sizeof(struct node));

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 21


Data Structures and Applications (CS204) Module 3

if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter Data?");
scanf("%d",&item);
ptr->data = item;
if(head == NULL)
{
head = ptr;
ptr -> next = head;
printf("\nnode inserted\n");

}
else
{
temp = head;
while(temp -> next != head)
{
temp = temp -> next;
}
temp -> next = ptr;
ptr -> next = head;
printf("\nnode inserted\n");
}

}
void randominsert()
{
int i,loc,item;
struct node *ptr, *temp;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 22


Data Structures and Applications (CS204) Module 3

printf("\nOVERFLOW");
}
else
{
printf("\nEnter element value");
scanf("%d",&item);
ptr->data = item;
printf("\nEnter the location after which you want to insert ");
scanf("\n%d",&loc);
temp=head;
for(i=1;i<loc;i++)
{
temp = temp->next;
if(temp == head)
{
printf("\ncan't insert\n");
return;
}
}
ptr ->next = temp ->next;
temp ->next = ptr;
printf("\nNode inserted");
}
}

void begin_delete()
{
struct node *ptr;
if(head == NULL)
{
printf("\nUNDERFLOW");
}
else if(head->next == head)
{
ptr=head;
free(ptr);
head=NULL;
printf("\nnode deleted\n");
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 23


Data Structures and Applications (CS204) Module 3

else
{ ptr = head;
while(ptr -> next != head)
ptr = ptr -> next;
ptr->next = head->next;
free(head);
head = ptr->next;
printf("\nnode deleted\n");

}
}
void last_delete()
{
struct node *ptr, *preptr;
if(head==NULL)
{
printf("\nUNDERFLOW");
}
else if (head ->next == head)
{
ptr = head;
free(ptr);
head=NULL;
printf("\nnode deleted\n");
}
else
{
ptr = head;
while(ptr ->next != head)
{
preptr=ptr;
ptr = ptr->next;
}
preptr->next = ptr -> next;
free(ptr);
printf("\nnode deleted\n");

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 24


Data Structures and Applications (CS204) Module 3

}
void random_delete()
{
struct node *ptr,*ptr1;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=1;i<=loc;i++)
{
ptr1 = ptr;
ptr = ptr->next;
if(ptr == head)
{
printf("\nCan't delete");
return;
}
}
ptr1 ->next = ptr ->next;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}

void search()
{
struct node *ptr;
int item,i=1,flag=0;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
if(head ->data == item)
{
printf("item found at location %d",i);

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 25


Data Structures and Applications (CS204) Module 3

flag=1;
}
else
{
while (ptr->next != head)
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
}
if(flag == 0)
{
printf("Item not found\n");
}
}

void display()
{
struct node *ptr;
ptr=head;
int count=0;
if(head == NULL)
{
printf("\nnothing to print");
}

else
{
printf("\n printing values ... \n");
count=1;
while(ptr -> next != head)

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 26


Data Structures and Applications (CS204) Module 3

count=count+1;
printf("%d\n", ptr -> data);
ptr = ptr -> next;
}
printf("%d\n", ptr -> data);
printf("\n The number of nodes %d",count);
}

}
3.5 Doubly linked list
Doubly linked list is a complex type of linked list in which a node contains a pointer to the previous as
well as the next node in the sequence. Therefore, in a doubly linked list, a node consists of three parts:
node data, pointer to the next node in sequence (next pointer), pointer to the previous node (previous
pointer). A sample node in a doubly linked list is shown in the figure.

A doubly linked list containing three nodes having numbers from 1 to 3 in their data part, is shown in the
following image.

In C, structure of a node in doubly linked list can be given as :


struct node
{
struct node *prev;
int data;
struct node *next;
};

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 27


Data Structures and Applications (CS204) Module 3

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.

Memory Representation of a doubly linked list


Memory Representation of a doubly linked list is shown in the following image. Generally, doubly
linked list consumes more space for every node and therefore, causes more expensive basic operations
such as insertion and deletion. However, we can easily manipulate the elements of the list since the list
maintains pointers in both the directions (forward and backward).

In the following image, the first element of the list that is i.e. 13 stored at address 1. The head pointer
points to the starting address 1. Since this is the first element being added to the list therefore the prev of
the list contains null. The next node of the list resides at address 4 therefore the first node contains 4 in
its next pointer.

We can traverse the list in this way until we find any node containing null or -1 in its next part.

Node Creation

struct node
{
struct node *prev;
int data;
struct node *next;
};
struct node *head=NULL;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 28


Data Structures and Applications (CS204) Module 3

Operations on doubly linked list


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 specified node.

4 Deletion at beginning Removing the node from beginning of the list

5 Deletion at the end Removing the node from end of the list.

Removing the node which is present just after the given


6 Deletion of the specified node
location.

Comparing each node data with the item to be searched and


7 Searching return the location of the item in the list if the item found else
return null.

Visiting each node of the list at least once in order to perform


8 Traversing
some specific operation like searching, sorting, display, etc.

Menu Driven Program in C to implement all the operations of doubly linked list
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 29


Data Structures and Applications (CS204) Module 3

struct node *next;


int data;
};
struct node *head=NULL;
void insertion_beginning();
void insertion_last();
void insertion_specified();
void deletion_beginning();
void deletion_last();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n 4.
Delete from Beginning\n 5.Delete from last\n6.Delete the node after the given data\n7.Search\n8.Show\n
9.Exit\n");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
insertion_beginning();
break;
case 2:
insertion_last();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beginning();
break;
case 5:
deletion_last();
break;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 30


Data Structures and Applications (CS204) Module 3

case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void insertion_beginning()
{
struct node *ptr;
int item;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter Item value");
scanf("%d",&item);
ptr->data=item;
if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
head=ptr;
printf("\nnode inserted\n");
}
else

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 31


Data Structures and Applications (CS204) Module 3

{
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;
printf("\nNode inserted\n");
}

}
}
void insertion_last()
{
struct node *ptr,*temp;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value");
scanf("%d",&item);
ptr->data=item;
if(head == NULL)
{
ptr->next = NULL;
ptr->prev = NULL;
head = ptr;
printf("\nnode inserted\n");
}
else
{
temp = head;
while(temp->next!=NULL)
{
temp = temp->next;
}
temp->next = ptr;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 32


Data Structures and Applications (CS204) Module 3

ptr ->prev=temp;
ptr->next = NULL;
printf("\nnode inserted\n");
}
}
}
void insertion_specified()
{
struct node *ptr,*temp;
int item,loc,i;
ptr = (struct node *)malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\n OVERFLOW");
}
else
{
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
printf("Enter the location");
scanf("%d",&loc);
temp=head;
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;
}
}
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
ptr->next->prev=ptr;
printf("\nnode inserted\n");
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 33


Data Structures and Applications (CS204) Module 3

void deletion_beginning()
{
struct node *ptr;
if(head == NULL)
{
printf("\n List is empty");
}
else if(head->next == NULL)
{
ptr=head;
free(ptr);
head = NULL;
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;
free(ptr);
printf("\nnode deleted\n");
}

}
void deletion_last()
{
struct node *ptr;
if(head == NULL)
{
printf("\n List is empty ");
}
else if(head->next == NULL)
{
ptr=head;
head = NULL;
free(ptr);
printf("\nnode deleted\n");

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 34


Data Structures and Applications (CS204) Module 3

}
else
{
ptr = head;
while(ptr->next != NULL)
{
ptr = ptr -> next;
}
ptr -> prev -> next = NULL;
free(ptr);
printf("\nnode deleted\n");
}
}
void deletion_specified()
{
struct node *ptr;
int loc,i;
printf("\n Enter the location of the node after which you want to perform deletion \n");
scanf("%d",&loc);
ptr=head;
for(i=1;i<=loc;i++)
{
ptr = ptr->next;
if(ptr == NULL)
{
printf("\nCan't delete");
return;
}
}
ptr->prev->next=ptr->next;
ptr->next->prev=ptr->prev;
free(ptr);
printf("\nDeleted node %d ",loc+1);
}
void display() //Same as Singly Linked List display() function
{
int count=0;
struct node *ptr;
ptr = head;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 35


Data Structures and Applications (CS204) Module 3

if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");
while (ptr!=NULL)
{
count++;
printf("\n%d",ptr->data);
ptr = ptr -> next;
}
printf("\n The number of nodes %d",count);
}
}
void search() //Same as Singly List List search() function
{
struct node *ptr;
int item,i=1,flag=0;
ptr = head;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("\nitem found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 36


Data Structures and Applications (CS204) Module 3

}
if(flag==0)
{
printf("\nItem not found\n");
}
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 37


Data Structures and Applications (CS204) Module 3

3.6 Linked List implementation of Stack


In this approach, each element of the stack is represented as a node in a linked list. The head of the
Singly Linked List represents the top of the stack. The basic operations are performed by updating the
links between the nodes in the linked list. This implementation is more flexible as it can grow and shrink
dynamically as elements are added and removed from the stack.

#include <stdio.h>
#include <stdlib.h>
void push();
void pop();
void display();
struct node
{
int data;
struct node *next;
};
struct node *top=NULL;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 38


Data Structures and Applications (CS204) Module 3

void main ()
{
int choice=0;
printf("\n*********Stack operations using linked list*********\n");
while(choice != 4)
{
printf("\n1.Push\n2.Pop\n3.Show\n4.Exit");
printf("\n Enter your choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf("Please Enter valid choice ");
}
}
}
void push() //similar to begin_insert() of singly linked list
{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 39


Data Structures and Applications (CS204) Module 3

printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = top;
top = ptr;
printf("\n Item Pushed");
}

}
void pop() //similar to begin_delete() of singly linked list
{
struct node *ptr;
if(top == NULL)
{
printf("\nStack is empty\n");
}
else
{
ptr=top;
printf("\n Item popped %d",ptr->data);
top = ptr->next;
free(ptr);

}
}
void display()
{
struct node *ptr;
ptr=top;
if(ptr == NULL)
{
printf("Stack is empty\n");
}
else
{
printf("Printing Stack elements \n");
while(ptr!=NULL)
{
printf("%d\t",ptr->data);

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 40


Data Structures and Applications (CS204) Module 3

ptr = ptr->next;
}
}
}
3.7 Linked List implementation of Queue
In a linked queue, each node of the queue consists of two parts i.e. data part and the link part. Each
element of the queue points to its immediate next element in the memory.

In the linked queue, there are two pointers maintained in the memory i.e. front pointer and rear pointer.
The front pointer contains the address of the starting element of the queue while the rear pointer
contains the address of the last element of the queue.

Insertion and deletions are performed at rear and front end respectively. If front and rear both are
NULL, it indicates that the queue is empty.

The linked representation of queue is shown in the following figure.

Enqueue operation
The insert operation append the queue by adding an element to the end of the queue. The new element
will be the last element of the queue. Firstly, allocate the memory for the new node ptr by using the
following statement.

ptr = (struct node *) malloc (sizeof(struct node));


There can be the two scenario of inserting this new node ptr into the linked queue.

In the first scenario, we insert element into an empty queue. In this case, the condition front =
NULL becomes true. Now, the new element will be added as the only element of the queue and the next
pointer of front and rear pointer both, will point to NULL.
In the second case, the queue contains more than one element. The condition front = NULL becomes
false. In this scenario, we need to update the end pointer rear so that the next pointer of rear will point to
the new node ptr. Since, this is a linked queue, hence we also need to make the rear pointer point to the
newly added node ptr. We also need to make the next pointer of rear point to NULL.
Dequeue Operation

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 41


Data Structures and Applications (CS204) Module 3

Deletion operation removes the element that is first inserted among all the queue elements. Firstly, we
need to check either the list is empty or not. The condition front == NULL becomes true if the queue is
empty, then we cannot delete.

Otherwise, we will delete the element that is pointed by the pointer front. For this purpose, copy the node
pointed by the front pointer into the pointer ptr. Now, shift the front pointer, point to its next node and
free the node pointed by the node ptr.

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *front=NULL;
struct node *rear=NULL;
void enqueue();
void dequeue();
void display();
void main ()
{
int choice;
while(choice != 4)
{
printf("\n1.insert an element\n2.Delete an element\n3.Display the queue\n4.Exit\n");
printf("\nEnter your choice ?");
scanf("%d",&choice);
switch(choice)
{
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
display();
break;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 42


Data Structures and Applications (CS204) Module 3

case 4:
exit(0);
break;
default:
printf("\nEnter valid choice??\n");
}
}
}
void enqueue() //similar to lastinsert() of singly linked list
{
struct node *ptr;
int item;
ptr = (struct node *) malloc (sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW\n");
}
else
{
printf("\nEnter value?\n");
scanf("%d",&item);
ptr -> data = item;
if(front == NULL)
{
front = ptr;
rear = ptr;
front -> next = NULL;
rear -> next = NULL;
printf("\n Item enqueued");

}
else
{
rear -> next = ptr;
ptr->next = NULL;
rear = ptr;
printf("\n Item enqueued");
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 43


Data Structures and Applications (CS204) Module 3

}
void dequeue () //similar to begin_delete() of singly linked list
{
struct node *ptr;
if(front == NULL)
{
printf("\nEmpty Queue\n");

}
else if(front->next==NULL)
{
ptr = front;
printf("\n Item dequeued %d",ptr->data);
front = rear=NULL;
free(ptr);
}
else
{
ptr = front;
printf("\n Item dequeued %d",ptr->data);
front = ptr-> next;
free(ptr);
}
}
void display()
{
struct node *ptr;
ptr = front;
if(front == NULL)
{
printf("\nEmpty queue\n");
}
else
{ printf("\nprinting values .....\n");
while(ptr != NULL)
{
printf("\n%d\n",ptr -> data);
ptr = ptr -> next;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 44


Data Structures and Applications (CS204) Module 3

}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 45


Data Structures and Applications (CS204) Module 3

3.8 Additional List Operations:


Search a node with a particular data
void search()
{
struct node *ptr; Consider the below as the node structure:
int item,i=1,flag=0; struct node
ptr = head; {
if(ptr == NULL) int data;
{ struct node *next;
printf("\nEmpty List\n"); };
} Also, consider struct node *head as the start
else of a singly linked list.
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
if(flag==0)
{
printf("Item not found\n");
}
}
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 45


Data Structures and Applications (CS204) Module 3

Concatenation of singly linked list


Consider a node in a singly linked list as under, struct node *head1 as the start of list1 and struct node
*head2 as the start of list2. The concat() function concatenates head1 and head2 lists and returns the
concatenated list.
struct node
{
int data;
struct node *next;
};
struct node *head1;
struct node *head2;
struct node* concat(struct node *head1,struct node *head2)
{
struct node *ptr;
if(head1 == NULL && head2==NULL)
{
printf("\n Concatenation is not possible because list is empty");
return NULL;
}
if(head1 == NULL)
{
return head2;
}
if(head2 == NULL)
{
return head1;
}
ptr=head1;
while (ptr->next!=NULL)
{
ptr = ptr -> next;
}
ptr->next=head2;
return head1;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 46


Data Structures and Applications (CS204) Module 3

3.9 Header linked operations: Traversing, Searching, Insertion, and Deletion

A header linked list is a type of linked list that has a header node at the beginning of the list. In a header
linked list, HEAD points to the header node instead of the first node of the list.

The header node does not represent an item in the linked list. This data part of this node is generally used
to hold any global information about the entire linked list like information about number of nodes, max
node and min node values. The next part of the header node points to the first node in the list.

A header linked list can be divided into two types:

1) Grounded header linked list that stores NULL in the last node’s next field.

2) Circular header linked list that stores the address of the header node in the next part of the last
node of the list.

// Header Linked List Operations

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *head=NULL;
void begin_insert ();
void begin_delete();
void display();
void search();

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 47


Data Structures and Applications (CS204) Module 3

void main ()
{
int choice =0;
head = (struct node *) malloc(sizeof(struct node));
head->data=0;
head->next=NULL;
while(choice != 6)
{
printf("\n1.Insert in begining\n2.Delete from Beginning");
printf("\n3Search for an element\n4.Show\n5.Total nodes\n 6.Exit ");
printf("\nEnter your choice?\n");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
begin_insert();
break;
case 2:
begin_delete();
break;
case 3:
search();
break;
case 4:
display();
break;
case 5:
printf("\n Total number of nodes %d", head->data);
break;
case 6:
free(head)
exit(0);
break;
default:
printf("Please enter valid choice..");
}
}
}
void begin_insert()

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 48


Data Structures and Applications (CS204) Module 3

{
struct node *ptr;
int item;
ptr = (struct node *) malloc(sizeof(struct node));
if(ptr == NULL)
{
printf("\nOVERFLOW");
}
else
{
printf("\nEnter value\n");
scanf("%d",&item);
ptr->data = item;
ptr->next = head->next;
head->next=ptr;
head->data++;
printf("\nNode inserted");
}
}
void begin_delete()
{
struct node *ptr;
if(head->next== NULL)
{
printf("\nList is empty\n");
}
else
{
ptr = head->next;
head ->next= ptr->next;
free(ptr);
printf("\nNode deleted from the begining ...\n");
head->data--;
}
}
void search()
{
struct node *ptr;
int item,i=1,flag=0;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 49


Data Structures and Applications (CS204) Module 3

ptr = head->next;
if(ptr == NULL)
{
printf("\nEmpty List\n");
}
else
{
printf("\nEnter item which you want to search?\n");
scanf("%d",&item);
while (ptr!=NULL)
{
if(ptr->data == item)
{
printf("item found at location %d ",i);
flag=1;
break;
}
i++;
ptr = ptr -> next;
}
if(flag==0)
{
printf("Item not found\n");
}
}

}
void display()
{
int count=0;
struct node *ptr;
ptr = head->next;
if(ptr == NULL)
{
printf("Nothing to print");
}
else
{
printf("\nprinting values . . . . .\n");

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 50


Data Structures and Applications (CS204) Module 3

while (ptr!=NULL)
{
printf("\n%d",ptr->data);
ptr = ptr -> next;
}

}
}
3.10 Polynomial using header linked list
Just like array representation of polynomials, we can also represent polynomials using linked lists.
Let us consider a node in a polynomial using linked list as under:

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 51


Data Structures and Applications (CS204) Module 3

A zero polynomial is a polynomial in which all the coefficients are zero. It is typically represented as:

P(x)=0.
Let us handle this zero polynomial with a header node introduced into each polynomial that is, each
polynomial, zero or nonzero, contains one additional node called header node (i.e), We will implement a
header linked list to represent the polynomial. Let us also consider the exp field of the header node to -1.
header 0 -1 NULL Zero Polynomial

Polynomial Node Creation:


struct polynode
{
int coef;
int exp;
struct polynode* next;
};
Let us consider an insert() function that adds a new term (node) with coef as coefficient and exp as exponent
to the end of a polynomial represented as a header linked list called poly.
void insert(struct polynode* poly, int coef, int exp);
The addpolynomial() function adds two polynomials head1 and head2 represented as singly linked lists with
header nodes and the lists are not empty.
struct polynode* addpolynomial(struct polynode* head1, struct polynode* head2)
{
struct polynode *head = (struct polynode *) malloc(sizeof(struct polynode));
head->exp=-1;
head->coeff=0;
head->next=NULL;
struct polynode *result = head;
struct polynode *poly1 =head1->next;
struct polynode *poly2 =head2->next;

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 52


Data Structures and Applications (CS204) Module 3

while (poly1 != NULL && poly2 != NULL)


{
if (poly1->exp == poly2->exp)
{
insert(result, poly1->coef + poly2->coef, poly1->exp);
poly1 = poly1->next;
poly2 = poly2->next;
}
else if (poly1->exp > poly2->exp)
{
insert(result, poly1->coef, poly1->exp);
poly1 = poly1->next;
}
else
{
insert(result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}
}
while (poly1!= NULL)
{
insert(result, poly1->coef, poly1->exp);
poly1 = poly1->next;
}
while (poly2!= NULL) {
insert(result, poly2->coef, poly2->exp);
poly2 = poly2->next;
}
return result;
}

Prof.V.Sonia Devi, Dept. of CSE, CITech 2024-25 Page 53

You might also like