fork download
  1. #include<stdio.h>
  2. #define S 3
  3. int s[S],top=-1,i;
  4. void push()
  5. {
  6. if(top==S-1)
  7. {
  8. printf("stack is overflow\n");
  9. }
  10. top++;
  11. scanf("%d",&s[top]);
  12. }
  13. void pop()
  14. {
  15. if(top==-1)
  16. {
  17. printf("stack is empty\n");
  18.  
  19. }
  20. else
  21. {
  22. top=top-1;
  23. }
  24.  
  25. }
  26. void display()
  27. {
  28. for(i=top;i>=0;i--)
  29. {
  30. printf("%d",s[i]);
  31. }
  32. }
  33. void peek()
  34. {
  35. printf("%d",s[top]);
  36. }
  37. void main()
  38. {
  39. int n;
  40. printf("Stack menu\n");
  41. printf("\n1.push\n2.pop\n3.display\n4.peek\n5.exit\n");
  42. printf("enter option\n");
  43. scanf("%d",&n);
  44. switch(n)
  45. {
  46. case 1:push();break;
  47. case 2:pop();break;
  48. case 3:display();break;
  49. case 4:peek();break;
  50. case 5:exit(0);
  51. default:printf("invalid option");
  52. }
  53. }
Success #stdin #stdout 0s 5516KB
stdin
1
stdout
Stack menu

1.push
2.pop
3.display
4.peek
5.exit
enter option