The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Stack - Push

The push operation is used to insert an element into the stack. The new element is added to the top most position of the stack.

C program - Push Operation in Stack

Here is the program to demonstrate push operation in stack.

stack-push.c
#include <stdio.h>
#include <malloc.h>
#define MAX 10
int stack[MAX], top = -1;
void push(int stack[], int val);
void display(int stack[]);
int main()
{
int val, choice;
do {
printf("\n 1. PUSH");
printf("\n 2. DISPLAY");
printf("\n 3. EXIT");
printf("\n Enter your option : ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\n Enter the number to be pushed on stack : ");
scanf("%d",&val);
push(stack, val);
break;
case 2:
display(stack);
break;
}
} while(choice != 3);
return 0;
}
void push(int stack[], int val)
{
if(top == MAX-1)
{
printf("\n STACK OVERFLOW");
}
else {
top++;
stack[top] = val;
}
}
void display(int stack[])
{
int i;
if(top == -1)
printf("\n STACK IS EMPTY");
else {
for(i = top;i >= 0;i--)
printf("\n%d",stack[i]);
}
}
1. PUSH
2. DISPLAY
3. EXIT
Enter your option : 1
Enter the number to be pushed on stack : 5

1. PUSH
2. DISPLAY
3. EXIT
Enter your option : 1
Enter the number to be pushed on stack : 1 

1. PUSH
2. DISPLAY
3. EXIT
Enter your option : 1
Enter the number to be pushed on stack : 2

1. PUSH
2. DISPLAY
3. EXIT
Enter your option : 2
2
1
5

1. PUSH
2. DISPLAY
3. EXIT
Enter your option : 3

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report