Niloy007

Linked List Concatination

Feb 19th, 2020
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.06 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. struct node {
  5.     int data;
  6.     struct node *next, *back;
  7. };
  8.  
  9. typedef struct node Node;
  10.  
  11. void display(Node *start) {
  12.     Node *temp;
  13.     temp = start;
  14.     while(temp != NULL) {
  15.         printf("%d ", temp->data);
  16.         temp = temp->next;
  17.     }
  18.     printf("\n");
  19. }
  20.  
  21. Node *concatinate(Node *start, Node *head) {
  22.     Node *temp = start;
  23.     while(temp->next != NULL) {
  24.         temp = temp->next;
  25.     }
  26.     temp->next = head;
  27.     return start;
  28. }
  29.  
  30.  
  31. int main() {
  32.     Node *start, *temp, *last, *head;
  33.     start = NULL;
  34.     head = NULL;
  35.     int ans, searchData;
  36.  
  37.     printf("Do you want to add item for Start node?\n");
  38.     scanf("%d", &ans);
  39.  
  40.     while(ans == 1) {
  41.         if(start == NULL) {
  42.             start = new Node();
  43.             printf("Enter data:\n");
  44.             scanf("%d", &start->data);
  45.             start->next = NULL;
  46.             start->back = NULL;
  47.             last = start;
  48.         } else { head = NULL;
  49.             temp = new Node();
  50.             printf("Enter data:\n");
  51.             scanf("%d", &temp->data);
  52.             temp->next = NULL;
  53.             last->next = temp;
  54.             temp->back = last;
  55.             last = temp;
  56.         }
  57.  
  58.         printf("Do you want to add item for Start node?\n");
  59.         scanf("%d", &ans);
  60.     }
  61.  
  62.     printf("Do you want to add item for Head node?\n");
  63.     scanf("%d", &ans);
  64.  
  65.  
  66.     while(ans == 1) {
  67.         if(head == NULL) {
  68.             head = new Node();
  69.             printf("Enter data:\n");
  70.             scanf("%d", &head->data);
  71.             head->next = NULL;
  72.             head->back = NULL;
  73.             last = head;
  74.         } else {
  75.             temp = new Node();
  76.             printf("Enter data:\n");
  77.             scanf("%d", &temp->data);
  78.             temp->next = NULL;
  79.             last->next = temp;
  80.             temp->back = last;
  81.             last = temp;
  82.         }
  83.  
  84.         printf("Do you want to add item for Head node?\n");
  85.         scanf("%d", &ans);
  86.     }
  87.  
  88.     start = concatinate(start, head);
  89.     display(start);
  90. }
Add Comment
Please, Sign In to add comment