Doubly Linked List
Learn Theory -Doubly Linked List
/*********************************************************************/
#include<stdio.h>
#include<stdlib.h>
struct Node
{
int data;
struct Node *prev,*next;
}*first=NULL,*last=NULL,*temp=NULL,*trav=NULL;
void create()
{
temp=(struct Node*)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&temp->data);
temp->prev=NULL;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
temp->prev=last;
last=temp;
}
}
void display()
{
temp=first;
printf("NULL");
while(temp!=NULL)
{
printf(" %d<---->",temp->data);
temp=temp->next;
}
printf(" NULL\n");
}
void insert_begin()
{
temp=(struct Node*)malloc(sizeof(struct Node));
printf("Enter data");
scanf("%d",&temp->data);
temp->prev=NULL;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
temp->next=first;
first->prev=temp;
first=temp;
}
}
void insert_at_end()
{
temp=(struct Node*)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&temp->data);
temp->prev=NULL;
temp->next=NULL;
if(first==NULL)
{
first=temp;
last=temp;
}
else
{
last->next=temp;
temp->prev=last;
last=temp;
}
}
void insert_at_pos()
{
int pos,i;
printf("Enter the position:");
scanf("%d",&pos);
if(pos==1)
{
insert_begin();
}
else
{
trav=first;
for(i=1;i<pos;i++)
{
trav=trav->next;
}
if(trav==NULL)
{
insert_at_end();
}
else
{
temp=(struct Node*)malloc(sizeof(struct Node));
printf("Enter data:");
scanf("%d",&temp->data);
temp->prev=NULL;
temp->next=NULL;
trav->prev->next=temp;
temp->prev=trav->prev;
temp->next=trav;
trav->prev=temp;
}
}
}
void del_from_begin()
{
if(first==NULL)
{
printf("List is empty\n");
}
else
{
trav=first;
first=first->next;
first->prev=NULL;
trav->next=NULL;
free(trav);
}
}
void del_from_end()
{
if(first==NULL)
{
printf("List is empty\n");
}
else
{
trav=first;
while(trav->next!=NULL)
{
trav=trav->next;
}
trav->prev->next=NULL;
last=trav->prev;
trav->prev=NULL;
free(trav);
}
}
void del_from_pos()
{
int pos,i;
printf("Enter the position:");
scanf("%d",&pos);
if(pos==1)
{
del_from_begin();
}
else
{
trav=first;
for(i=1;i<pos;i++)
{
trav=trav->next;
}
if(trav==NULL)
{
printf("position beyond list\n");
}
else if(trav->next==NULL)
{
del_from_end();
}
else
{
if(first==NULL)
{
printf("List is empty\n");
}
else
{
trav->prev->next=trav->next;
trav->next->prev=trav->prev;
trav->prev=NULL;
trav->next=NULL;
free(trav);
}
}
}
}
void main()
{
int ch;
do
{
printf("\nMenu\n----\n1. Create\n2. Display\n3. Insert At Beginning\n4. Insert At End\n5. Insert At a Specified Position\n6. Delete From Beginning\n7. Delete From End\n8. Delete From a Specified Position\n9. Exit");
printf("\nEnter the choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
display();
break;
case 2:
display();
break;
case 3:
insert_begin();
display();
break;
case 4:
insert_at_end();
display();
break;
case 5:
insert_at_pos();
display();
break;
case 6:
del_from_begin();
display();
break;
case 7:
del_from_end();
display();
break;
case 8:
del_from_pos();
display();
break;
case 9:
exit(0);
default:
printf("There is no such operation:\n");
}
}
while(1);
}
Comments
Post a Comment