C Program to Illustrate Stack Operations using MACROS

«
»

This is a C Program to Illustrate Stack Operations using MACROS.

Problem Description

This program illustrates the stack operations using MACROS.

Problem Solution

1. Use #define function to define the macros.
2. Define separate functions for the operations like push, pop, display etc.
3. Use switch statement to access these functions.

advertisement
Program/Source Code

Here is source code of the C Program to Illustrate Stack Operations using MACROS. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. /*
  2.  * C Program to Illustrate Stack Operations using MACROS
  3.  */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6.  
  7. #define MAX 5
  8. #define EMPTY "Stack is Empty"
  9. #define OVERFLOW "Stack Overflow"
  10. #define ISOVERFLOW top >= MAX - 1 /*Macro to find whether the stack is full*/
  11. #define ISEMPTY top == -1    /*Macro to find whether the stack is empty*/
  12.  
  13. void push(int);
  14. void pop();
  15. void display();
  16. void stacksize();
  17. void destroy();
  18. void stackfull();
  19.  
  20. int top = -1;
  21. int stack[MAX];
  22.  
  23. void main()
  24. {
  25.     int choice, value;
  26.  
  27.     while (1)
  28.     {
  29.         printf("Enter Your choice:\n");
  30.         printf("1.PUSH\n2.POP\n3.DISPLAY\n4.STACKSIZE\n5.DESTROY\n6.SATCKFULL CHECK\n7.EXIT");
  31.         scanf("%d", &choice);
  32.         switch (choice)
  33.         {
  34.         case 1:
  35.             printf("enter the value to be pushed on to the stack");
  36.             scanf("%d", &value);
  37.             push(value);
  38.             continue;
  39.         case 2:
  40.             pop();
  41.             continue;
  42.         case 3:
  43.             display();
  44.             continue;
  45.         case 4:
  46.             stacksize();
  47.             continue;
  48.         case 5:
  49.             destroy();
  50.             continue;
  51.         case 6:
  52.             stackfull();
  53.             continue;
  54.         case 7:
  55.             exit(0);
  56.         default:
  57.             printf("YOU HAVE ENTERD A WRONG CHOICE");
  58.         }
  59.     }
  60. }
  61.  
  62. /*Function to add an element into the stack*/
  63. void push(int value)
  64. {
  65.     if (ISOVERFLOW)
  66.     {
  67.         printf(OVERFLOW);
  68.         return;
  69.     }
  70.     top++;
  71.     stack[top] = value;
  72. }
  73.  
  74. /*Function to delete an element from the stack*/
  75. void pop()
  76. {
  77.     if (ISEMPTY)
  78.     {
  79.         printf(EMPTY);
  80.         return;
  81.     }
  82.     printf("the popped element is %d", stack[top]);
  83.     top--;
  84. }
  85.  
  86. /*Function to display all the elements in the stack*/
  87.  
  88. void display()
  89. {
  90.     int temp = top;
  91.  
  92.     if (ISEMPTY)
  93.     {
  94.         printf(EMPTY);
  95.         return;
  96.     }
  97.     while (temp + 1)
  98.     {
  99.         printf("%d\n", stack[temp]);
  100.         temp--;
  101.     }
  102. }
  103.  
  104. /* Function to check whether the stack is full using macro */
  105. void stackfull()
  106. {
  107.     int temp = top, count = 0;
  108.  
  109.     if (ISEMPTY)
  110.     {
  111.         printf(EMPTY);
  112.         return;
  113.     }
  114.     while (temp + 1)
  115.     {
  116.         printf("%d\n", stack[temp]);
  117.         temp--;
  118.         count++;
  119.     }
  120.     if (count >= MAX)
  121.     {
  122.         printf("Stack is full");
  123.     }
  124.     else
  125.     {
  126.         printf("there are %d more spaces in the stack", (MAX-count));
  127.     }
  128. }
  129.  
  130. /* Function to return the size of the stack */
  131. void stacksize()
  132. {
  133.     int temp = top, count = 0;
  134.     if (ISEMPTY)
  135.     {
  136.         printf(EMPTY);
  137.         return;
  138.     }
  139.     while (temp + 1)
  140.     {
  141.           temp--;
  142.         count++;
  143.     }
  144.     printf("the size of the stack is %d\n", count);
  145. }
  146.  
  147. /* Function to delete all the elements in the stack */
  148.  
  149. void destroy()
  150. {
  151.     if (ISEMPTY)
  152.     {
  153.         printf("nothing is there to destroy");
  154.     }
  155.     while (top != -1)
  156.     {
  157.         pop();
  158.     }
  159. }
Program Explanation

1. Ask the user for the operations like push, pop, display etc.
2. According to the entered option access the respective functions. Use switch statement to access the functions.
3. Use #define function to define macros as mentioned in the program.
4. In the push() function, ask the user to enter the number to be inserted and store the value in the variable value and copy this value into the array stack[]. Use the variable top to represent the top of the stack.
5. In the pop() function, delete the element at the top of the array and decrement the variable top.
6. In the display() function, print all the elements from the top of stack to the bottom.
7. In the stack_size() function, print the number of elelments in the array stack.
8. In the destroy() function, pop all the elements from the array stack[].
9. In the stackfull() function, check if the array stack[] is full or not.

Note: Join free Sanfoundry classes at Telegram or Youtube
advertisement
advertisement
Runtime Test Cases
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT3
Stack is EmptyEnter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT2
Stack is EmptyEnter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT1
enter the value to be pushed on to the stack1
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT1
enter the value to be pushed on to the stack2
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT1
enter the value to be pushed on to the stack3
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT1
enter the value to be pushed on to the stack4
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT1
enter the value to be pushed on to the stack5
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT3
5
4
3
2
1
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT4
the size of the stack is 5
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT6
5
4
3
2
1
Stack is fullEnter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT2
the popped element is 5Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT2
the popped element is 4Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT2
the popped element is 3Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT3
2
1
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT5
the popped element is 2the popped element is 1Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT1
enter the value to be pushed on to the stack12
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT3
12
Enter Your choice:
1.PUSH
2.POP
3.DISPLAY
4.STACKSIZE
5.DESTROY
6.SATCKFULL CHECK
7.EXIT

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

Take Data Structure I Practice Tests - Chapterwise!
Start the Test Now: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.