Stack is an area of memory that holds all local
variables and parameters used by any function. It also remembers the order in
which functions are called so that function returns occur correctly. Stack is a
LIFO data structure. Stack operations are PUSH (insert operation), POP (Delete
operation), and Display (stack). Each time a function is called, its local
variables and parameters are “pushed onto” the stack. When the function
returns, these locals and parameters are “popped”.
#include <stdio.h>
#define MAXSIZE 25
struct stack {
int stk[MAXSIZE];
int top;
};
typedef struct stack STACK;
STACK s;
int
pop(void);
void display(void);
int main () {
int choice;
int option = 1;
s.top = -1;
printf ("\tSTACK OPERATION\n");
while (option) {
printf
("\n------------------------------------------\n");
printf ("
1 --> PUSH \n");
printf ("
2 --> POP \n");
printf ("
3 --> DISPLAY \n");
printf ("
4 --> EXIT \n");
printf
("------------------------------------------\n");
printf ("\nEnter your choice: ");
scanf
("%d", &choice);
switch (choice) {
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: return 0;
}
fflush (stdin);
printf ("\nDo you want to continue(Type 0 or
1)? ");
scanf
("%d", &option);
}}
/* Function
to add an element to the stack */
void push () {
int num;
if (s.top == (MAXSIZE - 1)) {
printf ("\nStack is Full.\n");
return;
}
else {
printf ("\nEnter the element to be pushed:
");
scanf ("%d", &num);
s.top = s.top + 1;
s.stk[s.top] = num;
}
return;
}
/* Function
to delete an element from the stack */
int pop () {
int num;
if (s.top == - 1) {
printf ("\nStack is Empty.\n");
return (s.top);
}
else {
num = s.stk[s.top];
printf ("\npoped element is = %d\n",
s.stk[s.top]);
s.top = s.top - 1;
}
return(num);
}
/* Function
to display the status of the stack */
void display () {
int i;
if (s.top == -1) {
printf ("\nStack is empty.\n");
return;
}
else {
printf ("\nThe status of the stack
is:\n");
for (i = s.top; i >= 0; i--) {
printf ("%d\n", s.stk[i]);
}}
printf ("\n");
}