You are on page 1of 4

#include<stdio.

h>
#include<conio.h>
#include<stdlib.h>
#define MAX 20
struct node
{
int data;
struct node*next;
struct node*prev;
};
struct node *head;
struct node *temp;
struct node *last;
//head=NULL;
//temp=NULL;
//last=NULL;
int m=0;

/*****************CREATING LINK LIST****************/


void create()
{
int i=0;
printf("\nEnter no of nodes to be created:");
scanf(" %d",&m);
printf("\nEnter data: ");
for(i=0;i<m;i++)
{
temp=(struct node*)malloc(sizeof(struct node));
scanf(" %d",&temp->data);
temp->next=NULL;
temp->prev=NULL;
if(head == NULL)
{
head=temp;
}
else
{
last=head;
while(last->next != NULL)
{
last=last->next;
}
last->next=temp;
temp->prev=last;
}

}
}

/*****************INSERTING IN LINK LIST****************/


void insert()
{
int pos=0,i=0;
last=head;
temp=NULL;
if(head==NULL)
{
printf("\nFirst create link list");
}
else
{
printf("\nEnter the pos at to be inserted: ");
scanf(" %d",&pos);
if(pos > (m+1))
{
printf("\nWromg position ");
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
printf("\nEnter data to be inserted: ");
scanf("%d",&temp->data);
temp->next=NULL;
temp->prev=NULL;
if(pos == 1)
{
head->prev=temp;
temp->next=head;
head=temp;
}
if(pos <= m && pos != 1)
{
for(i=1;i<pos-1;i++)
{
last=last->next;
}
temp->next=last->next;
(last->next)->prev=temp;
last->next=temp;
temp->prev=last;
}
if(pos == (m+1))
{
last=head;
while(last->next!=NULL)
{
last=last->next;
}
last->next=temp;
temp->prev=last;
}
m++;

}
}
}
/*****************DELETION IN LINK LIST****************/
void delet()
{
int p=0,i=0;
if(head==NULL)
{
printf("\nLink list is empty");
}
else
{
printf("\nEnter position for deletion : ");
scanf("%d",&p);
if((p<=m) && (p>=1) )
{
if(p==1)
{
head=head->next;
head->prev=NULL;
}
else
{
last=head;
for(i=1;i<p-1;i++)
{
last=last->next;
}
last->next=(last->next)->next;
((last->next)->next)->prev=last;
}
}
else
{
printf("\nSuch position does not exist ");
}
}

/*****************DISPLAYING LINK LIST****************/


void traverse()
{
int t=0;
if(head == NULL)
{
printf("\nLink list empty\n");
}
else
{
printf("\n1)Forward Traverse\n2)Backward traverse ");
printf("\nEnter choice: ");
scanf("%d",&t);
last=head;
if(t==1)
{
printf("\nForward link list: ");
while(last!=NULL)
{
printf(" %d",last->data);
last=last->next;
}

}
else if(t==2)
{
while(last->next!=NULL)
{
last=last->next;
}
printf("\nReverse link list is: ");
while(last!=NULL)
{
printf(" %d",last->data);
last=last->prev;
}
}
else
{
printf("\nwrong choice\n");
}
}
}

void main()
{
int ch=0;
clrscr();
while(ch!=5)
{
printf("\n1)create\n2)insert\n3)delet\n4)traverse\n5)Exit\n");
printf("Enter operation to perform: ");
scanf(" %d",&ch);
switch(ch)
{
case 1: create();
break;
case 2: insert();
break;
case 3: delet();
break;
case 4: traverse();
break;
case 5: printf("\n***EXIT***\n");
break;
default: printf("\nEnter proper choice");

}
// printf("\nEnter operation to perform");
// scanf(" %d",&ch);
}

getch();
}

You might also like