Stack
Stack
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.
• Other applications can be Backtracking, Knight tour problem, rat
in a maze, N queen problem and sudoku solver
• In Graph Algorithms like Topological Sorting and Strongly
Connected Components
Implementation:
There are two ways to implement a stack:
• Using array
• Using linked list
1. Increment the variable Top so that it can now refere to the next
memory location.
2. Add element at the position of incremented top. This is referred
to as adding new element at the top of the stack.
Algorithm:
1. begin
2. if top = n then stack full
3. top = top + 1
4. stack (top) : = item;
5. end
Algorithm :
1. begin
2. if top = 0 then stack empty;
3. item := stack(top);
4. top = top - 1;
5. end;