C++ 如何删除最新添加的图书?

C++ 如何删除最新添加的图书?,c++,pointers,stack,C++,Pointers,Stack,我不知道如何删除我们在源代码中添加的最新书籍。我想做一个堆栈实现程序。因此,如果我们删除一本书,程序将删除最后添加的书,而不是像下面我的源代码中那样删除第一本添加的书。我不太理解堆栈使用指针。有人能修改我的代码吗?谢谢 #include <iostream> #include <string> #include <stdlib.h> using namespace std; class Book { int code, year; str

我不知道如何删除我们在源代码中添加的最新书籍。我想做一个堆栈实现程序。因此,如果我们删除一本书,程序将删除最后添加的书,而不是像下面我的源代码中那样删除第一本添加的书。我不太理解堆栈使用指针。有人能修改我的代码吗?谢谢

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;


class Book {
    int code, year;
    string language, name, title;
    Book *head, *next, *prev, *link;
public:
    Book (string & name, string & title, int code, string & language, int year) {
    head = NULL;
    this -> name = name;
    this -> title = title;
    this -> language = language;
    this -> code = code;
    this -> year = year;
    };
~ Book (void) {
    delete head;
};
void display (void);
void add (void);
void dellete (void);
};

void Book :: add (void) {
string name, title, language;
int year, code;
system("color E2");
cout << endl << "Author:", cin >> name;
cout << "Title:", cin >> title;
cout << "ISBN code(13 digits):", cin >> code;
cout << "Language:", cin >> language;
cout << "Year of publication:", cin >> year;

Book * p = new Book (name, title, code, language, year);
p -> next = head;
head = p;
}

void Book :: dellete (void) {
string name, title, language;
int year, code;
system("color B0");
Book *p, *prev, *next;
if(head==NULL)
{
    cout << "There is no book in the stack\n";
}
else if(head->next==NULL)
{
    p = head;
    head = NULL;
    free(p);
    cout << "All book has been deleted. Now the stack is empty\n";
}
else
{
    p = head;
    while(p->next !=NULL)
    {
        prev = p;
        p = p->next;
    }
    prev->next = NULL;
    free(p);
    cout << "A book has been deleted\n";
}
}

void Book :: display (void) {
Book * p = head;
while (p) {
    system("color B5");
    cout << "----------------------------- \n";
    cout << "Author:" << p -> name << endl;
    cout << "Title:" << p -> title << endl;
    cout << "Number of books" << p -> code << endl;
    cout << "Language:" << p -> language << endl;
    cout << "Year of publication:" << p -> year << endl;
    cout << endl;
    p = p -> next;
}
}

int main (int argc, char const ** argv) {

string blank = "";
Book * B = new Book (blank, blank, 0, blank, 0);
int opt;
for (;;) {
    system("color A0");
    cout << "----------------------------- \n";
    cout << "1) Add a book.\n";
    cout << "2) Show all books.\n";
    cout << "3) Delete a book\n";
    cout << "4) Exit. \n";

    cout << "Options:", cin >> opt;

    switch (opt) {
            case 1:
                B -> add ();
            break;
            case 2:
                B -> display ();
            break;
            case 3:
                B -> dellete ();
            break;
            case 4:
                exit (0);
                default:
                continue;
    }
}

return 0;
}


我希望有人能帮我修复这个代码。谢谢。

您不必在delete函数中遍历头部指针,下面的代码将修复您看到的问题

void Book :: dellete (void) 
{
    string name, title, language;
    int year, code;
    system("color B0");
    Book *p, *prev, *next;
    if(head==NULL)
    {
        cout << "There is no book in the stack\n";
    }
    else if(head->next==NULL)
    {
        p = head;
        head = NULL;
        free(p);
        cout << "All book has been deleted. Now the stack is empty\n";
    }
    else
    {
        p = head;
        head = p->next; // Modified code part
        free(p);
        cout << "A book has been deleted\n";
    }
}

您可能需要清理代码,但首先您可以查看此更改

似曾相识!我以前见过这个代码。我甚至对它发表了评论。它仍然混淆了书是一本书和一个列表。一本书包含几页和几段文字。书籍很少指向其他书籍或包含其他书籍。另请参见