'How to implement user input in a linked list?
For this code I need to be able to use user input of books they have read and put them in a linked list. I have most of the code done but when I try putting books in the list the code isn't adding the books to the list. How can I fix this? this is the function.cpp file
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;
}
This is the main.cpp file
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);
}
}
}
}
Last is the function.h file
#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();
Solution 1:[1]
OK got it
In deleteLast
if (head->next == NULL)
{
delete head;
return;
}
You forgot to update head
if (head->next == NULL)
{
delete head;
head = NULL; <<<<=====
return;
}
also in the delete function you don't update tail, you need
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;
You really need to learn to use your debugger
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | user4581301 |
