C++ C++;从链表中删除

C++ C++;从链表中删除,c++,loops,pointers,linked-list,iteration,C++,Loops,Pointers,Linked List,Iteration,因此,在我当前的程序中,我的任务是存储分支向量。这些分支包含字符串名称和指向节点的指针 这些节点存储一本书,其中包含作者姓名、标题和份数 总的来说,这应该创建链表的数据结构 目前,我正试图编写一个程序来删除或“签出”某个分支的某本书 将这些书添加到分支AO后 斯坦·穆恩 孙比尔 克里斯·格兰德 蓝天 我想看看克里斯写的那本书。如果这本书有多份(本例中没有),它只会减少一份。如果只有一个副本,则会删除它,将以前指向它的对象设置为地面指向的对象,然后删除地面与其下一个对象之间的链接 但是,由于某些原

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

这些节点存储一本书,其中包含作者姓名、标题和份数

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

目前,我正试图编写一个程序来删除或“签出”某个分支的某本书

将这些书添加到分支AO后

斯坦·穆恩

孙比尔

克里斯·格兰德

蓝天

我想看看克里斯写的那本书。如果这本书有多份(本例中没有),它只会减少一份。如果只有一个副本,则会删除它,将以前指向它的对象设置为地面指向的对象,然后删除地面与其下一个对象之间的链接

但是,由于某些原因,在执行此功能后,我的链接列表没有任何更改。这有什么原因吗

提前谢谢

#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();
}
#包括“Library.h”
#包括
#包括
#包括
使用名称空间std;
整数合计;
整数指数;
库::库()
{
}
结构节点//包含所有内容
{
字符串作者;
字符串标题;
整版;
节点*下一步;
};
struct Branch//只存储分支和指向节点的点,其中包含信息。
{
字符串b_名称;
节点*下一步;
};
向量库;
void库::start()
{
int-choice=0;
做
{
cout author==auth&&node->title==titl&&branch->b_name==bran)
如果(节点->复制>1)节点->复制--;成功=真;中断;
如果(节点->副本==1)
{
上一步->下一步=节点->下一步;
节点->下一步=nullptr;
成功=真实;
打破
}
如果(!成功)
{
上一个=节点;
节点=节点->下一步;
}
}
如果(成功)
{
cout这个呢:

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

}
void库::签出()
{
字符串验证;
弦标题;
麦麸;
布尔成功=假;
无法认证>>标题>>麸皮;
for(无符号整数i=0;i下一步;
节点*previous=nullptr;
while(节点)
{
如果(节点->作者==auth&&node->title==titl&&branch->b_name==bran)
{
如果(节点->副本>1)
{
节点->副本--;
成功=真实;
打破
}
其他的
{
如果(先前)
{
上一步->下一步=节点->下一步;
}
其他的
{
分支->下一步=节点->下一步;
}
删除节点;
成功=真实;
打破
}
}
上一个=节点;
节点=节点->下一步;
}
如果(成功)
{

可能不是此特定问题的原因,但您的函数不应该调用
start()
,它们应该只返回。请尝试修复缩进。在缺少的地方添加花括号。(例如,在if(!success)之后)@molbdnilo好的,它不是通过声明其值等于node然后递增node来确定的吗?但是,我可以看出如果它是链接列表中的第一本书,这会导致问题。如果我错了,请更正我。不要编写自己的链接列表。使用
std::list
。使用此选项时,它不会删除特定的相反,它会删除链接列表中的第一本书。ups!gimmi a second-done(感谢您的评论)工作得非常好!非常感谢!多年来一直困扰着这个问题!