ToniDev

Implementare stiva

Jan 31st, 2022
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. /*Folosim macro pentru a putea modifica mai usor marimea stivei in program*/
  5. #define LIM 10
  6.  
  7. /*Verificare daca stiva este vida sau nevida*/
  8. bool isEmpty(int top) {
  9.     if (top == 0)
  10.         return true;
  11.     return false;
  12. }
  13.  
  14. /*Functia pentru inserare elemente in stiva*/
  15. void push(int stiva[], int& top, int numar) {
  16.     stiva[top] = numar;
  17.     top = top + 1;
  18. }
  19.  
  20. /*Returneaza elementul de pe pozitia top (ultimul element)*/
  21. int peekTop(int stiva[], int top) {
  22.     return stiva[top];
  23. }
  24.  
  25. /*Functia care elimina utltimul element*/
  26. void pop(int stiva[], int& top) {
  27.     int rezultat = peekTop(stiva, top);
  28.     top = top - 1;
  29. }
  30.  
  31. /*Returneaza marimea stivei*/
  32. int size(int top) {
  33.     return top;
  34. }
  35.  
  36. void afisare(int stiva[], int& top)
  37. {
  38.     for (int i = 0; i < top; i++)
  39.         cout << stiva[i] << " ";
  40.  
  41.     cout << endl;
  42. }
  43.  
  44. int main()
  45. {
  46.     /*Declarare variabile (stiva statica & top)*/
  47.     int stiva[LIM], top = 0;
  48.  
  49.     push(stiva, top, 2);
  50.     push(stiva, top, 4);
  51.     push(stiva, top, 6);
  52.     push(stiva, top, 7);
  53.     push(stiva, top, 8);
  54.  
  55.     cout << "Afisare stiva: " << endl;
  56.     afisare(stiva, top);
  57.  
  58.     pop(stiva, top);
  59.     pop(stiva, top);
  60.     pop(stiva, top);
  61.     cout << endl;
  62.  
  63.     cout << "Afisare stiva: " << endl;
  64.     afisare(stiva, top);
  65.  
  66.  
  67.     return 0;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment