简体   繁体   中英

C++ Iterating through a linked list

So in my current program I am tasked to store a vector of branches. These branches contain a string name, and a pointer to a node.

These nodes store a book, which has an author name, a title, and the number of copies.

Overall, this should create the data structure of a linked list.

Currently I am attempting to write a program to print all the books within a branch.

After adding these books to the branch Alex.

Stan Moon

Bill Sun

Chris Ground

I attempt to print them all out using the printall() function. However, I simply get blank output. Is there something I am missing?

Thanks in advance!

#include "Library.h"


#include <vector>
#include <string>
#include <iostream>

using namespace std;
int total;
int index;

Library::Library()
{

}


struct Node //Has everything in it
{
    string author;
    string title;
    int copies;


    Node* next;
};

struct Branch // Stores just the branch, and a point to the node with information in it.
{
    string b_name;
    Node* next;
};



vector<Branch> lib;

void Library::start()
{
    int choice;

    cout << "Please select a choice." << endl;
    cout << " " << endl;
    cout << "1. Create a branch and insert its books" << endl;
    cout << "2. Given an author name, a title and a branch name, CHECKOUT that book from the branch." << endl;
    cout << "3. Given an author name, title and a branch name, RETURN that book to the branch." << endl;
    cout << "4. Given an author name, title and branch name, FIND the number of copies of that book are available in that branch." << endl;
    cout << "5. PRINT all books contained in a branch." << endl;
    cout << "6. Exit the program." << endl;

    cin >> choice;

    if (choice == 1)
    {

        insert();

    }

    if (choice == 5)
    {
        printAll();
    }

}

void Library::insert()
{
    string br;
    string auth;
    string titl;

    cout << "What is the name of the branch?" << endl;
    cin >> br;

    Branch *branch = new Branch();

    branch->b_name = br;
    lib.push_back(*branch);

    if (total == 0)
    {
        cout << "What is the author and title of the book?" << endl;
        cin >> auth >> titl;

        Node *book = new Node();
        book->author = auth;
        book->title = titl;
        book->copies++;
        book->next = NULL;
        branch->next = book;
        total++;
    }

    do
    {
        cout << "What is the author and title of the book?" << endl;
        cin >> auth >> titl;
        Node *book = new Node();
        book->author = auth;
        book->title = titl;
        book->copies++;
        book->next = branch->next;
        branch->next = book;
        total++;
    } while (auth != "NONE" && titl != "NONE");



    start();



}
void Library::checkout()
{
    string auth;
    string titl;
    string bran;

}

void Library::Return()
{
//TODO
}

void Library::find()
{
//TODO
}

void Library::printAll()
{
    for (unsigned int i = 0; i < lib.size(); i++)
    {
        while (lib.at(i).next != NULL)
        {
            cout << "There are " << lib.at(i).next->copies << "of " << lib.at(i).next->title << "by " << lib.at(i).next->author << "in branch " << lib.at(i).b_name << endl;
            lib.at(i).next = lib.at(i).next->next;
        }
    }
    start();
}

I found some problems of your source code:

  • You should hold pointer in the lib vector, or it will be cloned when push_back
  • The insert function was wrongly implemented
  • You should use loop instead of recusive call, it maybe cause stack overflow

Check the modified version:

#include "Library.h"
#include <vector>
#include <string>
#include <iostream>

using namespace std;
int total;
int index;

Library::Library()
{

}

struct Node //Has everything in it
{
    string author;
    string title;
    int copies;
    Node* next;
};

struct Branch // Stores just the branch, and a point to the node with information in it.
{
    string b_name;
    Node* next;
};

vector<Branch*> lib;

void Library::start()
{
    int choice = 0;
    do
    {
        cout << "Please select a choice." << endl;
        cout << " " << endl;
        cout << "1. Create a branch and insert its books" << endl;
        cout << "2. Given an author name, a title and a branch name, CHECKOUT that book from the branch." << endl;
        cout << "3. Given an author name, title and a branch name, RETURN that book to the branch." << endl;
        cout << "4. Given an author name, title and branch name, FIND the number of copies of that book are available in that branch." << endl;
        cout << "5. PRINT all books contained in a branch." << endl;
        cout << "6. Exit the program." << endl;

        cin >> choice;

        switch (choice)
        {
        case 1:
            insert();
            break;

        case 5:
            printAll();
            break;

            // TODO: other choises
        }
    } while (choice != 6);
}

void Library::insert()
{
    string br;
    string auth;
    string titl;

    cout << "What is the name of the branch?" << endl;
    cin >> br;

    Branch *branch = new Branch();
    branch->b_name = br;
    lib.push_back(branch);
    Node* lastNode = nullptr;

    do
    {
        cout << "What is the author and title of the book?" << endl;
        cin >> auth >> titl;
        Node *book = new Node();
        book->author = auth;
        book->title = titl;
        book->copies++;
        if (lastNode == nullptr) {
            branch->next = book;
        }
        else {
            lastNode->next = book;
        }
        lastNode = book;
    } while (auth != "NONE" && titl != "NONE");
}

void Library::checkout()
{
    string auth;
    string titl;
    string bran;
}

void Library::Return()
{
    //TODO
}

void Library::find()
{
    //TODO
}

void Library::printAll()
{
    for (unsigned int i = 0; i < lib.size(); i++)
    {
        auto* branch = lib[i];
        auto* node = branch->next;
        while (node)
        {
            cout << "There are " << 
                node->copies << " of " << 
                node->title << " by " << 
                node->author << " in branch " << 
                branch->b_name << endl;
            node = node->next;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM