# What is stack? What are the different applications of stack? Explain stack operations with example.

Your limit has been exceed. We have implemented this system because, We got difficulty on managing our servers. Please donate some amount to remove this limit.

Quota: 0 / 30

Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out).

Applications of stack:

• Balancing of symbols
• Infix to Postfix /Prefix conversion
• Redo-undo features at many places like editors, photoshop.
• Forward and backward feature in web browsers
• Used in many algorithms like Tower of Hanoi, tree traversals, stock span problem, histogram problem.
• Backtracking is one of the algorithm designing techniques. Some examples of backtracking are the Knight-Tour problem, N-Queen problem, find your way through a maze, and game-like chess or checkers in all these problems we dive into someway if that way is not efficient we come back to the previous state and go into some another path. To get back from a current state we need to store the previous state for that purpose we need a stack.
• In Graph Algorithms like Topological Sorting and Strongly Connected Components
• In Memory management, any modern computer uses a stack as the primary management for a running purpose. Each program that is running in a computer system has its own memory allocations
• String reversal is also another application of stack. Here one by one each character gets inserted into the stack. So the first character of the string is on the bottom of the stack and the last element of a string is on the top of the stack. After Performing the pop operations on the stack we get a string in reverse order.

Mainly the following two basic operations are performed in the stack:

1. PUSH Operation:

• The PUSH operation is used to insert or add elements in the stack
• When we add an item to stack, We say that we push into the stack
• The last item put into the stack is at top
• Top is incremented when PUSH operation occurs
• PUSH operation increases size of stack
• We check overflow condition during PUSH operation

Example:

void push(int data) {
if(!isFull()) {
top = top + 1;
stack[top] = data;
} else {
printf("Could not insert data, Stack is full.\n");
}
}

2. POP Operation:

• The POP operation is used to remove or delete elements from the stack
• When we delete an item to stack, We say that we pop from the stack
• When an item is popped, it is always the top item which is removed.
• Top is decremented when POP operation occurs
• POP operation decrease size of stack
• We check underflow condition during POP operation

Example:

int pop(int data) {

if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
} else {
printf("Could not retrieve data, Stack is empty.\n");
}
}