Advertisement
Andites
Nov 22nd, 2022
45
0
Never
This is comment for paste тимп 2,5
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Рабочий, но стремный
  2.  
  3.  
  4. #include <cmath>
  5. #include <vector>
  6. #include <iostream>
  7. using namespace std;
  8.  
  9. struct Node
  10. {
  11. int data;
  12. Node* next;
  13. };
  14.  
  15. class List {
  16. private:
  17. Node* head;
  18. public:
  19. List() {
  20. head = NULL;
  21. }
  22.  
  23. void addNode(int d) {
  24. Node* nd = new Node;
  25. nd->data = d;
  26. nd->next = NULL;
  27. if (head == NULL) {
  28. head = nd;
  29. }
  30. else {
  31. Node* current = head;
  32. while (current->next != NULL) {
  33. current = current->next;
  34. }
  35. current->next = nd;
  36. }
  37. }
  38.  
  39. void PrintList() {
  40. Node* current = head;
  41. while (current != NULL) {
  42. cout << current->data << " ";
  43. current = current->next;
  44. }
  45. }
  46.  
  47. void sort(List& list, vector<int>& vec,int &pos,vector<int>& vec2) {
  48. Node* current = head;
  49. while (current != NULL) {
  50. int cur = current->data;
  51. if (pos % 2 != 0) {
  52. vec.push_back(cur);
  53. } else vec2.push_back(cur);
  54. pos++;
  55. current = current->next;
  56. }
  57. for(int i=0;i<vec.size();i++){
  58. vec2.push_back(vec[i]);
  59. }
  60. for (int i = 0; i < vec.size(); i++) {
  61. list.addNode(vec[i]);
  62. }
  63.  
  64. }
  65.  
  66. void del(int n) {
  67. Node* temp = head, * helping = head;
  68.  
  69. for (int i = 0; i < n; i++) {
  70. helping = temp;
  71. temp = temp->next;
  72. }
  73.  
  74. if (temp == head)
  75. {
  76. head = temp->next;
  77. }
  78. else {
  79. helping->next = temp->next;
  80. }
  81. free(temp);
  82. }
  83. };
  84.  
  85. int main()
  86. {
  87. vector<int>vec, vec2;
  88. setlocale(LC_ALL, "Russian");
  89. List list,newlist;
  90. srand(time(NULL));
  91. int n;
  92. cout << "Размер:"; cin >> n;
  93. int* mas = new int[n];
  94. for (int i = 0; i < n; i++) {
  95. int x = rand() % 10;
  96. mas[i] = x;
  97. list.addNode(x);
  98. }
  99. int pos=0;
  100. cout << "old list:\n"; list.PrintList();
  101. list.sort(list, vec,pos,vec2);
  102. list.delete();
  103. for (int i = 0; i < n; i++) {
  104. list.addNode(vec2[i]);
  105. }
  106. cout << "\nnew list:\n";
  107. list.PrintList();
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement