Lab 8 C++ and Provide all code fully Instructions Test 1: An empty list Test 1 in main.cpp prints an empty list (using the output operator) and calls its size accessor function to verify that the size is 0. Here's what you'll need to implement to get this working: Size() is done for you in the .hpp. Fill in the default constructor definition in the .cpp. This is responsible for setting the first, last, and size member variables to reasonable values for an empty list. Fill in the output operator definition in the .cpp. The tests rely on it printing in a very specific format: it should print all elements with only one space after each element. There should be no newline after the last element. Move the /* in RunListTests() in main.cpp to the beginning of Test 2, and see if you pass Test 1 by obtaining the following output: *** List tests on original list: TEST PASSED: 1a TEST PASSED: 1b ALL passed Test 2: Inserting elements at the beginning of the list The next task is to implement InsertFirst(). This function should insert the value v at the beginning of the list. It should return true if successful and false if unsuccessful. Un-comment InsertFirst in the .hpp, and add a definition for it in LinkedList.cpp. You'll need to take the following steps: Allocate a new node. If the allocation wasn't successful (that is if the newnode == NULL), return false. If it was successful, populate the node's value. Draw a copy of the list with the new node inserted at the beginning. Make a list of the pointers that need to move. Are there any special cases that only apply to one of the example lists? Put your pointer operations in order, using temps if necessary. Write the code! Before you compile, trace through the code on your 3 sample lists. Again, it's good to try this by yourself, but if you get stuck, we covered most of this code in class, and is available in the lecture notes. Move the /* in main to allow you to run Test 2, and compile and run your code. Test 3: Inserting elements at the end of the list InsertLast() works exactly like InsertFirst(), except that the new node should be incorporated at the end of the list rather than the beginning. Test 4: Deleting the first element of the list Test 4 uses the DeleteFirst() member function, which should delete the first element if possible and return true if successful and false if unsuccessful. Tests 5, 6, 7: Deleting the LAST element of the list For DeleteLast, test it out on 2 sample lists: one with exactly 2 items, and one with about 5 items. When DeleteLast is implemented, put tests 5, 6, and 7 back in. Test 5 checks DeleteLast, and Tests 6 and 7 cover some corner cases. Your output should end with: TEST PASSED: 7a TEST PASSED: 7b ALL passed Test 8: The copy constructor Remember to re-add the copy constructor to your .hpp. Initialize your first, last, and size member variables to indicate an empty list. Write a loop to iterate over other. For each element of other, you should call InsertLast() on yourself, passing the value of that element. Pseudocode: // Initialize first, last, size for empty list // Create a temp pointer and initialize // while I'm not done iterating through the other list InsertLast(// value of current element in other list) // increment my temp pointer Test 9: The equality operator The equality operator should return true if ALL of the following conditions are true. That means you can bail out and return false as soon as you find that one of the conditions is false. The sizes of the two lists must be equal The values of the first, second, …., last elements must be equal Test 10: The assignment operator For the actual assignment operator… Check for self-assignment. Here's the code for this: if (this == &other) return *this; The & in front of "other" means "address of." In other words, if this and otherare stored in the same memory location, then you are doing a self-assignment and should get! out! now! Now that you've established that you're not self-assigning, you can delete all of your current elements. Follow the same process as you did for the destructor. Borrow the code from your copy constructor that walks through the other list and inserts each of its values into your list return *this! All of the tests in main() should pass.
Lab 8 C++ and Provide all code fully
Instructions
Test 1: An empty list
Test 1 in main.cpp prints an empty list (using the output operator) and calls its size accessor function to verify that the size is 0. Here's what you'll need to implement to get this working:
- Size() is done for you in the .hpp.
- Fill in the default constructor definition in the .cpp. This is responsible for setting the first, last, and size member variables to reasonable values for an empty list.
- Fill in the output operator definition in the .cpp. The tests rely on it printing in a very specific format: it should print all elements with only one space after each element. There should be no newline after the last element.
Move the /* in RunListTests() in main.cpp to the beginning of Test 2, and see if you pass Test 1 by obtaining the following output:
*** List tests on original list: TEST PASSED: 1a
TEST PASSED: 1b
ALL passed
Test 2: Inserting elements at the beginning of the list
The next task is to implement InsertFirst(). This function should insert the value v at the beginning of the list. It should return true if successful and false if unsuccessful.
Un-comment InsertFirst in the .hpp, and add a definition for it in LinkedList.cpp. You'll need to take the following steps:
- Allocate a new node. If the allocation wasn't successful (that is if the newnode == NULL), return false. If it was successful, populate the node's value.
- Draw a copy of the list with the new node inserted at the beginning. Make a list of the pointers that need to move. Are there any special cases that only apply to one of the example lists?
- Put your pointer operations in order, using temps if necessary.
- Write the code!
- Before you compile, trace through the code on your 3 sample lists.
Again, it's good to try this by yourself, but if you get stuck, we covered most of this code in class, and is available in the lecture notes.
Move the /* in main to allow you to run Test 2, and compile and run your code.
Test 3: Inserting elements at the end of the list
InsertLast() works exactly like InsertFirst(), except that the new node should be incorporated at the end of the list rather than the beginning.
Test 4: Deleting the first element of the list
Test 4 uses the DeleteFirst() member function, which should delete the first element if possible and return true if successful and false if unsuccessful.
Tests 5, 6, 7: Deleting the LAST element of the list
For DeleteLast, test it out on 2 sample lists: one with exactly 2 items, and one with about 5 items.
When DeleteLast is implemented, put tests 5, 6, and 7 back in. Test 5 checks DeleteLast, and Tests 6 and 7 cover some corner cases.
Your output should end with:
TEST PASSED: 7a
TEST PASSED: 7b
ALL passed
Test 8: The copy constructor
Remember to re-add the copy constructor to your .hpp.
- Initialize your first, last, and size member variables to indicate an empty list.
- Write a loop to iterate over other. For each element of other, you should call InsertLast() on yourself, passing the value of that element.
Pseudocode:
// Initialize first, last, size for empty list
// Create a temp pointer and initialize
// while I'm not done iterating through the other list
InsertLast(// value of current element in other list)
// increment my temp pointer
Test 9: The equality operator
The equality operator should return true if ALL of the following conditions are true. That means you can bail out and return false as soon as you find that one of the conditions is false.
- The sizes of the two lists must be equal
- The values of the first, second, …., last elements must be equal
Test 10: The assignment operator
For the actual assignment operator…
- Check for self-assignment. Here's the code for this:
if (this == &other) return *this;
The & in front of "other" means "address of." In other words, if this and otherare stored in the same memory location, then you are doing a self-assignment and should get! out! now!
- Now that you've established that you're not self-assigning, you can delete all of your current elements. Follow the same process as you did for the destructor.
- Borrow the code from your copy constructor that walks through the other list and inserts each of its values into your list
- return *this!
All of the tests in main() should pass.
Step by step
Solved in 4 steps with 5 images