Question: Here is the assignment: Objective: Create a sorted singly linked list of numbers based upon user input. Program logic: Ask for a number, add that

Here is the assignment:

Objective: Create a sorted singly linked list of numbers based upon user input.

Program logic: Ask for a number, add that number to the list in sorted position, print the list. Repeat until they enter -1 for the number.

Below is my code and I need help with implementing sortList:

#include

using namespace std;

class ValueNode { private:

ValueNode* next = NULL; int number;

public:

ValueNode(int value) { number = value; }

ValueNode* nextElement() { return next; }

void setNextElement(ValueNode* numberA) { next = numberA; }

int printElement() { return number; }

void sortList(ValueNode** head) { ValueNode* previous = (*head); ValueNode* current = (*head)->next;

while (current != NULL) { if (current->next < previous->next) // { previous->next = current->next; //

current->next = (*head); //move to beginning (*head) = current;

current = previous; }

else { previous = current; }

current = current->next; } } };

int main() { int entry = 0;

ValueNode** head = NULL;

ValueNode* previousNode = NULL; ValueNode* currentNode = NULL;

while (entry != -1) { cout << "Enter a value: "; cin >> entry;

if (entry == -1) { break; }

ValueNode* v = new ValueNode(entry);

if (previousNode == NULL) { (*head) = v; previousNode = v; }

else { previousNode->setNextElement(v); previousNode = v;

//v->sortList(head); } } if (head != NULL) { ValueNode* nextElement = (*head);

cout << "Value: "; while (nextElement) { cout << nextElement->printElement() << " "; nextElement = nextElement->nextElement(); }

} }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!