/*Stack Operations using Arrays.*/
struct stack
{
int s[10];
int top;
};
main()
{
int ch,ele;
struct stack s1;
s1.top=-1;
clrscr();
while(1)
{
printf("\nSTACK OPERATIONS\n");
printf("1.PUSH\n");
printf("2.POP\n");
printf("3.DISPLAY\n");
printf("4.EXIT\n");
printf("Enter your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Enter the element you want to push : ");
scanf("%d",&ele);
push(&s1,ele);
break;
case 2: ele=pop(&s1);
if(ele==-1)
printf("Stack Underflow. No elements to pop");
else
printf("The popped element is %d",ele);
break;
case 3: display(s1);
break;
case 4: exit();
}
}
}
push(struct stack *s1,int ele)
{
if(s1->top==9)
printf("Stack Overflow. Cannot push");
else
s1->s[++(s1->top)]=ele;
}
pop(struct stack *s1)
{
if(s1->top==-1)
return -1;
else
return s1->s[(s1->top)--];
}
display(struct stack s1)
{
if(s1.top==-1)
printf("Stack Empty. Nothing to display.");
else
while(s1.top!=-1)
printf(" %d ",s1.s[s1.top--]);
}
No comments:
Post a Comment