0% found this document useful (0 votes)
59 views12 pages

DS Lab-5

The document describes a C program to perform operations on doubly linked lists. It includes functions to insert nodes at the beginning, end, or a specified location of the list. It also includes functions to delete nodes from the beginning, end, or by specifying a value. The main function allows the user to select an operation and call the corresponding function. The functions implement the operations by updating the next and previous pointers of the nodes.

Uploaded by

Kavibharath R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views12 pages

DS Lab-5

The document describes a C program to perform operations on doubly linked lists. It includes functions to insert nodes at the beginning, end, or a specified location of the list. It also includes functions to delete nodes from the beginning, end, or by specifying a value. The main function allows the user to select an operation and call the corresponding function. The functions implement the operations by updating the next and previous pointers of the nodes.

Uploaded by

Kavibharath R
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Ex.

No:4 DOUBLY LINKED LIST

Aim:
To write a C program to perform various operations on Doubly Linked List.

Algorithm:
1. Initialize the node which contains three parts namely, data part, pointer to the previous
node and pointer to the next node.
2. If Choice=1, Execute Insertion from beginning.
3. If Choice=2, Execute Insertion at the end.
4. If Choice=3, Execute Insertion at the specified location.
5. If Choice=4, Execute Deletion from beginning.
6. If Choice=5, Execute deletion at the end.
7. If Choice=6, Execute Deletion at the specified location.
8. If Choice=7, Display the Linked list.
9. If Choice=8, Search the element in the Linked list.
10. Otherwise print “Wrong Choice”.

R.Kavibharath
1901102
Program:
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *prev;
struct node *next;
int data;
};
struct node *head;
void insertion_beg();
void insertion_end();
void insertion_specified();
void deletion_beg();
void deletion_end();
void deletion_specified();
void display();
void search();
void main ()
{
int choice =0;
while(choice != 9)
{
printf("\nChoose one option from the following list ...\n");
printf("\n1.Insert in begining\n2.Insert at last\n3.Insert at any random location\n4.Delete
from Beginning\n5.Delete from last\n6.Delete the node after the given data\n7.Search\
n8.Show\n9.Exit\n");
printf("\nEnter your choice:\n");
scanf("\n%d",&choice);
switch(choice)
{

R.Kavibharath
1901102
case 1:
insertion_beg();
break;
case 2:
insertion_end();
break;
case 3:
insertion_specified();
break;
case 4:
deletion_beg();
break;
case 5:
deletion_end();
break;
case 6:
deletion_specified();
break;
case 7:
search();
break;
case 8:
display();
break;
case 9:
exit(0);
break;
default:
printf("Please enter valid choice..");
}

R.Kavibharath
1901102
}
}
void insertion_beg()
{
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);

if(head==NULL)
{
ptr->next = NULL;
ptr->prev=NULL;
ptr->data=item;
head=ptr;
}
else
{
ptr->data=item;
ptr->prev=NULL;
ptr->next = head;
head->prev=ptr;
head=ptr;

R.Kavibharath
1901102
}
printf("\nNode inserted\n");
}

}
void insertion_end()
{
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;
}
else
{
temp = head;
while(temp->next!=NULL)
{

R.Kavibharath
1901102
temp = temp->next;
}
temp->next = ptr;
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
{
temp=head;
printf("Enter the location");
scanf("%d",&loc);
for(i=0;i<loc;i++)
{
temp = temp->next;
if(temp == NULL)
{
printf("\n There are less than %d elements", loc);
return;

R.Kavibharath
1901102
}
}
printf("Enter value");
scanf("%d",&item);
ptr->data = item;
ptr->next = temp->next;
ptr -> prev = temp;
temp->next = ptr;
temp->next->prev=ptr;
printf("\nnode inserted\n");
}
}
void deletion_beg()
{
struct node *ptr;
if(head == NULL)
{
printf("\n UNDERFLOW");
}
else if(head->next == NULL)
{
head = NULL;
free(head);
printf("\nnode deleted\n");
}
else
{
ptr = head;
head = head -> next;
head -> prev = NULL;

R.Kavibharath
1901102
free(ptr);
printf("\nnode deleted\n");
}

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

R.Kavibharath
1901102
void deletion_specified()
{
struct node *ptr, *temp;
int val;
printf("\n Enter the data after which the node is to be deleted : ");
scanf("%d", &val);
ptr = head;
while(ptr -> data != val)
ptr = ptr -> next;
if(ptr -> next == NULL)
{
printf("\nCan't delete\n");
}
else if(ptr -> next -> next == NULL)
{
ptr ->next = NULL;
}
else
{
temp = ptr -> next;
ptr -> next = temp -> next;
temp -> next -> prev = ptr;
free(temp);
printf("\nnode deleted\n");
}
}
void display()
{
struct node *ptr;
printf("\n printing values...\n");

R.Kavibharath
1901102
ptr = head;
while(ptr != NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
}
void search()
{
struct node *ptr;
int item,i=0,flag;
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+1);
flag=0;
break;
}
else
{

R.Kavibharath
1901102
flag=1;
}
i++;
ptr = ptr -> next;
}
if(flag==1)
{
printf("\nItem not found\n");
}
}
}

R.Kavibharath
1901102
Output:

Result:
Thus, the program to perform various operations on Doubly linked list was
executed Successfully

R.Kavibharath
1901102

You might also like