Program to implement Stack using structures

Stack is a Last In First Out linear data structure.  It can be implemented using structures.  An example to illustrate how Stacks can be implemented using structures is given below:

/*Program to implement Stack using structures*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#define MAX 10

struct stack{
int top;
int arr[MAX];
};

/*Push to insert an element at the top of the stack */
void push(struct stack *, int);

/* Pop to return the top-most element of the stack */
int pop(struct stack *);

/* Display the contents of stack */
void display(struct stack *);

int main(){
/* stack object created */
struct stack s;
int ch, num;
s.top = -1;
while(1){
printf(“nStack-Menu”);
printf(“n1. Pushn2. Popn3. Displayn4. Exit”);
printf(“nEnter your choice?”);
scanf(“%d”,&ch);
switch(ch){
case 1:
printf(“Enter number to push?”);
scanf(“%d”,&num);
push(&s,num);
break;
case 2:
num = pop(&s);
printf(“Popped element = %d”,num);
break;
case 3:
display(&s);
break;
case 4:
exit(0);
default:
printf(“Invalid choice!”);
}
getch();
}
}

void push(struct stack *s, int x){
if(s->top==MAX-1){
printf(“Stack full! Overflow.”);
return;
}
s->arr[++(s->top)]=x;
}

int pop(struct stack *s){
if(s->top==-1){
printf(“Stack empty! Underflow.”);
return -1;
}
return(s->arr[s->top–]);
}

void display(struct stack *s){
int t;
if(s->top==-1){
printf(“Stack empty!”);
return;
}
/*assigning top to another variable*/
for(t=s->top;t>=0;t–)
printf(“%dt”,s->arr[t]);
}