# Stack

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).

Note:-

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.

Example:-

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

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.