Skip to main content

Static Implementation Of Stack

Static Implementation Of Stack


CODE

πŸ‘‡

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define MAX 20
  4. typedef struct 
  5. {
  6. int data[MAX];
  7. int top;
  8. }STACK;

  9. void initstack(STACK *ps)
  10. {
  11. ps->top = -1;
  12. }

  13. void push(STACK *ps, int num)
  14. {
  15. ps->top++;
  16. ps->data[ps->top] = num;
  17. }

  18. int pop(STACK *ps)
  19. {
  20. return(ps->data[ps->top--]);
  21. }

  22. int isempty(STACK *ps)
  23. {
  24. return(ps->top == -1);
  25. }

  26. int isfull(STACK *ps)
  27. {
  28. return(ps->top==MAX-1);
  29. }
  30. void main()
  31. {
  32. int n, choice;
  33. STACK s1;
  34. initstack(&s1);
  35. do
  36. {
  37. printf("\n1.PUSH \n2.POP \n3.EXIT");
  38. printf("\n Enter Your Choice:");
  39. scanf("%d",&choice);
  40. switch(choice)
  41. {
  42. case 1:
  43. if(isfull (&s1))
  44. printf("\n Stack Overflow");
  45. else
  46. {
  47. printf("Enter the element to be pushed:");
  48. scanf("%d", &n);
  49. push(&s1, n);
  50. }
  51. break;
  52. case 2:
  53. if(isempty(&s1)==1)
  54. printf("\n Stack Underflow");
  55. else
  56. printf("The popped element is: %d\n", pop(&s1));
  57. break;
  58. }
  59. }
  60. while(choice != 3);
  61. }


πŸ‘‰ExecuteπŸ‘ˆ


//Output

/*

1.PUSH 

2.POP 

3.EXIT

 Enter Your Choice:1

Enter the element to be pushed:5


1.PUSH 

2.POP 

3.EXIT

 Enter Your Choice:2

The popped element is: 5


1.PUSH 

2.POP 

3.EXIT

 Enter Your Choice:3

*/
                                                                                         //ThE ProFessoR

Comments