Monday, 22 October 2012

pushed and poped in stack


/*A program for creation of stack in which first 3 items are
pushed and then poped of it*/


#include<stdio.h>
#include<stdlib.h>
#define hs 10

struct stack
{
char home[hs];
int top;
};

void push(struct stack *, char);
char pop(struct stack *);

void main()
{
struct stack st;
st.top=-1;
char x;
push(&st,'A');
printf("\n first item inserted is A\n");
push(&st,'B');
printf("\n 2nd item inserted is B \n");
push(&st,'C');
printf("\n 3rd item inserted is C \n");

x=pop(&st);
printf("\n first item poped is %c ", x);
x=pop(&st);
printf("\n 2nd item poped is %c ", x);
x=pop(&st);
printf("\n 3rd item poped is %c ", x);

} //end of main

void push(struct stack *p, char x)
{
if(p->top==hs-1)
{
printf("\n overflow\n");
exit(1);
}
p->home[++(p->top)]=x;
}

char pop(struct stack * p)
{
if(p->top==-1)
{
printf("\nunderflow\n");
exit(1);
}

char ch=p->home[(p->top)--];
return ch;
}


output is ......


No comments:

Post a Comment