C++, Write a class LinkList, which implements a sorted linked list of floats. Th
ID: 3821701 • Letter: C
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 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();
}
Explanation / Answer
#include <iostream>
#include <cstdlib>
using namespace std;
struct ListNode {
float value;
ListNode *next;
};
ListNode *head;
class LinkedList {
public:
void insertNode(float num);
void deleteNode(float num);
void destroyList();
void displayList();
void searchNode(float num);
void displayNth(int n);
void deleteNth(int n);
void displayStatistics();
LinkedList(void) {head = NULL;}
~LinkedList(void) {destroyList();}
};
void 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;
}
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 if (nodePtr->value > num)
{
newNode->next = head;
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;
}
}
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 {
cout << "List: ";
while (nodePtr != NULL) {
cout << nodePtr->value << " -> ";
nodePtr = nodePtr->next;
}
cout << "NULL ";
}
}
void LinkedList::searchNode(float num){
struct ListNode *nodePtr = head;
if(head==NULL) {
cout << "The list is empty! ";
return;
}
if(head->value == num) {
head = nodePtr->next;
cout << "At position: 1 ";
}
else {
int count = 1;
while((nodePtr!= NULL)&&(nodePtr->value != num)) {
nodePtr = nodePtr->next;
count++;
}
if(nodePtr==NULL) cout << "The value " << num << " is not in this list! ";
else {
cout << "At position: " << count << ' ';
}
}
}
void LinkedList::displayNth(int n){
if(!head) cout << " List is empty!";
ListNode* temp = head;
int location = 1;
while(location != n && temp){
temp = temp->next;
location++;
}
if(temp) cout << " Value at " << n << " = " << temp->value;
else cout << " Invalid location! ";
}
void LinkedList::deleteNth(int n){
struct ListNode *nodePtr = head, *prevNodePtr = NULL;
int location = 1;
if(head==NULL) {
cout << "The list is empty! ";
return;
}
if(location == n) {
head = nodePtr->next;
delete [] nodePtr;
return;
}
else {
while((nodePtr!= NULL)&&(location!=n)) {
prevNodePtr = nodePtr;
nodePtr = nodePtr->next;
location++;
}
if(nodePtr==NULL) cout << " Invalid location! ";
else {
prevNodePtr->next = nodePtr->next;
delete [] nodePtr;
}
}
}
void LinkedList::displayStatistics(){
int count = 1;
float sum = 0;
ListNode* temp = head;
while(temp){
sum += temp->value;
temp=temp->next;
count++;
}
cout << " Count: " << count
<< " Mean: " << sum/count
<< " Median(s): ";
displayNth(int(count/2 + 0.5));
cout << " ";
}
int main()
{
float num;
unsigned int choice, location;
LinkedList temp;
string menu = " 1 Add a value to the list 2 Search for a value in the list 3 Display the nth value in the list 4 Delete the nth value in the list 5 Display list statistics 6 Display the entire list 7 Exit the program ";
cout << menu;
while(1){
cin >> choice;
switch(choice){
case 1:
cout <<" Enter value: ";
cin >> num;
temp.insertNode(num);
break;
case 2:
cout <<" Enter value: ";
cin >> num;
temp.searchNode(num);
break;
case 3:
cout <<" Enter the location: ";
cin >> location;
temp.displayNth(location);
break;
case 4:
cout <<" Enter the location: ";
cin >> location;
temp.deleteNth(location);
break;
case 5:
cout <<" Statistics: ";
temp.displayStatistics();
break;
case 6:
cout <<' ';
temp.displayList();
break;
case 7:
exit(0);
break;
default:
cout << " Please choose proper option: ";
}
cout << menu;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.