Implementation of double linked list.

 Implementation of double linked list.


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

struct node
{
 int data;
 struct node *left;
 struct node *right;
};

void insertBegin();
void insertEnd();
void insertLoc();
void deleting();
void display();
int findLength();

struct node *root=NULL;

int main()
{
 int choice;
 while(1)
 {
  printf("Enter your choice to perform opreation on Double Linked List\n");
  printf("Press 1. for Insert Element at begining \n");
  printf("Press 2. for Insert Element at End \n");
  printf("Press 3. for Insert Element at given Location \n");
  printf("Press 4. for Delete element at given position \n");
  printf("Press 5. for Displaying Doubnle linked list \n");
  printf("Press 6. for find lenth \n");
  printf("Press 7. for Exit \n");
  printf("Enter your choice\n");
  scanf("%d",&choice);
  
  switch(choice)
  {
   case 1 : insertBegin();

      break;
      
   case 2 : insertEnd();
      break;
      
   case 3 : insertLoc();
      break;
      
   case 4 : deleting();
      break;
      
   case 5 : display();
      break;
      
   case 6 : printf("Length of Double linked list is : %d\n\n",findLength());
      break;
      
   case 7 : exit(0);
     
   default:  printf("Please Enter a valid choice\n");
  }
 }
}


void insertBegin()
{
 int nodedata;
 struct node *temp;
 temp=(struct node *)malloc(sizeof(struct node));
 printf("Please Enter Data which you want to Insert \n");
 scanf("%d",&nodedata);
 temp->data=nodedata;
 temp->left=NULL;
 temp->right=NULL;
 
 if(root==NULL)
 {
  root=temp;
 }
 else
 {
  temp->right=root;
  root->left=temp;
  root=temp;
 }
  printf("%d inserted into double linked list \n and Linked list is shown as\n",nodedata);
  display();
}

void insertEnd()
{
 int nodedata,len=1;
 struct node *temp,*p;
 temp=(struct node *)malloc(sizeof(struct node));
 printf("Enter data which you want to insert \n");
 scanf("%d",&nodedata);
 temp->data=nodedata;
 temp->left=NULL;
 temp->right=NULL;
 p=root;
 if(p==NULL)
 {
  root=temp;
 }
 else
 {
  while(len<findLength())
  {
   p=p->right;

   len++;
  }
  p->right=temp;
  temp->left=p;
 }
  printf("%d inserted into double linked list \n and Linked list is shown as\n",nodedata);
  display();
}

void insertLoc()
{
 int loc,len=1,nodedata;
 struct node *temp,*p;
 temp=root;
 printf("Enter Location where do you want to insert node \n");
 scanf("%d",&loc);
 if(loc>findLength())
 {
  printf("Invalid location \n");
 }
 else
 {

  while(len<loc-1)
  {
   temp=temp->right;
   len++;
  }
  p=(struct node *)malloc(sizeof(struct node));
  printf("Enter data which you want to insert int doubly linked list\n");
  scanf("%d",&nodedata);
  p->data=nodedata;
  p->right=temp->right;
  temp->right->left=p;
  temp->right=p;
  p->left=temp;
  printf("%d inserted into double linked list \n and Linked list is shown as\n",nodedata);
  display();
 }
 printf("\n");
}

void deleting()
{
 int loc,len=1;

 struct node *temp,*q;
 printf("Enter location frome where do you want to delete node \n");
 scanf("%d",&loc);
 if(loc==1)
 {
  printf("%d deleted from list \n",root->data);
  temp=root;
  root=root->right;
  root->left=NULL;
  free(temp);
 }
 else if(loc>findLength())
 {
  printf("Invalide Location \n");
 }
 else
 {
  temp=root;
  while(len<loc-1)
  {
   temp=temp->right;
   len++;
  }

  q=temp->right;
  temp->right=q->right;
  q->right->left=q->left;
  q->left=NULL;
  q->right=NULL;
  free(q);
 }
}

void display()
{
 struct node *temp;
 temp=root;
 if(temp==NULL)
 {
  printf("List is Empty \n");
 }
 while(temp!=NULL)
 {
  printf("<--%d-->",temp->data);
  temp=temp->right;
 }
 printf("\n\n");
}


int findLength()
{
 int count=0;
 struct node *temp;
 temp=root;
 if(temp==NULL)
 {
  printf("List is Empty \n");
 }
 while(temp!=NULL)
 {
  count++;
  temp=temp->right;
 }
 return count;
}
















Post a Comment

If you have furthur QUERIES please let us know

Previous Post Next Post