Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8208029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T09:12:02+00:00 2026-06-07T09:12:02+00:00

I am working on a singly linked list. Several components of the list are

  • 0

I am working on a singly linked list. Several components of the list are not yet set up in main. However, I am working out the definitions in the class and would like to overload the cout << operator to print the contents of the list.

The last definition in queue.cpp is where I am trying to have it print each node 1 by 1. I Successfully overloaded both cin and cout to work with the Game class, but I need to get cout working with the Queue class to print the nodes. I appreciate all the help/advice and feedback.

Here is my code so far:

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T09:12:03+00:00Added an answer on June 7, 2026 at 9:12 am

    Try this to output the Queue:

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

    All it does is takes the beginning of the queue, iterates over every Node, prints its game, and returns the os.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my stack implementation with linked list. The program is working correctly. Would
We learned how to implement singly linked list in class. And our professor kind
I am working on a single linked-list calculator in C (yes, it's homework). I
I'm programming this game in OpenGL, mostly working inside an single EAGLView (I'm not
When working with generators you can only pull out items on a single pass.
My app was working and I did not change a single thing in the
I can not figure out where my problem is but I am not able
I'm using Visual studio c++ 2010. My program was working fine until I linked
I know it is a minute code. I can't understand why my linked list
I'm currently working on implementing a list-type structure at work, and I need it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.