Tweet
/* Program to perform operations on a doubly linked list */
#include<stdio.h>
#include<malloc.h>
#define new (nd *)malloc(sizeof(nd))
struct dlist
{
struct dlist *prev;
int info;
struct dlist *next;
};
typedef struct dlist nd;
void create(nd **ptr)
{
char ch;
nd *t=*ptr;
do
{
printf("\nEnter data : ");
scanf("%d",&t->info);
printf("\nWant to continue ?(y/n) : ");
scanf("%s",&ch);
if(ch=='y'||ch=='Y')
{
t->next=new;
t->next->prev=t;
t=t->next;
}
}while(ch=='y'||ch=='Y');
t->next=NULL;
}
void display(nd *ptr)
{
printf("\nLinked List : ");
while(ptr!=NULL)
{
printf(" %d --> ",ptr->info);
ptr=ptr->next;
}
printf("\b\b\b\b ");
printf("\n");
}
void insert_front(nd **ptr)
{
nd *t=new;
printf("\n\nEnter the data to insert at the front : ");
scanf("%d",&t->info);
t->next=*ptr;
t->next->prev=t;
*ptr=t;
printf("\nData inserted...\n");
}
void insert_end(nd **ptr)
{
nd *t=*ptr;
nd *t1=new;
printf("\n\nEnter data to insert at the end : ");
scanf("%d",&t1->info);
while(t->next!=NULL)
t=t->next;
t->next=t1; // t -> traversing node variable
t->next->prev=t; // t1 -> new node to be inserted
t1->next=NULL;
}
void insert_pos(nd **ptr,int loc)
{
int i;
nd *t=*ptr,*t1=new;
printf("\n\nEnter data to insert at chosen position : ");
scanf("%d",&t1->info);
for(i=1;i<loc-1;i++)
t=t->next;
t1->next=t->next;
t->next->prev=t1;
t->next=t1;
t1->prev=t;
}
void del(nd **ptr,int x)
{
if(*ptr==NULL)
printf("\nList empty ...\n");
nd *t=*ptr;
nd *p=NULL;
while(t!=NULL)
{
if(t->info==x)
{
if(t==*ptr)
{
*ptr=t->next;
t->next->prev=*ptr;
free(t);
break;
}
else
{
p->next=t->next;
p->next->prev=p;
free(t);
break;
}
}
else
{
p=t;
t=t->next;
}
}
}
void reverse(nd **ptr)
{
nd *c=*ptr,*p,*temp;
p=temp=NULL;
while(c!=NULL)
{
temp=p;
p=c;
c=c->next;
p->next=temp;
p->prev=c;
}
*ptr=p;
}
int count(nd *ptr)
{
int c=0;
while(ptr!=NULL)
{
c++;
ptr=ptr->next;
}
return c;
}
main()
{
nd *h=new;
int ch,x,pos,c;
printf("\nProgram to perform operations on a doubly linked list : ");
int cnt;
do
{
printf("\n[1] Create node [2] Insert at front [3] Insert at end ");
printf("\n[4] Insert at any position [5] Delete node [6] Reverse the list ");
printf("\n[7] Display the list [8] Count nodes [9] Quit \n");
printf("\nEnter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create(&h);
printf("\nNode created ...\n");
display(h);
break;
case 2:
insert_front(&h);
printf("\nElement inserted ...\n");
display(h);
break;
case 3:
insert_end(&h);
printf("\nElement inserted ...\n");
display(h);
break;
case 4:
printf("\nEnter postion to insert : ");
scanf("%d",&pos);
cnt=count(h);
if(pos==1)
insert_front(&h);
else if(pos==cnt+1)
insert_end(&h);
else if(pos > cnt+1)
{
printf("\nwrong input ....\n");
break;
}
else
insert_pos(&h,pos);
printf("\nElement inserted ...\n");
display(h);
break;
case 5:
c=count(h);
printf("\nEnter element to delete : ");
scanf("%d",&x);
del(&h,x);
cnt=count(h);
if(cnt == c)
printf("\nElement not found...\n");
else
printf("\nElement deleted...\n");
display(h);
break;
case 6:
reverse(&h);
printf("\nList reversed ...\n");
display(h);
break;
case 7:
display(h);
break;
case 8:
cnt=count(h);
printf("\nNumber of nodes : %d\n",cnt);
break;
case 9:
break;
default :
printf("\nEnter correct choice : \n");
break;
}
}while(ch!=9);
printf("\n\n");
}
Double linked list operations in C Programming
Posted by
LAHAUL SETH
~
Double linked list operations in C Programming
2011-11-29T11:01:00+05:30
LAHAUL SETH
Linked List in C
|
Programming in C
|
Comments
Double linked list operations in C Programming
2011-11-29T11:01:00+05:30
LAHAUL SETH
Linked List in C
|
Programming in C
|