Advertisement
vencinachev

Homework4

May 22nd, 2021
1,128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.28 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Node
  6. {
  7.     int data;
  8.     Node* next;
  9. };
  10.  
  11. void Push(Node*& head, int element);
  12. int sizell(Node* head);
  13. void clear(Node*& head);
  14. void printElements(Node* head);
  15. void SortedInsert(Node*& head, int element);
  16. void InsertSort(Node*& head);
  17.  
  18.  
  19. int main()
  20. {
  21.     Node* llist = nullptr;
  22.     int n, num;
  23.     cout << "Enter number of elements: ";
  24.     cin >> n;
  25.     for (int i = 0; i < n; i++)
  26.     {
  27.         cin >> num;
  28.         Push(llist, num);
  29.     }
  30.     cout << "Before sort: ";
  31.     printElements(llist);
  32.     InsertSort(llist);
  33.     cout << "After sort: ";
  34.     printElements(llist);
  35.     return 0;
  36. }
  37.  
  38. void InsertSort(Node*& head)
  39. {
  40.     Node* sorted = nullptr;
  41.     Node* current = head;
  42.     while (current != nullptr)
  43.     {
  44.         SortedInsert(sorted, current->data);
  45.         current = current->next;
  46.     }
  47.     clear(head);
  48.     head = sorted;
  49. }
  50.  
  51. void SortedInsert(Node*& head, int element)
  52. {
  53.     if (head == nullptr || head->data > element)
  54.     {
  55.         Node* n = new Node();
  56.         n->data = element;
  57.         n->next = head;
  58.         head = n;
  59.     }
  60.     else
  61.     {
  62.         Node* current = head;
  63.         while (current->next != nullptr && current->next->data < element)
  64.         {
  65.             current = current->next;
  66.         }
  67.         Node* n = new Node();
  68.         n->data = element;
  69.         n->next = current->next;
  70.         current->next = n;
  71.     }
  72. }
  73.  
  74.  
  75. void removeFirst(Node*& head)
  76. {
  77.     Node* current = head;
  78.     head = head->next;
  79.     delete current;
  80. }
  81.  
  82. void Push(Node*& head, int element)
  83. {
  84.     Node* n = new Node();
  85.     n->data = element;
  86.     n->next = head;
  87.     head = n;
  88. }
  89.  
  90. int sizell(Node* head)
  91. {
  92.     int count = 0;
  93.     Node* current = head;
  94.     while (current != nullptr)
  95.     {
  96.         count++;
  97.         current = current->next;
  98.     }
  99.     return count;
  100. }
  101.  
  102. void printElements(Node* head)
  103. {
  104.     Node* current = head;
  105.     while (current != nullptr)
  106.     {
  107.         cout << current->data << " ";
  108.         current = current->next;
  109.     }
  110.     cout << endl;
  111. }
  112.  
  113. void clear(Node*& head)
  114. {
  115.     Node* current = head;
  116.     Node* prev = head;
  117.     while (current != nullptr)
  118.     {
  119.         prev = current;
  120.         current = current->next;
  121.         delete prev;
  122.     }
  123.     head = nullptr;
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement