Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Finish this program and show the output C++ language Floating Point Link List Ex

ID: 3816916 • Letter: F

Question

Finish this program and show the output

C++ language

Floating Point Link List Example

// This program demonstrates the deleteNode member function

#include <iostream>

#include <cstdlib>

#include "FloatList.h"

using namespace std;

char menu()

{ char ch;

      

       cout<<" What Operation would like to perform ? ";

       cout<<" C.              Create a List ";

       cout<<" D.              Delete a List ";

       cout<<" I.              Insert a List ";

       cout<<" S.              Show a List ";

       cout<<" Q.              Quit the Program ";

       cout<<" Enter your choice ==> ";

       cin>>ch;

       return ch;

}

int main()

{      FloatList list;

       float val;

       char choice;

       choice = menu();

while (choice !='Q' && choice != 'q')

{      system("cls");

       switch(choice) {

       case 'C': case 'c':

              // Build the list

              list.appendNode(2.5);

              list.appendNode(7.9);

              list.appendNode(12.6);

              cout << "Here are the initial values: ";

              list.displayList();

              cout << endl;

              break;

       case 'D': case 'd':

              cout<<" Here is the contents of the current list ";

              list.displayList();

              cout<<" Enter your value to be destroyed ..";

              cin>>val;

              list.deleteNode(val);

              cout << " Here are the nodes left. ";

              list.displayList();

              break;

       case 'I': case 'i':

              cout<<" Enter your value to be inserted ..";

              cin>>val;

              list.insertNode(val);

              cout << " Here are the new nodes. ";

              list.displayList();

              break;

       case 'S': case 's':

              cout<<" Here are the list of nodes .... ";

              list.displayList();

       }

       choice = menu();

       }

       cout<<" Here are the left over nodes before quitting ... ";

       list.displayList();

       cout<<endl<<endl;

       return 0;

}

Linklist header File:

// Specification file for the FloatList class

#ifndef FLOATLIST_H

#define FLOATLIST_H

class FloatList

{

private:

       // Declare a structure for the list

       struct ListNode

       {

              float value;

              struct ListNode *next;

       };

       ListNode *head;       // List head pointer

public:

       FloatList()   // Constructor

              { head = NULL; }

       ~FloatList(); // Destructor

       void appendNode(float);

       void insertNode(float);

       void deleteNode(float);

       void displayList();

};

#endif

LinkList CPP File:

// Implementation file for the FloatList class

#include <iostream> // For cout and NULL

#include "FloatList.h"

using namespace std;

//**************************************************

// appendNode appends a node containing the        *

// value pased into num, to the end of the list.   *

//**************************************************

void FloatList::appendNode(float num)

{

       ListNode *newNode, *nodePtr;

       // Allocate a new node & store num

       newNode = new ListNode;

       newNode->value = num;

       newNode->next = NULL;

       // If there are no nodes in the list

       // make newNode the first node

       if (!head)

              head = newNode;

       else   // Otherwise, insert newNode at end

       {

              // Initialize nodePtr to head of list

              nodePtr = head;

              // Find the last node in the list

              while (nodePtr->next)

                      nodePtr = nodePtr->next;

              // Insert newNode as the last node

              nodePtr->next = newNode;

       }

}

//**************************************************

// displayList shows the value                     *

// stored in each node of the linked list          *

// pointed to by head.                             *

//**************************************************

void FloatList::displayList()

{

       ListNode *nodePtr;

       nodePtr = head;

       while (nodePtr!= NULL)

       {

              cout << nodePtr->value << endl;

              nodePtr = nodePtr->next;

       }

}

//**************************************************

// The insertNode function inserts a node with     *

// num copied to its value member.                 *

//**************************************************

void FloatList::insertNode(float num)

{

       ListNode *newNode, *nodePtr, *previousNode = NULL;

       // Allocate a new node & store num

       newNode = new ListNode;

       newNode->value = num;

      

       // If there are no nodes in the list

       // make newNode the first node

       if (!head)

       {

              head = newNode;

              newNode->next = NULL;

       }

       else   // Otherwise, insert newNode

       {

              // Initialize nodePtr to head of list and previousNode to NULL.

              nodePtr = head;

              previousNode = NULL;

              // Skip all nodes whose value member is less

              // than num.

              while (nodePtr != NULL && nodePtr->value < num)

              {     

                      previousNode = nodePtr;

                      nodePtr = nodePtr->next;

              }

              // If the new node is to be the 1st in the list,

              // insert it before all other nodes.

              if (previousNode == NULL)

              {

                      head = newNode;

                      newNode->next = nodePtr;

              }

              else   // Otherwise, insert it after the prev. node.

              {

                      previousNode->next = newNode;

                      newNode->next = nodePtr;

              }

       }

}

//**************************************************

// The deleteNode function searches for a node     *

// with num as its value. The node, if found, is   *

// deleted from the list and from memory.          *

//**************************************************

void FloatList::deleteNode(float num)

{

       ListNode *nodePtr, *previousNode;

       // If the list is empty, do nothing.

       if (!head)

              return;

      

       // Determine if the first node is the one.

       if (head->value == num)

       {

              nodePtr = head->next;

              delete head;

              head = nodePtr;

       }

       else

       {

              // Initialize nodePtr to head of list

              nodePtr = head;

              // Skip all nodes whose value member is

              // not equal to num.

              while (nodePtr != NULL && nodePtr->value != num)

              {     

                      previousNode = nodePtr;

                      nodePtr = nodePtr->next;

              }

              // If nodePtr is not at the end of the list,

              // link the previous node to the node after

              // nodePtr, then delete nodePtr.

              if (nodePtr)

              {

                      previousNode->next = nodePtr->next;

                      delete nodePtr;

              }

       }

}

//**************************************************

// Destructor                                      *

// This function deletes every node in the list.   *

//**************************************************

FloatList::~FloatList()

{

       ListNode *nodePtr, *nextNode;

       nodePtr = head;

       while (nodePtr != NULL)

       {

              nextNode = nodePtr->next;

              delete nodePtr;

              nodePtr = nextNode;

       }

}

Explanation / Answer

As there were no instruction on what needs to be finished, I toook the liberty and fixed minor bugs

// main.cpp

// This program demonstrates the deleteNode member function
#include <iostream>
#include <cstdlib>
#include "FloatList.h"
using namespace std;


char menu()
{ char ch;
  
cout<<" What Operation would like to perform ? ";
cout<<" C. Create a List ";
cout<<" D. Delete a List ";
cout<<" I. Insert a List ";
cout<<" S. Show a List ";
cout<<" Q. Quit the Program ";
cout<<" Enter your choice ==> ";
cin>>ch;
return ch;
}

int main()
{ FloatList list;
float val;
char choice;
choice = menu();
while (choice !='Q' && choice != 'q')
{ system("cls");
switch(choice) {
case 'C': case 'c':
// Build the list
list.appendNode(2.5);
list.appendNode(7.9);
list.appendNode(12.6);
cout << "Here are the initial values: ";
list.displayList();
cout << endl;
break;
case 'D': case 'd':
cout<<" Here is the contents of the current list ";
list.displayList();
cout<<" Enter your value to be destroyed ..";
cin>>val;
list.deleteNode(val);
cout << " Here are the nodes left. ";
list.displayList();
break;
case 'I': case 'i':
cout<<" Enter your value to be inserted ..";
cin>>val;
list.insertNode(val);
cout << " Here are the new nodes. ";
list.displayList();
break;
case 'S': case 's':
cout<<" Here are the list of nodes .... ";
list.displayList();
}
choice = menu();
}

cout<<" Here are the left over nodes before quitting ... ";
list.displayList();
cout<<endl<<endl;
return 0;
}

// FloatList.h

// Specification file for the FloatList class
#ifndef FLOATLIST_H
#define FLOATLIST_H

class FloatList
{
private:
// Declare a structure for the list
struct ListNode
{
float value;
struct ListNode *next;
};

ListNode *head; // List head pointer

public:
FloatList(); // Constructor
~FloatList(); // Destructor
void appendNode(float);
void insertNode(float);
void deleteNode(float);
void displayList();
};


#endif

// FloatList.cpp

// Implementation file for the FloatList class
#include <iostream> // For cout and NULL
#include "FloatList.h"
using namespace std;

FloatList::FloatList()
{
head = NULL;
}

//**************************************************
// appendNode appends a node containing the *
// value pased into num, to the end of the list. *
//**************************************************
void FloatList::appendNode(float num)
{
ListNode *newNode, *nodePtr;

// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
newNode->next = NULL;

// If there are no nodes in the list
// make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at end
{
// Initialize nodePtr to head of list
nodePtr = head;

// Find the last node in the list
while (nodePtr->next)
nodePtr = nodePtr->next;

// Insert newNode as the last node
nodePtr->next = newNode;
}
}

//**************************************************
// displayList shows the value *
// stored in each node of the linked list *
// pointed to by head. *
//**************************************************

void FloatList::displayList()
{
ListNode *nodePtr;

nodePtr = head;
while (nodePtr!= NULL)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->next;
}
}

//**************************************************
// The insertNode function inserts a node with *
// num copied to its value member. *
//**************************************************

void FloatList::insertNode(float num)
{
ListNode *newNode, *nodePtr, *previousNode = NULL;

// Allocate a new node & store num
newNode = new ListNode;
newNode->value = num;
  
// If there are no nodes in the list
// make newNode the first node
if (!head)
{
head = newNode;
newNode->next = NULL;
}
else // Otherwise, insert newNode
{
// Initialize nodePtr to head of list and previousNode to NULL.
nodePtr = head;
previousNode = NULL;

// Skip all nodes whose value member is less
// than num.
while (nodePtr != NULL && nodePtr->value < num)
{   
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

// If the new node is to be the 1st in the list,
// insert it before all other nodes.
if (previousNode == NULL)
{
newNode->next = head;
head = newNode;
}
else // Otherwise, insert it after the prev. node.
{
previousNode->next = newNode;

newNode->next = nodePtr;
}
}
}

//**************************************************
// The deleteNode function searches for a node *
// with num as its value. The node, if found, is *
// deleted from the list and from memory. *
//**************************************************

void FloatList::deleteNode(float num)
{
ListNode *nodePtr, *previousNode;

// If the list is empty, do nothing.
if (!head)
return;
  
// Determine if the first node is the one.
if (head->value == num)
{
nodePtr = head->next;
delete head;
head = nodePtr;
}
else
{
// Initialize nodePtr to head of list
nodePtr = head;

// Skip all nodes whose value member is
// not equal to num.
while (nodePtr != NULL && nodePtr->value != num)
{   
previousNode = nodePtr;
nodePtr = nodePtr->next;
}

// If nodePtr is not at the end of the list,
// link the previous node to the node after
// nodePtr, then delete nodePtr.
if (nodePtr)
{
previousNode->next = nodePtr->next;
delete nodePtr;
}
}
}

//**************************************************
// Destructor *
// This function deletes every node in the list. *
//**************************************************

FloatList::~FloatList()
{
ListNode *nodePtr, *nextNode;

nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->next;
delete nodePtr;
nodePtr = nextNode;
}
}

Sample output:

       What Operation would like to perform ?
           C. Create a List
           D. Delete a List
           I. Insert a List
           S. Show a List
           Q. Quit the Program
       Enter your choice ==> Here are the initial values: C
2.5
7.9
12.6

       What Operation would like to perform ?
           C. Create a List
           D. Delete a List
           I. Insert a List
           S. Show a List
           Q. Quit the Program
       Enter your choice ==> I
Enter your value to be inserted ..0.5

Here are the new nodes.
0.5
2.5
7.9
12.6
       What Operation would like to perform ?
           C. Create a List
           D. Delete a List
           I. Insert a List
           S. Show a List
           Q. Quit the Program
       Enter your choice ==>D
Here is the contents of the current list
0.5
2.5
7.9
12.6

Enter your value to be destroyed ..3.5

Here are the nodes left.
0.5
2.5
7.9
12.6
       What Operation would like to perform ?
           C. Create a List
           D. Delete a List
           I. Insert a List
           S. Show a List
           Q. Quit the Program
       Enter your choice ==> D
Here is the contents of the current list
0.5
2.5
7.9
12.6

Enter your value to be destroyed ..12.6

Here are the nodes left.
0.5
2.5
7.9
       What Operation would like to perform ?
           C. Create a List
           D. Delete a List
           I. Insert a List
           S. Show a List
           Q. Quit the Program
       Enter your choice ==> S

Here are the list of nodes ....
0.5
2.5
7.9
       What Operation would like to perform ?
           C. Create a List
           D. Delete a List
           I. Insert a List
           S. Show a List
           Q. Quit the Program
       Enter your choice ==> Q

Here are the left over nodes before quitting ...
0.5
2.5
7.9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote