Skip to content

Instantly share code, notes, and snippets.

@KishorJena
Created September 1, 2018 07:57
Show Gist options
  • Save KishorJena/17ea1e47c4c186ce9f1e71c5b39dbd9f to your computer and use it in GitHub Desktop.
Save KishorJena/17ea1e47c4c186ce9f1e71c5b39dbd9f to your computer and use it in GitHub Desktop.
#include<stdio.h>
#include<conio.h>
#define max 5
int stack[max],i,flag;
int top=-1;
int Peek();
int isEmpty();
int isFull();
void Push();
void Pop();
void Show();
void main(){
int ch=4;
clrscr();
do{
switch(ch)
{
case 1: Push();
break;
case 2: Pop();
break;
case 3: Show();
break;
}
printf("\nEnter Choice 1:Push 2:Pop 3:Show 0:Exit ");
scanf("%d",&ch);
}while(ch!=0);
}
int Peek()
{
int tp=top;
return tp;
}
int isFull()
{
int flag;
if(top==max)
flag=1;
else
flag=0;
return flag;
}
int isEmpty()
{
if(top<0)
return 1;
else
return 0;
}
void Push()
{
if(top>=max-1) // Doubt: if top>max-1 loop takes up to elements...
{
printf("\n\tOverflow");
}
else{
// for(i=0;i<=max-1;i++)
// {
top++;
printf("\nEnter element - ");
scanf("%d",&stack[top]);
// }
printf("\tPushed!");
}
}
void Pop()
{
if(top<0)
printf("\n\tUnderflow!");
else
{
top--;
printf("\n\tPopped!");
}
}
void Show()
{ if(top<0)
{
printf("\nPeek : NA");
flag=1;
}
else
{
printf("\nPeek : %d",stack[top]);
}
printf("\nContent : ");
if(flag=1)
{
for(i=top; i>=0; i--)
{
printf(" %d",stack[i] );
}
}
else
{
printf(" NA");
}
// printf(" Empty");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment