Implementation of stack using C program
#include <stdio.h>
#include <stdlib.h>
#define size 3
struct Stack
{
int stack[size];
};
struct Stack s;
void push();
void pop();
int isFull();
int isEmpty();
void peek();
void display();
int top = -1, data;
int main()
{
int choice;
while (1)
{
printf("1. push.\n");
printf("2. pop.\n");
printf("3. Dsiplay all.\n");
printf("4. peek.\n");
printf("5. Exit.\n");
printf("Enter your choice :");
scanf("%d", &choice);
switch (choice)
{
case 1:
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
peek();
break;
case 5:
exit(0);
default:
printf("Invalid choice\n");
break;
}
}
return 0;
}
void push()
{
if (isFull())
{
printf("Stack is Full!!!\n\n");
}
else
{
printf("Enter a data to push into stack: ");
scanf("%d", &data);
++top;
s.stack[top]=data;
}
}
void pop()
{
if (isEmpty())
{
printf("Stack is Empty!!!\n\n");
}
else
{
printf("%d is poped.\n\n\n", s.stack[top]);
top--;
}
}
int isFull(){
if(top==size-1){
return 1;
}else
{
return 0;
}
}
int isEmpty(){
if(top==-1){
return 1;
}else
{
return 0;
}
}
void peek(){
if (isEmpty())
{
printf("Stack is empty!!\n");
}else
{
printf("%d is peeked .\n\n\n", s.stack[top]);
}
}
void display(){
if (isEmpty())
{
printf("Stack is empty!!\n");
}else{
printf("All data:\n");
for (int i = 0; i <= top; i++)
{
printf("%d ", s.stack[i]);
}
printf("\n\n");
}
}
OUTPUT: