Linked list

0 comments

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

   }
}

About the Author

Follow me


{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}