How to delete the latest added book? : <p>I don't know how to delete the latest book that we added on this source code. I want to make a stack implementation program. So if we delete a book, the program will delete the last-added book, not the first-added book like in my source code below. I'm not too understand with stack using pointers. Can anyone fix my code? Thanks</p>

<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;stdlib.h&gt;

using namespace std;


class Book {
    int code, year;
    string language, name, title;
    Book *head, *next, *prev, *link;
public:
    Book (string &amp; name, string &amp; title, int code, string &amp; language, int year) {
    head = NULL;
    this -&gt; name = name;
    this -&gt; title = title;
    this -&gt; language = language;
    this -&gt; code = code;
    this -&gt; 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 &lt;&lt; endl &lt;&lt; "Author:", cin &gt;&gt; name;
cout &lt;&lt; "Title:", cin &gt;&gt; title;
cout &lt;&lt; "ISBN code(13 digits):", cin &gt;&gt; code;
cout &lt;&lt; "Language:", cin &gt;&gt; language;
cout &lt;&lt; "Year of publication:", cin &gt;&gt; year;

Book * p = new Book (name, title, code, language, year);
p -&gt; 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 &lt;&lt; "There is no book in the stack\n";
}
else if(head-&gt;next==NULL)
{
    p = head;
    head = NULL;
    free(p);
    cout &lt;&lt; "All book has been deleted. Now the stack is empty\n";
}
else
{
    p = head;
    while(p-&gt;next !=NULL)
    {
        prev = p;
        p = p-&gt;next;
    }
    prev-&gt;next = NULL;
    free(p);
    cout &lt;&lt; "A book has been deleted\n";
}
}

void Book :: display (void) {
Book * p = head;
while (p) {
    system("color B5");
    cout &lt;&lt; "----------------------------- \n";
    cout &lt;&lt; "Author:" &lt;&lt; p -&gt; name &lt;&lt; endl;
    cout &lt;&lt; "Title:" &lt;&lt; p -&gt; title &lt;&lt; endl;
    cout &lt;&lt; "Number of books" &lt;&lt; p -&gt; code &lt;&lt; endl;
    cout &lt;&lt; "Language:" &lt;&lt; p -&gt; language &lt;&lt; endl;
    cout &lt;&lt; "Year of publication:" &lt;&lt; p -&gt; year &lt;&lt; endl;
    cout &lt;&lt; endl;
    p = p -&gt; 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 &lt;&lt; "----------------------------- \n";
    cout &lt;&lt; "1) Add a book.\n";
    cout &lt;&lt; "2) Show all books.\n";
    cout &lt;&lt; "3) Delete a book\n";
    cout &lt;&lt; "4) Exit. \n";

    cout &lt;&lt; "Options:", cin &gt;&gt; opt;

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

return 0;
</code></pre>

<p>}</p>

<p>I hope there's someone who help me to fix this code. Thanks.</p>
