I
I
Iskander Mammadov2016-03-02 18:15:37
Programming
Iskander Mammadov, 2016-03-02 18:15:37

How to implement a queue of class objects in C++?

The essence of the program: the game "Drunkard".
Main task: to implement a queue of game cards.
My map class:

class card {
public:
  card() {
    next = NULL;
  }
  void setValue(int i); // Записывает значние в карту
  int getValue(); // Получает значение карты
  void printCard(); // Вывод карты
  void setNext(card &adr); // Записать адрес следующей карты
  card* getNext(); // Получить адрес следующей карты
private:
  int value; // Значение карты
  card *next; // Адрес следующей карты
};

Deck class (actually, this is the queue of cards):
class player {
public:
  player() {
  }
  void enqueue(int value); // Добавить карту в руку игрока (в очередь)
  card* dequeue(); // Получить карту из руки игрока (из очереди)
  int empty(); // Остались ли у игрока еще карты
  void clear(); // Очистить руку игрока
  void printPlayer(); // Вывести все карты игрока (для тестирования)
private:
  card *lastCard = NULL; // Ссылка на последнюю карту в очереди
  card *firstCard = NULL; // Ссылка на первую карту в очереди
};

And the methods for adding and removing a card from the queue, respectively:
void player::enqueue(int value){
  if (empty()) {
    firstCard = new card;
    firstCard->setValue(value);
    lastCard = new card;
    firstCard->setNext(*lastCard);
  }
  else {
    card *newCard = new card;
    lastCard->setValue(value);
    lastCard->setNext(*newCard);
    lastCard = newCard;
  }
}

card* player::dequeue() {
  card *tmp = firstCard;
  firstCard = firstCard->getNext();
  return tmp;
}

I create the first map so that I know the beginning, and in the first map I assign a pointer to the next map to the variable next, this way I get a queue of sequentially connected maps. But I implemented it incorrectly, because after adding the card to the queue, I create the next card, where the value of the next card added to the queue will be entered. It turns out that after adding the map, I have an empty element at the end of the queue.
I feel like the solution is on the surface, but I can't figure it out. Tell me please.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Valery Dmitriev, 2016-03-03
@alexgugr

Your class playerdoes not contain an explicitly declared destructor. Whereas in a method void player::enqueue(int value)you allocate dynamic memory. Most likely you will have a memory leak.
Use smart pointers, or write a destructor to clean everything up.
The method prototype itself void player::enqueue(int value)is not architecturally correct. The argument must not be an int. Depending on what you ultimately want, you need to implement either void player::enqueue(const card&), or void player::enqueue(card&&), or both.
In general, working with raw pointers in C++ is not the best idea. Again, without knowing the final task, it's hard to say, but you need to return player::dequeue() either a smart pointer, or a reference to an object, or the object itself. But not a pointer.
Classcardyou store information about the next card. This is again not the right architectural solution. This class should be as simple as possible. And the queue must be implemented separately. Now you have some strange spreading of the logic of the player class over two classes.
Why don't you use the standard queueSTL container?

E
evgeniy_lm, 2016-03-02
@evgeniy_lm

Why create a trail. map? You have a trace in the class. the map is declared as a pointer, i.e. if the card is the last, then the pointer points to nowhere, this can also be a sign of the last card. Here is the code you create a new map then in the next field of the "old last map" write a pointer to an instance of the "new last map"

P
Peter, 2016-03-02
@petermzg

There are a maximum of 54 cards in the deck, there are enough bits in int64 to store the disposal of cards.
The Card class is redundant.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question