#include <stdio.h>
#include <stdlib.h>
#define size 5
int stack[size], top = -1;
int isFull()
{
if (top == size - 1)
{
return 1;
}
return 0;
}
int isEmpty()
{
if (top == -1)
{
return 1;
}
return 0;
}
void push(int ele)
{
if (isFull())
{
printf("\nStack overflow\n\n");
}
else
{
top++;
stack[top] = ele;
//Another way
// stack[++top] = ele;
}
}
int pop()
{
if (isEmpty())
{
printf("Stack is Empty\n");
}
else
{
return stack[top--];
}
}
void peek()
{
if (isEmpty())
{
printf("\nStack is empty\n\n");
}
else
{
printf("Peek Element is: %d", stack[top]);
}
}
void display()
{
if (isEmpty())
{
printf("\nStack is empty\n\n");
}
else
{
printf("Stack Element \n");
for (int i = 0; i <= top; i++)
{
printf(" %d ", stack[i]);
}
}
}
void pos_ele()
{
int i;
printf("Which location element you want to see: ");
scanf("%d", &i);
printf("%d", stack[i]);
}
int main()
{
int a, ele;
while (1)
{
printf("\n1. Push\n");
printf("2. Pop\n");
printf("3. Peek\n");
printf("4. Display\n");
printf("5. Exit\n");
printf("6. Find Position Element\n");
printf("Enter your choice: ");
scanf("%d", &a);
switch (a)
{
case 1:
printf("Enter the Element: ");
scanf("%d", &ele);
push(ele);
break;
case 2:
ele = pop();
if (ele == 0)
{
printf("stack is empty\\underflow\n");
}
else
{
printf("%d is popped", ele);
}
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
exit(0);
case 6:
pos_ele();
break;
default:
printf("Wrong Input!");
break;
}
}
return 0;
}
Comments
Post a Comment