DS – Stack Operations

Operations on Stack

push():

  • A function which is used to insert an element on to the Stack
  • While pushing an element, we need to check whether the Stack is Full or not.
  • If Stack is full then display “Stack is Full” else insert element on Top.

pop():

  • A function which is used to delete element from Stack.
  • Pop() function always remove Top element of Stack.
  • While removing element, we need to check Stack is Empty or not.
  • If Stack is Empty -> display “Stack is Empty” message.
  • If Stack is Not Empty -> it removes the Top location element.

display() or traverse():

  • A function which is used to display all elements in the Stack
  • If stack is Empty, we return with Error message.
  • If Stack is not Empty, we display all elements from top to 0 indices

peek():

  • A function that returns the top element of Stack.
  • peek() function will not remove the element like pop() function
  • peek() function  return error message if stack is empty.
Scroll to Top