Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Stack {
  6. int* S;
  7. int top;
  8. };
  9.  
  10.  
  11. void push(Stack s, int &top, int wartosc) {
  12. top++;
  13. s.S[top] = wartosc;
  14. }
  15.  
  16. int pop(Stack s,int &top) {
  17. int el;
  18. if (top > -1) {
  19. el = s.S[top];
  20. top--;
  21. } else {
  22. cout << "Blad" << endl;
  23. }
  24. return el;
  25. }
  26.  
  27. void usun(Stack &s, int n) {
  28. int* t;
  29. t = new int[n];
  30. Stack tmp;
  31. tmp.S = t;
  32. int wartosc;
  33. tmp.top = -1;
  34. for (int i = 0; i < n-1; ++i) {
  35.  
  36. push(tmp,tmp.top,pop(s,s.top));
  37.  
  38. }
  39.  
  40. if (s.top < 0) {
  41. cout << "Stos nie ma tylu elementow!" << endl;
  42.  
  43. } else {
  44. //
  45. cout << "Pomocniczy:"<<endl;
  46. for (int i = 0; i < n-1; i++) {
  47. cout << tmp.S[i] << " ";
  48. }
  49. cout << endl;
  50. // KONIEC
  51. pop(s,s.top);
  52.  
  53. for (int i = 0; i <= n; i++) {
  54.  
  55. push(s,s.top,pop(tmp,tmp.top));
  56. }
  57. }
  58.  
  59. }
  60.  
  61.  
  62. int main()
  63. {
  64. Stack sztos;
  65. int m;
  66. cout << "Podaj rozmiar stosu: ";
  67. cin >> m;
  68. int* tab;
  69. tab = new int[m];
  70. sztos.S = tab;
  71. sztos.top = -1;
  72.  
  73. push(sztos,sztos.top,5);
  74. push(sztos,sztos.top,4);
  75. push(sztos,sztos.top,2);
  76. push(sztos,sztos.top,6);
  77. push(sztos,sztos.top,7);
  78. push(sztos,sztos.top,3);
  79. push(sztos,sztos.top,0);
  80. push(sztos,sztos.top,11);
  81. push(sztos,sztos.top,8);
  82. //TESTY;
  83. cout << "SZtosik przed usunieciem: " << endl;
  84. for(int i = 0; i < m; i++) {
  85. cout << sztos.S[i] << " ";
  86. }
  87. cout << endl;
  88. // HEJ
  89.  
  90. int x;
  91. cout << "Ktory element usunac?";
  92. cin >> x;
  93. usun(sztos,x);
  94.  
  95. cout << "SZtosik po usunieciu: " << endl;
  96. for(int i = 0; i < m-1; i++) {
  97. cout << sztos.S[i] << " ";
  98. }
  99. cout << endl;
  100.  
  101.  
  102.  
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement