Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i this assignment you will make some changes to the LinkedBag class template from Chapter 4 . You ill modify one of the existing methods

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

i this assignment you will make some changes to the LinkedBag class template from Chapter 4 . You ill modify one of the existing methods and add a new method. tart with the first (nonrecursive) version of the LinkedBag class template from Chapter 4. I have put ie files that you need to get started in the Programming Assignments section of Blackboard. Important: To use nullptr and other features of C++11, you need a compiler that is fairly recent. If your compiler was released in the last couple of years, you are probably okay. You may need to turn these features on in your IDE. For example, I typically use Dev-C++ with the GCC compiler TDM-GCC 4.9.2. Tc use C++11 features in Dev-C++, I had to select Tools/Compiler Options. Under the General tab in the "Add the following commands when calling the compiler" section I had to add: std =gnu++11 Requirement: Do not make any changes to the member variables (data members) for this class template. Your modified class template should only have 2 member variables - headPtr and itemCount In particular, do not add a pointer to the last node in the linked list. To find the last node, you will need to use a temporary pointer and traverse the list. lodify add method iscussion: In a bag, the order of the entries does not matter. When you add a new entry to a bag, it Iakes sense to choose an implementation that is simple and fast. For a typical array implementation, le easiest place to add a new entry is at the end of the occupied part of the array. For the linked nplementation, the easiest place to add a new entry is at the beginning of the linked-list. It would not Iake much sense to add a new entry at the end of the linked-list because this would be slower and iore complex. s an academic exercise, revise the public method add in class template LinkedBag so that the new ode is inserted at the end of the linked chain instead of at the beginning. dd a second remove method dd a new remove method that removes a random entry from the bag. This new method should asically work like the existing remove method except that a random entry is removed. Note: Leave le existing remove method in the interface and class template. The prototype will be: ool remove(); - You will need to add this method to your bag interface file. That is, Baginterface.h should include a pure virtual method that matches this new method. You must include documentation (comments) similar to the comments for the other methods in the interface. - Add the method prototype inside the class definition in LinkedBag.h. - Add the method implementation in LinkeBag.h. ou can implement this method by borrowing code from the existing remove method. Here is an iformal algorithm: canRemoveItem = bag not empty if canRemoveItem generate a random number in the range 0 through itemcount - 1 think of the linked list as an array - the first node contains ntry 0 , etc. the random number indicates which node contains the entry to be eleted use a temporary pointer to traverse the linked list to the node to it is easier to remove the first node than any other node copy the entry from the first node to the node that contains the ntry to be deleted delete the first node end if return canRemoveltem lere is an example program that shows how to generate a random integer in a given range using the and() function. The rand() function is defined in header file cstdlib. This program displays a list of 4 andom integers in the range 0 through 99 : include include sing namespace std; / include definition for rand() e sure that your driver includes code to test these modifications. his is linkedbag.h / Destroys object and frees memory allocated by object. (See C++ Interlude 2 ) */ virtual BagInterface () \{\} ; I/ end BagInterface endif i this assignment you will make some changes to the LinkedBag class template from Chapter 4 . You ill modify one of the existing methods and add a new method. tart with the first (nonrecursive) version of the LinkedBag class template from Chapter 4. I have put ie files that you need to get started in the Programming Assignments section of Blackboard. Important: To use nullptr and other features of C++11, you need a compiler that is fairly recent. If your compiler was released in the last couple of years, you are probably okay. You may need to turn these features on in your IDE. For example, I typically use Dev-C++ with the GCC compiler TDM-GCC 4.9.2. Tc use C++11 features in Dev-C++, I had to select Tools/Compiler Options. Under the General tab in the "Add the following commands when calling the compiler" section I had to add: std =gnu++11 Requirement: Do not make any changes to the member variables (data members) for this class template. Your modified class template should only have 2 member variables - headPtr and itemCount In particular, do not add a pointer to the last node in the linked list. To find the last node, you will need to use a temporary pointer and traverse the list. lodify add method iscussion: In a bag, the order of the entries does not matter. When you add a new entry to a bag, it Iakes sense to choose an implementation that is simple and fast. For a typical array implementation, le easiest place to add a new entry is at the end of the occupied part of the array. For the linked nplementation, the easiest place to add a new entry is at the beginning of the linked-list. It would not Iake much sense to add a new entry at the end of the linked-list because this would be slower and iore complex. s an academic exercise, revise the public method add in class template LinkedBag so that the new ode is inserted at the end of the linked chain instead of at the beginning. dd a second remove method dd a new remove method that removes a random entry from the bag. This new method should asically work like the existing remove method except that a random entry is removed. Note: Leave le existing remove method in the interface and class template. The prototype will be: ool remove(); - You will need to add this method to your bag interface file. That is, Baginterface.h should include a pure virtual method that matches this new method. You must include documentation (comments) similar to the comments for the other methods in the interface. - Add the method prototype inside the class definition in LinkedBag.h. - Add the method implementation in LinkeBag.h. ou can implement this method by borrowing code from the existing remove method. Here is an iformal algorithm: canRemoveItem = bag not empty if canRemoveItem generate a random number in the range 0 through itemcount - 1 think of the linked list as an array - the first node contains ntry 0 , etc. the random number indicates which node contains the entry to be eleted use a temporary pointer to traverse the linked list to the node to it is easier to remove the first node than any other node copy the entry from the first node to the node that contains the ntry to be deleted delete the first node end if return canRemoveltem lere is an example program that shows how to generate a random integer in a given range using the and() function. The rand() function is defined in header file cstdlib. This program displays a list of 4 andom integers in the range 0 through 99 : include include sing namespace std; / include definition for rand() e sure that your driver includes code to test these modifications. his is linkedbag.h / Destroys object and frees memory allocated by object. (See C++ Interlude 2 ) */ virtual BagInterface () \{\} ; I/ end BagInterface endif

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Security

Authors: Alfred Basta, Melissa Zgola

1st Edition

1435453905, 978-1435453906

More Books

Students also viewed these Databases questions

Question

dy dx Find the derivative of the function y=(4x+3)5(2x+1)2.

Answered: 1 week ago

Question

What is DDL?

Answered: 1 week ago