这是一个链表程序的作业,我相信我几乎完成了这个程序。在主方法的这几行中,我一直得到4个错误。我想知道是否有人能告诉我瞄准镜出了什么问题。谢谢
错误:' addFirst '未在此范围内声明addFirst(书目);
错误:' addLast '未在此范围内声明addLast(书目);
错误:' isInTheList '未在此范围内声明isInTheList(书目);
错误:' deleteBook '未在此范围内声明deleteBook(书目);
#include <iostream>
#include <string>
#include "Program3.h"
#include <limits> // for numeric limits
using namespace std;
//Implementation for class BookList START
void BookList::addFirst(BookNode * book){ //To add a new head node
book -> setNext(head);
head = book;
}
void BookList::addLast(BookNode * book){ //To add a new node at the end
if(!head){
addFirst(book);
}
else{
BookNode * temp = head;
while(temp->getNext() != NULL){
temp = temp->getNext();
}
temp -> setNext(book);
}
}
void BookList::traverse(){ //TO go through the whole linked list
BookNode * temp = head;
while(temp != NULL){
std::cout << temp -> getTitle()<<std::endl;
temp = temp -> getNext();
}
}
bool BookList::isInTheList(std::string title){ //To check the linked list for a specific title
BookNode * temp = head;
while(temp != NULL){
if(temp -> getTitle() == title){
return true;
}
temp = temp -> getNext();
}
return false;
}
bool BookList::deleteBook(std::string title){ //to delete a book/title
if(head == NULL){ // or if(!head) To check if the linked list is empty
return false;
}
BookNode * prev = NULL;
BookNode * cur = head;
if( cur -> getTitle() == title){ //Checking if the title is the first node
head = head -> getNext();
delete cur;
return true;
}
while(cur != NULL && cur -> getTitle() != title){ //Checking through the linked list for the title
prev = cur; //The while loop will stop when it has gone though the whole linked list or if it matches with the title
cur = cur -> getNext();
}
if(cur == NULL){ //There is no matching entry
return false;
}
else{
prev -> setNext( cur -> getNext() );
delete cur;
return true;
}
}
BookList::~BookList(){
while( head != NULL){
string tempTitle = head->getTitle();
deleteBook(tempTitle);
cout << "Book " << tempTitle << "has been deleted" <<endl;
}
}
int getUserChoice(){
int choice = 0;
cout << "Welcome to the e-library, please make a choice from the menu below" << endl;
cout << "1. Add a book at the beginning" << endl;
cout << "2. Add a book at the end" << endl;
cout << "3. Find a book in the list" << endl;
cout << "4. Delete a book in the list" << endl;
cout << "5. Print all the books in the list" << endl;
cout << "6. Exit " << endl;
if(cin >> choice){ //confirming that cin succeeded
if(choice > 0 && choice < 7){
cin.ignore(); //dump newline character
return choice;
}
else{
cin.ignore();//dump newline character
return 0;
}
}
else{
cin.clear(); //bring cin back from failed status
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
return 0;
}
}
int main(){
BookList booklist; //Create an object of ArrayList
int choice = getUserChoice(); //Prompting user for an input
while (choice != 6 ){
switch(choice){
case 0:
cout << "Invalid choice. Please Choose between 1 and 6." <<endl; // 0 is not a given choice, loop reruns
break;
case 1:
addFirst(booklist);
break;
case 2:
addLast(booklist);
break;
case 3:
isInTheList(booklist);
break;
case 4:
deleteBook(booklist);
break;
case 5:
booklist.traverse();
break;
default:
break;
}
choice = getUserChoice(); // prompt user to enter the choice again
return 0;
}
}
,然后我的头文件保存为Program.h在同一文件夹中,代码如下
#ifndef PGM_03_H//<-
#define PGM_03_H//<- both these need to have the same name
#include <string>
using namespace std;
/*class definition for BookNode
* which is non-compatible. i.e it has a pointer to the next BookNode
*/
class BookNode{
private :
std::string bookTitle;
BookNode * next;
public :
//default constructor
BookNode(){bookTitle = "";next = NULL;}
//custom constructor
//it initializes book with title
BookNode(string title){
bookTitle = title;
next = NULL;
}
//getter functions
std::string getTitle(){
return bookTitle;
}
BookNode * getNext(){
return next;
}
//setter functions
void setTitle(std::string newTitle){
bookTitle = newTitle;
}
void setNext(BookNode * newNext){
next = newNext;
}
};
/*
* class definition for BookList
* which is a linked list that uses object of BookNode as node.
* it has only one variable: head, which is a BookNode pointer.
*/
class BookList{
private:
BookNode * head;
public:
//default constructor
BookList(){
head = NULL;
}
//destructor, which will be called automatically
//it deletes all nodes in the linkedlist
~BookList();
//add new node as the first node in the booklist
void addFirst(BookNode *);
//add new node as the last node in the booklist
void addLast(BookNode *);
//traverse function. It will print out info on BookNode
void traverse();
//check if the given book is in the list
bool isInTheList(std::string);
//delete the given book
//return true if it was deleted
//return false if it was not found
bool deleteBook(std::string);
};
#endif
addFirst()
等是BookList
类的成员,但是您调用它们就好像它们只是函数一样。您需要一个BookList类的实例来处理这些方法。
您还将BookList
传递给期待BookNode
的addFirst
。你希望它看起来像:
booklist.addfirst(node);