Elements present in stack variable top - Data Structure

Q.  How many elements are present in the stack if the variable top exhibits pointing towards the topmost element in the Array?
- Published on 28 Aug 15

a. top +1
b. top - 1
c. zero
d. infinite

ANSWER: top +1
 

    Discussion

  • Nirja Shah   -Posted on 18 Nov 15
    If the top variable is at the last index of array, then the stack is full. Here meaning of last index is that, suppose the stack size is 10, then the top will be at ninth index (0 to 9).
    So the total number of elements are presents in the stack = top+1. You can also check the overflow condition of stack as given below code.
    If (top== size-1)
    {
        cout << ”stack is full”;
    }

Post your comment / Share knowledge


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)