So I need help with having the user input into just one head of the linked list and once the user types '+' into the program, the linked list will insert a new node to the list and start again and once the user types the '=', the two heads add with each other.
Thank you for the help in advance.
I'll provide my current code, output and desired output:
Code:
#include<iostream>
#include<string>
using namespace std;
struct Node {
string data;
Node* next;
};
void addToList(Node* head);
int main() {
bool quit = false;
int choice;
Node* head = new Node;
head->next = NULL;
while (!quit) {
cout << "1. add to list" << endl
<< "2. quit" << endl;
cin >> choice;
switch (choice) {
case 1: addToList(head);
break;
case 2: quit = true;
break;
default:
cout << "That is not a valid input, quitting program";
quit = true;
}
}
}
void addToList(Node* head) {
bool quit = false;
string temp;
Node* current;
while (!quit) {
cout << "Enter a word(quit to stop)";
cin >> temp;
if (temp == "quit") {
quit = true;
}
else {
// Allocate the new node here:
current = new Node;
current->data = temp;
// the new node is inserted after the empty head
// because head is an empty node in your implementation:
current->next = head->next;
head->next = current;
// set current to head->next, because the head is empty in your implementation:
current = head->next;
while (current)
{
cout << current->data << endl;
current = current->next;
}
}
}
return;
}
Current Output:
Enter a number(quit to stop)3
3
Enter a number(quit to stop)4
4
3
Enter a number(quit to stop)5
5
4
3
Desired Output:
Enter a number(quit to stop)3
3
Enter a number(quit to stop)4
34
Enter a number(quit to stop)5
345
Enter a number(quit to stop)+
Enter a number(quit to stop)1
1
Enter a number(quit to stop)2
12
Enter a number(quit to stop)=
357