Guest User

Untitled

a guest
Feb 11th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include<iostream>
  2. #include<climits>
  3. #include<stack>
  4. using namespace std;
  5. class Stacks{
  6. int *data;
  7. int nextIndex;
  8. int capacity;
  9. public:
  10. Stacks()
  11. {
  12. data=new int[4];
  13. nextIndex=0;
  14. capacity=4;
  15. }
  16. int size()
  17. {
  18. return nextIndex;
  19. }
  20. bool isEmpty()
  21. {
  22. return nextIndex==0;
  23. }
  24. void push(int element)
  25. {
  26. if(nextIndex==capacity){
  27. int *newData=new int[2*capacity];
  28. for(int i=0;i<capacity;i++){
  29. newData[i]=data[i];
  30. }
  31. capacity *=2;
  32. delete [] data;
  33. data=newData;
  34. }
  35. data[nextIndex]=element;
  36. nextIndex++;
  37.  
  38. }
  39. int pop()
  40. {
  41. if(isEmpty())
  42. {
  43. cout<<"Stack is empty"<<endl;
  44. return INT_MIN;
  45. }
  46. nextIndex--;
  47. return data[nextIndex];
  48. }
  49. int top()
  50. {
  51. if(isEmpty())
  52. {
  53. cout<<"Stack is empty"<<endl;
  54. return INT_MIN;
  55. }
  56. return data[nextIndex-1];
  57. }
  58. };
  59. int main()
  60. {
  61. Stacks s;
  62. s.push(10);
  63. s.push(20);
  64. s.push(30);
  65. s.push(40);
  66. s.push(50);
  67.  
  68. cout<<s.top()<<endl;
  69. cout<<s.pop()<<endl;
  70. cout<<s.pop()<<endl;
  71. cout<<s.pop()<<endl;
  72. cout<<s.size()<<endl;
  73. cout<<s.isEmpty()<<endl;
  74. }
Add Comment
Please, Sign In to add comment