Advertisement
Guest User

Untitled

a guest
Jul 20th, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. #include <stdio.h>
  2. #define SIZE 100
  3. int S[SIZE]; // stack declaration
  4. int top = -1 ; // initially stack is empty
  5.  
  6. void push(int x){
  7. if(top == (SIZE - 1) )
  8. {
  9. printf("\nStack overflow.....\n");
  10. return;
  11. }
  12. S[++top] = x;
  13. }
  14.  
  15. void pop(){
  16. if(top == -1)
  17. {
  18. printf("\nStack underflow.....\n");
  19. return;
  20. }
  21. top--;
  22. }
  23.  
  24. int topElement(){
  25. return S[top];
  26. }
  27.  
  28. int isEmpty(){
  29. if(top == -1)
  30. return 1;
  31. else
  32. return 0;
  33. }
  34.  
  35. void print(){
  36. int i;
  37. printf("\nthe stack is : \n");
  38. for(i=0;i<=top;i++){
  39. printf("%d\t",S[i]);
  40. }
  41. }
  42.  
  43. int main(){
  44.  
  45. push(2);print();
  46. push(10);print();
  47. pop();print();
  48. push(7);print();
  49. push(5);print();
  50. printf("\nthe top element is : %d\n",topElement());
  51. if(isEmpty() == 1) printf("\nthe Stack is empty\n");
  52. else printf("\nthe Stack is not empty\n");
  53. pop();print();
  54. pop();print();
  55.  
  56. return 0;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement