peek() operation in Stack data structure

What is the use of peek() operation in stack data structure ?


8 Answers
1-8 of  8
8 Answers
  • peek means the highest value or topmost value ..so this function is used for displaying topmost value of stack

  • Function

  • Peek() operation is used to display top most value of the stack. Function for peek() in c language: void peek { if ( top ==-1) printf("stack is empty\n"); else printf("top value is %d \n",stack[top]); }

  • Intuitively, peek returns the same value as pop, but does not change the data. Behavior when the collection is empty varies – most often this yields an underflow error, identically to a pop on an empty collection, but some implementations provide a function which instead simply returns (without error), essentially implementing if isempty then return, else peek.
    This behavior can be axiomatized in various ways. For example, a common VDM (Vienna Development Method) description of a stack defines top (peek) and remove as atomic, where top returns the top value (without modifying the stack), and remove modifies the stack (without returning a value).[1] In this case pop is defined in terms of top and remove.
    Alternatively, given pop, the operation peek can be axiomatized as:
    peek(D) = pop(D)
    peek(D), D = D
    meaning "returns the same value as pop", and "does not change the underlying data" (value of data after peek same as before peek).

  • peek() is a library function used in stack data structure to get the topmost value of the stack 

  • Peek() is one of a operation of stack . It returns the value of the top most element of the stack without deleting that element from the stack.

Data Structure

Didn't get the answer.
Contact people of Talent-Data Structure directly by clicking here