fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. int STACK[5];
  5.  
  6. int TOP = -1;
  7.  
  8. void push(int data){
  9. TOP = TOP + 1;
  10. STACK[TOP] = data;
  11. }
  12.  
  13. void pop(){
  14. if (TOP==-1){
  15. printf("The stack is empty");
  16. }else{
  17. TOP = TOP-1;
  18. }
  19. }
  20.  
  21. void printStack(){
  22. if(TOP==-1){
  23. printf("The List is Empty");
  24. }else{
  25. for(int i=0; i<=TOP; i++){
  26. printf("%d->", STACK[i]);
  27. }
  28. }
  29. printf("\n");
  30. // return 1;
  31. }
  32.  
  33. void peek(){
  34. if(TOP == -1){
  35. printf("Stack is Empty \n");
  36. }else{
  37. printf("The peeked element is : %d \n", STACK[TOP]);
  38. }
  39. }
  40.  
  41. int main(){
  42. push(9);
  43. printStack();
  44. push(12);
  45. push(3);
  46. push(5);
  47. peek();
  48. printStack();
  49. pop();
  50. printStack();
  51. peek();
  52. printStack();
  53.  
  54. return 0;
  55. }
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
9->
The peeked element is : 5 
9->12->3->5->
9->12->3->
The peeked element is : 3 
9->12->3->