C Program: Stack Implementation Using Array
#include<stdio.h>
#include<stdlib.h>
#include <stdbool.h>
struct stack {
int data[20];
int top;
};
void intialise(struct stack*val){
val->top= -1;
}
bool isfull(struct stack*val){
if(val->top==19){
return true;
}
else{
return false;
}
}
bool isempty(struct stack*val){
if(val->top==-1){
return true;
}
else
return false;
}
void push(struct stack *val, int element) {
if (isfull(val)) {
printf("Stack overflowed\n");
} else {
val->top++;
val->data[val->top] = element;
}
}
void pop(struct stack *val) {
if (isempty(val)) {
printf("Stack underflowed\n");
} else {
printf("Popped element: %d\n", val->data[val->top]);
val->top--;
}
}
int display(struct stack*val){
if(val->top>-1){
for(int i=0;i<val->top;i++){
printf("%d",val->data[i]);
}
}
return 0;
}
int main(){
struct stack val;
intialise(&val);
int x,y,z;
printf("Enter the value of x,y,z");
scanf("%d %d %d",&x,&y,&z);
push(&val,x);
push(&val,y);
push(&val,z);
printf("\n");
display(&val);
if(isfull(&val)){
printf("Stack is full");
}
else{
printf("Stack is not full");
}
printf("\n");
pop(&val);
display(&val);
}