gpt4 book ai didi

C++ 从链表中删除

转载 作者:行者123 更新时间:2023-11-28 04:41:10 25 4
gpt4 key购买 nike

所以在我当前的程序中,我的任务是存储一个分支 vector 。这些分支包含一个字符串名称和一个指向节点的指针。

这些节点存储一本书,其中有作者姓名、书名和份数。

总的来说,这应该创建链表的数据结构。

目前我正在尝试编写一个程序来从分支机构中删除或“ checkout ”某本书。

将这些书添加到分支AO之后。

斯坦月亮

比尔孙

克里斯地面

蓝天

我想看看 Chris 的《Ground》一书。如果这本书有多份拷贝(在本例中没有),它只会将拷贝数量减少一份。如果它只有一个拷贝,则将其删除,将先前指向它的对象设置为 Ground 指向的对象,然后删除 Ground 与其下一个之间的链接。

但是,出于某种原因,在执行此功能后,我的链表没有任何变化。这是有原因的吗?

提前致谢!

#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 2:
checkout();
break;

case 3:
Return();
break;

case 4:
find();
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;
if (auth == "NONE" && titl == "NONE") break;

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");

start();
}

void Library::checkout()
{
string auth;
string titl;
string bran;
bool success = false;

cout << "Insert author, title and branch" << endl;
cin >> auth >> titl >> bran;

for (unsigned int i = 0; i < lib.size(); i++)
{
auto* branch = lib[i];
auto* node = branch->next;
Node* previous = nullptr;

while (node)
{
if (node->author == auth && node->title == titl && branch->b_name == bran)
if (node->copies > 1) node->copies--; success = true; break;
if (node->copies == 1)
{
previous->next = node->next;
node->next = nullptr;
success = true;
break;
}

if (!success)
{
previous = node;
node = node->next;
}

}
if (success)
{
cout << "Complete!" << endl;
cout << "" << endl;
}


}
start();
}

最佳答案

这个怎么样:

    void Library::checkout()
{
string auth;
string titl;
string bran;
bool success = false;

cout << "Insert author, title and branch" << endl;
cin >> auth >> titl >> bran;

for (unsigned int i = 0; i < lib.size(); i++)
{
auto* branch = lib[i];
auto* node = branch->next;
Node* previous = nullptr;

while (node)
{
if (node->author == auth && node->title == titl && branch->b_name == bran)
{
if (node->copies > 1)
{
node->copies--;
success = true;
break;
}
else
{
if (previous)
{
previous->next = node->next;
}
else
{
branch->next = node->next;
}
delete node;
success = true;
break;
}
}
previous = node;
node = node->next;
}
if (success)
{
cout << "Complete!" << endl;
cout << "" << endl;
}
}
// no need to call start() as start() has a do/while loop
// start();

}

编辑:修复删除条件的错误放置

关于C++ 从链表中删除,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50170110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com