Stack is linear type of Data Structure.

Stack works on a FIRST IN LAST OUT  or LAST IN FIRST OUT fashion.

Lets see example stack meaning

Stack Example

Consider an 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 top most after that at second one and so on.

This illustration state it works as a stack or how stack works.

Operation performed on stack

1. Create

This operation is to create a empty stack it is a first step.

2. Push

The push operation in stack means to insert or to add element to stack.

3. Pop

The pop operation in stack means to delete or remove element from a stack.

4. Top

To return the top most element in a stack.

5. Empty

This operation is to check whether stack is empty or not.

6. Destroy

This operation is to destroy stack (I destroy the complete stack permanently).


The top is a pointer which point to a top of a stack and initially it is a pointing to – 1.

  1. New element added at top (operation push).
  2. Element deleted from top (operation pop).

The push, pop are main operations of stack.


Consider, stack with size = 3. initially top = -1.

1. Push: add value 10 to stack

2. Push: add value 20 to stack

3. Push: add value 30 to stack

4. Push: add value 40 to stack but our stack is full as (top=size-1) stack is full.

5. Pop: Delete value 30

6. Pop: Delete value 20

6. Pop: Delete value 10

7. Pop: Now, top==-1 therefore stack is empty.

This whole example illustrate that how stack work step by step.

Application of stack

  1. It is used to convert infix to postfix.
  2. To solve expressions.
  3. It is used to programming language where temporary variable stored on Stack.

Stack algorithm 

1. Write function to push element

2. write function to pop element

3 write function to display stack element.

Program for stack implementation using array

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109#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”);scanf(“%d”,&choice);switch (choice){case 1:if (top == 5)printf(“Error: Overflow\n\n”);else {printf(“Enter the value to insert.\n”);scanf(“%d”, &element);push(element);}break;case 2:if (top == 0)printf(“Error: Underflow.\n\n”);else {element = pop();printf(“Element removed from stack is %d.\n”, element);}break;case 3:if (!is_empty()) {element = top_element();printf(“Element at the top of stack is %d\n\n”, element);}elseprintf(“Stack is empty.\n\n”);break;case 4:if (is_empty())printf(“Stack is empty.\n\n”);elseprintf(“Stack is not empty.\n\n”);break;case 5:traverse();break;case 6:exit(0);}}}void push(int value) {stack[top] = value;top++;}int pop() {top–;return stack[top];}void traverse() {int d;if (top == 0) {printf(“Stack is empty.\n\n”);return;}printf(“There are %d elements in stack.\n”, top);for (d = top – 1; d >= 0; d–)printf(“%d\n”, stack[d]);printf(“\n”);}int is_empty() {if (top == 0)return 1;elsereturn 0;}int top_element() {return stack[top-1];}

Output :


Explanation :

1. First  we write a main function in which we display a menu that is operation performed on stack.

2. write a function to push element in stack.

3. Next we write a function to pop element from stack.

4. same we write function to display the elements in stack.

5. we take a input choice from user

6. and make a call to function user want to execute.