// i want create doubly linked list in c with following choices:
1- add to head
2-add to mid
3-add to tail
4- delete from head
5-delete from middle
6-delete from tail
7- search
8- print
9- exit
i write my program but founded some errors what is it?
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *prev,*next;
};
typedef struct node node;
node *head,*last,*temp,*t,*p,search;
int d;
void addhead();
void addmiddle();
void addtail();
void delhead();
void delmiddle();
void deltail();
void search();
void main()
{
int ch;
while(1)
{
clrscr();
printf("\n1. add to head ");
printf("\n2. add to Middle ");printf("\n3. add to tail ");
printf("\n4. Delete from head ");
printf("\n5. Delete from Middle ");
printf("\n6. Delete from tail ");
print("\n7.search");
printf("\n8. Exit");
printf("\nEnter your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
addhead();
disp();
break;
case 2:
addmiddle();
disp();
break;
case 3:
addtail();
disp();
break;
case 4:
delhead();
disp();
break;
case 5:
delmiddle();
disp();
break;
case 6:
deltail();
disp();
break;
case 7:
search();
disp();
break;
case 8:
exit(0);
default:
printf("\nInvalid Choice");
}
getch();
}
}
void addhead()
{
temp=(node*)malloc(sizeof(node));
printf("\nEneter the Data : ");
scanf("%d",&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
temp->next=head;
head->prev=temp;
head=temp;
}
}
void addmiddle()
{
int d;
temp=(node*)malloc(sizeof(node));
printf("\nEneter the Data : ");
scanf("%d",&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
printf("\nEnter the node after which insertion to be made : ");
scanf("%d",&d);
while(t!=NULL)
{
if(t->data==d)
{
temp->next=t->next;
temp->prev=t;
t->next=temp;
return;}
else
t=t->next;
}
printf("\nadd node not found");
}
}
void addtail()
{
node *t;
temp=(node*)malloc(sizeof(node));
printf("\nEneter the Data : ");
scanf("%d",&temp->data);
temp->next=temp->prev=NULL;
if(head==NULL)
head=temp;
else
{
t=head;
while(t->next!=NULL)
t=t->next;
t->next=temp;
temp->prev=t;
}
}
void delhaed()
{
if(head==NULL)
printf("\nList is Empty");
else
{
t=head;
printf("\nDeleted node is %d\n",t->data);
head=head->next;
head->prev=NULL;
free(t);
}
}
void delmiddle()
{
int d;
node *s,*n;
if(head==NULL)
printf("\nList is Empty");
else
{
printf("\nEnter the node data to be deleted : ");
scanf("%d",&d);
if(head->data==d)
{
t=head;
head=head->next;
head->prev=NULL;
printf("\nDeleted node is %d\n",t->data);
free(t);
}
else
{
t=head;
while(t->next!=NULL)
{
if(t->data==d){
s=t;
printf("\nDeleted node is %d\n",s->data);
p=t->prev;
n=t->next;
p->next=t->next;
n->prev=p;
free(s);
}
else
{
p=p->next;
t=t->next;
}
}
}
} }
void deltail()
{
if(head==NULL)
printf("\nList is Empty");
else if(head->next==NULL)
{
t=head;
printf("\nDeleted node is %d\n",t->data);
head=NULL;
}
else
{
t=head;
while(t->next!=NULL)
{
t=t->next;
}
p=t->prev;
p->next=NULL;
printf("\nDeleted node is %d\n",t->data);
free(t);
}
}
//search
voidsearch()
{
while(head!=NULL)
{
if(head->info==item) // if the values match,
return head; // return the matching node.
head=head->next; // otherwise, move on
}
return NULL;
}