F How to Implement Stack using an integer array using C? | CodeTheta

How to Implement Stack using an integer array using C?

April 17, 2014

This program implement a stack whose job is push and pop in array elements.

/* http://native-code.blogspot.com */

#include<stdio.h>
#include<conio.h>
#define MAX 20
int s[MAX];
int top=-1;
void push(int);
void display(int[]);
int pop();
void main()
{
int ch, x, p;
while(1)
{
printf("/* http://native-code.blogspot.com */");
printf("\n\n1: Push\n2: Pop\n3: Display\n4: EXIT\n");
scanf("%d", &ch);
switch(ch)
{
case 1:
if(top==MAX-1)
printf("Stack overflow");
else
{
printf("Enter element to push:\n");
scanf("%d", &x);
push(x);
}
break;
case 2:
if(top==-1)
printf("\nStack underflow");
else
{
p=pop();
printf("Popped no is: %d\n", p);
}
break;
case 3:
display(s);
printf("\n");
break;
case 4:
exit(0);
default:
printf("Wrong choice.\n");
}
}
getch();
}
void push(int x)
{
top++;
s[top]=x;
}
int pop()
{
int temp;
temp=s[top];
top--;
return(temp);
}
void display()
{
int i;
for(i=0; i<=top; i++)
printf("%d ", s[i]);
}

/* http://native-code.blogspot.com */

Post a Comment