Wednesday, 22 February 2017

Creation and manipulation of doubly linked list

Double linked list is a data structure similar to linked list except that the list can be traversed from both sides. Each node in the list contains two pointers one for the previous node and the other for the next node.

//C program to create, insert and delete elements from a doubly linked list
#include<stdio.h>
#include<conio.h>
struct node
{
   struct node *pre,*next;
   int data;
}*head,*newnode;
int count=0;
void create()
{
  newnode=(struct node *)malloc(sizeof(struct node));
  printf("Enter data for the node\n");
  scanf("%d",&newnode->data);
  newnode->next=NULL;
  newnode->pre=NULL;
}
void insert(int pos)
{
  struct node *tmp;
  int i=1;
  create();
  if (pos==1)
  {
    if (head==NULL)
    {
       head=newnode;
    }
    else
    {
       newnode->next=head;
       head->pre=newnode;
       head=newnode;
    }
   }
   else if (pos>count)
   {
 tmp=head;
 while (tmp->next != NULL)
 tmp=tmp->next;
 tmp->next=newnode;
 newnode->pre=tmp;
    }
    else
    {
       tmp=head;
       while (i<pos)
       {
   tmp=tmp->next;
   i++;
       }
       newnode->pre=tmp->pre;
       tmp->pre->next=newnode;
       tmp->pre=newnode;
       newnode->next=tmp;
    }
     count++;
}
void delete(int pos)
{
 struct node *tmp;
 int i=0;
 if(head==NULL)
 {
  printf("Empty list\n");
  exit(0);
 }
 if(pos==1)
 {
  tmp=head;
  head=head->next;
  head->pre=NULL;
  free(tmp);
 }
 else if(pos==count)
 {
  tmp=head;
  while(tmp->next!=NULL)
  {
    tmp=tmp->next;
  }
  tmp->pre->next=NULL;
  free(tmp);
 }
 else
 {
  tmp=head;
  while(i<pos-1)
  {
   tmp=tmp->next;
   i++;
  }
  tmp->next->pre=tmp->pre;
  tmp->pre->next=tmp->next;
  free(tmp);
 }
 count--;
}
void display()
{
   struct node *tmp;
   tmp=head;
   if (tmp==NULL)
   printf("List empty");
   else
   {
      while (tmp != NULL)
      {
  printf("%d->",tmp->data);
  tmp=tmp->next;
      }
   }
   printf("\n");
}
main()
{
  int choice,pos;
  struct node *tmp;
  clrscr();
  head=NULL;
  do
  {
    printf("1. Create\n");
    printf("2. Insert\n");
    printf("3. Delete\n");
    printf("4. Display\n");
    printf("Enter your choice ");
    scanf("%d",&choice);
    switch (choice)
    {
      case 1: create();
       if (head==NULL)
       {
   head=newnode;
   count=1;
       }
       else
       {
    tmp=head;
    while (tmp->next!=NULL)
    tmp = tmp->next;
    tmp->next=newnode;
    newnode->pre=tmp;
    count++;
        }
        break;
       case 2: printf("Enter the position to insert ");
        scanf("%d",&pos);
        insert(pos);
        break;
       case 3:printf("Enter position to delete ");
       scanf("%d",&pos);
       delete(pos);
       break;
       case 4: display();
  break;
       default: exit(0);
    }
   }while(1);
}
/*
OUTPUT
--------
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 1
Enter data for the node
1
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 1
Enter data for the node
2
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 2
Enter the position to insert 1
Enter data for the node
0
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
0->1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 3
Enter position to delete 1
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->2->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 3
Enter position to delete 2
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 4
1->
1. Create
2. Insert
3. Delete
4. Display
Enter your choice 5
*/