Page 48 - Data Structures Interactive Book
P. 48
the stack is empty when the top pointer is NULL. These checks prevent invalid operations and
ensure program stability.
5.4 Applications in Computer Science
Stacks are widely used in computer science because of their simple yet powerful
behavior. They appear in many algorithms and system-level processes.
5.4.1 Expression Evaluation (Postfix, Prefix, Infix)
Stacks are essential for evaluating and converting mathematical expressions. For
example, converting an infix expression (like A + B * C) into postfix (A B C * +) requires a stack
to temporarily hold operators. During evaluation, operands are pushed onto the stack, and
operators are applied in the correct order, ensuring accurate results.
5.4.2 Balanced Parentheses Checking
Stacks are used to verify whether parentheses, brackets, or braces in an expression
are properly balanced. Each opening symbol is pushed onto the stack, and each closing symbol
checks the top of the stack. If the symbols match, the stack is popped; otherwise, the
expression is invalid. This principle is widely used in compilers and interpreters.
5.4.3 Function Call Management (Recursion)
Recursion relies heavily on stacks. Each function call is pushed onto the call stack with
its local variables and return address. When the function completes, it is popped from the
stack, and control returns to the previous function. This mechanism allows recursive
algorithms to work correctly, such as computing factorials or traversing trees.
5.5 Limitations of Stacks
Despite their usefulness, stacks have limitations. Array-based stacks suffer from fixed
size, which can lead to overflow if too many elements are pushed. Linked list-based stacks
48

