作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以在我当前的程序中,我的任务是存储一个分支 vector 。这些分支包含一个字符串名称和一个指向节点的指针。
这些节点存储一本书,其中有作者姓名、书名和印数。
总的来说,这应该创建链表的数据结构。
目前我正在尝试编写一个程序来打印一个分支中的所有书籍。
将这些书添加到分支Alex之后。
斯坦月亮
比尔孙
克里斯地面
我尝试使用 printall() 函数将它们全部打印出来。但是,我只是得到空白输出。有什么我想念的吗?
提前致谢!
#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();
}
最佳答案
我发现了你源码的一些问题:
lib
vector 中保存指针,否则它会在push_back时被克隆insert
函数执行错误查看修改后的版本:
#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;
}
}
}
关于C++遍历链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50167211/
我是一名优秀的程序员,十分优秀!