QUESTION: Implementation of Stack Using Array. Code
QUESTION: Implementation of Stack Using Array. Code
Code:-
#include<stdio.h>
#include<stdlib.h>
#define max 10
int stack[100],i,j,top=-1;
void push();
void pop();
void show();
int peek();
int isEmpty();
int isFull();
int main()
{
push();
pop();
show();
peek();
isEmpty();
isFull();
push();
}
void push()
{
if(top==max)
{
printf("overflow:\n");
}
else{
int no;
printf("enter number of elements to be pushed:\n");
scanf("%d",&no);
while(no>0){
top=top+1;
int val;
printf("enter the value which is to be inserted:");
scanf("%d",&val);
stack[top]=val;
no=no-1;
}
}
}
void pop()
{
int j;
printf("enter number of elements to be poped:\n");
scanf("%d",&j);
if(top==-1)
{
printf("underflow:\n");
}
else
{
while(j>0)
{
printf("deleted element is %d\n",stack[top]);
top=top-1;
j=j-1;
}
}
}
void show()
{
printf("now,elements in stacks:\n");
for(int i=top;i>=0;i--)
{
printf("%d\n",stack[i]);
}
if(top==-1)
{
printf("stack is empty:\n");
}
}
int peek(){
if(top==-1)
{
return -1;
}
else
{
printf("top in the stack =%d\n",stack[top]);
}
}
int isEmpty()
{
if(top==-1)
{
printf("yes,stack is empty:");
}
}
int isFull()
{
if(top==max-1)
{
printf("stack is full\n");
}
}
Output: