Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. // lab2v23.cpp: определяет точку входа для консольного приложения.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6.  
  7. using namespace std;
  8.  
  9. struct List
  10. {
  11. int x;
  12. List *Next;
  13. };
  14.  
  15. /*ФУНКЦИЯ ДОБАВЛЕНИЯ ЭЛЕМЕНТА В СТЕК */
  16. List* Add(int x, List *top)
  17. {
  18. List *temp = new List;
  19.  
  20. temp->x = x;
  21. temp->Next = top;
  22. top = temp;
  23.  
  24. return(top);
  25. }
  26.  
  27. /*ФУНКЦИЯ СОРТИРОВКИ СТЕКА*/
  28. List* Sort(int s, List *top)
  29. {
  30. List *temp1, *temp2, *temp3;
  31.  
  32. for (int i = 1; i < s; i++) {
  33. temp1 = top;
  34. temp2 = top->Next;
  35.  
  36. if (temp1->x > temp2->x) {
  37. top = temp2;
  38. temp1->Next = temp2->Next;
  39. temp2->Next = temp1;
  40. }
  41.  
  42. temp1 = top;
  43. temp2 = top->Next;
  44. temp3 = temp2->Next;
  45.  
  46. for (int j = 1; j < s - i; j++) {
  47.  
  48. if (temp2->x > temp2->Next->x) {
  49. temp1->Next = temp3;
  50. temp2->Next = temp3->Next;
  51. temp3->Next = temp2;
  52. }
  53.  
  54. cout << j << temp1->x << temp2->x << temp3->x << endl;
  55.  
  56. temp1 = temp2;
  57. temp2 = temp3;
  58. temp3 = temp3->Next;
  59. }
  60. }
  61.  
  62. return(top);
  63. }
  64.  
  65. /*ФУНКЦИЯ ОТОБРАЖЕНИЯ СТЕКА*/
  66. void Show(List *top)
  67. {
  68.  
  69. while (top->Next != NULL){
  70.  
  71. cout << top->x << " ";
  72. top = top->Next;
  73. }
  74. }
  75.  
  76.  
  77. int main()
  78. {
  79. List *top = new List;
  80. top->Next = NULL;
  81.  
  82. int x, s;
  83.  
  84. cout << "Enter size" << " ";
  85. cin >> s;
  86.  
  87. for (int i = 0; i < s; i++) {
  88.  
  89. cout << "Enter value" << " ";
  90. cin >> x;
  91.  
  92. top = Add(x, top);
  93. }
  94.  
  95. Show(top);
  96.  
  97. top = Sort(s, top);
  98.  
  99. Show(top);
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement