Umang
Umang

Reputation: 139

Unable to insert elements in circular doubly linked list

I am unable to insert elements in this circular doubly linked list. First element gets entered without any issue. But as soon as I try to insert the second element, a problem is encountered. The problem is: program terminates itself. I don't know exactly where I am going wrong. Any help is appreciated. Thanks in advance.

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
  int data;
  struct node *next;
  struct node *prev;
}list;

list *start=NULL;
list *end=NULL;

void insert();
void display();
void reverse_display();

void main()
{
  int n;

  printf("1: Insert Elements\n");
  printf("2: Display\n");
  printf("3: Reverse Display\n");

  for(;;)
  {
    printf("Enter choice: ");
    scanf("%d",&n);

    switch(n)
    {
      case 1: insert();
      break;

      case 2: display();
      break;

      case 3: reverse_display();
      break;

      default: printf("Wrong Input!!!\n");
      exit(0);
    }
  }
}


void insert()
{
  int num;
  list *new_node , *ptr;

  printf("Enter the number: ");
  scanf("%d",&num);

  new_node = (list *)malloc(sizeof(list));
  new_node->data = num;

  if(start == NULL)
  {
      new_node->next = start;
      new_node->prev = end;
      start = new_node;
      end = new_node;
  }
  else
  {
    ptr = start;
    while(ptr->next != start)
      ptr = ptr->next;
    ptr->next = new_node;
    new_node->prev = ptr;
    new_node->next = start;
    start->prev = new_node;
    end = new_node;
  }
}

void display()
{
  list *ptr;
  ptr = start;
  printf("\nElements in original order:\n");
  if(start == NULL)
    printf("Empty List!!!\n");
  else
  {
    while(ptr->next!=start)
  {
    printf("%d\n",ptr->data);
    ptr=ptr->next;
  }
  printf("%d\n",ptr->data);
  }
}

void reverse_display()
{
  list *ptr , *temp;
  ptr = end;
  printf("\nElements in reverse order\n");
  while(ptr->prev!=end)
  {
    printf("%d\n",ptr->data);
    ptr = ptr->prev;
  }
  printf("%d\n",ptr->data);
}

Upvotes: 1

Views: 109

Answers (1)

H.S.
H.S.

Reputation: 12669

In a doubly circular linked list, if there is one node then it's next and prev pointer should point to itself. In insert(), you are doing

  if(start == NULL)
  {
      new_node->next = start;
      new_node->prev = end;
     ......

due to this, the next and prev pointers end up pointing to NULL because start and end are initially set to NULL. Now, when you insert another element in the list, you are end up accessing NULL pointer

    while(ptr->next != start)
       ptr = ptr->next;

and that's why you are observing program termination.

When inserting first node, you should properly set the next and prev pointers. In your code, just set the start and end pointer before setting next and rev, like this:

  if(start == NULL)
  {
      start = new_node;
      end = new_node;
      new_node->next = start;
      new_node->prev = end;
  }

Additional:

  • Using void as return type of main function is not as per standards. The return type of main function should be int.
  • Follow good programming practice, always check the malloc return.
  • Do not cast the malloc return.

Upvotes: 4

Related Questions