Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. const int size=5;
  2. class cstack
  3. {
  4. public:
  5. void print();
  6. bool isempty();
  7. bool isfull();
  8. void push(int el); // add last
  9. void pop(); // delete last
  10. int search(int el);
  11. void gettop();
  12. cstack();
  13.  
  14. private:
  15. int stack[size];
  16. int top;
  17.  
  18. };
  19.  
  20.  
  21. #include "class.h"
  22. #include <iostream>
  23. using namespace std;
  24. int main()
  25. {
  26. cstack s1;
  27. s1.push(4);
  28. s1.push(2);
  29. s1.push(5);
  30. s1.pop();
  31. s1.print();
  32. } // end main
  33.  
  34. cstack::cstack()
  35. {
  36. top=0;
  37. }
  38. bool cstack::isempty()
  39. {
  40. if (top==0)
  41. return true;
  42. else
  43. return false;
  44.  
  45. }
  46. bool cstack::isfull()
  47. {
  48. if (top==size)
  49. return true;
  50. else
  51. return false;
  52.  
  53. }
  54. void cstack::push(int el)
  55. { // isfull check before
  56. stack[top]=el;
  57. top++;
  58.  
  59. }
  60.  
  61. void cstack::pop()
  62. {
  63. if(!isempty()){
  64. stack[top-1];
  65. top--;}
  66. else
  67. cout<<"empty";
  68. }
  69.  
  70. int cstack::search(int el)
  71. {
  72. int pos=-1;
  73. //isempty check here
  74. for(int i=top; i>=0;i--)
  75. {
  76. if(stack[i]==el)
  77. {pos = i;
  78. break;}
  79. else
  80. cout<<"element not found"<<endl;
  81. }
  82. return pos;
  83. }
  84. void cstack::gettop()
  85. {
  86.  
  87. stack[top-1];
  88.  
  89. }
  90.  
  91. void cstack::print()
  92. {
  93. for(int i=top-1; i>=0;i--)
  94. {
  95. cout<<stack[i]<<" ";
  96.  
  97. }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement