/* Implement Stack Using Array In C */
#include<stdio.h>
#include<stdlib.h>
#define MAX 10
struct stack
{
int arr[MAX];
int top;
};
void initstack(struct stack*);
void push(struct stack *s,int item);
int pop(struct stack *s);
int main()
{
struct stack s;
int i,item;
int ch;
initstack(&s);
while(1)
{
printf("Enter 1. for Push\n");
printf("Enter 2. for pop\n");
printf("Enter 3. for exit.\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter item:");
scanf("%d",&item);
push(&s,item);
break;
case 2:
i=pop(&s);
printf("item poped: %d\n",i);
break;
case 3:
exit(1);
break;
default:
printf("Wrong Input\n");
}
}
return 0;
}
void initstack(struct stack *s)
{
s->top=-1;
}
void push(struct stack *s,int item)
{
if(s->top==MAX-1)
{
printf("stack is full\n");
}
else
{
s->top++;
s->arr[s->top]=item;
}
}
int pop(struct stack *s)
{
int data;
if(s->top==-1)
{
printf("Stack is empty.\n");
exit(1);
}
else
{
data=s->arr[s->top];
s->top--;
return data;
}
}
No comments:
Post a Comment