Implementation of Stack with the help of Array:-
Method 1 (Simple)
#include
#define max 10
void push();
void pop();
void display();
void peek();
int top=-1, stack[max];
int main(){
int ch;
while(1){
printf("\n press1 for push:-");
printf("\n press2 for pop:-");
printf("\n press3 for peek:-");
printf("\n press4 for Display:-");
printf("\n Enter your choice:");
scanf("%d",&ch);
switch(ch){
case 1: push();
break;
case 2: pop();
break;
case 3: peek();
break;
case 4: display();
break;
default: printf("\n Wrong choice");
break;
}
}
return 0;}
void push(){
int item;
if(top==max-1)
printf("\n stack is overflow");
else{
printf("\n Enter the new insert item:-");
scanf("%d",&item);
top=top+1;
stack[top]=item;
}
}
void pop(){
int item;
if(top==-1)
printf("\n stack is underflow");
else{
item=stack[top];
printf("\n remove item from stack is :%d",item);
top=top-1;
}
}
void peek(){
int item;
if(top==-1){
printf("\nstack is underflow");
}
else{
item=stack[top];
printf("\n The top most element of the stack is :%d",item);
}
}
void display(){
int i;
if(top==-1){
printf("stack is Empty");
}
else{
printf("\n The elements of the Stack are:");
for(i=top; i>=0; i--){
printf("%d\t",stack[i]);
}
}
}
Output
press1 for push:-
press2 for pop:-
press3 for peek:-
press4 for Display:-
Enter your choice:1
Enter the new insert item:-10
press1 for push:-
press2 for pop:-
press3 for peek:-
press4 for Display:-
Enter your choice:2
remove item from stack is :10
press1 for push:-
press2 for pop:-
press3 for peek:-
press4 for Display:-
Enter your choice: