0% found this document useful (0 votes)
15 views

Stack Using Arrays

Uploaded by

eloziiopfoze
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Stack Using Arrays

Uploaded by

eloziiopfoze
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#define MAX 10

int top=-1;
int stack[MAX];
int full, empty=1;

void print(void);
void push(int);
int pop(void);

void main(void)
{
int item, ch=1;
clrscr();

while(ch)
{
printf("\n1. Push Item");
printf("\n\n2. Pop Item");
printf("\n\n3. Print Stack");
printf("\n\n4. Exit");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Enter the item to push");
scanf("%d",&item);
push(item);
break;
case 2:
item=pop();
printf("Poped item is %d", item);
break;
case 3:
print();
break;
case 4:
break;
}
scanf("%d",&ch);
}
}
void push(int x)
{
if(full)
{
printf("Stack is full\n");
return;
}
else
{
top=top+1;
stack[top]=x;
empty=0;
}
if(top==MAX)
full=1;
return;
}

int pop(void)
{
int x;
if(empty)
{
return -1;
}
else
{
x=stack[top];
top--;
if(top==-1)
empty=1;
return x;
}
}
void print(void)
{
int i;
if(empty)
{
printf("Stack is empty");
return;
}
else
{
for(i=0; i<=top; i++)
printf("%d, ",stack[i]);
return;
}
}

You might also like