Data structures : Stack using array program



#include
#define max 10
struct stack
{
int data[max];
int top;
};
struct stack s;
main()
{
s.top=-1;
int choice;
int isfull();
int isempty();
void push();
int pop();
void display();
do
{
printf("\n1-Add element 2-Delete element 3-Display element 4-Exit\nEnter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: push(); break;
case 2: printf("\nThe deleted element is %d",pop()); break;
case 3: display();
}
}while(choice!=4);
}
int isempty()
{
if(s.top==-1)
return 1;
else
return 0;
}
int isfull()
{
if(s.top==max-1)
return 1;
else
return 0;
}
void push()
{
int value;
printf("\nEnter the value... ");
scanf("%d",&value);
if(s.top{
s.top+=1;
s.data[s.top]=value;
printf("\n# Value Inserted #");
}
else
printf("\nStack Overflow!");
}
int pop()
{
int x;
if (isempty())
{
printf("\nStack empty!");
return -1;
}
else
{
x=s.data[s.top];
s.top-=1;
return x;
}
}
void display()
{
int i;
if (isempty())
printf("\nStack is empty!");
else
{
printf("The elements in the stack are...\n");
for(i=0;i<=s.top;i++)
printf("%d\n",s.data[i]);
printf("\nThe number of elements in the stack are %d",i);
}
}


No comments: