Question: Please write the code for this program. Thank you. When building C++ programs, we can arrange groups of dynamically allocated self-referential structs or classes into

Please write the code for this program. Thank you.

When building C++ programs, we can arrange groups of dynamically allocated self-referential structs or classes into a flexible data structure known as a linked list. Linked lists are ubiquitous in C++ programs. They are used for arranging and analyzing groups of data representing things from stocks and bonds in a portfolio to the locations of players in a video game. Despite the fact that they can be used to represent disparate things in such a wide range of C++ applications, most linked lists are built and manipulated using the same small set of basic operations. In this assignment we will be building and manipulating a list of integers. These integers will be entered by the user. We will be using the following self-referential C++ class for each item in the list:

class ListNode

{

int val;

ListNode * next;

public:

ListNode();

int getValue() const;

void setValue(int);

ListNode * getNext() const;

void setNext(ListNode *);

};

The definition of the ListNode constructor and its methods appears below:

ListNode::ListNode()

{

val = -1;

}

int ListNode::getValue() const

{

return val;

}

void ListNode::setValue(int newval)

{

val = newval;

}

ListNode * ListNode::getNext() const

{

return next;

}

void ListNode::setNext(ListNode * p)

{

next = p;

}

Build a C++ program that interacts with a user to create a list of class ListNode elements. Your program should continually ask the user to enter integers one at a time, and should build a list of class ListNode items, one class ListNode element for each integer entered. If the user enters an integer of value 0, your program should stop asking the user to enter integers. After the user has finished entering all integers, your program must print out all the integers that the user entered, separated by commas. Your program must then calculate the average of all the integer values that the user entered. Your program must use the list of class ListNode elements to print all the integers that the user entered and to calculate the average. The following depicts a session of a working program that meets the requirements of this assignment. Please carefully read it and make sure that your program behaves as closely to this as possible.

-bash-4.1 $ ./a.out

Please enter an integer: 5

Please enter an integer: 9

Please enter an integer: 4

Please enter an integer: 0

You entered the integers: 5,9,4

The average of these integers is 6

-bash-4.1 $ ./a.out

Please enter an integer: 29

Please enter an integer: 71

Please enter an integer: 2323

Please enter an integer: -4

Please enter an integer: 904

Please enter an integer: 0

You entered the integers 29,71,2323,-4,904

The average of these integers is 664.6

-bash-4.1 $ ./a.out

Please enter an integer: 0

You entered no integers

-bash-4.1 $ ./a.out

Please enter an integer: 8

Please enter an integer: 0

You entered the integers 8

The average of these integers is 8

Please note that there is no upper bound on the number of integers that a user can choose to enter during a given session. This means that your list could contain an arbitrarily large number of class ListNode elements. Your list must be built using dynamic memory. This means that your program must allocate each class ListNode element using the new operator. To build your assignment, you must start by copying the declaration for class ListNode into a file called ListNode.h. Then copy the definition of the class constructor and methods for class ListNode into a file called ListNode.cpp. Once you have done this, create a file called lab11main.cpp and begin adding the logic necessary to complete your program. You may choose to add data members or methods to class ListNode, but you are forbidden from placing data or logic unrelated to class ListNode inside ListNode.h or ListNode.cpp. If you want to create new files other than lab11main.cpp to work in that is fine, but you must turn in all files necessary to build and test your program. When you have completed your program and have tested it, copy all files into a directory called lab11, then use the linux tar command to create an archive called lab11.tar.

At last, create logic in your program to relinquish all class ListNode elements (using the C++ delete operator) in your list before your program terminates.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!