Advertisement
nicuvlad76

Untitled

Dec 10th, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. class Stiva
  2. {
  3. private:
  4. int varf=0;
  5. struct stiva{int X, Max=0;}S[100001];
  6. public:
  7. // depune valoarea x in stiva
  8. void Push(int x)
  9. {
  10. S[++varf].X=x;
  11. if(varf==1) S[varf].Max=x;
  12. else
  13. if(x>S[varf-1].Max) S[varf].Max=x;
  14. else S[varf].Max=S[varf-1].Max;
  15. }
  16. // elimina un element din stiva daca stiva nu e vida
  17. void Pop()
  18. {
  19. if(!Empty())varf--;
  20. }
  21. // returneaza varful stivei
  22. // daca stiva este vida, returneaza -1
  23. int Top()
  24. {
  25. if(Empty()) return -1;
  26. return S[varf].X;
  27. }
  28. // returneaza valoarea maxima din stiva
  29. // daca stiva este vida, returneaza -1
  30. int Max()
  31. {
  32. if(Empty()) return -1;
  33. return S[varf].Max;
  34. }
  35. // returneaza 1 daca stiva este vida
  36. // returneaza 0 daca stiva nu e vida
  37. int Empty()
  38. {
  39. if(varf>0) return 0;
  40. return 1;
  41. }
  42. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement