如何在链表中实现用户输入?
How to implement user input in a linked list?
对于此代码,我需要能够使用用户输入的他们已阅读的书籍并将它们放入链接列表中。我已经完成了大部分代码,但是当我尝试将书籍放入列表时,代码并未将书籍添加到列表中。我怎样才能解决这个问题?
这是 function.cpp 文件
void displayMenu()
{
cout << "[1] Add Book\n"
<< "[2] Size Of List\n"
<< "[3] Display List\n"
<< "[4] Remove Last Book\n"
<< "[5] Delete List\n"
<< "[6] Quit Program\n"
<< "Enter Choice: ";
}
int getChoice(int & choice1)
{
cin >> choice1;
while (choice1 < 1 || choice1 > 6) {
cout << endl;
cout << "Invalid Entry!!" << endl;
cout << "Enter Choice: ";
cin >> choice1;
}
return choice1;
}
int endProgram(bool & start2)
{
start2 = false;
cout << "\n\n\t\tThank you for using this system!!\n\n";
return start2;
}
void clear()
{
system("clear");
}
void linkedList::addBook()
{
Book *ptr;
bool quit = false;
string temp = "";
while (!quit)
{
cout << "Enter a book(enter quit to stop): ";
cin >> temp;
if (temp == "quit")
{
quit = true;
return;
}
ptr = new Book;
ptr->data = temp;
ptr->next = NULL;
if(head == NULL)
{
head = ptr;
tail = ptr;
}
else
{
tail->next = ptr;
tail = tail->next;
}
}
return;
}
void linkedList::displayList()
{
Book *ptr;
ptr = head;
while (ptr != NULL)
{
cout << ptr->data << endl;
ptr = ptr->next;
}
}
void linkedList::listSize()
{
Book *ptr;
int counter = 0;
ptr = head;
while (ptr != NULL)
{
ptr = ptr->next;
counter++;
}
cout << "Number of books in the list: " << counter;
}
void linkedList::deleteLast()
{
if (head == NULL)
return;
if (head->next == NULL)
{
delete head;
head = NULL;
return;
}
// Find the second last node
Book* ptr = head;
while (ptr->next->next != NULL)
ptr = ptr->next;
// Delete last node
delete (ptr->next);
// Change next of second last
ptr->next = NULL;
if(ptr == NULL)
cout << "Last book is cleared!" << endl;
}
void linkedList::deleteList()
{
Book *ptr;
while (head != NULL)
{
ptr = head->next;
delete head;
head = ptr;
}
if(head == NULL)
cout <<"List is cleared!" << endl;
}
这是 main.cpp 文件
int main() {
int choice = 0;
bool start = true;
linkedList a;
while(start != false)
{
while(choice != 6)
{
displayMenu();
getChoice(choice);
if(choice == 1)
{
clear();
a.addBook();
}
if(choice == 2)
{
clear();
a.listSize();
}
if(choice == 3)
{
clear();
a.displayList();
cout << endl;
}
if(choice == 4)
{
clear();
a.deleteLast();
}
if(choice == 5)
{
clear();
a.deleteList();
}
if(choice == 6)
{
clear();
endProgram(start);
}
}
}
}
最后一个是 function.h 文件
#include <iostream>
#include <string>
using namespace std;
struct Book
{
string data;
Book *next;
};
class linkedList
{
private:
Book *head,*tail;
public:
linkedList()
{
head = NULL;
tail = NULL;
}
void addBook();
void displayList();
void listSize();
void deleteList();
void deleteLast();
};
void displayMenu();
int getChoice(int & choice1);
int endProgram(bool & start2);
void clear();
好的知道了
在deleteLast
if (head->next == NULL)
{
delete head;
return;
}
你忘记更新头像了
if (head->next == NULL)
{
delete head;
head = NULL; <<<<=====
return;
}
同样在删除函数中你不更新tail,你需要
Book* ptr = head;
while (ptr->next->next != NULL)
ptr = ptr->next;
// Delete last node
delete (ptr->next);
tail = ptr;<<<<<<<<<<<===================
// Change next of second last
ptr->next = NULL;
你真的需要学习使用你的调试器
对于此代码,我需要能够使用用户输入的他们已阅读的书籍并将它们放入链接列表中。我已经完成了大部分代码,但是当我尝试将书籍放入列表时,代码并未将书籍添加到列表中。我怎样才能解决这个问题? 这是 function.cpp 文件
void displayMenu()
{
cout << "[1] Add Book\n"
<< "[2] Size Of List\n"
<< "[3] Display List\n"
<< "[4] Remove Last Book\n"
<< "[5] Delete List\n"
<< "[6] Quit Program\n"
<< "Enter Choice: ";
}
int getChoice(int & choice1)
{
cin >> choice1;
while (choice1 < 1 || choice1 > 6) {
cout << endl;
cout << "Invalid Entry!!" << endl;
cout << "Enter Choice: ";
cin >> choice1;
}
return choice1;
}
int endProgram(bool & start2)
{
start2 = false;
cout << "\n\n\t\tThank you for using this system!!\n\n";
return start2;
}
void clear()
{
system("clear");
}
void linkedList::addBook()
{
Book *ptr;
bool quit = false;
string temp = "";
while (!quit)
{
cout << "Enter a book(enter quit to stop): ";
cin >> temp;
if (temp == "quit")
{
quit = true;
return;
}
ptr = new Book;
ptr->data = temp;
ptr->next = NULL;
if(head == NULL)
{
head = ptr;
tail = ptr;
}
else
{
tail->next = ptr;
tail = tail->next;
}
}
return;
}
void linkedList::displayList()
{
Book *ptr;
ptr = head;
while (ptr != NULL)
{
cout << ptr->data << endl;
ptr = ptr->next;
}
}
void linkedList::listSize()
{
Book *ptr;
int counter = 0;
ptr = head;
while (ptr != NULL)
{
ptr = ptr->next;
counter++;
}
cout << "Number of books in the list: " << counter;
}
void linkedList::deleteLast()
{
if (head == NULL)
return;
if (head->next == NULL)
{
delete head;
head = NULL;
return;
}
// Find the second last node
Book* ptr = head;
while (ptr->next->next != NULL)
ptr = ptr->next;
// Delete last node
delete (ptr->next);
// Change next of second last
ptr->next = NULL;
if(ptr == NULL)
cout << "Last book is cleared!" << endl;
}
void linkedList::deleteList()
{
Book *ptr;
while (head != NULL)
{
ptr = head->next;
delete head;
head = ptr;
}
if(head == NULL)
cout <<"List is cleared!" << endl;
}
这是 main.cpp 文件
int main() {
int choice = 0;
bool start = true;
linkedList a;
while(start != false)
{
while(choice != 6)
{
displayMenu();
getChoice(choice);
if(choice == 1)
{
clear();
a.addBook();
}
if(choice == 2)
{
clear();
a.listSize();
}
if(choice == 3)
{
clear();
a.displayList();
cout << endl;
}
if(choice == 4)
{
clear();
a.deleteLast();
}
if(choice == 5)
{
clear();
a.deleteList();
}
if(choice == 6)
{
clear();
endProgram(start);
}
}
}
}
最后一个是 function.h 文件
#include <iostream>
#include <string>
using namespace std;
struct Book
{
string data;
Book *next;
};
class linkedList
{
private:
Book *head,*tail;
public:
linkedList()
{
head = NULL;
tail = NULL;
}
void addBook();
void displayList();
void listSize();
void deleteList();
void deleteLast();
};
void displayMenu();
int getChoice(int & choice1);
int endProgram(bool & start2);
void clear();
好的知道了
在deleteLast
if (head->next == NULL)
{
delete head;
return;
}
你忘记更新头像了
if (head->next == NULL)
{
delete head;
head = NULL; <<<<=====
return;
}
同样在删除函数中你不更新tail,你需要
Book* ptr = head;
while (ptr->next->next != NULL)
ptr = ptr->next;
// Delete last node
delete (ptr->next);
tail = ptr;<<<<<<<<<<<===================
// Change next of second last
ptr->next = NULL;
你真的需要学习使用你的调试器