(Q) Stack modificationsΒΆ
Goal: I will understand the division between class definition and class implementation in object-oriented programming, and how this division affects program modifications.
This exercise will be a ground for the later topic on modularity.
Instructions: Let us consider a special case of a linked list, called stack. A stack means a data structure, which allows adding elements on the top (beginning) and removing them from the top (beginning). Recall that a linked list allows adding and removing elements at any location.
The earlier exercise on moving cards is very close to a stack, but it provided also other methods, besides the aforementioned basic stack operations, for changing the content of the stack.
Especially a stack could be implemented by using an array (or a vector). In such an implementation, the bottom element would be at the index 0, and you would need an index variable telling the topmost element.
The only difference between these two implementation ways is that in linked list implementation the stack can grow without any limit (when assuming that the memory cannot be exhausted), but in array implementation you must specify the maximum size of the array (stack). You need not pay attention to such differences in the questions below.
Consider the three files used in the card exercise:
cards.hh
, cards.cpp
, and main.cpp
,
and try to figure out, which modifications are necessary, when
moving from the implementation based on a linked list to that
based on an array.
Restrict only to the basic stack operations, i.e. the methods
add
and remove
.
Also the method print_from_top_to_bottom
can be considered as
a stack operation.