Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

The Following is a c++ assignment specifically about inheritance, information hiding, and exception handling. All the information needed is provided below (the code that will

The Following is a c++ assignment specifically about inheritance, information hiding, and exception handling.

All the information needed is provided below (the code that will be worked on + the header file).

note: hpp files has all the information on what to code for each function

Thank you

image text in transcribedimage text in transcribedimage text in transcribed//cpp file

#include #include #include #include "SL_list.hpp" using std::cout; using std::endl; using std::ostream; using std::ostringstream; using std::string;

/* LIST OF INVARIANTS: Singly-linked list. first is either null (list is empty) or is pointing to the first node. last is either null (list is empty) or = first if only one element or pointing to the last Node if list has > 2 elements. current points to the Node that has had the most recent activity (or null if list empty) Each Node's next points to the next Node in the list, except for the last node, whose next pointer is a nullptr. num_elements = the number of elements. Nodes are not accessible to a user-programmer, only a non-null pointer to the current cursor position. */

SL_list::SL_list() { // STUDENT INPUT REQUIRED }

// NO NEED TO CHANGE THIS ONE SL_list::SL_list(const SL_list & copy) { cout

SL_list::~SL_list() { // STUDENT INPUT REQUIRED }

bool SL_list::is_empty() const { // STUDENT INPUT REQUIRED return false; //PLACEHOLDER FOR COMPILATION }

// both of the following 2 functions do the same thing int SL_list::size() const { // STUDENT INPUT REQUIRED return 0; //PLACEHOLDER FOR COMPILATION } int SL_list::get_num_elements() const { // STUDENT INPUT REQUIRED return 0; //PLACEHOLDER FOR COMPILATION }

Cursor* SL_list::get_current_location() { // STUDENT INPUT REQUIRED return nullptr; //PLACEHOLDER FOR COMPILATION }

Student SL_list::get_first_element() { // STUDENT INPUT REQUIRED //PLACEHOLDER FOR COMPILATION Student dummy; return dummy; }

Student SL_list::get_last_element() { // STUDENT INPUT REQUIRED //PLACEHOLDER FOR COMPILATION Student dummy; return dummy; }

Student SL_list::get_current_element() const { // STUDENT INPUT REQUIRED //PLACEHOLDER FOR COMPILATION Student dummy; return dummy; }

Student SL_list::get_next() { // STUDENT INPUT REQUIRED //PLACEHOLDER FOR COMPILATION Student dummy; return dummy; }

bool SL_list::contains(const Student & target) { // STUDENT INPUT REQUIRED //PLACEHOLDER FOR COMPILATION return false; }

Cursor* SL_list::find_prev(const Student & target) { // STUDENT INPUT REQUIRED return nullptr; //PLACEHOLDER FOR COMPILATION }

Cursor* SL_list::insert_first(const Student & element) { // STUDENT INPUT REQUIRED return nullptr; //PLACEHOLDER FOR COMPILATION }

Cursor* SL_list::insert_after(const Student & element, Cursor * curr_pos) { // STUDENT INPUT REQUIRED return nullptr; //PLACEHOLDER FOR COMPILATION }

Student SL_list::remove_first() { // STUDENT INPUT REQUIRED //PLACEHOLDER FOR COMPILATION Student dummy; return dummy; }

Student SL_list::remove_after(Cursor* prev) { // STUDENT INPUT REQUIRED //PLACEHOLDER FOR COMPILATION Student dummy; return dummy; }

ostream& operator

int main() { cout

The constructor, copy constructor and destructor The constructor's job is always to initialize the class' attributes. Any pointers that are not pointing to anything can be initialized to nullptr. The copy constructor is completed for you. It is a design choice that a SL_List object is not to be copied. If anyone tries, they will get a complaint. The destructor will only need to clean up any remaining Nodes in the list. Because Node objects are created individually during each insert action, and deleted during a remove action, the destructor can call a remove function on each of the remaining nodes. The getters and the setters There are several of these: size, get_num_elements and is_empty: These are familiar by now. get_current_location: Because the Node is derived from Cursor, any object of type Node is a Cursor object as well . So, we (the designers) can return the pointer to the current Node and it will be auto-cast to a Cursor when it reaches its destination. Note that when the list is empty, then the pointers to first, last and current are all nullptr and we cannot return a nullptr, so we throw an exception instead. We have seen this in numbers: An int is a double and we can auto-cast any int to a double without losing information, but if we cast a double to a int, we must do it manually, understanding that we may be losing some information. get_first_element, get_last_element and get_currrent_element: These return the actual element in the positions. It should be obvious under what circumstances an exception is thrown. get_next: The user-programmer can traverse the list of elements by repeatedly calling this function. Each call updates the current location to move to the next element and return that element. searching In lecture, we demonstrated the similarities (and differences) of searching through an index-based list and a linked list. There are two functions that require some traversing and searching for a target: contains: Pretty simple search. If the target element is an element in the list, then current is updated. find_prev: Because of the nature of how the links are set up in a singly-linked list, finding the previous node to the element one is searching for is more useful. The current location then allows for one-step inserting or removing of the next element. We will dedicate some discussion about this function during lecture. inserting A successful insertion changes the list significantly, so it is imperative that the programmer make sure that the invariants hold before the function completes its final curly brace. Insertion requires a dynamic allocation of a new Node. Note that the following example demonstrates the creation of a new first node using the Node constructor: first=newNode(element,first); In the case where an insert occurs after a non-null Node (perhaps called prev) the following should look familiar: current = new Node(element, prev->next); insert_first: Should be the simplest one to implement. insert_after: In this function, a pointer to a Cursor object is provided as an input parameter. Note that the user-programmer would have gained access to this object only by having one passed from the SL_List list. Since the Cursor object was created by the Node constructor, the object still maintains its Node'ness and can be cast back (manually) to its original type without any loss of data. QUESTION TO THINK ABOUT: Why can't the user-programmer create their own Cursor object? removing Because an insert creates a new node, we must make sure that a remove operation deletes that node once it has been successfully excised from the list. As with the insertion, we can easily remove the first element or an element that follows the current location. It is recommend that you draw out the steps of re-directing pointers on a white board before implementing this activity. There is a bit of handling of pointers, elements and nodes in these functions

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

Mastering Apache Cassandra 3 X An Expert Guide To Improving Database Scalability And Availability Without Compromising Performance

Authors: Aaron Ploetz ,Tejaswi Malepati ,Nishant Neeraj

3rd Edition

1789131499, 978-1789131499

More Books

Students also viewed these Databases questions