Question: (PARTIAL CODE GIVEN) Create a C++ program that uses linked lists. (Missing only one function) This program displays a menu that allows you to create,
(PARTIAL CODE GIVEN) Create a C++ program that uses linked lists. (Missing only one function)
This program displays a menu that allows you to create, delete, insert, and show a list.
You will see that the initial list already includes the values 5, 10, and 15, in that order.
What is missing is the function to delete from the list. All other functions are complete.
When I choose to delete, I should be able to type in what value I'd like to delete.
Let's say, I choose to delete the number 5 from the list mentioned above.. the list should now be 10, 15
Here is the incomplete program that is missing the Delete function. Please test the program to make sure it runs well.
#include
using namespace std;
class numList {
private:
// Declare a structure for the list
struct nodeType
{
int value;
struct nodeType *link;
};
nodeType *head; // list head pointer
public:
numList() // Constructor
{
head = NULL;
}
~numList(); //Destructor
void appendNode(int);
void insertNode(double);
// void deleteNode(double); NEEDS TO BE DEFINED BELOW
void displayList();
};
// appendNode appends a node containing the value passed into num, to the end of the list.
void numList::appendNode(int num) {
nodeType *newNode, *nodePtr;
// Allocate a new node and store num
newNode = new nodeType;
newNode->value = num; // The value in this node will be whatever is passed through the parameters
newNode->link = NULL; // Pointing to nothing, pointing to no address
// If there are no nodes in the list, make newNode the first node
if (!head)
head = newNode;
else // Otherwise, insert newNode at the end
{
// Initialize nodePtr to head of list
nodePtr = head;
// Find the last node in the list
// While the link, what it is pointing to is not empty, it will keep going.
// At some point, it will be false and the while loop will stop.
while (nodePtr->link)
nodePtr = nodePtr->link;
// Insert newNode as the last node
nodePtr->link = newNode;
}
} // end of appendNode
// displaylist shows the vaue stored in each node of the linked lists
// Shows the list
void numList::displayList() {
nodeType *nodePtr;
nodePtr = head;
while (nodePtr != NULL)
{
cout << nodePtr->value << endl;
nodePtr = nodePtr->link;
}
}
//Definition for void insertNode(double);
void numList::insertNode(double d)
{ nodeType *add, *nodePtr;
// Allocate a new node and store num
add = new nodeType;
add->value = d; // The value in this node will be whatever is passed through the parameters
add->link = NULL; // Pointing to nothing, pointing to no address struct ListNode* node; if (head == NULL || head->value >= add->value) { add->link = head; head = add; } else { nodePtr = head; while (nodePtr->link!=NULL && nodePtr->link->value < add->value) { nodePtr = nodePtr->link; } add->link = nodePtr->link; nodePtr->link = add; }
}
// MISSING HERE the definition for void deleteNode(double);
void numList::deleteNode(double d)
{
}
// Destructor, this function deletes every node in the list.
numList::~numList() {
nodeType *nodePtr, *nextNode;
nodePtr = head;
while (nodePtr != NULL)
{
nextNode = nodePtr->link;
delete nodePtr;
nodePtr = nextNode;
}
}
char menu()
{
char ch;
cout << "\t\tWhat operation would like to perform? " << endl;
cout << "\t\t\tC. Create a list: " << endl;
cout << "\t\t\tD. Delete a list: " << endl;
cout << "\t\t\tI. Insert a list: " << endl;
cout << "\t\t\tS. Show a list : " << endl;
cout << "\t\t\tQ. Quit the program: " << endl;
cout << "\t\t Enter your choice ==> ";
cin >> ch;
return ch;
}
// Main
int main()
{
numList list;
int val; double d;
char choice;
choice = menu();
while (choice != 'Q' && choice != 'q')
{
system("cls");;
switch (choice){
case 'C': case 'c':
// Build the list
list.appendNode(5);
list.appendNode(10);
list.appendNode(15);
cout << "Here are the initial values: ";
list.displayList();
cout << endl;
break;
case 'D' : case 'd': // this is for Delete, which is missing
break;
case 'I' : case 'i': // this is for Insert, which is missing cout<<"Enter value to insert "; cin>>d; list.insertNode(d);
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;
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
