This is a C Program to Illustrate Stack Operations using MACROS.
This program illustrates the stack operations using MACROS.
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.
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.
/*
* C Program to Illustrate Stack Operations using MACROS
*/
#include <stdio.h>
#include <stdlib.h>
#define MAX 5
#define EMPTY "Stack is Empty"
#define OVERFLOW "Stack Overflow"
#define ISOVERFLOW top >= MAX - 1 /*Macro to find whether the stack is full*/
#define ISEMPTY top == -1 /*Macro to find whether the stack is empty*/
void push(int);
void pop();
void display();
void stacksize();
void destroy();
void stackfull();
int top = -1;
int stack[MAX];
void main()
{
int choice, value;
while (1)
{
printf("Enter Your choice:\n");
printf("1.PUSH\n2.POP\n3.DISPLAY\n4.STACKSIZE\n5.DESTROY\n6.SATCKFULL CHECK\n7.EXIT");
scanf("%d", &choice);
switch (choice)
{
case 1:
printf("enter the value to be pushed on to the stack");
scanf("%d", &value);
push(value);
continue;
case 2:
pop();
continue;
case 3:
display();
continue;
case 4:
stacksize();
continue;
case 5:
destroy();
continue;
case 6:
stackfull();
continue;
case 7:
exit(0);
default:
printf("YOU HAVE ENTERD A WRONG CHOICE");
}
}
}
/*Function to add an element into the stack*/
void push(int value)
{
if (ISOVERFLOW)
{
printf(OVERFLOW);
return;
}
top++;
stack[top] = value;
}
/*Function to delete an element from the stack*/
void pop()
{
if (ISEMPTY)
{
printf(EMPTY);
return;
}
printf("the popped element is %d", stack[top]);
top--;
}
/*Function to display all the elements in the stack*/
void display()
{
int temp = top;
if (ISEMPTY)
{
printf(EMPTY);
return;
}
while (temp + 1)
{
printf("%d\n", stack[temp]);
temp--;
}
}
/* Function to check whether the stack is full using macro */
void stackfull()
{
int temp = top, count = 0;
if (ISEMPTY)
{
printf(EMPTY);
return;
}
while (temp + 1)
{
printf("%d\n", stack[temp]);
temp--;
count++;
}
if (count >= MAX)
{
printf("Stack is full");
}
else
{
printf("there are %d more spaces in the stack", (MAX-count));
}
}
/* Function to return the size of the stack */
void stacksize()
{
int temp = top, count = 0;
if (ISEMPTY)
{
printf(EMPTY);
return;
}
while (temp + 1)
{
temp--;
count++;
}
printf("the size of the stack is %d\n", count);
}
/* Function to delete all the elements in the stack */
void destroy()
{
if (ISEMPTY)
{
printf("nothing is there to destroy");
}
while (top != -1)
{
pop();
}
}
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.
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
- Check Computer Science Books
- Practice Design & Analysis of Algorithms MCQ
- Practice Programming MCQs
- Check Programming Books
- Apply for Computer Science Internship