Saturday, 17 September 2016

Singly Linked List: Concatenate two lists

Singly Linked List: Concatenate two lists


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

struct node
{
                int data;
                struct node *link;
};
typedef struct node * NODE;

NODE getnode()
{
                NODE x;
                x = (NODE)malloc(sizeof(struct node));
                if(x==NULL)
                {
                                printf("\nInsufficient memory");
                                exit(0);
                }
                x->link = NULL;
                return x;
}

NODE insert(NODE first)
{
                int val;
                NODE temp, cur;
                temp = getnode();

                printf("\nEnter the value: ");
                scanf("%d", &val);
                temp->data = val;

                if(first==NULL)
                                return temp;

                cur = first;
                while(cur->link!=NULL)
                {
                                cur = cur->link;
                }
                cur->link = temp;
                return first;
}

NODE concat(NODE first, NODE second)
{
                NODE cur;
                if(first==NULL)
                                return second;
                if(second==NULL)
                                return first;
                cur =first;
                while(cur->link!=NULL)
                {
                                cur = cur->link;
                }
                cur->link = second;
                return first;
}

void display(NODE first)
{
                NODE cur;
                cur = first;
                printf("\nContents are:\n");
                if(cur == NULL)
                                printf("\nList is empty. Nothing to display.");
                else
                {
                                while(cur!=NULL)
                                {
                                                printf("| %d | %d | => ", cur->data, cur->link);
                                                cur = cur->link;
                                }
                }
}

void main()
{
                int ch, i , n1,n2;
                NODE first = NULL;
                NODE second =NULL;

                printf("\nEnter number of nodes for list1: ");
                scanf("%d", &n1);
                for(i=0;i<n1;i++)
                                first = insert(first);

                printf("\nEnter number of nodes for list2: ");
                scanf("%d", &n2);
                for(i=0;i<n2;i++)
                                second = insert(second);

                first = concat(first, second);
                display(first);
}


Output:

Enter number of nodes for list1: 4
Enter the value: 11
Enter the value: 12
Enter the value: 13
Enter the value: 14
Contents are:
| 11 | 4986632 | => | 12 | 4994040 | => | 13 | 4994056 | => | 14 | 0 |

Enter number of nodes for list2: 3
Enter the value: 15
Enter the value: 16
Enter the value: 17
Contents are:
| 15 | 4994088 | => | 16 | 4994104 | => | 17 | 0 |

After concatenation:
Contents are:
| 11 | 4986632 | => | 12 | 4994040 | => | 13 | 4994056 | => | 14 | 4994072 | => | 15 | 4994088 | =>  | 16 | 4994104 | => | 17 | 0 |

No comments:

Post a Comment