Operations on Linked List

#include<stdio.h>
#include<malloc.h>

typedef struct node
{
    int data;
    struct node *next;
}node;

void display(node *head)
{
    while(head!=NULL)
    {
        printf("%d ",head->data);
        head=head->next;
    }
}

node* create(node *head)
{
    node *p,*q;
    int x,n;
    p=head;
    printf("\nNumber of elements ");
    scanf("%d",&n);
    if(n==0)
    {
        head=NULL;
        return head;
    }
    else
    {
        printf("Enter Data ");
        for(x=0;x<n;x++)
        {
            scanf("%d",&p->data);
            p->next=NULL;
            if(x<n-1)
            {
                q=(node*)malloc(sizeof(node));
                p->next=q;
                p=q;
            }
        }
        return head;
    }
}

void concat(node *h1,node *h2)
{
    node *p;
    p=h1;
    if(h1==NULL)
        p=h2;
    else
    {
        while(h1->next!=NULL)
        {
            h1=h1->next;
        }
        h1->next=h2;
    }
    printf("Concatinated linked list is ");
    display(p);
    printf("\n");
}

void cou(node *head)
{
    int i=0;
    while(head!=NULL)
    {
        head=head->next;
        i++;
    }
    printf("Number of nodes is %d\n",i);
}

void copy(node *h1,node *h2)
{
    int i=0,j=0;
    node *p,*q;
    p=h1;q=h2;
    while(p!=NULL)
    {
        p=p->next;
        i++;
    }
    while(q!=NULL)
    {
        q=q->next;
        j++;
    }
    p=h1;
    q=h2;
    if(i!=j)
    {
        printf("Cannot be copied !!\n");
    }
    else
    {
        while(q!=NULL)
        {
            p->data=q->data;
            p=p->next;
            q=q->next;
        }
        printf("Copied linked list is ");
        display(h1);
        printf("\n");
    }
}

void search(node *h,int x)
{
    int pos=-1,i=0;
    for(i=0;h->next!=NULL;i++)
    {
        if(h->data==x)
        {
            pos=i;
            break;
        }
        h=h->next;
    }
    if(pos==-1)
        printf("Element not found !!\n");
    else
        printf("Element is found at %d\n",pos+1);
}

void split(node *head)
{
    int x,i=1;
    node *h1,*h2;
    printf("Enter position to split ");
    scanf("%d",&x);
    h1=head;
    while(i!=x)
    {
        head=head->next;
        i++;
    }
    h2=head->next;
    head->next=NULL;
    printf("1st linked list : ");
    display(h1);
    printf("\n2nd linked list : ");
    display(h2);
    printf("\n");
}

void rev(node *head)
{
    node *p,*q,*r;
    p=NULL;
    q=head;
    r=q->next;
    while(q!=NULL)
    {
        q->next=p;
        p=q;
        q=r;
        if(r!=NULL)
            r=r->next;
    }
    printf("Reversed linked list is ");
    display(p);
    printf("\n");
}

int main()
{
 int x,count;
 node *h1,*h2;
 h1=(node*)malloc(sizeof(node));
 h2=(node*)malloc(sizeof(node));
 do
 {
  printf("\nEnter\n1.Concatinate\n2.Count\n3.Copy\n4.Search\n5.Split\n6.Reverse\n7.Exit\n");
  scanf("%d",&x);
  switch(x)
  {
   case 1:
    printf("Enter elements of 1st Linked List ");
    h1=create(h1);
    printf("Enter elements of 2nd Linked List ");
    h2=create(h2);
    concat(h1,h2);
    break;

   case 2:
    printf("Enter elements of Linked List ");
    h1=create(h1);
    cou(h1);
    break;

   case 3:
    printf("Enter elements of 1st Linked List ");
    h1=create(h1);
    printf("Enter elements of 2nd Linked List ");
    h2=create(h2);
    copy(h1,h2);
    break;

   case 4:
    printf("Enter elements of Linked List ");
    h1=create(h1);
    printf("Enter element to search ");
    scanf("%d",&count);
    search(h1,count);
    break;

   case 5:
    printf("Enter elements of Linked List ");
    h1=create(h1);
    split(h1);
    break;

   case 6:
    printf("Enter elements of Linked List ");
    h1=create(h1);
    rev(h1);
    break;
  }
 }while(x!=7);
 return 0;
}

/*Output

Enter
1.Concatenate
2.Count
3.Copy
4.Search
5.Split
6.Reverse
7.Exit
1
Enter elements of 1st Linked List
Number of elements 3
Enter Data 12 32 45
Enter elements of 2nd Linked List
Number of elements 3
Enter Data 56 87 98
Concatenated linked list is 12 32 45 56 87 98

Enter
1.Concatenate
2.Count
3.Copy
4.Search
5.Split
6.Reverse
7.Exit
2
Enter elements of Linked List
Number of elements 5
Enter Data 21 32 65 54 87
Number of nodes is 5

Enter
1.Concatenate
2.Count
3.Copy
4.Search
5.Split
6.Reverse
7.Exit
3
Enter elements of 1st Linked List
Number of elements 3
Enter Data 21 32 54
Enter elements of 2nd Linked List
Number of elements 3
Enter Data 98 55 66
Copied linked list is 98 55 66

Enter
1.Concatenate
2.Count
3.Copy
4.Search
5.Split
6.Reverse
7.Exit
4
Enter elements of Linked List
Number of elements 5
Enter Data 15 59 26 77 33
Enter element to search 77
Element is found at 4

Enter
1.Concatenate
2.Count
3.Copy
4.Search
5.Split
6.Reverse
7.Exit
5
Enter elements of Linked List
Number of elements 5
Enter Data 26 15 59 47 36
Enter position to split 2
1st linked list : 26 15
2nd linked list : 59 47 36

Enter
1.Concatenate
2.Count
3.Copy
4.Search
5.Split
6.Reverse
7.Exit
6
Enter elements of Linked List
Number of elements 5
Enter Data 1 2 3 4 5
Reversed linked list is 5 4 3 2 1

Enter
1.Concatenate
2.Count
3.Copy
4.Search
5.Split
6.Reverse
7.Exit
7
*/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s