DOUBLY LINKED LIST


DOUBLY LINKED LIST PROGRAMMING IN C



#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
void create();
void insert(int);
void delet(int);
void display();
int ch,pos;
char ch1;
struct link
{
int data;
struct link *llink,*rlink;
}*start,*end,*temp;
void main()
{
start=end=NULL;
while(ch!=6)
{
clrscr();
printf("\n\tDOUBLY LINKED LIST\n");
printf("\n1.Creation \n2.Insertion \n3.Deletion \n4.Display \n5.Exit");
printf("\nEnter your choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
printf("\nEnter the postion");
scanf("%d",&pos);
insert(pos);
break;
case 3:
printf("\n[B]ackward list");
printf("\n[F]orward list");
ch1=toupper(getch());
display();
printf("\nEnter the position:");
scanf("%d",&pos);
delet(pos);
break;
case 4:
printf("\n[B]ackward list");
printf("\n[F]orward list");
ch1=toupper(getch());
display();
break;
case 5:
exit(0);
break;
}
getch();
}
getch();
}
void create()
{
int dt;
printf("\nEnter the data:");
scanf("%d",&dt);
if(start==NULL)
{
start=(struct link*)malloc(sizeof(struct link));
start->llink=NULL;
start->rlink=NULL;
start->data=dt;
end=start;
}
else
{
temp=(struct link*)malloc(sizeof(struct link));
temp->llink=end;
temp->rlink=NULL;
end->rlink=temp;
temp->data=dt;
end=temp;
}
printf("\nValue inserted");
}
void insert(int pos)
{
struct link *pre,*t;
int i=1,dt;
temp=start;
while(temp)
{
pre=temp->llink;
if(pos==i)
{
printf("\nEnter the data");
scanf("%d",&dt);
t=(struct link*)malloc(sizeof(struct link));
if(pos==1)
{
t->llink=NULL;
t->rlink=start;
start->rlink=t;
t->llink=NULL;
t->data=dt;
start=t;
}
else if(temp==end)
{
t->llink=end;
t->rlink=NULL;
t->data=dt;
end->rlink=t;
end=t;
}
else
{
t->llink=pre;
pre->rlink=t;
t->rlink=temp;
temp->llink=t;
t->data=dt;
}
}
temp=temp->rlink;
i++;
}
}

void delet(int pos)
{
struct link *pre,*nxt;
int i=1;
temp=start;
while(temp)
{
if(pos==i)
{
pre=temp->llink;
nxt=temp->rlink;
if(pos==1)
{
nxt->llink=NULL;
start->rlink=NULL;
free(start);
start=temp;
}
else if(temp==end)
{
temp->llink=NULL;
pre->rlink=NULL;
free(end);
end=pre;
}
else
{
pre->rlink=nxt;
nxt->llink=pre;
free(temp);
}
}
temp=temp->rlink;
i++;
}
}
void display()
{
if(start==NULL)
printf("\nList is empty");
else if(ch!='F')
{
temp=start;
printf("\nForward list");
while(temp)
{
printf("%d",temp->data);
temp=temp->rlink;
if(temp!=0)
{
printf("->");
}
}
printf("\n");
}
else
{
temp=end;
printf("Backward list");
while(temp)
{
printf("%d->",temp->data);
temp=temp->llink;
}
}
}

Comments

Popular posts from this blog

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE

BOOKS DETAILS USING C STRUCTURE

TRAVELING SALESMAN USING BRANCH AND BOUND TECHNIQUE