I implemented doubly linked list using the following program in C but the create function is not working as intended. strangely the insert functions work great.
if i create a list with more than two values and display it it enters an infinite loop.
i've been struggling on this for days....please help guys
if i create a list with more than two values and display it it enters an infinite loop.
Code:
#include <stdio.h>
#include <stdlib.h>
struct dll
{
int ele;
struct dll *right;
struct dll *left;
}*head,*last;
int empty=1,in=0;
void create(void);
void display(void);
int find(int key);
void insfirst(void);
void inslast(void);
void insmid(void);
void delete(void);
int main(void)
{
int data,ch,ch2;
while(1)
{
printf("\n1.Create\n2.Insert\n3.Display\n4.Find\n5.Delete\n6.Exit");
printf("\nEnter choice:");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
break;
case 2:printf("1.Insert first\n2.Insert Middle\n3.Insert last\n");
scanf("%d",&ch2);
if(ch2==1)
insfirst();
if(ch2==2)
insmid();
if(ch2==3)
inslast();
break;
case 3:display();
break;
case 4:printf("Enter Data:");
scanf("%d",&data);
find(data);
break;
case 5:delete();
break;
case 6:return 0;
}
}
}
void create(void)
{
int ch;
int key;
struct dll *tmp;
tmp=(struct dll *)malloc(sizeof(struct dll));
while(1)
{
printf("Enter the data:");
scanf("%i",&key);
tmp->ele=key;
if(head==NULL)
{ tmp->right=NULL;
tmp->left=NULL;
head=tmp;
last=head;
empty=0;
}
else
{
tmp->right=NULL;
tmp->left=last;
last->right=tmp;
last=tmp;
}
printf("Continue? [1/0]");
scanf("%d",&ch);
if(ch==0)
break;
}
}
void display(void)
{
struct dll *tmp;
for( tmp=head;tmp!=NULL;tmp=tmp->right)
{
printf("%d\t",tmp->ele);
}
}
int find(int key)
{ struct dll *tmp;
for(tmp=head;tmp!=NULL;tmp=tmp->right)
{ in++;
if(tmp->ele==key)
{
printf("Found in %d pos",in);
in=0;
return 0;
}
}
printf("Not found");
return -1;
}
void inslast(void)
{
struct dll *tmp;
tmp=(struct dll*)malloc(sizeof(struct dll));
printf("Enter the data:");
scanf("%d",&tmp->ele);
last->right=tmp;
tmp->left=last;
tmp->right=NULL;
last=tmp;
}
void insfirst(void)
{ struct dll *tmp;
tmp=(struct dll*)malloc(sizeof(struct dll));
printf("Enter the data:");
scanf("%d",&tmp->ele);
head->left=tmp;
tmp->right=head;
tmp->left=NULL;
head=tmp;
}
void insmid(void)
{ struct dll *tmp1,*pos,*tmp2;
tmp1=(struct dll*)malloc(sizeof(struct dll));
tmp2=(struct dll*)malloc(sizeof(struct dll));
pos=(struct dll*)malloc(sizeof(struct dll));
int num;
printf("Enter after which element");
scanf("%d",&num);
pos=head;
while(pos!=NULL)
{
if(pos->ele==num)
break;
pos=pos->right;
}
if(pos!=NULL)
{
printf("Enter the data:");
scanf("%d",&tmp1->ele);
tmp1->right=pos->right;
tmp2=pos->right;
pos->right=tmp1;
tmp1->left=pos;
tmp2->left=tmp1;
}
else
printf("Element not found");
}
void delete(void)
{
struct dll *tmp1,*pos,*tmp2;
tmp1=(struct dll*)malloc(sizeof(struct dll));
pos=(struct dll*)malloc(sizeof(struct dll));
tmp2=(struct dll*)malloc(sizeof(struct dll));
int ele;
printf("Enter the element to be deleted:");
scanf("%d",&ele);
pos=head;
while(pos!=NULL)
{
if(pos->ele==ele)
break;
pos=pos->right;
}
if(pos!=NULL)
{
tmp1=pos->left;
tmp2=pos->right;
if(tmp1!=NULL) //not first ele
tmp1->right=tmp2;
else if(tmp1==NULL)
head=tmp2;
if(tmp2!=NULL) //not last ele
tmp2->left=tmp1;
else if(tmp2==NULL)
last=tmp1;
free(pos);
}
else
printf("Element not found");
}
i've been struggling on this for days....please help guys