Advertisement
codeido

Untitled

Jun 19th, 2020
1,821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.79 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. typedef struct Node
  6. {
  7.     int number;
  8.     struct Node *next;
  9. } Node;
  10.  
  11. Node *makeNode(int number)
  12. {
  13.     Node *temp = new Node;
  14.     temp -> number = number ;
  15.     temp -> next = NULL;
  16.  
  17.     return temp;
  18. }
  19.  
  20. void addLast(Node *head, Node *node)
  21. {
  22.     Node *current = head;
  23.  
  24.     while (current->next != NULL)
  25.     {
  26.         // Run until the last node in the list
  27.         current = current->next;
  28.     }
  29.  
  30.     current->next = node;
  31. }
  32.  
  33. void printList(Node *head)
  34. {
  35.     Node *current = head;
  36.     while (current != NULL)
  37.     {
  38.         cout << current->number << "->";
  39.         current = current-> next;
  40.     }
  41.     cout << "NULL" << endl;
  42. }
  43.  
  44. Node *function(Node *head)
  45. {
  46.     Node *head2 = NULL, *current2 = NULL;
  47.     Node *current = head;
  48.  
  49.     while (current != NULL)
  50.     {
  51.         if (head2 == NULL)
  52.         {
  53.             head2 = current->next;
  54.             current2 = head2;
  55.         }
  56.         else
  57.         {
  58.             current2->next = current->next;
  59.             current2 = current2->next;
  60.         }
  61.  
  62.         if (current->next != NULL)
  63.         {
  64.             current->next = current->next->next;
  65.         }
  66.  
  67.         current = current->next;
  68.     }
  69.  
  70.     return head2;
  71. }
  72.  
  73. int main()
  74. {
  75.     Node *list1 = makeNode(2), *list2;
  76.  
  77.     addLast(list1, makeNode(4));
  78.     addLast(list1, makeNode(8));
  79.     addLast(list1, makeNode(16));
  80.     addLast(list1, makeNode(24));
  81.     addLast(list1, makeNode(46));
  82.     addLast(list1, makeNode(50));
  83.     addLast(list1, makeNode(7));
  84.  
  85.     cout << "List 1:" << endl;
  86.     printList(list1);
  87.     cout << endl;
  88.  
  89.     list2 = function(list1);
  90.  
  91.     cout << "List 1 modified:" << endl;
  92.     printList(list1);
  93.     cout << endl;
  94.  
  95.     cout << "List 2:" << endl;
  96.     printList(list2);
  97.     return 0;
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement