// 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;
}

Edited 6 Years Ago by walas: n/a

You said "founded some errors" ... what did you find?

I recommend adding some debug code, for example, to print or check validity of the entire linked list after every operation.

The most probable error would be pointer errors ofcourse. As the above message says you should print the list after every operation to check if the operation was successful.

If you send the error codes we may help better.

Why dont you try coding one function at a time, compile it.. see if it works then go on to the the next function. It will make life a whole lot simpler for you....
Also learn to format your code.It will be much easier for you to read it

Edited 6 Years Ago by abhimanipal: n/a

ok, what is program for all choices?

help me please

thanks !!!

In this forum, it is not allowed to ask for someone else to do your work for you, with no effort on your part.

This is what YOU need to do:
1) Reformat your code to make it readable to people in this forum.
2) Add a function to print the linked list,
and call it immediately inside the "while(1)"
3) run the code, observe the debug output to confirm the linked list is initially empty, select one operation, observe the output.
4) examine the output, think about it, draw conclusions.
5) collect and post your conclusions (what is right, what is wrong), and the output.
6) post the new code.

Those are EXACTLY what anyone else would need to do, in your situation.
Follow the rules and do it yourself.

I watched this program today .. firstly thnks for the program .. yeah it has errors. but i have removed it..
the error main is disp dunction. u havent created any file or part of it... i created dat part ..
2nd error is for search .. disable all function of search den it will work properly... enjoys

Regards- Himanshu jindal
+91-8054689490
tpc, patiala

THANKS for this code. I edited it n made my own double link list (needed for my program). Although code is correct but found some error n corrected it for myself.DOnt remember exactly the problem that was there but thanks. :)

how can i edit your code???? i found some error when i was runing on my computer. u forgot to link prev node when inderting middle(if i am not wrong). for disp function writing code here.

    int disp()    // for displaying from first to last
    { t=last;
    printf("%d",t->data);
    do    
    {t=t->prev;
    printf("%d",t->data);
    } while(t->prev!=NULL);
    return 0;
    }


    int disp()    // for displaying from last to first
    { t=head;
    printf("%d",t->data);
    do    
    {t=t->next;
    printf("%d",t->data);
    } while(t->next!=NULL);
    return 0;
    }
This article has been dead for over six months. Start a new discussion instead.