Q1Data Structures
Question
2 marks
How stack is represented using dynamic array? Explain with example.
Answer
A dynamic array stack automatically resizes its underlying contiguous memory block when its capacity is fully exhausted.
A dynamic array stack is initialized with a fixed capacity. When push() operations exceed this capacity, the system automatically allocates a new, larger array (typically double the size), meticulously copies over all existing elements, and subsequently frees the old memory. For example, in C++, std::vector effortlessly handles this dynamic resizing underlying the stack logic.