sahajjain01

11.Implement stack using array.

Aug 15th, 2016
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.37 KB | None | 0 0
  1. //Program to implement stack using array.
  2. #include<stdio.h>
  3. #include<conio.h>
  4.  
  5. struct stack {
  6.     int arr[10];
  7.     int top;
  8. } st;
  9.  
  10. int isFull() {
  11.     if (st.top == 9) {
  12.         return 1;
  13.     }
  14.     else {
  15.           return 0;
  16.     }
  17. }
  18.  
  19. int isEmpty() {
  20.     if (st.top == -1) {
  21.         return 1;
  22.     }
  23.     else {
  24.         return 0;
  25.     }
  26. }
  27.  
  28. void push() {
  29.     if (!isFull()) {
  30.         int val;
  31.         printf("Enter value to be pushed: ");
  32.         scanf("%d", &val);
  33.  
  34.         st.top++;
  35.         st.arr[st.top] = val;
  36.  
  37.         printf("Successfully pushed %d in the stack.", val);
  38.         getch();
  39.     }
  40.     else {
  41.         printf("The Stack is Full.");
  42.         getch();
  43.     }
  44. }
  45.  
  46. void pop() {
  47.     if (!isEmpty()) {
  48.         printf("Successfully popped %d from the stack.", st.arr[st.top]);
  49.         st.top--;
  50.         getch();
  51.     }
  52.     else {
  53.         printf("The stack is Empty.");
  54.         getch();
  55.     }
  56. }
  57.  
  58. void display() {
  59.     int i;
  60.  
  61.     if (isEmpty()) {
  62.         printf("The stack is Empty.");
  63.         getch();
  64.     }
  65.     else {
  66.         for (i = st.top; i >= 0; i--) {
  67.             printf("%d\n", st.arr[i]);
  68.         }
  69.         getch ();
  70.     }
  71. }
  72.  
  73. void main()
  74. {
  75.     char choice;
  76.  
  77.     clrscr();
  78.  
  79.     st.top = -1;
  80.  
  81.     while (1) {
  82.         clrscr();
  83.  
  84.         printf("1.Push\n2.Pop\n3.Display\n4.Exit\n");
  85.         choice = getch();
  86.  
  87.         switch (choice) {
  88.             case '1':
  89.                 push();
  90.                 break;
  91.  
  92.             case '2':
  93.                 pop();
  94.                 break;
  95.  
  96.             case '3':
  97.                 display();
  98.                 break;
  99.  
  100.             case '4':
  101.                 return;
  102.  
  103.             default:
  104.                 printf("Invalid input.");
  105.                 getch();
  106.         }
  107.     }
  108. }
Add Comment
Please, Sign In to add comment