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