-3

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

#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;

}

I hope there's someone who help me to fix this code. Thanks.

1 Answers1

0

you don't have to loop through your head pointer in delete function the below code will fix the problem you are seeing

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

You might have to clean up your code but to start with you can look at this change

Maddy
  • 774
  • 5
  • 14