#include <stdio.
h>
#define MAX 6
/*
Author: prasad balkawade
Title:stack using array
*/
typedef struct stack {
int data[MAX];
int top;
} stack;
void init(stack *);
int empty(stack *);
int full(stack *);
int pop(stack *);
void push(stack *, int);
void print(stack *);
int main() // use int main(), not void main
{
stack s;
int x, op;
init(&s);
do {
printf("\n\n1) Push\n2) Pop\n3) Print\n4) Quit");
printf("\nEnter Your choice: ");
scanf("%d", &op);
switch (op) {
case 1:
printf("\nEnter a number: ");
scanf("%d", &x);
if (!full(&s))
push(&s, x);
else
printf("\nStack is full...");
break;
case 2:
if (!empty(&s)) {
x = pop(&s);
printf("\nPopped value = %d", x);
} else
printf("\nStack is empty...");
break;
case 3:
print(&s);
break;
}
} while (op != 4);
return 0;
}
void init(stack *s) {
s->top = -1;
}
int empty(stack *s) {
return (s->top == -1);
}
int full(stack *s) {
return (s->top == MAX - 1);
}
void push(stack *s, int x) {
s->top++;
s->data[s->top] = x;
}
int pop(stack *s) {
int x = s->data[s->top];
s->top--;
return x;
}
void print(stack *s) {
int i;
printf("\nStack elements: ");
for (i = s->top; i >= 0; i--)
printf("%d ", s->data[i]);
printf("\n");
}
OUTPUT :
1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 1
Enter a number: 10
1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 1
Enter a number: 20
1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 2
Popped value = 20
1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 3
Stack elements: 10
1) Push
2) Pop
3) Print
4) Quit
Enter Your choice: 4
=== Code Execution Successful ===