Sie sind auf Seite 1von 2

/* implementation of stack operation */

#include<stdio.h>
#include<conio.h>
void push(int b[ ]);
void pop(int b[ ]);
void peek(int b[ ]);
#define MAXSIZE 10
int top=-1;
void main()
{
int a[MAXSIZE],ch;
do
{
printf("enter your choice 1 push,2 pop, 3 peek, 4 exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
push(a);
break;
case 2:
pop(a);
break;
case 3:
peek(a);
break;
}
} while(ch!=4);
getch();
}
void push(int b[ ])
{
int item;
if(top==MAXSIZE-1)
{
printf("overflow");
}

else
{
printf("enter new item");
scanf("%d",&item);
top=top+1;
b[top]=item;
}
}
void pop(int b[])
{
int item;
if(top==-1)
{
printf("underflow");
}
else
{
item=b[top];
top=top-1;
printf("item removed=%d",item);
}
}
void peek(int b[])
{
int item;
if(top==-1)
{
printf("underflow");
}
else
{
item=b[top];
printf("item at top=%d",item);
}
}

Das könnte Ihnen auch gefallen