Advertisement
saira12tabassum19

stack

Nov 7th, 2019
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. typedef struct Stack stack;
  4.  
  5. void push(int n);
  6. void pop();
  7. int top_v();
  8. int empty();
  9. void display();
  10. int s_s=0;
  11.  
  12. struct Stack{
  13.  
  14. int x;
  15. stack *next;
  16.  
  17. }*top=NULL;
  18. int top_v()
  19. {
  20.  
  21. return top->x;
  22.  
  23. }
  24. int empty()
  25. {
  26. return !top;
  27. }
  28. void push(int n)
  29. {
  30. s_s++;
  31. stack *c;
  32. c=(stack*)malloc(sizeof(stack));
  33. if(top==NULL)
  34. {
  35. c->x=n;
  36. c->next=NULL;
  37. top=c;
  38. }
  39. else{
  40.  
  41. c->x=n;
  42. c->next=top;
  43. top=c;
  44.  
  45. }
  46.  
  47.  
  48.  
  49.  
  50. }
  51. void display()
  52. {
  53.  
  54. printf("\n");
  55. stack *c=top;
  56. while(c!=NULL)
  57. {
  58. printf("%d\n",c->x);
  59. c=c->next;
  60. }
  61. printf("\n");
  62.  
  63. }
  64.  
  65. void pop()
  66. {
  67.  
  68. if(top==NULL)
  69. {
  70. printf("The stack is underflow!!\n");
  71. }
  72. else{
  73.  
  74. top=top->next;
  75.  
  76. }
  77.  
  78.  
  79. }
  80.  
  81.  
  82. int main()
  83. {
  84.  
  85.  
  86. int n;
  87.  
  88. do{
  89. printf("1. push.\n");
  90. printf("2. pop.\n");
  91. printf("3. display.\n");
  92. printf("4. stack is empty or not.\n");
  93. printf("5. top value.\n");
  94. printf("10. exit.\n");
  95. printf("Enter your choice: ");
  96. scanf("%d",&n);
  97. if(n==1)
  98. {
  99. int x;
  100. scanf("%d",&x);
  101. push(x);
  102. display();
  103. }
  104. else if(n==2)
  105. {
  106. pop();
  107. display();
  108. }
  109. else if(n==3)
  110. {
  111. display();
  112. }
  113. else if(n==4)
  114. {
  115. if(empty())printf("The stack is empty\n\n");
  116. else printf("The stack is not empty\n\n");
  117.  
  118. }
  119. else if(n==5)
  120. {
  121. printf("Current top value %d\n\n",top_v());
  122. }
  123. else{
  124. printf("Wrong input\n");
  125. }
  126.  
  127. }while(n!=10);
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement