Stack program in c: To implement stack data structure in c programming.

What is the stack?
The stack is a linear type of Data Structure. It works on a FIRST IN LAST OUT or LAST IN FIRST OUT fashion.
Stack Example
Consider a container or CD’S that is we put CD’S on one another and if we want to take CD’S out then we have to remove the CD which is at the topmost after that the second one and so on.

The operation performed on a stack
Stack implementation using the array
1. Create: This operation is to create an empty stack it is the first step.
2. Push: The push operation in a stack means to insert or to add the element to stack.
3. Pop: The pop operation in a stack means to delete or remove the element from a stack.

Note:- The top is a pointer that points to a top of a stack and initially it is a pointing to – 1.

Algorithm for Stack program in c

1. Write a function to push element
2. write a function to pop element
3 write a function to display the stack element.

Stack program in c using array

#include <stdio.h>
#include <stdlib.h>

int stack[5];
void push();
int pop();
void traverse();
int is_empty();
int top_element();
int top = 0;

int main()
int element, choice;

for (;;)
printf("Stack Operations.\n");
printf("1. Insert into stack (Push operation).\n");
printf("2. Delete from stack (Pop operation).\n");
printf("3. Print top element of stack.\n");
printf("4. Check if stack is empty.\n");
printf("5. Traverse stack.\n");
printf("6. Exit.\n");
printf("Enter your choice.\n");

switch (choice)
case 1:
if (top == 5)
printf("Error: Overflow\n\n");
else {
printf("Enter the value to insert.\n");
scanf("%d", &element);

case 2:
if (top == 0)
printf("Error: Underflow.\n\n");
else {
element = pop();
printf("Element removed from stack is %d.\n", element);

case 3:
if (!is_empty()) {
element = top_element();
printf("Element at the top of stack is %d\n\n", element);
printf("Stack is empty.\n\n");

case 4:
if (is_empty())
printf("Stack is empty.\n\n");
printf("Stack is not empty.\n\n");

case 5:

case 6:

void push(int value) {
stack[top] = value;

int pop() {
return stack[top];

void traverse() {
int d;

if (top == 0) {
printf("Stack is empty.\n\n");

printf("There are %d elements in stack.\n", top);

for (d = top - 1; d >= 0; d--)
printf("%d\n", stack[d]);

int is_empty() {
if (top == 0)
return 1;
return 0;

int top_element() {
return stack[top-1];

Output :

Stack program in c

Write A Comment