Advertisement
Andites

тимп 2,5

Nov 22nd, 2022
98
0
Never
2
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  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) {
  45. int pos = 0;
  46.  
  47. Node* current = head;
  48. while (current != NULL) {
  49. int cur = current->data;
  50. if (pos % 2 != 0) {
  51. vec.push_back(cur);
  52. }
  53. pos++;
  54. current = current->next;
  55. }
  56. current = head;
  57. pos = 0;
  58. while (current != NULL) {
  59. int cur = current->data;
  60. if (pos % 2 != 0) {
  61. list.del(pos);
  62. }
  63. pos++;
  64. current = current->next;
  65. }
  66. for (int i = 0; i < vec.size(); i++) {
  67. list.addNode(vec[i]);
  68. }
  69. }
  70.  
  71. void del(int n) {
  72. Node* temp = head, * helping = head;
  73.  
  74. for (int i = 0; i < n; i++) {
  75. helping = temp;
  76. temp = temp->next;
  77. }
  78.  
  79. if (temp == head)
  80. {
  81. head = temp->next;
  82. }
  83. else {
  84. helping->next = temp->next;
  85. }
  86. free(temp);
  87. }
  88. };
  89.  
  90. int main()
  91. {
  92. vector<int>vec;
  93. setlocale(LC_ALL, "Russian");
  94. List list;
  95. srand(time(NULL));
  96. int n;
  97. cout << "Размер:"; cin >> n;
  98. int* mas = new int[n];
  99. for (int i = 0; i < n; i++) {
  100. int x = rand() % 10;
  101. mas[i] = x;
  102. list.addNode(x);
  103. }
  104. cout << "old list:\n"; list.PrintList();
  105. list.sort(list, vec);
  106. cout << "\nnew list:\n";
  107. list.PrintList();
  108.  
  109. }
  110.  
Advertisement
Comments
  • Andites
    2 years
    # text 1.84 KB | 0 0
    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.  
  • Andites
    2 years
    # text 1.78 KB | 0 0
    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. }
Add Comment
Please, Sign In to add comment
Advertisement