Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

in c++ Filename : Lab18a.cpp #include IntList.h #include #include void displayHelp() { std::cout int main() { IntList myList; // Create an instance of the IntList

in c++

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Filename: Lab18a.cpp

#include "IntList.h" #include #include

void displayHelp() { std::cout

int main() { IntList myList; // Create an instance of the IntList class char command; // Variable to store the user's command int value; // Variable to store the user-input value

bool firstCommand = true; //loop through the command until program finished. do { if (!firstCommand) { std::cout

std::cout > command; // Get the user's command

switch (command) { case 'a': std::cout > value; myList.appendNode(value); // Append a node to the end of the list break; case 'c': std::cout > value; myList.deleteNode(value); // Delete a node from the list break; case 'i': std::cout > value; myList.insertNode(value); // Insert a node into the list in sorted order break; case 'm': myList.maxValue(); // Display the maximum value in the list

break; case 'p': myList.print(); // Print the contents of the list break; case 'h': displayHelp(); // Display the help text break; case 't': std::cout

return 0; }

Filename: IntList.h

#ifndef INTLIST_H #define INTLIST_H

class IntList { private: struct ListNode { int value; ListNode *next; }; // List head pointer ListNode *head;

public: // Constructor IntList();

// Destructor ~IntList();

// Append a new node at the end of the list void appendNode(int);

// Insert a new node into the list, maintaining a sorted order void insertNode(int);

// Remove a node from the list void deleteNode(int);

// Output the contents of the list to the screen void print();

// Display the list length (number of nodes) to the caller int length();

// Display the largest data value in the list void maxValue();

// Return the total (sum) of all list values to the caller int total(); };

#endif

Filename: IntList.cpp

#include "IntList.h" #include

// Constructor IntList::IntList() { head = nullptr; }

// Loop through the list and delete each node IntList::~IntList() { while (head) { ListNode *temp = head; head = head->next; delete temp; } }

// Append a new node at the end of the list void IntList::appendNode(int value) { ListNode *newNode = new ListNode{value, nullptr}; if (!head) { head = newNode; } else { ListNode *temp = head; while (temp->next) { temp = temp->next; } temp->next = newNode; } }

// Insert a new node into the list, maintaining a sorted order void IntList::insertNode(int value) { ListNode *newNode = new ListNode{value, nullptr}; if (!head || head->value >= value) { newNode->next = head; head = newNode; } else { ListNode *prev = nullptr; ListNode *current = head; while (current && current->value next; } newNode->next = current; prev->next = newNode; } }

// Remove a node from the list void IntList::deleteNode(int value) { ListNode *prev = nullptr; ListNode *current = head;

while (current && current->value != value) { prev = current; current = current->next; }

if (current) { // Node with the specified value found if (!prev) { // If it's the head, update head to the next node head = current->next; } else { // Update prev's next to skip the current node prev->next = current->next; } delete current; } else { // Node with the specified value not found std::cout

// Output the contents of the list to the screen void IntList::print() { ListNode *temp = head; std::cout value next next; } }

// Display the list length (number of nodes) to the caller int IntList::length() { int count = 0; ListNode *temp = head; while (temp!=NULL) { count++; temp = temp->next; } return count; }

// Display the largest data value in the list void IntList::maxValue() { if (!head) { std::cout value; ListNode *temp = head->next; while (temp) { if (temp->value > maxVal) { maxVal = temp->value; } temp = temp->next; } std::cout

// Return the total (sum) of all list values to the caller int IntList::total() { int sum = 0; ListNode *temp = head; while (temp) { sum += temp->value; temp = temp->next; } return sum; }

The commands (from Lab 18a) that must still be supported are: a APPEND a new node at the end of the list c COUNT the nodes in the list (display the list length). d DELETE a node from the list i INSERT a node into the list, maintaining the sorted order. m display the MAXIMUM (largest) value in the list. p PRINT the contents of the list. h HELP text q QUIT (end the program) t display the TOTAL of all values in the list. The APPEND, DELETE, and INSERT commands must prompt the user to enter an integer value. New Interactive Commands for Lab 20a The new commands that must be supported are: r RECURSIVELY print the contents of the list (in the normal "forward" direction). b Recursively print the contents of the list BACKWARDS. (We discussed how to do this during class.) n (lower-case ' N ') recursively count the NUMBER of nodes in the list. s (lower-case ' S ') recursively calculate the SUM of all values in list. New EXTRA CREDIT (optional) Command I (lower-case ' L ') recursively calculate the LARGEST value in the list. Sample Input / Output A sample input / output session is shown beginning below, and continuing on the following pages. (In this example, the text that the user enters is shown in a larger, bold font. In actuality, all text appears in the same font.) The initial steps in this sample input/output session utilize commands that were originally implemented in Lab18a. Then, after the linked list contains enough sample data, the new (recursive) commands are demonstrated. This sample input / output session also includes the optional (extra credit) "I" (lower-case L) command. Sample Input / Output Command: h Supported commands: APPEND a value to the list. BACKWARDS RECURSIVE PRINT: use recursion to print the list backwards. COUNT nodes in the list, using a loop. DELETE a value from the list. INSERT a value into the list. (lower-case 'L') recursively determine LARGEST value in list. calculate MAXIMUM value in the list. use RECURSION to count the NUMBER of nodes in the list. PRINT the list contents. RECURSIVE PRINT: use recursion to print the list contents. recursively calculate SUM of all values in list. Calculate TOTAL of all values in the list. print help text. quit (end the program). Command: i Enter number to insert into the list: 4 Command: i Enter number to insert into the list: 54 Command: i Enter number to insert into the list: 3 Command: i Enter number to insert into the list: 99 Sample Input / Output Command: i Enter number to insert into the list: 55 Command: i Enter number to insert into the list: 6 Command: p head=0000021E51E515A0 0000021E51E515A0: value =3 next =0000021E51E51B40 0000021E51E51B40: value =4 next=0000021E51E51BE0 0000021E51E51BE0: value =6 next=0000021E51E51EB0 0000021E51E51EB0:value=54 next=0000021E51E514B0 0000021E51E514B0: value =55 next =0000021E51E51460 0000021E51E51460 : value =99 next =0000000000000000 Command: C Number of nodes in list, head=0000021E51E515A0, count=6 Command: m MAX data value =99 Command: t Total of all value fields in list, head=0000021E51E515A0, total=221 Command: a Enter number to append to the list: 2 Command: p head =0000021E51E515A0 0000021E51E515A0: value =3 next=0000021E51E51B40 0000021E51E51B40: value =4 next=0000021E51E51BE0 0000021E51E51BE0: value =6 next=0000021E51E51EB0 0000021E51E51EB0:value=54 next=0000021E51E514B0 0000021E51E514B0:value=55next=0000021E51E51460 0000021E51E51460 : value =99 next=0000021E51E51640 0000021E51E51640: value =2 next =0000000000000000 Command: r Recursive print of list, head=0000021E51E515A0 0000021E51E515A0: value =3 next=0000021E51E51B40 0000021E51E51B40: value =4 next=0000021E51E51BE0 0000021E51E51BE0: value =6 next=0000021E51E51EB0 0000021E51E51EB0: value =54 next=0000021E51E514B0 0000021E51E514B0: value =55 next=0000021E51E51460 0000021E51E51460 : value =99 next=0000021E51E51640 0000021E51E51640: value =2 next =0000000000000000 Sample Input / Output Command: b BACKWARDS recursive print of list, head=0000021E51E515A0 0000021E51E51640: value =2 next =000000000000000 0000021E51E51460: value =99 next =0000021E51E51640 0000021E51E514B0: value =55 next =0000021E51E51460 0000021E51E51EB0: value =54 next =0000021E51E514B0 0000021E51E51BE0: value =6 next =0000021E51E51EB0 0000021E51E51B40: value =4 next =0000021E51E51BE0 0000021E51E515A0: value =3 next =0000021E51E51B40 Command: C Number of nodes in list, head=0000021E51E515A0, count=7 Command: n Number of nodes in the list =7 Command: t Total of all value fields in list, head=0000021E51E515A0, total=223 Command: s The sum of all data values is 223 . Command: m MAX data value =99 Command: 1 LARGEST data value =99 Command: q Exiting program with status =0

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions