Unit 4Stacks
Follows IGNOU MCS-208, Unit 4
A stack is a list in which items are added and removed at one end only, so the last item in is the first item out. This simple rule turns out to be exactly what is needed to undo actions, check brackets, evaluate arithmetic expressions and run function calls, which is why every processor and every programming language has a stack built in. This unit defines the stack ADT, implements it with an array and with a linked list, shows how several stacks can share one array, and works through its classic applications.
In this unit you will learn to
- explain the stack ADT and the LIFO rule, and trace push and pop operations;
- implement a stack with an array and with a linked list, with overflow and underflow checks;
- compare the two implementations;
- store two or more stacks in one array and handle their boundaries;
- convert infix expressions to postfix (and prefix) form with a stack;
- evaluate postfix expressions with a stack;
- use stacks to check brackets and explain how they support function calls and recursion.
Sections in this unit
- 4.1The Stack ADTWhat a stack is, the LIFO rule, the push, pop, peek and empty operations, overflow and underflow, and how to trace a sequence of operations.9 min
- 4.2Stack Implementation Using ArraysStoring a stack in an array with a top index, the push and pop algorithms with overflow and underflow checks, and a corrected Program 4.1.13 min
- 4.3Stack Implementation Using Linked ListsA linked stack that pushes and pops at the head of a singly linked list, why it has no fixed size, and a corrected Program 4.2.11 min
- 4.4Multiple Stacks in One ArraySharing one array between two stacks that grow towards each other, and dividing an array into n segments with bottom and top indices.15 min
- 4.5Expression Notations and Infix to Postfix ConversionInfix, prefix and postfix notation, operator precedence and associativity, and the stack algorithm that converts infix to postfix.14 min
- 4.6Evaluating Postfix ExpressionsEvaluating a postfix expression with an operand stack in one left-to-right pass, including operand order and error detection.8 min
- 4.7More Applications: Brackets, Function Calls and RecursionChecking balanced brackets with a stack, how the call stack runs functions and recursion, and other everyday uses such as undo and backtracking.10 min