Bird
0
0
DSA Cprogramming~5 mins

Peek Top Element of Stack in DSA C - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the 'peek' operation do in a stack?
The 'peek' operation returns the top element of the stack without removing it, allowing you to see the current top value.
Click to reveal answer
beginner
In a stack implemented using an array, which index usually represents the top element?
The top element is usually at the index represented by the 'top' variable, which points to the last inserted element.
Click to reveal answer
intermediate
What should the 'peek' function do if the stack is empty?
If the stack is empty, the 'peek' function should handle it gracefully, often by returning a special value or indicating an error, since there is no top element to show.
Click to reveal answer
beginner
Show the basic C code snippet to peek the top element of a stack implemented with an array and a 'top' index.
int peek(int stack[], int top) { if (top == -1) { // Stack is empty return -1; // or some error code } return stack[top]; }
Click to reveal answer
beginner
Why is it important that 'peek' does not remove the top element from the stack?
Because 'peek' is meant only to view the top element without changing the stack's state, so other operations can still use the stack as expected.
Click to reveal answer
What does the 'peek' operation return in a stack?
AThe top element without removing it
BRemoves and returns the top element
CReturns the bottom element
DClears the stack
If the stack is empty, what should the peek function do?
AReturn the top element
BRemove the bottom element
CAdd a new element
DReturn an error or special value
In a stack implemented with an array, what does the 'top' variable represent?
AIndex of the top element
BNumber of elements in the stack
CIndex of the bottom element
DSize of the array
Which of these is true about the peek operation?
AIt returns the top element and removes it
BIt returns the top element without removing it
CIt removes the top element
DIt clears the stack
What value is commonly used to indicate an empty stack in C implementations?
Atop = 0
Btop = NULL
Ctop = -1
Dtop = 1
Explain how the peek operation works in a stack implemented with an array.
Think about how you find the last added item without changing the stack.
You got /4 concepts.
    Describe why peek is useful and how it differs from pop in stack operations.
    Consider what happens to the stack after each operation.
    You got /4 concepts.