(E) Moving cards againΒΆ

Goal: I will learn handling smart pointers (of type shared_ptr).

Instructions: Retrieve the code template: templates/10/reverse/ -> student/10/reverse/.

Let us continue working with the deck of cards. However, this time we will use pointers of the type shared_ptr. In this assignment, the actions are very similar to the card deck assignment of the previous round.

The file cards.hh defines the class interface to be implemented, and you must not edit it. Because we are practicing dynamic memory management, your task is to implement a class without using STL containers, strings, or C++ arrays.

This time, you will find the implementations of two of the methods in the file cards.cpp. You only need to implement the methods remove, working similarly to the earlier deck, and reverse, arranging the cards in the deck into a reverse order.

Here is an example on how the class should work with the given main test program:

constructor
Enter amount of test cards: 5

print (deck is empty)

add * n

print
1: 4
2: 3
3: 2
4: 1
5: 0

reverse

print
1: 0
2: 1
3: 2
4: 3
5: 4

remove 0
remove 1
remove 2
remove 3
remove 4

destructor

Again, you can edit the testing main program as you wish, and wider testing helps you implement your program. The automated test system tests the class without the main program, which means that the system does not care what you write in your own testing main program.

Tips for completing the assignment:

  • Even if remove works exactly as it did in the previous assignment, here, you are supposed to think about what the difference is in implementation if you use shared_ptr instead of a normal pointer.
  • While creating operations, please remember all the situations you have to take into account when handling a linked data structure (the first element, the last element, an empty list, etc).

A+ presents the exercise submission form here.