This blog is under construction

Saturday, 25 May 2013

C Program To Concatenate Two Linked Lists

See Also:


Example Program To Join/Concatenate Two Linked Lists:



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

  struct node {
        int data;
        struct node *nxtPtr;
  };

  struct node *head1, *head2, *head3;
  /*
   * creates node and fill the given data
   */
  struct node * nodeCreation(int data) {
        struct node *ptr = (struct node *) malloc(sizeof (struct node));
        ptr->data = data;
        ptr->nxtPtr = NULL;
        return ptr;
  }

  /* insert node in ascending order */
  void insertNode(struct node ** myNode, int data) {
        struct node *lPtr, *mPtr, *nPtr = *myNode;
        lPtr = nodeCreation(data);
        /* insert at the front of the list */
        if (*myNode == NULL || (*myNode)->data > data) {
                *myNode = lPtr;
                (*myNode)->nxtPtr = nPtr;
                return;
        }

        /* insert at the end or middle of the list */
        while (nPtr) {
                mPtr = nPtr;
                nPtr = nPtr->nxtPtr;
                if (!nPtr) {
                        mPtr->nxtPtr = lPtr;
                        break;
                } else if ((data > mPtr->data) && (data < nPtr->data)) {
                        lPtr->nxtPtr = nPtr;
                        mPtr->nxtPtr = lPtr;
                        break;
                }
        }
        return;
  }

  /* concatenate list 1 and list 2 */
  void concatList(struct node **list1, struct node **list2) {
        struct node *temp;
        if (*list1 == NULL) {
                *list1 = *list2;
        } else if (*list2) {
                temp = *list1;
                while (temp->nxtPtr) {
                        temp = temp->nxtPtr;
                }
                temp->nxtPtr = *list2;
        }
        return;
  }

  /* delete the given list */
  struct node * removeList(struct node *ptr) {
        struct node *temp;
        while (ptr){
                temp = ptr->nxtPtr;
                free(ptr);
                ptr = temp;
        }
        return NULL;
  }

  /* traverse the given list and print the contents in each node */
  int listTraversal(struct node *ptr) {
        int i = 0;
        while (ptr) {
                printf("%d ", ptr->data);
                ptr = ptr->nxtPtr;
                i++;
        }
        return (i);
  }

  int main (int argc, char *argv[]) {
        int data, i, n;
        FILE *fp1, *fp2;
        fp1 = fopen(argv[1], "r");
        fp2 = fopen(argv[2], "r");
        if (!fp1 || !fp2) {
                printf("Unable to open file\n");
                fcloseall();
                exit(0);
        }

        /* scan data from input file to insert into list1*/
        while (fscanf(fp1, "%d", &data) != EOF) {
                insertNode(&head1, data);
        }

        /* scan data from input file to insert into list2 */
        while (fscanf(fp2, "%d", &data) != EOF) {
                insertNode(&head2, data);
        }

        printf("\nData in First Linked List:\n");
        n = listTraversal(head1);
        printf("\nNo of elements in linked list: %d\n", n);
        printf("\n\nData in Second Linked List:\n");
        n = listTraversal(head2);
        printf("\nNo of elements in linked list: %d\n\n", n);
        concatList(&head1, &head2);
        printf("\nData in concatenated List:\n");
        n = listTraversal(head1);
        printf("\nNo of elements in concatenated list: %d\n\n", n);
        head1 = removeList(head1);
        return 0;
  }



  Output: (Concatenation of two Linked Lists - Example Program in C)
  jp@jp-VirtualBox:~/$ cat input1.txt 
  1 3 5 7 9 11 13 15

  jp@jp-VirtualBox:~/$ cat input2.txt 

  2 4 6 8 10 12 14 16
  
  jp@jp-VirtualBox:~/$ ./a.out input1.txt input2.txt 
  Data in First Linked List:
  1 3 5 7 9 11 13 15 
  No of elements in linked list: 8

  Data in Second Linked List:
  2 4 6 8 10 12 14 16 
  No of elements in linked list: 8

  Data in concatenated List:
  1 3 5 7 9 11 13 15 2 4 6 8 10 12 14 16 
  No of elements in concatenated list: 16



No comments:

Post a Comment