Circular Double link list in C - Coders PlayGround

Learn to code and change the future!

Friday, 4 August 2017

Circular Double link list in C

Hello guys,

#include<stdio.h>
#include<stdlib.h>
struct node
{
 int data;
 struct node * next;
};
struct node *head1=NULL,*head2=NULL,*head=NULL;
int c;
void create();
void display();
void insbeg();
void insend();
void insloc();
void delbeg();
void delend();
void delloc();
void search();
void count();
void concantente();
void reverse();
void split();
void copy();
int return_count();
struct node* getnode();
main()
{
  int choice;
  do
  {
   printf("\n__MENU__\n");
  printf("\n1.create list");
  printf("\n2.insert at the beginning");
  printf("\n3.insert at the end");
  printf("\n4.insert at any location");
  printf("\n5.delete at the beginning");
  printf("\n6.delete at the end");
  printf("\n7.delete at any location");
  printf("\n8.search for an element");
  printf("\n9.count total elements");
  printf("\n10.concatente");
  printf("\n11.reverse the list");
  printf("\n12.copy list elements");
  printf("\n13.splitting a list");
  printf("\n14.display");
  printf("\n15...EXIT...\n");
   printf("\nENTER your choice\n");
   scanf("%d",&choice);
   switch(choice)
   {
    case 1: create();break;
    case 2:insbeg();break;
    case 3:insend();break;
    case 4:insloc();break;
    case 5:delbeg();break;
    case 6:delend();break;
    case 7:delloc();break;
    case 8:search();break;
    case 9:count();break;
    case 10:concantente();break;
    case 11:reverse();break;
    case 12:copy();break;
    case 13:split();break;
    case 14: display();break;

    case 15:printf("~~~~~~~THANK YOU~~~~~~~\n");break;

   default:printf("INVALID input !!!");break;   
  }
  }while(choice!=15);
}


void create()
{
 {
  int x, i,n;
  head=NULL;
  struct node* newnode,*temp;
  temp=head;
  printf("Enter no. of elements in the LIST \n");
  scanf("%d",&n);
  printf("Enter %d elements\n",n);
  for(i=0;i<n;i++)
  {
   temp=head; 
   newnode=getnode();
   scanf("%d",&x);
      newnode->data=x;
      if(head==NULL)
      {
       head=newnode;
       newnode->next=head;
   }
      else
   {
          while(temp->next!=head)
    temp=temp->next;
    temp->next=newnode;
    newnode->next=head; 
   }
  }
 }
}

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

void insbeg()
{
 struct node* temp,*newnode;
 temp=head;
 newnode=getnode();
 printf("\nEnter element\n");
 scanf("%d",&newnode->data);
 if(head==NULL)
 {
  head=newnode;
  newnode->next=head;
 }
 else
 {
  while(temp->next!=head)
   temp=temp->next;
  newnode->next=temp->next;
  temp->next=newnode;
  head=newnode; 
  
 }
}

void insend()
{
 struct node* temp,*newnode;
 temp=head;
 newnode=getnode();
 printf("\nEnter element\n");
 scanf("%d",&newnode->data);
 if(head==NULL)
 {
  head=newnode;
  newnode->next=head;
 }
 else
 {
  while(temp->next!=head)
  temp=temp->next;
  temp->next=newnode;
  newnode->next=head; 
 }
}

void insloc()
{
 int pos,total;
 struct node* temp,*newnode;
 temp=head;
 newnode=getnode();
 printf("\nEnter POSITION\n");
 scanf("%d",&pos);
 if(pos==1)
 {
  insbeg();
 }
 else
 {
  total=return_count();
  if(pos>(total+1))
  printf("\nInvalid POSITION !!!!\n");
  else
  {
   if(pos==(total+1))
   {
    insend();
   }
   else
   {
    printf("\nEnter element\n");
    scanf("%d",&newnode->data);
    while(pos>2)
    {
     temp=temp->next;
     pos--;
    }
    newnode->next=temp->next;
    temp->next=newnode;
   }
   
  }
 }
}

void delbeg()
{
 struct node* temp, *del;
 temp=head;
 del=head;
 if(head==NULL)
  display();
 else if(temp->next==head)
 {
  head=NULL;
  free(temp);
 }
 else
 {
  while(temp->next!=head)
   temp=temp->next;
  temp->next=head->next;
  head=head->next;
  free(del);
  
 }
 
}

void delend()
{
 struct node* temp, *pretemp;
 temp=head;
 if(head==NULL)
  display();
 else if(temp->next==head)
 {
  head=NULL;
  free(temp);  
 }
 else
 {
  while(temp->next!=head)
  {
   pretemp=temp;
   temp=temp->next;  
  }
  pretemp->next=temp->next;
  free(temp);
 }
 
}

void delloc()
{
 int total,pos;
 struct node* temp,*pretemp;
 temp=head;
 if(head==NULL)
  display();
 else
 {
  printf("\nEnter POSITION\n");
  scanf("%d",&pos);
  total=return_count();
  
  if(pos==1)
  {
   delbeg();
  }
  
  else if(pos>total)
   printf("\nInvalid POSITION !!!! \n");
  else if(pos==total)
  {
   delend();
  }
  else
  {
   while(pos>=2)
   {
    pretemp=temp;
    temp=temp->next;
    pos--;
   }
   pretemp->next=temp->next;
   free(temp);
  }
 }
}

void search()
{
 int a,i=1,value;
 struct node* temp;
 temp=head;
 if(head==NULL)
 printf("\nlist is empty !!!!!\n");
 else
 {
  printf("Enter element to be searched\n");
  scanf("%d",&value);
     if(temp->data==value)
     printf("\n%d is at position 1.\n ",value);
     else
     {
      while(temp->next!=head)
      {
       temp=temp->next;
       i++;
       if(temp->data==value)
       break;
       
   }
   if(temp->data==value)
   printf("\n%d is at position %d.\n ",value,i);
   else if(temp->next==head)
   printf("\nELEMENT NOT FOUND !!!\n");
  }
 }
 
}

void count()
{


 int i = 1;
 struct node* temp;
 temp=head;
 if(head==NULL)
 printf("\nlist is empty!!!\n");
 else
 {
  while(temp->next!=head)
  {
   temp=temp->next;
   i++;
  }
  printf("\nTotal Count = %d\n",i);
 }
}

void concantente()
{ 
 head=NULL;
 int n,i;
 struct node *temp,*newnode;
 printf("Enter the no. of elements in LIST 1\n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  newnode=getnode();
  scanf("%d",&newnode->data);
  temp=head;
  if(head==NULL)
  {
   newnode->next=NULL;
   head=newnode;
  }
  else
  {
   while(temp->next!=NULL)
    temp=temp->next;
   temp->next=newnode;
   newnode->next=NULL;
  }
  
 }
 printf("Enter the no. of elements in LIST 2\n");
 scanf("%d",&n);
 printf("Enter %d elements\n",n);
 for(i=0;i<n;i++)
 {
  newnode=getnode();
  scanf("%d",&newnode->data);
  temp=head;
  if(head==NULL)
  {
   newnode->next=NULL;
   head=newnode;
  }
  else
  {
   while(temp->next!=NULL)
    temp=temp->next;
   temp->next=newnode;
   newnode->next=NULL;
  }
 }
 newnode->next=head;
 printf("\nList concatented !\n");
}

void reverse()
{
 struct node *temp,*newnode, *temp1;
 if(head==NULL)
 printf("List is Empty. Create a list to reverse it.!\n");
 else
 {
  head1=NULL;
  temp=head;
  
  while(temp->next!=head)
  {
   temp1=head1;
   newnode=getnode();
   newnode->data=temp->data;
   if(head1==NULL)
   {
    head1=newnode;
    newnode->next=head1;
   }
   else
   {
    while(temp1->next!=head1)
     temp1=temp1->next;
    newnode->next=temp1->next;
    temp1->next=newnode;
    head1=newnode;
   }
   
   temp=temp->next;
  }
  newnode=getnode();
  newnode->data=temp->data;
  temp1=head1;
   while(temp1->next!=head1)
   temp1=temp1->next;
  head=head1;
  temp1->next=newnode;
  newnode->next=head;
  head=newnode;
  printf("\nList reversed !!!!\n");
 }
 
}

void copy()
{
 struct node *temp,*temp1, *newnode;
 temp=head;
 head1=NULL;
 if(head==NULL)
  display();
 else
 {
  while(temp->next!=head)
  { 
   temp1=head1;
   newnode=getnode();
   newnode->data=temp->data;
   if(head1==NULL)
   {
    head1=newnode;
    newnode->next=head1;
   }
   else
   {
    while(temp1->next!=head1)
     temp1=temp1->next;
    temp1->next=newnode;
    newnode->next=head1;
   }
   temp=temp->next;
  }
  newnode=getnode();
  newnode->data=temp->data;
  temp1=head1;
  while(temp1->next!=head1)
   temp1=temp1->next;
  temp1->next=newnode;
  head=head1;
  newnode->next=head;
  
  
 }
 printf("\nCopied\n");
 
}

void split()
{
 struct node *temp;
 int pos,i,total;
 temp=head;
 head1=NULL;
 if(head==NULL)
 printf("\nList is empty\n\n");
 else
 {
  if(temp->next==head)
   printf("list cannot be splitted as only 1 element is present\n");
  else
  {
   printf("Enter splitting position\n");
   scanf("%d",&pos);
   total=return_count();
   if(pos>=total)
    printf("\nInvalid Postion !!!!!\n");
   else
   {
    for(i=1;i<pos;i++)
     temp=temp->next;
    head2=temp->next;
    temp->next=head;
    
    printf("list 1 :\t");
    temp=head;
    while(temp->next!=head)
    {
     printf("%d\t",temp->data);
     temp=temp->next;
    }
    printf("%d\t",temp->data);
    
    printf("\nlist 2 :\t");
    temp=head2;
    while(temp->next!=head)
    {
     printf("%d\t",temp->data);
     temp=temp->next;
    }
    printf("%d\t",temp->data);
   }
  }
 }
}


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

int return_count()
{
 int i = 1;
 struct node* temp;
 temp=head;
 if(head==NULL)
 return 1;
 while(temp->next!=head)
 {
  temp=temp->next;
  i++;
 }
 return i;
}

/*
OUTPUT

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
1
Enter no. of elements in the LIST
3
Enter 3 elements
10
20
30

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     10      20      30
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
2

Enter element
100

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     100     10      20      30
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     100     10      20      30
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
3

Enter element
150

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     100     10      20      30      150
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     100     10      20      30      150
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
4

Enter POSITION
5

Enter element
500

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     100     10      20      30      500     150
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
5

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
5

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     20      30      500     150
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
6

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     20      30      500
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
7

Enter POSITION
10

Invalid POSITION !!!!

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     20      30      500
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
7

Enter POSITION
3

ENTER your choice
14
List elements are :     20      30  

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...


__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...


ENTER your choice
8
Enter element to be searched
40

ELEMENT NOT FOUND !!!

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     20      30
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
8
Enter element to be searched
30

30 is at position 2.

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
9

Total Count = 2

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
10
----- LIST 1 ------
Enter no. of elements in the LIST
3
Enter 3 elements
20
30
40
----- LIST 2 ------
Enter no. of elements in the LIST
3
Enter 3 elements
1
2
3

List concatented !

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     20      30      40      1       2       3
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
11

! List Reversed !

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     3       2       1       40      30      20
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
11

! List Reversed !

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
14
List elements are :     20      30      40      1       2       3
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
12

Copied List elements are :      20      30      40      1       2       3

__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
13
Enter splitting position
3
list 1 :        20      30      40
list 2 :        1       2       3
__MENU__

1.create list
2.insert at the beginning
3.insert at the end
4.insert at any location
5.delete at the beginning
6.delete at the end
7.delete at any location
8.search for an element
9.count total elements
10.concatente
11.reverse the list
12.copy list elements
13.splitting a list
14.display
15...EXIT...

ENTER your choice
15
~~~~~~~THANK YOU~~~~~~~

--------------------------------
*/


No comments:

PropellerAds