this is my function to inpute data in a double linked list
void input(int element){
struct node *p=head,*q=(struct node *)malloc(sizeof(struct node));
if(p==NULL){
head=q;
q->data=element;
q->prev=NULL;
q->next=NULL;
}
else{
while(p->next!=NULL){
p=p->next;
}
p->next=q;
q->prev=p;
q->next=NULL;
q->data=element;
}}
this is my function to display data in double linked list
void display(){
struct node *p=head;
while(p!=NULL){
p=p->next;
printf("%d ",p->data);
}
printf("\n");}
if i input one or more data to my double linkd list and try to display my linked list ,the programs terminate with error code -1073741819 (0xC0000005)
if i input 1,2,3,4 to my linked list output is:
2 3 4 Process finished with exit code -1073741819 (0xC0000005)
here is the complete code of my program:
struct node {
int data;
struct node *next;
struct node *prev;
}*head=NULL;
void display(){
struct node *p=head;
while(p!=NULL){
p=p->next;
printf("%d ",p->data);
}
printf("\n");
}
void input(int element){
struct node *p=head,*q=(struct node *)malloc(sizeof(struct node));
if(p==NULL){
head=q;
q->data=element;
q->prev=NULL;
q->next=NULL;
}
else{
while(p->next!=NULL){
p=p->next;
}
p->next=q;
q->prev=p;
q->next=NULL;
q->data=element;
}
}
int main() {
int x;
while(1){
printf("Press 1.INSERT 4.DISPLAY ALL ELEMENTS 5.QUIT \n");
scanf("%d",&x);
if(x==1){
int element;
printf("\n write your element");
scanf("%d",&element);
input(element);
}
else if(x==4){
display();
}
else if (x==5){
return 0;
}
}
}
There is a bug in the function display due to the invalid order of two statements.
Instead of
void display(){
struct node *p=head;
while(p!=NULL){
p=p->next;
printf("%d ",p->data);
}
printf("\n");}
It should look at least like
void display(){
struct node *p=head;
while(p!=NULL){
printf("%d ",p->data);
p=p->next;
}
printf("\n");}
User contributions licensed under CC BY-SA 3.0