/* Stack using array*/
#include<stdio.h>
#define size 5
struct stack
{
int top,S[size];
}st;
int stfull()
{
if(st.top==size-1)
{
return 1;
}
else
{
return 0;
}
}
int stempty()
{
if(st.top==-1)
{
return 1;
}
else
{
return 0;
}
}
void push(int value)
{
st.top++;
st.S[st.top]=value;
}
void pop()
{
int r;
r=st.S[st.top];
st.top--;
}
void peep()
{
printf("\nTop element is %d\n",st.S[st.top]);
}
void display()
{
int i;
printf("\n");
for(i=st.top;i>=0;i--)
{
printf("%d\n",st.S[i]);
}
}
void main()
{
int choice,data;
st.top=-1;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit\nYour choice:");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
if(stfull())
{
printf("\nStack is Full\n");
}
else
{
printf("\nEnter value:");
scanf("%d",&data);
push(data);
}
break;
case 2:
if(stempty())
{
printf("\nStack is Empty\n");
}
else
{
pop();
}
break;
case 3:peep();
break;
case 4:if(stempty())
{
printf("\nAdd at least one element\n");
}
else
{
display();
}
break;
case 5:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
}
#include<stdio.h>
#define size 5
struct stack
{
int top,S[size];
}st;
int stfull()
{
if(st.top==size-1)
{
return 1;
}
else
{
return 0;
}
}
int stempty()
{
if(st.top==-1)
{
return 1;
}
else
{
return 0;
}
}
void push(int value)
{
st.top++;
st.S[st.top]=value;
}
void pop()
{
int r;
r=st.S[st.top];
st.top--;
}
void peep()
{
printf("\nTop element is %d\n",st.S[st.top]);
}
void display()
{
int i;
printf("\n");
for(i=st.top;i>=0;i--)
{
printf("%d\n",st.S[i]);
}
}
void main()
{
int choice,data;
st.top=-1;
while(1)
{
printf("\n1.Push\n2.Pop\n3.Peep\n4.Display\n5.Exit\nYour choice:");
scanf("\n%d",&choice);
switch(choice)
{
case 1:
if(stfull())
{
printf("\nStack is Full\n");
}
else
{
printf("\nEnter value:");
scanf("%d",&data);
push(data);
}
break;
case 2:
if(stempty())
{
printf("\nStack is Empty\n");
}
else
{
pop();
}
break;
case 3:peep();
break;
case 4:if(stempty())
{
printf("\nAdd at least one element\n");
}
else
{
display();
}
break;
case 5:exit(0);
break;
default:printf("\nEnter proper choice\n");
}
}
}
No comments:
Post a Comment