Doubly Linked List Program


//Doubly Linked List

#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *prev;
 struct node *next;
}*start;

void create_list()
{
 struct node *q,*new_node;
 int i,n,m;
 printf("How many nodes you want ?");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
  new_node=(struct node *)malloc(sizeof(struct node));
  printf("Enter the element: ");
  scanf("%d",&new_node->data);
  new_node->next=NULL;
  if(start==NULL)
  {
   new_node->prev=NULL;
   start->prev=new_node;
   start=new_node;
  }
  else
  {
   q=start;
  while(q->next!=NULL)
  q=q->next;
  q->next=new_node;
  new_node->prev=q;
 }
}
}
void display()
{
 struct node *q;
 if(start==NULL)
 {
  printf("List is empty\n");
  return;
 }
 q=start;
 printf("List is\n");
 while(q!=NULL)
 {
  printf("%4d",q->data);
  q=q->next;
 }
 printf("\n");
}

void addatbeg()
{
 struct node *new_node;
 new_node=(struct node *)malloc(sizeof(struct node));
 printf("Enter the element ");
 scanf("%d",&new_node->data);
 new_node->prev=NULL;
 new_node->next=start;
 start->prev=new_node;
 start=new_node;
}

void addafter()
{
 struct node *new_node,*q;
 int i,m,pos;
 printf("Enter the element: ");
 scanf("%d",&m);
 printf("\nEnter the position after which this element is inserte");
 scanf("%d",&pos);
 q=start;
 for(i=0;i<pos-1;i++)
 {
  q=q->next;
  if(q==NULL)
  {
   printf("There are less than %d elements\n",pos);
   return;
  }
}
 new_node=(struct node *)malloc(sizeof(struct node));
 new_node->data=m;
 q->next->prev=new_node;
 new_node->next=q->next;
 new_node->prev=q;
 q->next=new_node;
}

void del()
{
 struct node *new_node,*q;
 int m;
 printf("Enter the element for deletion ");
 scanf("%d",&m);

 if(start->data==m)
 {
  new_node=start;
  start=start->next;    //first element deleted
  start->prev=NULL;
  free(new_node);
  return;
}
q=start;
while(q->next->next!=NULL)
{
 if(q->next->data==m)      //element deleted in between
 {
  new_node=q->next;
  q->next=new_node->next;
  new_node->next->prev=q;
  free(new_node);
  return;
 }
 q=q->next;
}
if(q->next->data==m)  //last element deleted
{
 new_node=q->next;
 free(new_node);
 q->next=NULL;
 return;
}
printf("Element %d not found\n",m);
} //end of del()

void count()
{
 struct node *q;
 int cnt=0;
 q=start;

 while(q!=NULL)
 {
  q=q->next;
  cnt++;
 }
 printf("Number of elements are %d\n",cnt);
}

void rev()
{
 struct node *p1,*p2;
 p1=start;
 p2=p1->next;
 p1->next=NULL;
 p1->prev=p2;
 while(p2!=NULL)
 {
  p2->prev=p2->next;
  p2->next=p1;
  p1=p2;
  p2=p2->prev;  //next of p2 changed to prev
 }
 start=p1;
}

void main()
{
 char choice;
 while(1)
 {
  printf("1. Create List\n");
  printf("2. Add at Begining\n");
  printf("3. Add After\n");
  printf("4. Delete\n");
  printf("5. Display\n");
  printf("6. Count\n");
  printf("7. Reverse\n");
  printf("8. Exit\n");
  printf("Enter your choice ");
  scanf("%d",&choice);
  switch(choice)
  {
   case 1: create_list();
  break;
   case 2: addatbeg();
  break;
   case 3: addafter();
  break;
   case 4: del();
  break;
   case 5: display();
  break;
   case 6: count();
  break;
   case 7: rev();
  break;
   case 8: exit(0);

   default: printf("Out of choice !");
  }
}
}

Post a Comment

Previous Post Next Post