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.

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

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:

  1. Size() is done for you in the .hpp.
  2. 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.
  3. 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:

  1. 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.
  2. 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?
  3. Put your pointer operations in order, using temps if necessary.
  4. Write the code!
  5. 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…

  1. 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!

  1. 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.
  2. Borrow the code from your copy constructor that walks through the other list and inserts each of its values into your list
  3. return *this!

All of the tests in main() should pass.

Expert Solution
steps

Step by step

Solved in 4 steps with 5 images

Blurred answer
Knowledge Booster
Linked List Representation
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education