Double Link List in C - Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Double Link List in C

Hello guys,

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node *next,*prev;
};
struct node *start=NULL;

void display(struct node *t)        
{
 int i=0;
 struct node *temp;
 temp=t;
 if(temp==NULL)
 {
  printf("\n\nSorry,List is empty!!!!!\n");
 }
 else
 {
  printf("\n\n\nThe Linked list elements are:\n");
  while(temp!=NULL)
  {
   printf("%d\t",temp->data);
   temp=temp->next;
  }
  printf("\n\n\n");
 }
}
 

struct node *getnode()        
{
 return((struct node *)malloc(sizeof(struct node)));
}


void freenode(struct node *ptr) 
{
 free(ptr);
}

void search()                                         
{
 struct node *temp;
 int d,c=0,k;
 printf("Enter the number to be searched : \t");
 scanf("%d",&k);
 temp=start;
 if (temp==NULL)
 {
  printf("List is empty!\n");
 }
 else
 {
  while(temp!=NULL)
   {
    c++;
    if(temp->data==k)
     {
      d=c;
      break;
     }
    temp=temp->next;
   }
   if(temp==NULL)
   {
    printf("Element not found\n");
   }
   else
   {
    printf("Element is present at position %d",d);
   }
 }
}

 
int count()       
{
 struct node *temp;
 int c=0;
 temp=start;
 if (temp==NULL)
 {
  printf("List is empty!\n");
  return 0;
 }
 else
 { 
  while(temp!=NULL)
  {
   c++;
   temp=temp->next;
  }
  return(c);
 }
}

void delbeg()       
{
 struct node *temp;
 temp=start;
 if(start== NULL)
 {
  printf("List is empty!\n");
 }
 else
 {
  printf("First element %d successfully deleted\n",temp->data);
  if(start->next!=NULL)
  {
   start=start->next;
   start->prev=NULL;
   freenode(temp);
   display(start);
  }
  else
  {
   start=NULL;
   display(start);
  }
 }
}

void delend()        
{
 int x;
 struct node *temp,*ptr;
 temp=start;
 if(start== NULL)
 {
  printf("List is empty!\n");
 }
 else
 {
  if(temp->next!=NULL)
  {
   while(temp->next!=NULL)
   {
    ptr=temp;
    temp=temp->next;
   }
   x=temp->data;
   ptr->next=NULL;
  }
  else
  {
   x=temp->data;
   start=NULL;
  }
 }
 printf("Last element %d successfully deleted\n",x);
 freenode(temp);
 display(start);
}


void delloc()       
{
 int x,p,i,c;
 struct node *t,*del;
 t=start;
 printf("Enter the location of node to be deleted\n");
 scanf("%d",&p);
 c=count();
 if(start== NULL)
 {
  printf("Empty list\n");
 }
 else if(p==1)
  delbeg();
 else if(p==c)
 {
  delend();
 }
 else
 { 
  for(i=1;i<p-1;i++)
  {
   if(t->next==NULL)
   {
    printf("There are less than %d elements in the list\n",p);
    break;
   }
  t=t->next;  
  }
  if(i==p-1)
  {
   if(t->next!=NULL)
   {
    x=(t->next)->data;
    del=t->next;
    t->next=(t->next)->next;
    (t->next)->prev=t;
    freenode(del);
    printf("Element %d present at desired node is successfully deleted\n",x);
    display(start);
   }
   else
   {
    printf("\nThere are less than %d elements in the list\n",p);
   }
  }
 }
}
 
void insertbeg()        
{
 struct node *newnode;
 int n;
 printf("Enter the data to be inserted : \n");
 scanf("%d",&n);
 newnode=getnode();
 newnode->data=n;
 newnode->next=start;
 newnode->prev=NULL;
 start->prev=newnode;
 printf("\nElement %d successfully inserted in the first position",n);
 start=newnode;
 display(start);
}
 
void insertend()        
{
 struct node *temp,*newnode;
 int n;
 printf("Enter the data to be inserted : \n");
 scanf("%d",&n);
 temp=start;
 if(temp==NULL)
 {
  newnode=getnode();
  start=newnode;
  newnode->data=n;
  newnode->next=NULL;
  newnode->prev=NULL;
  display(start);
 }
 else
 {
   while(temp->next!=NULL)
   {
    temp=temp->next;
   }
  newnode=getnode();
  newnode->data=n;
  newnode->next=NULL;
  newnode->prev=temp;
  temp->next=newnode;
  display(start);
 }
}

void insertloc()        
{
 struct node *temp,*newnode;
 int n,i,p,c;
 printf("Enter the position in which element is to be inserted\n");
 scanf("%d",&p);
 temp=start;
 c=count();
 if(p==1)
 {
  insertbeg(); 
 }
 else if(start==NULL)
 {
  printf("List is empty!!!!!\n");
 }
 else if(p==c+1)
 {
  insertend();
 }
 else
 {
  for(i=1;i<p-1;i++)
  {
   if(temp->next==NULL)
   {
    printf("The no. of Elements are less than %d\n ",p);
    break;
   }
   temp=temp->next;
  }
  if(i==p-1)
  {
   printf("Enter the data to be inserted : \n");
   scanf("%d",&n);
   newnode=getnode();
   newnode->data=n;
   newnode->next=temp->next;
   (temp->next)->prev=newnode;
   temp->next=newnode;
   newnode->prev=temp;
   display(start);
  }
 }
}
 
struct node *create()
{
 struct node *newnode,*temp;
 int num;
 printf("\nEnter a LIST\n");
 printf("\nPlease enter data (Press -1 to end)\n");
 scanf("%d",&num);
 start=NULL;
 temp=start;
 while(num!=-1)
 {
  if(temp==NULL)
  {
   newnode=getnode();
   newnode->data=num;
   newnode->next=NULL;
   newnode->prev=NULL;
   start=newnode;
   temp=start;
  }
  else
  {
   newnode=getnode();
   newnode->data=num;
   newnode->next=NULL;
   newnode->prev=temp;
   temp->next=newnode;
   temp=temp->next;
  }
  printf("\nPlease enter data(Press -1 to end)\n");
  scanf("%d",&num);
 } 
 display(start); 
 return(start);
}

void concatenate ()
{
 struct node *newnode,*temp,*t,*list1,*list2;
 temp = start;
 printf("List1 : \n");
 list1 = temp;
 printf("Enter the elements for list2  : \n");
 list2=create();
 temp=list1;
 t=list2;
 while(temp->next!=NULL)
 {
  temp=temp->next;
 }
 if(list2!=NULL)
 {
  temp->next=list2;
  t->prev=temp;
 }
 printf("\nConcatenated list : \n");
 display(list1);
}

void split()
 {
 struct node *list1,*list2,*temp;
 int k,c=0,i,m;
 temp = start;
 list1=temp;
 m=count();
 do
 {
  printf("\nEnter the no of elements in list1\n");
  scanf("%d",&k);
  if(k>m)
  {
   printf("\nInvalid number\n");
   break;
  } 
  temp=list1;
  for(i=1;i<k;i++)
  {
   temp=temp->next;
  }
  if(temp->next!=NULL)
  {
   list2=temp->next;
   temp->next=NULL;
   list2->prev=NULL;
  }
  else
  {
   list2=NULL;
  }
  printf("\n\nLIST 1\n\n");
  display(list1);
  printf("\n\nLIST 2\n\n");
  display(list2);
 }while(0);
 start = list1;
}
 


/*void split()
{
 struct node *list1,*list2,*temp;
 int k,c=0,i,m;
 temp = start;
 list1=temp;
 m=count();
 do
 {
  printf("Enter the no of elements in list1 : \n");
  scanf("%d",&k);
  if(k>m)
  {
   printf("Invalid number\n");
   break;
  } 
  temp=list1;
  for(i=1;i<k;i++)
  {
   temp=temp->next;
  }
  if(temp->next!=NULL)
  {
   list2=temp->next;
   temp->next=NULL;
   list2->prev=NULL;
  }
  else
  {
   list2=NULL;
  }
  printf("List1 : \n");
  display(list1);
  printf("List2 : \n");
  display(list2);
 }while(0);
}*/

void reverse()              
{
 struct node *temp,*newnode,*t;
 temp=start;
 if (temp==NULL)
 {
  printf("List is empty!\n");
 }
 else
 {
  while(temp->next!=NULL)
  {
   temp=temp->next;
  }
  printf("\nThe reversed linked list elements are:\n");
  while(temp!=NULL)
  {
   printf("%d\t",temp->data);
   temp=temp->prev;
  }
 }
}
 
 
void copy()
{
 struct node *temp,*list1,*list2;
 temp = start;
 list1 = temp;
 printf("List1 : \n");
 display(list1);
 list2= list1;
 printf(" List2 : \n");
 display(list2);
}

void main()
{
 int ch,k;
 do
 {
  printf("~~~ MENU ~~~\n");
  printf("1. Create List \n");
  printf("2. Insert At Beginning \n");
  printf("3. Insert At End \n");
  printf("4. Insert At Location \n");
  printf("5. Delete At Beginning\n");
  printf("6. Delete At End \n");
  printf("7. Delete At Location\n");
  printf("8. Display \n");
  printf("9. Search\n");
  printf("10. Count\n");
  printf("11. Copy \n");
  printf("12. Concatenate \n");
  printf("13. Split \n");
  printf("14. Reverse \n");
  printf("15. Quit \n");
  printf("\nEnter your choice\n");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:      
    start=create();
    break;
   case 2:      
    insertbeg();
    break;
   case 3:      
    insertend();
    break;
   case 4:      
    insertloc();
    break;
   case 5:      
    delbeg();
    break;
   case 6:      
    delend();
    break;
   case 7:      
    delloc();
    break;

   case 8: display(start);  
    break;
   case 9:      
    search();
    break;
   case 10:     
    k=count();
    printf("\n Count = %d\n",k);
    break;
   case 11:
    copy();     
    break;
   case 12:
    concatenate ();   
    break;
   case 13:
    split();    
    break;
   case 14:     
    reverse();
    break;
   case 15:
    printf("\nThank you !!!\n");   
    break;
   default:
    printf("\nInvalid input!!!\n"); 
  }
 }while(ch!=15);
} 

/*

~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
1

Enter a LIST

Please enter data (Press -1 to end)
56

Please enter data(Press -1 to end)
44

Please enter data(Press -1 to end)
29

Please enter data(Press -1 to end)
48

Please enter data(Press -1 to end)
27

Please enter data(Press -1 to end)
-1



The Linked list elements are:
56 44 29 48 27 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
2
Enter the data to be inserted : 
555

Element 555 successfully inserted in the first position


The Linked list elements are:
555 56 44 29 48 27 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
3
Enter the data to be inserted : 
333



The Linked list elements are:
555 56 44 29 48 27 333 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
4
Enter the position in which element is to be inserted
5
Enter the data to be inserted : 
5656



The Linked list elements are:
555 56 44 29 5656 48 27 333 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
5
First element 555 successfully deleted



The Linked list elements are:
56 44 29 5656 48 27 333 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
6
Last element 333 successfully deleted



The Linked list elements are:
56 44 29 5656 48 27 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
4
Enter the position in which element is to be inserted
2
Enter the data to be inserted : 
525



The Linked list elements are:
56 525 44 29 5656 48 27 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
7
Enter the location of node to be deleted
5
Element 5656 present at desired node is successfully deleted



The Linked list elements are:
56 525 44 29 48 27 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
9
Enter the number to be searched :  29
Element is present at position 4~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
10

 Count = 6
~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
11
List1 : 



The Linked list elements are:
56 525 44 29 48 27 


 List2 : 



The Linked list elements are:
56 525 44 29 48 27 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
12
List1 : 
Enter the elements for list2  : 

Enter a LIST

Please enter data (Press -1 to end)
888

Please enter data(Press -1 to end)
777

Please enter data(Press -1 to end)
999

Please enter data(Press -1 to end)
-1



The Linked list elements are:
888 777 999 



Concatenated list : 



The Linked list elements are:
56 525 44 29 48 27 888 777 999 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 
Enter your choice
1

Enter a LIST

Please enter data (Press -1 to end)
56

Please enter data(Press -1 to end)
44

Please enter data(Press -1 to end)
29

Please enter data(Press -1 to end)
48

Please enter data(Press -1 to end)
27

Please enter data(Press -1 to end)
-1

~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
13

Enter the no of elements in list1
2

LIST 1
The Linked list elements are:
56 44 

LIST 2
The Linked list elements are:
29 27 48 

~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
14

The reversed linked list elements are:
999 777 888 27 48 29 44 525 56 
~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
5
First element 888 successfully deleted



The Linked list elements are:
777 999 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
5
First element 777 successfully deleted



The Linked list elements are:
999 


~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
5
First element 999 successfully deleted


Sorry,List is empty!!!!!
~~~ MENU ~~~
1. Create List 
2. Insert At Beginning 
3. Insert At End 
4. Insert At Location 
5. Delete At Beginning
6. Delete At End 
7. Delete At Location
8. Display 
9. Search
10. Count
11. Copy 
12. Concatenate 
13. Split 
14. Reverse 
15. Quit 

Enter your choice
15

Thank you !!!


*/


PropellerAds