C Program to Implement a Stack

«
»

This is a C program to Implement a stack.

Problem Description

This program implements the stack operation.

Problem Solution

1. Use three functions for three operations like push, pop and display.
2. Use switch statement to access these functions.
3. Exit.

advertisement
Program/Source Code

Here is source code of the C program to implement a stack. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C program to implement stack. Stack is a LIFO data structure.
  3.  * Stack operations: PUSH(insert operation), POP(Delete operation)
  4.  * and Display stack.
  5.  */
  6. #include <stdio.h>
  7. #define MAXSIZE 5
  8.  
  9. struct stack
  10. {
  11.     int stk[MAXSIZE];
  12.     int top;
  13. };
  14. typedef struct stack STACK;
  15. STACK s;
  16.  
  17. void push(void);
  18. int  pop(void);
  19. void display(void);
  20.  
  21. void main ()
  22. {
  23.     int choice;
  24.     int option = 1;
  25.     s.top = -1;
  26.  
  27.     printf ("STACK OPERATION\n");
  28.     while (option)
  29.     {
  30.         printf ("------------------------------------------\n");
  31.         printf ("      1    -->    PUSH               \n");
  32.         printf ("      2    -->    POP               \n");
  33.         printf ("      3    -->    DISPLAY               \n");
  34.         printf ("      4    -->    EXIT           \n");
  35.         printf ("------------------------------------------\n");
  36.  
  37.         printf ("Enter your choice\n");
  38.         scanf    ("%d", &choice);
  39.         switch (choice)
  40.         {
  41.         case 1:
  42.             push();
  43.             break;
  44.         case 2:
  45.             pop();
  46.             break;
  47.         case 3:
  48.             display();
  49.             break;
  50.         case 4:
  51.             return;
  52.         }
  53.         fflush (stdin);
  54.         printf ("Do you want to continue(Type 0 or 1)?\n");
  55.         scanf    ("%d", &option);
  56.     }
  57. }
  58. /*  Function to add an element to the stack */
  59. void push ()
  60. {
  61.     int num;
  62.     if (s.top == (MAXSIZE - 1))
  63.     {
  64.         printf ("Stack is Full\n");
  65.         return;
  66.     }
  67.     else
  68.     {
  69.         printf ("Enter the element to be pushed\n");
  70.         scanf ("%d", &num);
  71.         s.top = s.top + 1;
  72.         s.stk[s.top] = num;
  73.     }
  74.     return;
  75. }
  76. /*  Function to delete an element from the stack */
  77. int pop ()
  78. {
  79.     int num;
  80.     if (s.top == - 1)
  81.     {
  82.         printf ("Stack is Empty\n");
  83.         return (s.top);
  84.     }
  85.     else
  86.     {
  87.         num = s.stk[s.top];
  88.         printf ("poped element is = %dn", s.stk[s.top]);
  89.         s.top = s.top - 1;
  90.     }
  91.     return(num);
  92. }
  93. /*  Function to display the status of the stack */
  94. void display ()
  95. {
  96.     int i;
  97.     if (s.top == -1)
  98.     {
  99.         printf ("Stack is empty\n");
  100.         return;
  101.     }
  102.     else
  103.     {
  104.         printf ("\n The status of the stack is \n");
  105.         for (i = s.top; i >= 0; i--)
  106.         {
  107.             printf ("%d\n", s.stk[i]);
  108.         }
  109.     }
  110.     printf ("\n");
  111. }
Program Explanation

1. Ask the user for the operation like push, pop, display and exit. Use the variable top to represent the top of the stack.
2. According to the option entered, access its respective function using switch statement.
3. In the function push(), firstly check if the stack is full. If it is, then print the output as “Stack is Full”. Otherwise take the number to be inserted as input and store it in the variable num. Copy the number to the array stk[] and increment the variable top by 1.
4. In the function pop(), firstly check if the stack is empty. If it is, then print the output as “Stack is Empty”. Otherwise print the top most element of the array stk[] and decrement the variable top by 1.
5. In the function display(), using for loop print all the elements of the array.
6. Exit.

advertisement
Runtime Test Cases
STACK OPERATION
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
0
$ a.out
STACK OPERATION
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
2
poped element is = 34
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3
Stack is empty
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
50
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
1
Enter the element to be pushed
60
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
3
 
The status of the stack is
60
50
 
Do you want to continue(Type 0 or 1)?
1
------------------------------------------
      1    -->    PUSH
      2    -->    POP
      3    -->    DISPLAY
      4    -->    EXIT
------------------------------------------
Enter your choice
4

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Reference Books in C Programming, Data-Structures and Algorithms

advertisement
If you wish to look at other example programs on Stacks & Queues, go to C Programming Examples on Stacks & Queues. If you wish to look at programming examples on all topics of C, go to C Programming Examples.

advertisement
advertisement
advertisement
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn