Sunteți pe pagina 1din 2

Document: 3 stack.

//STACK WITH PUSH,POP,PEEP AND CHANGE OPEARTION

#include<stdio.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
struct stack
{
int s[5];
int top;
};

void push(struct stack *st1,int x1)


{
if(st1->top>=4)
{
printf("\n The stack is Overflow\n");
delay(2000);
exit(0);
}
else
{
st1->top=st1->top+1;
st1->s[st1->top]=x1;
}
}

void pop(struct stack *st1)


{
int x;

if(st1->top<=-1)
{
printf("\n The stack is Underflow\n");
delay(2000);
exit(0);
}
else
{
x=st1->s[st1->top];
st1->top=st1->top-1;
printf("The element popped is:%d\n",x);
}
}

void peep(struct stack *st1,int pos)


{
int x1;
x1=st1->s[st1->top-pos+1];
Page 1
Document: 3 stack.c

printf("The element retrived from %d position from top is


%d\n",pos,x1);
}

void change(struct stack *st1,int pos,int nval )


{
st1->s[st1->top-pos+1]=nval;
}
void main()
{
struct stack st;

int ch,x,nv,p;
st.top=-1;
clrscr();
while(1)
{
printf("\n Enter 1 for push, 2 for pop, 3 for peep, 4 for change,5 for
exit\n");

scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter x:");
scanf("%d",&x);
push(&st,x);
break;

case 2:
pop(&st);
break;

case 3:
printf("Enter position from top:\n");
scanf("%d",&p);
peep(&st,p);
break;

case 4:
printf("Enter position from top which value to change:\n");
scanf("%d",&p);
printf("Enter new value:\n");
scanf("%d\n",&nv);
change(&st,p,nv);
break;

case 5:
Page 2

S-ar putea să vă placă și