0% found this document useful (0 votes)
29 views

Implementation of Doubly Linked List

This C program implements a doubly linked list with functions to create, insert, display, and delete nodes from the beginning, end, or a particular position in the list. The main function provides a menu of options to call these linked list functions and continuously operates on the list until the user selects the exit option.

Uploaded by

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

Implementation of Doubly Linked List

This C program implements a doubly linked list with functions to create, insert, display, and delete nodes from the beginning, end, or a particular position in the list. The main function provides a menu of options to call these linked list functions and continuously operates on the list until the user selects the exit option.

Uploaded by

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

#include <stdio.

h>
#include<stdlib.h>

struct node
{
int data;
struct node*next;
struct node*prev;
};
struct node*head,*newnode,*tail,*temp;

void create()
{
head=0;
int choice=1;
while(choice)
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data to be stored:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
if(head==0)
head=tail=newnode;
else
{
tail->next=newnode;
newnode->prev=tail;
tail=tail->next;
}
printf("Do you want to continue(0/1):");
scanf("%d",&choice);
}
}

void insert_beg()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data to be stored:");
scanf("%d",&newnode->data);
newnode->prev=0;
newnode->next=0;
head->prev=newnode;
newnode->next=head;
head=newnode;
}

void insert_particular()
{
int pos;
int i=1;
printf("Enter the position you want to insert the node:");
scanf("%d",&pos);
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data to be stored:");
scanf("%d",&newnode->data);
temp=head;
while(i<pos-1)
{
temp=temp->next;
i++;
}
newnode->prev=temp;
newnode->next=temp->next;
temp->next=newnode;
newnode->next->prev=newnode;
}

void insert_end()
{
newnode=(struct node*)malloc(sizeof(struct node));
printf("Enter the data to be stored:");
scanf("%d",&newnode->data);
newnode->next=0;
temp=head;
while(temp->next!=0)
temp=temp->next;
temp->next=newnode;
newnode->prev=temp;
}

void display()
{
tail=head;
while(tail!=0)
{
printf("\n%d",tail->data);
tail=tail->next;
}
}

void delete_beg()
{
temp=head;
head=head->next;
temp->next->prev=0;
free (temp);
printf("\nFirst node is deleted");
}

void delete_particular()
{
int pos,i=1;
printf("\nEnter the position of node to be deleted:");
scanf("%d",&pos);
temp=head;
while(i<pos)
{
temp=temp->next;
i++;
}
temp->prev->next=temp->next;
temp->next->prev=temp->prev;
free(temp);
}
void delete_end()
{
struct node*prev_node;
temp=head;
while(temp->next!=0)
{
prev_node=temp;
temp=temp->next;
}
prev_node->next=0;
free(temp);
printf("Last node is deleted");
}

void main()
{
create();
display();
printf("\n1.Insert at beginning\n2.Insert at particular\n3.Insert at
end\n4.Display\n5.Delete at beginning\n6.Delete at particular\n7.Delete
at end\n8.Exit");
int option=0;
while (option!=8)
{
printf("\nEnter the option:");
scanf("%d",&option);
switch(option)
{
case 1:
{
insert_beg();
break;
}
case 2:
{
insert_particular();
break;
}
case 3:
{
insert_end();
break;
}
case 4:
{
display();
break;
}
case 5:
{
delete_beg();
break;
}
case 6:
{
delete_particular();
break;
}
case 7:
{
delete_end();
break;
}
case 8:
{
printf("Exit...");
break;
}

}
}
}

You might also like