Advertisement
a53

oop_3

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