Programming
Linked list

Question Description

What is the error in following code for doubly linked list?

#include<stdio.h>
#include<stdlib.h>
struct node
{
   int data;
   struct node* next;
   struct node* prev;
};
void add_node(struct node* ptr);
void delete_node(struct node* ptr);
void display_list(struct node* ptr);
int main()
{
   struct node* head=NULL;
   int choice;
   again:
   printf("\n1) Add node");
   printf("\n2) Delete node");
   printf("\n3) Display list");
   printf("\n4) Exit");
   printf("\nEnter choice : ");
   scanf("%d",&choice);
   switch(choice)
   {
     case 1:
       add_node(head);break;
     case 2:
       delete_node(head);break;
     case 3:
       display_list(head);break;
     case 4:
       exit(0);
     default:
       goto again;
   }
   printf("Do you want to continue : ");
   scanf("%d",&choice);
   if(choice=='y'||choice=='Y')
     goto again;
   else
     exit(0);
   return 0;
}
void add_node(struct node* ptr)
{
   struct node* temp=(struct node*)malloc(sizeof(struct node));
   if(ptr==NULL)
   {
     ptr->next=temp;
     ptr->prev=NULL;
   }
   else
   {
     temp->next=ptr->next;
     ptr->next=temp;
   }
   display_list(ptr);
}

void delete_node(struct node* ptr)
{
   struct node* temp=NULL;
   if(ptr->next==NULL)
   {
     printf("\nUnderflow : No node present");
     exit(0);
   }
   else
   {
     temp=ptr->next;
     temp=temp->next;
     ptr=temp;
   }
   display_list(ptr);
}

void display_list(struct node* ptr)
{
   struct node* temp,*start=ptr;
   if(ptr->next==NULL)
   {
     printf("%d---->>>>",ptr->data);
   }
   else
   {
     while(start->next!=NULL)
     {
       printf("%d---->>>>----",start->data);
       temp=start->next;
       start=temp;
     }

   }
}

Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

Final Answer

i have done a lot of good work for some users with a website called studentoffortune with an earnings of $13,848 and 533A+ rating,i assure u wont be down with my work. hope for a positive response

fornicola (20)
UC Berkeley

Anonymous
I was having a hard time with this subject, and this was a great help.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4