我正在研究一个单链表。列表中的几个组件尚未在 main 中设置。但是,我正在制定类中的定义,并希望重载 cout << 运算符以打印列表的内容。
queue.cpp 中的最后一个定义是我试图让它逐个打印每个节点。我成功地重载了 cin 和 cout 以使用 Game 类,但我需要让 cout 使用 Queue 类来打印节点。我感谢所有的帮助/建议和反馈。
到目前为止,这是我的代码:
队列.h
#include <iostream>
#include <string>
#ifndef QUEUE_H_
#define QUEUE_H_
class Game
{
private:
std::string title;
public:
Game(const std::string & s);
Game(); //default constructor
~Game(); //destructor
friend std::ostream & operator<<(std::ostream & os, const Game & g); //allows cout << to be used object
friend std::istream & operator>>(std::istream & is, Game & g); //allows cin >> to be used with object
};
class Queue
{
private:
struct Node
{
Game game; //data stored in the node
struct Node * next; //pointer to next node
};
// enum {Q_SIZE = 10};
Node * front; //pointer to front of Queue
Node * rear; //pointer to rear of Queue
int items; //current number of games in Queue
const int qsize; //max number of games in Queue
public:
Queue(int qs); //create a queue with a qs limit
// Queue();
~Queue();
bool isempty() const;
bool isfull() const;
int queuecount() const;
bool enqueue(const Game & game); //add game to end
bool dequeue(Game & game); //remove game from front
friend std::ostream & operator<<(std::ostream & os, const Queue & q);
};
#endif
队列.cpp
#include "queue.h"
using namespace std;
//Game methods
Game::Game(const string & s)
{
title = s;
}
Game::Game() //default constructor can be blank or empty
{
// title = '\0'; //NULL
// title = "JEGORRRRRE.";
}
Game::~Game()
{
cout << " destructor called" << endl;
}
ostream & operator<<(ostream & os, const Game & g)
{
os << g.title;
return os;
}
istream & operator>>(istream & is, Game & g)
{
getline(cin, g.title); //code for reading line into a string object (pg. 131)
return is;
}
//Queue methods
Queue::Queue(int qs) : qsize(qs) //initialize qsize to qs because qsize is a const; must be done when the object is created
{
front = rear = NULL;
items = 0;
}
Queue::~Queue()
{
Node * temp;
while (front != NULL) //while queue is not yet empty
{
temp = front; //save address of front item
front = front ->next; //reset pointer to next item
delete temp; //delete former front
}
}
bool Queue::isempty() const
{
return items == 0;
}
bool Queue::isfull() const
{
return items == qsize;
}
int Queue::queuecount() const
{
return items;
}
bool Queue::enqueue(const Game & game)
{
if (isfull())
return false;
Node * add = new Node; //creates node
add->game = game; //add accesses game member of struct and sets it to game
add->next = NULL; //add accesses next member of struct and sets it to NULL
items++; //increments the item count
if (front == NULL) //if queue is empty
front = add; //place at front
else
rear->next = add; //else place at rear; in this case rear accesses the next member of struct and assigns the newly created node at next
rear = add; //rear points to new node; this line --> Node * add = new Node;
return true;
}
bool Queue::dequeue(Game & game) //place front item into item variable and remove from queue
{
if (front == NULL)
return false;
game = front->game; //set item to first item in queue
items--;
Node * temp = front; //save location of first item
front = front->next; //reset front to next item
delete temp; //delete former first item
if (items == 0)
rear = NULL;
return true;
}
ostream & operator<<(ostream & os, const Queue & q)//figure out how to print nodes..
{
//
//return os;
}
主文件
#include <iostream>
#include "queue.h"
using namespace std;
int main()
{
cout << "Top Video Games of all Time\n___________________________"
<< "\nenter max number of games: \n";
int q_size;
(cin >> q_size).get();
Queue list(q_size);
Game temp;
for (int c = 1; c < 11; c++)
{
cout << "Enter game title " << c << endl;
cin >> temp;
list.enqueue(temp);
}
// cout << list;
cout << "\n\n\n\n\n\n\n";
system("pause");
return 0;
}