Generic selectors
Exact matches only
Search in title
Search in content
Post Type Selectors

Array implementation of Stack

include

int stack[100],n,selection,top=-1,i;
int push()
{
int element;
if(top==n)
{
printf(“Stack overflow”);
}
else
{
printf(“Enter element to push”);
scanf(“%d”,&element);
top = top + 1;
stack[top] = element;
}
return 0;
}
int pop()
{
if(top==-1)
{
printf(“Stack is empty, cant pop”);
}
else
{
top = top-1;
}
return 0;
}
int showElement()
{
for(i=top;i>=0;i=i-1)
{
printf(“%d”,stack[i]);
}
if(top==-1)
{
printf(“\nStack is empty, no element to show”);
}
return 0;
}
int main()
{
printf(“\nEnter number of elements in stack”);
scanf(“%d”,&n);

while(selection!=4)
{

printf("\nWhat to do push=1, pop=2, showElements=3, enter you number");
scanf("%d",&selection);

switch(selection)
{
    case 1:
        {
            push();
            break;
        }
    case 2:
        {
            pop();
            break;
        }
    case 3:
        {
            showElement();
            break;
        }
    case 4:
        {
            printf("\nClosing");
            break;
        }
    default:
        {
            printf("\nYou entered wrong selection number");
        }
};

}
return 0;
}

Output: