Question: (PARTIAL CODE GIVEN) Create a C++ program that uses linked lists. This program displays a menu that allows you to create, delete, insert, and show
(PARTIAL CODE GIVEN) Create a C++ program that uses linked lists.
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 and insert.
For example, if I choose to insert a 7 into that list, the new list should now be 5, 7, 10, 15
or if I choose to insert a 99, the new list should now be 5, 7, 10, 15, 99
Thus, it knows where to place the value in the list.
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 7.. the list should now be 5, 10, 15, 99
Here is the incomplete program that is missing the Delete and Insert functions that have been commented out in the class.
#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);
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;
}
}
// MISSING HERE the definition for void insertNode(double);
void numList::insertNode(double)
{
}
// MISSING HERE the definition for void deleteNode(double;
void numList::deleteNode(double)
{
}
// 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;
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
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
