fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5.  
  6. const int Max_Size =100;
  7. class stack
  8. {
  9.  
  10. int top;
  11. int item[Max_Size];
  12.  
  13. public:
  14. stack()
  15. {
  16. top = -1;
  17.  
  18. }
  19.  
  20.  
  21. void push(int element)
  22. {
  23. if (top >= Max_Size-1) //هيبدا من 0ل99 وكده يبقا 100 ايليمنت
  24. {
  25. cout<< "Stack is Full\n";
  26. }
  27. else
  28. {
  29. top++;
  30. item[top] =element;
  31. }
  32.  
  33. }
  34.  
  35.  
  36. bool IsEmpty()
  37. {
  38. if (top==-1)
  39. {
  40. return true;
  41. }
  42. else
  43. {
  44. return false;
  45. }
  46.  
  47. }
  48. void pop()
  49. {
  50. if (IsEmpty())
  51. {
  52. cout<<"stack is empty \n";
  53. }
  54. else
  55. {
  56. top--;
  57. }
  58.  
  59. }
  60.  
  61. void GetTop(int &stacktop)
  62. {
  63. if (IsEmpty())
  64. {
  65. cout<<"stack is empty \n";
  66. }
  67. else
  68. {
  69. stacktop = item[top];
  70. cout << stacktop<<endl;
  71. }
  72.  
  73. }
  74.  
  75. void Display()
  76. {
  77. cout<<"[";
  78. for (int i = top; i >= 0; i-- ) //هنا عملتها العكس عشان اخر عنصر اضيفو هيكون اول واحد يطبع
  79. {
  80. cout<<item[i]<<" ,";
  81. }
  82. cout<<"]";
  83. cout<<endl;
  84.  
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91. };
  92.  
  93. int main(int argc, char const *argv[])
  94. {
  95. stack ibrahem;
  96. ibrahem.push(5);
  97. ibrahem.push(10);
  98. ibrahem.push(15);
  99. ibrahem.push(20);
  100. ibrahem.pop();
  101. ibrahem.Display();
  102. int y=0;
  103. ibrahem.GetTop(y);
  104. return 0;
  105. }
  106.  
Success #stdin #stdout 0.01s 5292KB
stdin
Standard input is empty
stdout
[15 ,10 ,5 ,]
15