2077 1

CSC211 ยท 2077 ยท 10 marks

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

What is stack? What are the different applications of stack? Explain stack operations with example.[10]

Data Structures and AlgorithmsChapter 2: StackBasic Concept of Stack, Stack as an ADT, Stack Operations, S

Quick answer

A stack is a linear data structure in which an element may be inserted or deleted only at one end (called the top of the stack). Elements are removed from a stack in the reverse order of that in which they were inserted.

Stack: Definition, Applications, and Operations

1. What is a Stack?

Stack follows the principle of LIFO (Last In First Out), meaning the last element inserted is the first one to be removed.

Stack uses a variable called top which always points to the topmost element in the stack.

Example: Consider a stack of plates. You can only add or remove a plate from the top.

     |  30  |  <-- top
     |  20  |
     |  10  |
     |______|

2. Applications of Stack

#ApplicationDescription
1Expression EvaluationUsed to evaluate arithmetic expressions (postfix/prefix)
2Infix to Postfix ConversionOperators are pushed/popped using stack based on precedence
3Function Call ManagementSystem uses call stack to manage function calls and returns
4RecursionEach recursive call is stored on the stack until base case is reached
5Undo/Redo OperationsText editors use stack to undo or redo operations
6BacktrackingUsed in maze solving, puzzle solving algorithms
7Syntax ParsingCompilers use stack to check balanced parentheses and syntax
8Browser HistoryBack button in browsers uses stack to track visited pages

3. Stack Operations

There are two primary operations on a stack:

a) PUSH Operation

The PUSH operation is used to add or insert an element into the stack.

  • The element is always added at the top of the stack.
  • Top is incremented when PUSH operation occurs.
  • Before pushing, we check for Overflow condition (stack is full).

Overflow Condition: If top == MAX - 1, the stack is full and no more elements can be pushed.

Algorithm for PUSH:

Algorithm PUSH(stack, top, MAX, item):
  Step 1: If top == MAX - 1 then
              Print "Stack Overflow"
              Return
  Step 2: top = top + 1
  Step 3: stack[top] = item
  Step 4: Return

Example of PUSH:

Push elements 10, 20, 30 one by one:

Initial Stack:        After PUSH(10):     After PUSH(20):     After PUSH(30):
  top = -1              top = 0             top = 1             top = 2
  |      |              |  10  |            |  20  |            |  30  | <-- top
  |      |              |      |            |  10  |            |  20  |
  |      |              |      |            |      |            |  10  |
  |______|              |______|            |______|            |______|

b) POP Operation

The POP operation is used to delete or remove an element from the stack.

  • The element is always removed from the top of the stack.
  • Top is decremented when POP operation occurs.
  • Before popping, we check for Underflow condition (stack is empty).

Underflow Condition: If top == -1, the stack is empty and no element can be popped.

Algorithm for POP:

Algorithm POP(stack, top):
  Step 1: If top == -1 then
              Print "Stack Underflow"
              Return
  Step 2: item = stack[top]
  Step 3: top = top - 1
  Step 4: Return item

Example of POP:

Pop elements from the stack containing 30, 20, 10:

Initial Stack:        After POP():        After POP():        After POP():
  top = 2               top = 1             top = 0             top = -1
  |  30  | <-- top      |  20  | <-- top    |  10  | <-- top    |      |
  |  20  |              |  10  |            |      |            |      |
  |  10  |              |      |            |      |            |      |
  |______|              |______|            |______|            |______|
  Popped: 30            Popped: 20          Popped: 10          Stack Empty

c) PEEK / Top Operation

Returns the topmost element without removing it.

Algorithm PEEK(stack, top):
  Step 1: If top == -1 then
              Print "Stack is Empty"
              Return
  Step 2: Return stack[top]

4. Complete Example: Infix to Postfix Conversion Using Stack

Convert infix expression (A + B / C * D + E) - F to postfix using stack.

The precedence rules used:

  • ^ or $ -- Highest (Level 1)
  • *, / -- Medium (Level 2)
  • +, - -- Lowest (Level 3)
Symbol ScannedStackPostfix
((
A(A
+( +A
B( +A B
/( + /A B
C( + /A B C
*( + *A B C /
D( + *A B C / D
+( +A B C / D * +
E( +A B C / D * + E
)EmptyA B C / D * + E +
--A B C / D * + E +
F-A B C / D * + E + F
EndEmptyA B C / D * + E + F -

When the second + is scanned, the * already on the stack and the + below it both have precedence at least as high as the incoming +, so both are popped to the output before the new + is pushed. Emptying the stack at the end gives

Postfix: A B C / D * + E + F -

The stack made this conversion possible because operators must be held back until an operator of lower or equal precedence, or a closing parenthesis, appears, and a stack returns them in exactly the reverse order in which they were held.


5. Conclusion

A stack is a linear structure restricted to a single access point, the top, which gives it LIFO behaviour and makes PUSH, POP and PEEK all constant time operations once the overflow and underflow conditions have been checked. That single restriction is precisely what makes it useful: function calls and recursion unwind in reverse order of their invocation, undo history is replayed backwards, backtracking abandons the most recent choice first, and expression conversion and evaluation need operators released in reverse order of their being held. Whether it is implemented over an array with an integer top or over a linked list with a head pointer, the behaviour seen by the user of the ADT stays the same.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.