/*Doubly Linked List*/
#include<stdio.h>
#include<conio.h>
typedef struct dll{
int data;
struct dll *next;
struct dll *prev;
}node;
node *getnode()
{
int item;
node *temp;
temp=(node *)malloc(sizeof(node));
printf("\nenter data:");
scanf("%d",&item);
temp->data=item;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
node *create()
{
int ch;
node *trv,*start,*neww;
start=trv=NULL;
do
{
neww=getnode();
if(start==NULL)
{
start=neww;
trv=start;
}
else
{
trv->next=neww;
neww->prev=trv;
trv=trv->next;
}
printf("\nEnter any key to continue & 0 for stop:");
scanf("%d",&ch);
}while(ch!=0);
return start;
}
node *insert_first(node *start)
{
node *trv,*neww;
trv=start;
neww=getnode();
if(start==NULL)
{
start=neww;
}
else
{
neww->next=start;
start->prev=neww;
start=neww;
}
return start;
}
node *insert_last(node *start)
{
node *trv,*neww;
trv=start;
neww=getnode();
if(start==NULL)
{
start=neww;
}
else
{
while(trv->next!=NULL)
{
trv=trv->next;
}
trv->next=neww;
neww->prev=trv;
}
return start;
}
node *del_first(node *start)
{
node *trv;
trv=start;
start=start->next;
start->prev=NULL;
free(trv);
return start;
}
node *del_last(node *start)
{
node *trv,*p;
trv=start;
if(start==NULL)
{
printf("\nEmpty\n");
}
else if(start->next==NULL)
{
free(start);
start=NULL;
}
else
{
while(trv->next!=NULL)
{
p=trv;
trv=trv->next;
}
}
p=trv->prev;
p->next=NULL;
free(trv);
return start;
}
void display(node *start)
{
node *trv;
trv=start;
if(start==NULL)
{
printf("\nNo node to display\n");
}
printf("\n");
while(trv!=NULL)
{
printf("%d ",trv->data);
trv=trv->next;
}
}
void main()
{
node *f=NULL,*l=NULL;
node *s=NULL;
int ch;
clrscr();
while(1)
{
printf("\n1.Create\n2.Insert at front\n3.Insert at last\n4.Delete at front\n5.Delete at last\n6.Display\n10.exit\nYour choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:s=create();
break;
case 2:s=insert_first(s);
break;
case 3:s=insert_last(s);
break;
case 4:s=del_first(s);
break;
case 5:s=del_last(s);
break;
case 6:display(s);
break;
case 10:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
}
#include<stdio.h>
#include<conio.h>
typedef struct dll{
int data;
struct dll *next;
struct dll *prev;
}node;
node *getnode()
{
int item;
node *temp;
temp=(node *)malloc(sizeof(node));
printf("\nenter data:");
scanf("%d",&item);
temp->data=item;
temp->next=NULL;
temp->prev=NULL;
return temp;
}
node *create()
{
int ch;
node *trv,*start,*neww;
start=trv=NULL;
do
{
neww=getnode();
if(start==NULL)
{
start=neww;
trv=start;
}
else
{
trv->next=neww;
neww->prev=trv;
trv=trv->next;
}
printf("\nEnter any key to continue & 0 for stop:");
scanf("%d",&ch);
}while(ch!=0);
return start;
}
node *insert_first(node *start)
{
node *trv,*neww;
trv=start;
neww=getnode();
if(start==NULL)
{
start=neww;
}
else
{
neww->next=start;
start->prev=neww;
start=neww;
}
return start;
}
node *insert_last(node *start)
{
node *trv,*neww;
trv=start;
neww=getnode();
if(start==NULL)
{
start=neww;
}
else
{
while(trv->next!=NULL)
{
trv=trv->next;
}
trv->next=neww;
neww->prev=trv;
}
return start;
}
node *del_first(node *start)
{
node *trv;
trv=start;
start=start->next;
start->prev=NULL;
free(trv);
return start;
}
node *del_last(node *start)
{
node *trv,*p;
trv=start;
if(start==NULL)
{
printf("\nEmpty\n");
}
else if(start->next==NULL)
{
free(start);
start=NULL;
}
else
{
while(trv->next!=NULL)
{
p=trv;
trv=trv->next;
}
}
p=trv->prev;
p->next=NULL;
free(trv);
return start;
}
void display(node *start)
{
node *trv;
trv=start;
if(start==NULL)
{
printf("\nNo node to display\n");
}
printf("\n");
while(trv!=NULL)
{
printf("%d ",trv->data);
trv=trv->next;
}
}
void main()
{
node *f=NULL,*l=NULL;
node *s=NULL;
int ch;
clrscr();
while(1)
{
printf("\n1.Create\n2.Insert at front\n3.Insert at last\n4.Delete at front\n5.Delete at last\n6.Display\n10.exit\nYour choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:s=create();
break;
case 2:s=insert_first(s);
break;
case 3:s=insert_last(s);
break;
case 4:s=del_first(s);
break;
case 5:s=del_last(s);
break;
case 6:display(s);
break;
case 10:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
}
No comments:
Post a Comment