使用运算符重载打印链表

时间:2012-07-09 00:33:33

标签: c++ singly-linked-list

我正在制作单链表。列表中的几个组件尚未在main中设置。但是,我正在研究类中的定义,并希望重载cout<<运算符以打印列表的内容。

queue.cpp中的最后一个定义是我试图让它打印每个节点1的地方。我成功地重载了cin和cout以使用Game类,但我需要让cout使用Queue类打印节点。我感谢所有的帮助/建议和反馈。

到目前为止,这是我的代码:

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

queue.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;
}

的main.cpp

#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;
}

1 个答案:

答案 0 :(得分:1)

尝试此输出队列:

ostream & operator<<(ostream & os, const Queue & q)//figure out how to print nodes..
{
    for(Queue::Node* n = q.front; n != NULL; n = n->next)
    {
       os << n->game;
    }
    return os;
}

它所做的就是占用队列的开头,遍历每个Node,打印其game,然后返回os