fork download
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. struct stack{
  5. int size;
  6. int top;
  7. int *arr;
  8. };
  9.  
  10. // int peek(stack * s, int i){
  11. // int arrayind=s->top-i+1;
  12. // if(arrayind<0){
  13. // cout<<"NOT VALID!!!"<<endl;
  14. // return -1;
  15. // }
  16. // else{
  17. // return s->arr[arrayind];
  18. // }
  19. // }
  20.  
  21. int stacktop(stack * ptr){
  22. return ptr->arr[ptr->top];
  23. }
  24.  
  25. int stackbottom(stack * ptr){
  26. return ptr->arr[0];
  27. }
  28.  
  29. void push(struct stack* ptr, int val){
  30. if(ptr->top==ptr->size-1){
  31. cout<<"stack overflow"<<endl;
  32.  
  33. }
  34. else{
  35. ptr->top++;
  36. ptr->arr[ptr->top] = val;
  37.  
  38. }
  39. }
  40. int main(){
  41. stack * s=new stack;
  42. s->top=-1;
  43. // cin>>s->size;
  44. s->size=9;
  45. s->arr=new int;
  46. push(s, 1);
  47. push(s, 23);
  48. push(s, 99);
  49. push(s, 75);
  50. push(s, 3);
  51. push(s, 64);
  52. push(s, 57);
  53. push(s, 46);
  54. push(s, 89);
  55. // for (int j = 1; j <= s->top+1; j++)
  56. // {
  57. // cout<<" The value at position "<<j<<" is "<<peek(s,j)<<endl;
  58. // }
  59. // cout<<"The topmost element in stack is: "<<stacktop(s)<<endl;
  60. // cout<<"The bottommost element in stack is: "<<stackbottom(s)<<endl;
  61. cout<<stacktop(s)<<endl;
  62. cout<<stackbottom(s)<<endl;
  63. return 0;
  64. }
Success #stdin #stdout 0.01s 5436KB
stdin
Standard input is empty
stdout
89
1