Question: C++, Write a class LinkList, which implements a sorted linked list of floats. The class should have member functions that handle the following tasks: Initialize

C++, Write a class LinkList, which implements a sorted linked list of floats. The class should have member functions that handle the following tasks:

Initialize a new linked list to being empty.

Add a float value to the list this function should add the value to the list so that the list is always in sorted order from smallest to largest value.

Display the list.

Find a float value in the list and return the position number of the value in the list (assume position number counting starts with one). If the value is not found, this function should return zero.

Find the nth link in the list and return the address of the link. If the list does not have an nth link, or if the list is empty, this function should return NULL.

Delete the nth link in the list. If n is more than the number of links in the list, delete nothing, and give the user an error message.

Show these list statistics the total number of values in the list, the mean value, and the median value. Note: If there are an odd number of values in the list, the median is the middle value; otherwise, it is the average of the two middle values.

Note: The structure you create to represent the nodes of your linked list should contain only two member variables: a variable to store a floating-point number and a pointer to the structure.

Note: The LinkList class you create should contain only one member variable: one pointer to the structure for holding the address of the first item in the linked list.

Using your LinkList class, write a C++ program that creates one LinkList object and then repeatedly offers the user these options for working on this linked list:

a)Add a value to the list the value is added so that the list is sorted from smallest to largest.

b)Search for a value in the list if found, displays the position number of the value in the list; if not found, displays a message indicating this.

c)Display the nth value in the list if there is no nth value, display a message indicating this.

d)Delete the nth value in the list if there is no nth value, display a message indicating this.

e)Display list statistics (count, mean, median).

f)Display the entire list.

g)Exit the program.

CAN YOU HELP ME TO FINISH IT?

I TRIED TO START IT.

#include using namespace std;

struct ListNode { float value; ListNode *next; }; ListNode *head;

class LinkedList { public: int insertNode(float num); int appendNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} };

int LinkedList::appendNode(float num) { ListNode *newNode, *nodePtr = head; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member! "; return 1; } newNode->value = num; newNode->next = NULL; if(head == NULL) { cout << "List was empty - " << newNode->value; cout << " is part of list's first node. "; head = newNode; } else { while(nodePtr->next != NULL) nodePtr = nodePtr->next; nodePtr->next = newNode; } return 0; }

int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL;

newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member! "; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List was empty - " << newNode->value; cout << " is part of list's first node. "; head = newNode; } else { while((nodePtr != NULL) && (nodePtr->value < num)) { prevNodePtr = nodePtr; nodePtr = nodePtr->next; } if(prevNodePtr==NULL) newNode->next = head; else newNode->next = nodePtr; prevNodePtr->next = newNode; } return 0; }

void LinkedList::deleteNode(float num) { struct ListNode *nodePtr = head, *prevNodePtr = NULL;

if(head==NULL) { cout << "The list was empty! "; return; } if(head->value == num) { head = nodePtr->next; delete [] nodePtr; } else { while((nodePtr!= NULL)&&(nodePtr->value != num)) { prevNodePtr = nodePtr; nodePtr = nodePtr->next; } if(nodePtr==NULL) cout << "The value " << num << " is not in this list! "; else { prevNodePtr->next = nodePtr->next; delete [] nodePtr; } } }

void LinkedList::destroyList() { struct ListNode *nodePtr = head, *nextNode = nodePtr;

if(head==NULL) { cout << "The list is empty! "; return; } while (nodePtr != NULL) { nextNode = nodePtr->next; delete [] nodePtr; nodePtr = nextNode; } }

void LinkedList::displayList() { struct ListNode *nodePtr = head; if (nodePtr == NULL) cout << "The list is empty! "; else { while (nodePtr != NULL) { cout << nodePtr->value << endl; nodePtr = nodePtr->next; } } }

int main() { int num; char answer, choice; LinkedList temp; do { cout << "Please enter a value to put in the list --> "; cin >> num; temp.insertNode(num); cout << endl; cout << "Would you like to put another value into your list? "; cin >> answer; } while(toupper(answer)=='Y');

cout << "I will now display your list! "; cout << endl; temp.displayList(); }

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!