Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct node
  5. {
  6. int data;
  7. struct node *link;
  8. }*top = NULL;
  9.  
  10. #define MAX 5
  11.  
  12. // function prototypes
  13. void push();
  14. void pop();
  15. void empty();
  16. void stack_full();
  17. void stack_count();
  18. void destroy();
  19. void print_top();
  20.  
  21. void main()
  22. {
  23. int choice;
  24.  
  25. while (1)
  26. {
  27. printf("1. push an element \n");
  28. printf("2. pop an element \n");
  29. printf("3. check if stack is empty \n");
  30. printf("4. check if stack is full \n");
  31. printf("5. count/display elements present in stack \n");
  32. printf("6. empty and destroy stack \n");
  33. printf("7. Print top of the stack \n");
  34. printf("8. exit \n");
  35. printf("Enter your choice \n");
  36. scanf("%d",&choice);
  37. switch (choice)
  38. {
  39. case 1:
  40. push();
  41. break;
  42. case 2:
  43. pop();
  44. break;
  45. case 3:
  46. empty();
  47. break;
  48. case 4:
  49. stack_full();
  50. break;
  51. case 5:
  52. stack_count();
  53. break;
  54. case 6:
  55. destroy();
  56. break;
  57. case 7:
  58. print_top();
  59. break;
  60. case 8:
  61. exit(0);
  62. default:
  63. printf("wrong choice\n");
  64. }
  65. }
  66. }
  67.  
  68. // to insert elements in stack
  69. void push()
  70. {
  71. int val,count;
  72. struct node *temp;
  73. temp = (struct node*)malloc(sizeof(struct node));
  74.  
  75. count = st_count();
  76. if (count <= MAX - 1)
  77. {
  78. printf("\nEnter value which you want to push into the stack :\n");
  79. scanf("%d",&val);
  80. temp->data = val;
  81. temp->link = top;
  82. top = temp;
  83. }
  84. else
  85. printf("WARNING: STACK FULL\n");
  86. }
  87.  
  88. // to delete elements from stack
  89. void pop()
  90. {
  91. struct node *temp;
  92. if (top = = NULL)
  93. printf("**Stack is empty**\n");
  94. else
  95. {
  96. temp = top;
  97. printf("Value popped out is %d \n",temp->data);
  98. top = top->link;
  99. free(temp);
  100. }
  101. }
  102.  
  103. // to check if stack is empty
  104. void empty()
  105. {
  106. if (top = = NULL)
  107. printf("STACK IS EMPTY\n");
  108. else
  109. printf("elements are present, stack is not empty \n");
  110. }
  111.  
  112. // to check if stack is full
  113. void stack_full()
  114. {
  115. int count;
  116.  
  117. count = st_count();
  118. if (count = = MAX)
  119. {
  120. printf("stack is full\n");
  121. }
  122. else
  123. printf("stack is not full \n");
  124. }
  125.  
  126. // to count the number of elements
  127. void stack_count()
  128. {
  129. int count = 0;
  130. struct node *temp;
  131.  
  132. temp = top;
  133. while (temp! = NULL)
  134. {
  135. printf(" %d\n",temp->data);
  136. temp = temp->link;
  137. count++;
  138. }
  139. printf("size of stack is %d \n",count);
  140. }
  141.  
  142. int st_count()
  143. {
  144. int count = 0;
  145. struct node *temp;
  146. temp = top;
  147. while (temp! = NULL)
  148. {
  149. temp = temp->link;
  150. count++;
  151. }
  152. return count;
  153. }
  154.  
  155. // to empty and destroy the stack
  156. void destroy()
  157. {
  158. struct node *temp;
  159. temp = top;
  160. while (temp! = NULL)
  161. {
  162. pop();
  163. temp = temp->link;
  164. }
  165. printf("stack destroyed\n");
  166. }
  167.  
  168. // to print top element of stack
  169. void print_top()
  170. {
  171. if (top == NULL)
  172. printf("\n**Top is not available for an EMPTY stack**\n");
  173. else
  174. printf("\nTop of the stack is %d \n",top->data);
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement