[英]C++ Deleting from a Linked List
因此,在當前程序中,我負責存儲分支向量。 這些分支包含字符串名稱和指向節點的指針。
這些節點存儲一本書,該書具有作者姓名,書名和份數。
總的來說,這應該創建一個鏈表的數據結構。
目前,我正在嘗試編寫一個程序來從分支中刪除或“檢出”某本書。
將這些書添加到分支AO之后。
斯坦·月亮
孫Sun
克里斯·地面
藍天
我試圖看一看克里斯的書《地面》。 如果這本書有多份副本(本例中沒有),則只會減少一本。 如果只有一個副本,則將其刪除,將先前指向該對象的對象設置為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();
}
編輯:修復刪除條件的錯誤放置
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.