Advertisement
Guest User

Untitled

a guest
May 5th, 2019
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. // saa_zadacha_stek.cpp : This file contains the 'main' function. Program execution begins and ends there.
  2. // Да се създаде динамичен стек съдържащ положителни числа и извеждаме неговоро съдържание
  3.  
  4. #include "pch.h"
  5. #include <iostream>
  6. #include <conio.h>
  7. using namespace std;
  8.  
  9. struct S
  10. {
  11. int key;
  12. S *next;
  13. }*start = NULL;
  14.  
  15. void push(int n)
  16. {
  17. S *p;
  18. p = start;
  19. start = new S;
  20. start->key = n;
  21. start->next = p;
  22. }
  23.  
  24. int pop(int &n)
  25. {
  26. S *p;
  27. if (start)
  28. {
  29. p = start;
  30. n = start->key;
  31. start = start->next;
  32. delete p;
  33. return 1;
  34. }
  35. else
  36. return 0;
  37. }
  38. int main()
  39. {
  40. int num, br, A, n;
  41. do {
  42. cout << "Vuvedi stoinost na elementite v stack-a: ";
  43. cin >> num;
  44. if ( num > 0)
  45. push(num);
  46. } while (num);
  47.  
  48. /*for (int i = 0; i < br; i++)
  49. {
  50. cout << "Vuvedi stoinost: ";
  51. cin >> num;
  52. push(num);
  53. }*/
  54.  
  55. cout << "Vuvedi stoinost za N: " << endl;
  56. cin >> n;
  57.  
  58. for (int i = 0; i < n - 1; i++)
  59. pop(num);
  60.  
  61.  
  62.  
  63. cout << "Vuvedi stoinost za A: ";
  64. cin >> A;
  65.  
  66.  
  67. start->key = A;
  68. cout << "Rezult: " << endl;
  69. while (start != NULL)
  70. {
  71. cout << start->key<<endl;
  72. pop(start->key);
  73. }
  74.  
  75.  
  76. system("pause");
  77. return 0;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement