Data Structure (2130702)

BE | Semester-3   Summer-2017 | 05/31/2017

Q3) (a)

Write an algorithm to return the value of ith element from top of the stack.

Function: PEEP (S, TOP, I)

  • This function returns the value of the Ith element from the TOP of the stack.
  • The element is not deleted by this function.
  • Stack is represented by a vector S containing N elements.
1. [Check for stack Underflow]
  If TOP - I +1 = 0
  Then Write (‘STACK UNDERFLOW ON PEEP’)
     Return(0)
2. [Return Ith element from top of the stack]
  Return (S[TOP – I + 1])