Search This Blog

Tuesday 27 October 2015

different operations on Linked List- Copy, Concatenate, Split, reverse, count the no. of nodes.

OPERATION ON LINKED LIST(COUNT,COPY,CONCATENATE)

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
 int info;
 struct node * next;
};
struct node * start=NULL;
struct node * first=NULL;
void insertbegin(int item)
{
 struct node *p;
 p=(struct node *)malloc(sizeof(struct node));
 p->info=item;
 if(start==NULL)
 {
  p->next=NULL;
  start=p;
 }
 else
 {
  p->next=start;
  start=p;
 }}
void count()
{
int count=0;
struct node *p;
p=start;
while(p!=NULL)
{
 count=count+1;
 p=p->next;
}
printf("\nno of nodes is %d",count);
}
void copy()
{
 struct node *p,*q,*temp;
 p=start;
 while(p!=NULL)
 {  if(first==NULL)
  {
   temp=(struct node*)malloc(sizeof(struct node));
   temp->info=p->info;
   temp->next=NULL;
   first=temp;
   q=temp;
  }
  else
  { q->next=(struct node*)malloc(sizeof(struct node));
  q=q->next;
  q->info=p->info;
  q->next=NULL;
 }
 p=p->next;
}}
void concatenate()
{
 struct node *p;
 p=start;
 while(p->next!=NULL)
 {
  p=p->next;
 }
 p->next=first;
}
void display1()
{
 struct node *p;
 p=start;
 if(p==NULL)
 printf("\nempty");
 else
 {
  while(p!=NULL)
  {
   printf("\n%d",p->info);
   p=p->next;
  } }}
void display2()
{ struct node *p;
 p=first;
 if(p==NULL)
 printf("\nempty");
 else
 { while(p!=NULL)
  { printf("\n%d",p->info);
   p=p->next;
  }}}
void main()
{int ch,item;
 clrscr();
 printf("\nMENU OPTIONS\n1.insertion at begin\n2.count\n3.copy\n4.concatenate\n5.display first linked list\n6.display second linked list\n7.exit");
 do
 {
  printf("\nenter the choice");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:printf("\nenter the element");
                  scanf("%d",&item);
                  insertbegin(item);
                  break;
   case 2:count();
                  break;
   case 3:copy();
                  printf("\nfirst linked list is copied into second linked list");
                  break;
   case 4:concatenate();
                  printf("\nsecond linked list is joined at the end of first linked list");
                  break;
   case 5:display1();
                  break;
   case 6:display2();
                  break;
   case 7:exit(0);
   default:printf("\ninvalid choice...");
  } }while(ch!=7);
 getch();
}

OPERATION OF LINKED LIST(SPLIT)

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
 int info;
 struct node * next;
};
struct node * start=NULL;
struct node *first=NULL;
void insertbegin(int item)
{
 struct node *p;
 p=(struct node *)malloc(sizeof(struct node));
 p->info=item;
 if(start==NULL)
 {
  p->next=NULL;
  start=p;
 }
 else
 {
  p->next=start;
  start=p;
 }
}
void split(int key)
{
 struct node *p,*q,*s;
 p=start;
 while(p->info!=key)
 {
  q=p;
  p=p->next;
 }
 q->next=NULL;
 first=p;
}
void display1()
{
 struct node *p;
 p=start;
 if(p==NULL)
 printf("\nempty");
 else
 {
  while(p!=NULL)
  {
   printf("\n%d",p->info);
   p=p->next;
  }
 }
}
void display2()
{
 struct node *p;
 p=first;
 if(p==NULL)
 printf("\nempty");
 else
 {
  while(p!=NULL)
  {
   printf("\n%d",p->info);
   p=p->next;
  }
 }
}
void main()
{
 int ch,item;
 clrscr();
 printf("\nMENU OPTIONS\n1.insertion at begin\n2.split\n3.display first linked list\n4.display second linked list\n5.exit");
 do
 {
  printf("\nenter the choice");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:printf("\nenter the element");
                  scanf("%d",&item);
                  insertbegin(item);
                  break;
   case 2:printf("\nenter the element from where you want to split linked list");
                  scanf("%d",&item);
                  split(item);
                  break;
   case 3:display1();
                  break;
   case 4:display2();
                  break;
   case 5:exit(0);
   default:printf("\ninvalid choice...");
  }
 }while(ch!=5);
 getch();
}

                           OPERATION ON LINKED LIST(REVERSE)


#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
 int info;
 struct node *prev;
 struct node * next;
};
struct node * first=NULL;
struct node * tail=NULL;
struct node * f=NULL;
struct node * temp=NULL;
void insertbegin(int item)
{
 struct node *p;
 p=(struct node *)malloc(sizeof(struct node));
 p->info=item;
 if(first==NULL)
 {
  p->next=p->prev=NULL;
  first=tail=p;
 }
 else
 {
  p->prev=NULL;
  p->next=first;
  first->prev=p;
  first=p;
 }
}
void reverse()
{
 struct node *p,*temp,*q;
 p=tail;
 while(p!=NULL)
 {
  if(f==NULL)
  {
   temp=(struct node*)malloc(sizeof(struct node));
   temp->info=p->info;
   temp->next=NULL;
   temp->prev=NULL;
   f=temp;
   q=temp;
  }
  else
  {
   q->next=(struct node *)malloc(sizeof(struct node));
   q=q->next;
   q->info=p->info;
   q->next=NULL;
   q->prev=temp;
   temp=temp->next;
  }
 p=p->prev;
 }
}
void display2()
{
 struct node * k;
 k=f;
 if(f==NULL)
 printf("\nempty or reverse operation not done");
 else
 {
 while(k!=NULL)
 {
  printf("\n%d",k->info);
  k=k->next;
 }
 }
}
void display1()
{
 struct node *l;
 l=first;
 if(l==NULL)
 printf("\nempty");
 else
 {
  while(l!=NULL)
  {
   printf("\n%d",l->info);
   l=l->next;
  }
}
}
void main()
{
 int ch,item;
 clrscr();
 printf("\nMENU OPTIONS\n1.insertion at begin\n2.reverse\n3.display original linked list\n4.display reverse linked list\n5.exit");
 do
 {
  printf("\nenter the choice");
  scanf("%d",&ch);
  switch(ch)
  {
   case 1:printf("\nenter the element");
                  scanf("%d",&item);
                  insertbegin(item);
                  break;
   case 2:reverse();
                  printf("\nreverse of original linked list is done");
                  break;
   case 3:printf("\noriginal linked list");
                  display1();
                  break;
   case 4:printf("\nreversed linked list");
                  display2();
                  break;
   case 5:exit(0);
   default:printf("\ninvalid choice...");
  }
 }while(ch!=5);
 getch();
}

1 comment: