vencinachev

PismenZad2

Jun 16th, 2021 (edited)
556
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <stack>
  3.  
  4. using namespace std;
  5.  
  6. struct Node
  7. {
  8.     int data;
  9.     Node* next;
  10. };
  11.  
  12.  
  13. void RemoveSingles(Node*& head);
  14. void Push(Node*& head, int data);
  15. void Append(Node*& head, int data);
  16. void printList(Node* head);
  17. void DeleteList(Node*& head);
  18.  
  19. int main()
  20. {
  21.     Node* llist = nullptr;
  22.     int n;
  23.     cout << "Enter number of elements: ";
  24.     cin >> n;
  25.     cout << "Enter list elements: ";
  26.     for (int i = 0; i < n; i++)
  27.     {
  28.         int num;
  29.         cin >> num;
  30.         Append(llist, num);
  31.     }
  32.     RemoveSingles(llist);
  33.     cout << endl;
  34.     printList(llist);
  35.     return 0;
  36. }
  37.  
  38. void RemoveSingles(Node*& head)
  39. {
  40.     if (head == nullptr)
  41.     {
  42.         return;
  43.     }
  44.     stack<int> elements;
  45.     Node* current = head;
  46.     while (current != nullptr)
  47.     {
  48.         int num = current->data;
  49.         elements.push(num);
  50.         current = current->next;
  51.         cout << "Push: " << num << endl;
  52.         bool flag = false;
  53.         while (current != nullptr && current->data == num)
  54.         {
  55.             flag = true;
  56.             elements.push(current->data);
  57.             cout << "Push: " << current->data << endl;
  58.             current=current->next;
  59.         }
  60.         if (!flag)
  61.         {
  62.             cout << "Pop: " << elements.top() << endl;
  63.             elements.pop(); // remove single element
  64.         }
  65.     }
  66.     DeleteList(head);
  67.     while (!elements.empty())
  68.     {
  69.         Push(head, elements.top());
  70.         elements.pop();
  71.     }
  72. }
  73.  
  74. void Push(Node*& head, int data)
  75. {
  76.     Node* n = new Node();
  77.     n->data = data;
  78.     n->next = head;
  79.     head = n;
  80. }
  81.  
  82. void Append(Node*& head, int data)
  83. {
  84.     Node* n = new Node();
  85.     n->data = data;
  86.     if (head == nullptr)
  87.     {
  88.         n->next = head;
  89.         head = n;
  90.     }
  91.     else
  92.     {
  93.         Node* current = head;
  94.         while(current->next != nullptr)
  95.         {
  96.              current = current->next;
  97.         }
  98.         n->next = current->next;
  99.         current->next = n;
  100.     }
  101. }
  102.  
  103. void printList(Node* head)
  104. {
  105.     if (head == nullptr)
  106.     {
  107.         cout << "Empty list" << endl;
  108.     }
  109.     Node* current = head;
  110.     while (current != nullptr)
  111.     {
  112.         cout << current->data << " ";
  113.         current = current->next;
  114.     }
  115.     cout << endl;
  116. }
  117.  
  118. void DeleteList(Node*& head)
  119. {
  120.     Node* current = head;
  121.     Node* todelete;
  122.     while (current != nullptr)
  123.     {
  124.         todelete = current;
  125.         current = current->next;
  126.         delete todelete;
  127.     }
  128.     head = nullptr;
  129. }
  130.  
RAW Paste Data