2080 1

BIT201 ยท 2080 ยท 10 marks

What is stack? Explain different stack operations. Explain algorithm to evaluate postfix expression.

What is stack? Explain different stack operations. Explain algorithm to evaluate postfix expression.[10]

Data Structures and AlgorithmsChapter 3: Linear Data Structures: Stacks and QueuesPostfix expression evaluation algorithm

Quick answer

A stack is a linear data structure that follows the LIFO (Last In, First Out) principle. This means the element inserted last is the first one to be removed.

Stack: Definition, Operations, and Postfix Evaluation


1. What is a Stack?

A stack can be visualized as a pile of plates: you add a plate on top and also remove from the top.

Key characteristics:

  • All insertions and deletions occur at one end called the TOP
  • It is an ordered collection of elements
  • It can be implemented using arrays or linked lists

2. Stack Operations

The following are the primary operations performed on a stack:

a) PUSH (Insertion)

Adds an element to the top of the stack.

Algorithm: PUSH(STACK, TOP, MAXSIZE, ITEM)

1. If TOP = MAXSIZE - 1, then:
      Print "Stack Overflow" and EXIT
2. Set TOP = TOP + 1
3. Set STACK[TOP] = ITEM
4. Return

b) POP (Deletion)

Removes and returns the top element from the stack.

Algorithm: POP(STACK, TOP, ITEM)

1. If TOP = -1, then:
      Print "Stack Underflow" and EXIT
2. Set ITEM = STACK[TOP]
3. Set TOP = TOP - 1
4. Return ITEM

c) PEEK / TOP

Returns the top element without removing it.

1. If TOP = -1, then:
      Print "Stack is Empty" and EXIT
2. Return STACK[TOP]

d) isEmpty

Checks whether the stack is empty.

1. If TOP = -1, return TRUE
2. Else return FALSE

e) isFull

Checks whether the stack is full.

1. If TOP = MAXSIZE - 1, return TRUE
2. Else return FALSE

3. Postfix Expression Evaluation

What is a Postfix Expression?

In postfix notation (also called Reverse Polish Notation), the operator comes after its operands.

  • Infix: A + B
  • Postfix: A B +

Postfix expressions do not require parentheses and are easy to evaluate using a stack.


Algorithm to Evaluate Postfix Expression

Input: A postfix expression string
Output: The evaluated result

Algorithm EVALUATE_POSTFIX(expression):

1. Create an empty stack S
2. Scan the expression from LEFT to RIGHT, one token at a time
3. For each token:
      a. If token is an OPERAND (number):
            PUSH it onto stack S
      b. If token is an OPERATOR (+, -, *, /):
            i.  POP the top element, call it OPERAND2
            ii. POP the next top element, call it OPERAND1
            iii.Apply the operator: RESULT = OPERAND1 operator OPERAND2
            iv. PUSH RESULT onto stack S
4. After scanning all tokens:
      POP and return the final value from stack S
      (This is the result of the expression)

Note: The order matters: first popped value is the right operand, second popped value is the left operand.


Worked Example

Evaluate postfix expression: 5 3 + 2 * 4 -

StepTokenActionStack State
15PUSH 5[5]
23PUSH 3[5, 3]
3+POP 3 and 5, compute 5+3=8, PUSH 8[8]
42PUSH 2[8, 2]
5*POP 2 and 8, compute 8*2=16, PUSH 16[16]
64PUSH 4[16, 4]
7-POP 4 and 16, compute 16-4=12, PUSH 12[12]

Final Result = 12


Another Example

Evaluate: 6 2 3 + - 3 8 2 / + *

TokenActionStack
6PUSH[6]
2PUSH[6, 2]
3PUSH[6, 2, 3]
+POP 3,2 โ†’ 2+3=5, PUSH[6, 5]
-POP 5,6 โ†’ 6-5=1, PUSH[1]
3PUSH[1, 3]
8PUSH[1, 3, 8]
2PUSH[1, 3, 8, 2]
/POP 2,8 โ†’ 8/2=4, PUSH[1, 3, 4]
+POP 4,3 โ†’ 3+4=7, PUSH[1, 7]
*POP 7,1 โ†’ 1*7=7, PUSH[7]

Final Result = 7


Summary

OperationDescriptionTime Complexity
PUSHInsert element at topO(1)
POPRemove element from topO(1)
PEEKView top elementO(1)
isEmptyCheck if stack is emptyO(1)
isFullCheck if stack is fullO(1)
Postfix EvaluationScan and computeO(n)

Every basic stack operation touches only the top of the stack, so each of them runs in constant time. Postfix evaluation scans the expression once and performs a constant amount of work per token, so it is O(n) in time and O(n) in space for the stack in the worst case, where n is the number of tokens.


Conclusion

A stack is a linear list restricted to LIFO access through PUSH and POP at a single end, with PEEK, isEmpty and isFull as supporting operations. Its usefulness is best seen in expression evaluation: because postfix notation carries no parentheses and no precedence, a single left to right scan with one stack is enough, pushing every operand and replacing the top two operands by the result whenever an operator appears. The value left on the stack at the end of the scan is the value of the expression.

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