Stack using C

Stack using C

PROGRAM : For Stack using C


  1. #include <stdio.h>
  2.  
  3. #include <stdlib.h>
  4.  
  5.  
  6. #define MAX 4
  7.  
  8.  
  9.  
  10. struct stack
  11.  
  12. {
  13.  
  14. int a[MAX];
  15.  
  16. int top;
  17.  
  18. }st;
  19.  
  20. int stfull(){
  21.  
  22. if(st.top>=MAX-1)return 1;
  23.  
  24. else return 0;
  25.  
  26. }
  27.  
  28. int push(int val){
  29.  
  30. st.top++;
  31.  
  32. st.a[st.top]=val;
  33.  
  34. }
  35.  
  36. int stempty(){
  37.  
  38. if(st.top==-1)return 1;
  39.  
  40. else return 0;
  41.  
  42. }
  43.  
  44. int pop(){
  45.  
  46. st.top--;
  47.  
  48. return 0;
  49.  
  50. }
  51.  
  52. int display(){
  53.  
  54. int i;
  55.  
  56. if(stempty()==1)printf("\nstack is empty\n");
  57.  
  58. else{
  59.  
  60. printf("\n");
  61.  
  62. for(i=st.top;i>=0;i--){
  63.  
  64. printf("|%d|",st.a[i]);
  65.  
  66. }
  67.  
  68. }
  69.  
  70. return 0;
  71.  
  72. }
  73.  
  74.  
  75. /* Main function */
  76. int main()
  77.  
  78. {
  79.  
  80. int val;
  81.  
  82. int choice;
  83.  
  84. st.top=-1;
  85.  
  86. label:
  87.  
  88.  
  89.  
  90. printf("\nselect the choice\n");
  91.  
  92. printf("1=push\n2=pop\n3=peep\n4=display\n5=clear screen\n");
  93.  
  94. scanf("%d",&choice);
  95.  
  96.  
  97.  
  98. switch(choice){
  99.  
  100. case 1:
  101.  
  102.  
  103.  
  104. if(stfull()==1){
  105.  
  106. printf("\nstack is full\n");
  107.  
  108. }
  109.  
  110. else{
  111.  
  112. printf("\nenter value=");
  113.  
  114. scanf("%d",&val);
  115.  
  116. push(val);
  117.  
  118. }
  119.  
  120. goto label;
  121.  
  122. break;
  123.  
  124.  
  125.  
  126.  
  127.  
  128. case 2:
  129.  
  130. if(stempty()==1){printf("\nstack is empty\n");}
  131.  
  132. else {
  133.  
  134. printf("\n%d deleted\n",st.a[st.top]);
  135.  
  136. val=pop();
  137.  
  138.  
  139.  
  140. }
  141.  
  142. goto label;
  143.  
  144. break;
  145.  
  146. case 3: printf("\n a[st.top]=%d",st.a[st.top]);
  147.  
  148.  
  149.  
  150. goto label;
  151.  
  152. break;
  153.  
  154. case 4:
  155.  
  156. display();
  157.  
  158. goto label;
  159.  
  160. break;
  161.  
  162. case 5:
  163.  
  164. system("cls");
  165.  
  166. goto label;
  167.  
  168. break;
  169.  
  170. default:
  171.  
  172. exit(0);
  173.  
  174. }
  175.  
  176.  
  177.  
  178. return 0;
  179.  
  180. }
  181.  
  182.  

Comments

Popular posts from this blog

Introduction to ChatGPT: Explain what ChatGPT is, how it works, and what it is used for.

Multi Level Inheritance In cpp

Hybrid Inheritance In cpp