我有这段代码,提示用户在虚拟图书馆中输入书名,作者和ISBN。该代码将数据存储在链接列表中,这是我第一次使用链接列表。用户可以将一本书放入列表1或列表2中。然后,该用户应该能够比较两个列表中的哪些书籍通过输入ISBN。每当我尝试比较列表时,它只是空白。您能提供的任何帮助都将非常重要,谢谢!
我确保变量的所有名称都一致。至于排除实际链接列表是否有问题的疑难解答,老实说,我不知道。
#include <iostream>
#include<string>
using namespace std;
class linkList
{private:
struct node
{string title;
string author;
string ISBN;
node *next; };
node *head= NULL;
public:
linkList(){};
void printList();
void addBook();
void compareList(linkList a);
bool isInListISBN(string ISBN);
};
void displayMenu();
int main()
{
linkList book1;
linkList book2;
char selection;
string ISBN;
bool running=true;
while (running)
{ displayMenu();
cin>> selection;
switch(selection)
{case '1': book1.addBook();
break;
case '2': book2.addBook();
break;
case '3':
cout<<"Please enter ISBN to check if the book is in the list"<<endl;
cin>>ISBN;
if(book1.isInListISBN(ISBN))
{cout<<"ISBN "<<ISBN<< " is in book1"<<endl;}
if (book2.isInListISBN(ISBN))
{cout<<"ISBN "<<ISBN<< " is in book2"<<endl;}
if (!book1.isInListISBN(ISBN) && !book2.isInListISBN(ISBN))
{cout<< "ISBN "<<ISBN<<" could not be found, please verify entry is correct."<<endl;}
break;
case '4':
cout<<"book 1 : "<<endl;
book1.printList();
cout<<"book 2 : "<<endl;
book2.printList();
break;
case '5':
book1.compareList(book2);
break;
case '6':
cout<<"Thank you for visiting the virtual library, please come again. "<<endl;
running= false;
break;
}
}
}
void displayMenu()
{cout<<"Welcome to the virtual library. Select an option from the menu below."<<endl;
cout<<"--------------------------------------------------------------------"<<endl;
cout<<"1. Enter a book into list 1 "<<endl;
cout<<"2. Enter a book into list 2 "<<endl;
cout<<"3. Search for a book by ISBN "<<endl;
cout<<"4. Print all books in lists"<<endl;
cout<<"5. Compare lists "<<endl;
cout<<"6. Exit virtual library"<<endl; };
void linkList :: addBook()
{string title;
string author;
string ISBN;
cin.ignore();
cout<<"Please enter title of book"<<endl;
getline(cin, title);
cout<<"Please enter author of book"<<endl;
getline(cin, author);
cout<<"Please enter ISBN"<<endl;
getline(cin, ISBN);
node *n= new node;
node *c= head;
n->title= title;
n->author= author;
n->ISBN= ISBN;
n-> next= NULL;
//if there is no head node, n is assigned the head node
if (head== NULL)
{head= n;}
else
{while (c-> next != NULL)
{c= c-> next;}
c-> next = n;}
}
bool linkList :: isInListISBN(string ISBN)
{node *c= head;
while (c !=NULL);
{if (ISBN.compare(c->ISBN) ==0)
{return true;}
c= c-> next;
}
return false;
}
void linkList :: printList()
{cout<<"The linked list contains the following books: \n\n"<<endl;
node *c= head;
while(c != NULL)
{cout<<"Title: "<<c->title<<endl;
cout<<"Author: "<<c->author<<endl;
cout<<"ISBN: "<<c->ISBN<<endl;
c=c->next;
}
}
void linkList :: compareList(linkList a)
{bool matched = false;
node *c= head;
cout<<"Books in both lists: "<<endl;
while(c!=NULL)
{if (a.isInListISBN(c->ISBN))
{matched=true;
cout<< c->title<<endl;}
c=c->next;}
if(!matched)
{cout<<"None\n";}
}
我希望输出打印出具有相同ISBN的两个书名,而不是返回空白。