0% found this document useful (0 votes)
3 views5 pages

C Program for Doubly Linked List Operations

The document contains a C program that implements a doubly linked list with functionalities to insert and delete nodes at the beginning, end, and specific positions, as well as display the list. It includes functions for creating nodes, counting the number of nodes, and handling user input for various operations. The program runs in a loop until the user chooses to exit.

Uploaded by

mdateeq807
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)
3 views5 pages

C Program for Doubly Linked List Operations

The document contains a C program that implements a doubly linked list with functionalities to insert and delete nodes at the beginning, end, and specific positions, as well as display the list. It includes functions for creating nodes, counting the number of nodes, and handling user input for various operations. The program runs in a loop until the user chooses to exit.

Uploaded by

mdateeq807
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

#include<stdio.

h>
#include<stdlib.h>

struct node{
struct node *llink;
int data;
struct node *rlink;
}*head=NULL,*tail=NULL;

int count()
{
struct node *temp;
int i=1;
temp=head;
while(temp->rlink!=NULL)
{
temp=temp->rlink;
i++;
}
return(i);
}

struct node *create(int value)


{
struct node *temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=value;
temp->rlink=NULL;
temp->llink=NULL;
return temp;
}
void insert_begin(int value)
{
struct node *newnode;
newnode=create(value);
if(head==NULL)
{
head=tail=newnode;
}
else
{
newnode->rlink=head;
head->llink=newnode;
head=newnode;
}
}

void insert_end(int value)


{
struct node *newnode, *temp;
newnode=create(value);
if(head==NULL)
{
head=tail=newnode;
}
else
{
newnode->llink=tail;
tail->rlink=newnode;
tail=newnode;
}
}

void insert_pos(int value,int pos)


{
struct node *newnode, *temp1,*temp2,*temp;
int i,c=1;
newnode=create(value);
i=count();
if(pos==1)
insert_begin(value);
else if(pos>i+1)
{
printf("insertion is not posible");
return;
}
else if(pos==i+1)
{
insert_end(value);
}
else
{
temp=head;
while(c<=pos-1 && temp!=NULL)
{
temp=temp->rlink;
c++;
}
temp1=temp->llink;
temp1->rlink=newnode;
temp->llink=newnode;
newnode->llink=temp1;
newnode->rlink=temp;
}
}

void delete_begin()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=head;
head=head->rlink;
if(head==NULL)
tail=NULL;
else
head->llink=NULL;
free(temp);
}
}
void delete_end()
{
struct node *temp;
if(head==NULL)
{
printf("deletion is not possible");
}
else
{
temp=tail;
tail=tail->llink;
if(tail==NULL)
head=NULL;
else
tail->rlink=NULL;
free(temp);
}
}
void delete_pos(int pos)
{
struct node *temp1,*temp2,*temp;
int i,c=1;
i=count();
if(pos==1)
delete_begin();
else if(pos>i)
{
printf("Deletion is not posible");
return;
}
else if(pos==i)
{
delete_end();
}
else
{
temp=head;
while(c<pos && temp->rlink!=NULL)
{
temp=temp->rlink;
c++;
}
temp1=temp->llink;
temp2=temp->rlink;
temp1->rlink=temp2;
temp2->llink=temp1;
free(temp);
}
}
void display()
{
struct node *temp;
if(head==NULL)
{
printf("list is empty");
}
else
{
temp=head;
while(temp->rlink!=NULL)
{
printf("%d <-> ",temp->data);
temp=temp->rlink;
}
printf("%d",temp->data);
}
}

void main()
{
int ch,pos,value;
do
{
printf("\[Link] Begin\[Link] End\[Link]
Position\[Link] Begin\[Link] End\[Link]
Position\[Link]\[Link]\n");
printf("Enter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the value: ");
scanf("%d",&value);
insert_begin(value);
break;
case 2: printf("Enter value: ");
scanf("%d",&value);
insert_end(value);
break;
case 3: printf("Enter value: ");
scanf("%d",&value);
printf("Enter position you want to insert: ");
scanf("%d",&pos);
insert_pos(value,pos);
break;
case 4: delete_begin();
break;
case 5: delete_end();
break;
case 6: printf("Enter position you want to delete: ");
scanf("%d",&pos);
delete_pos(pos);
break;
case 7: display();
break;
case 8:break;
default: printf("\nyour choice is wrong!.. ");
}
}while(ch!=8);
}

OUTPUT

You might also like