Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

List { public: ItemList( ) = default; std::ostream& write( std::ostream& fout ) const; void add_front( const Item& new_item ); private: ItemNode* head = nullptr; ItemNode*

List { public: ItemList( ) = default; std::ostream& write( std::ostream& fout ) const; void add_front( const Item& new_item ); private: ItemNode* head = nullptr; ItemNode* tail = nullptr; }; std::ostream& operator<<( std::ostream& fout, const ItemList& list );

Start a library for ItemList by creating ItemList.h and .cpp now and placing the class definition shown above in the header file.

Since all the constructor for this class would need to do is set head and tail pointers to nullptr (but that is handled by the attribute initialization syntax in the class itself), there is nothing left for the constructor to do. This is indicated by marking the constructor as default. (Note: If no other constructors are being defined, this is optional.)

Adding to the Head of an Unordered List

Method add_front() accepts as its parameter a Item. The method will construct a new ItemNode containing this items data and add it to the beginning of the list. For an example of this, lets consider creating a new ItemNode that well refer to as item404.

The list method write() needs to display each item in the linked list. The first item could be displayed by accessing pointer head. It should first be noted that when dereferencing the value stored in a pointer variable, a value of nullptr will cause a fatal runtime error. This is analogous to the situation in mathematical formulae containing division, square roots, or logarithms, each of which should be checked for legitimate operands before being carried out. Thus the first list node could be safely printed with the following.

then add the prototype the class and write the method

void add_back( const Item& new_item );

then add the prototype

Item remove_front ( );

Removing from the head of a list is, as one would expect, a matter of undoing an addition to the head of the list. The method to perform this task can return the value of the Item data removed from the head; in the event the list was empty to begin with, an exception is thrown to indicate failure. In the event the list was not empty prior to the request, the head is made to point to the node following the current head. If this pointer is found to be nullptr, the list has just become empty and the tail pointer should be updated to reflect same; if this next pointer is not nullptr, it should be changed to nullptr as the node has been removed from the list and should no longer be tied to it. The node being removed must be deleted to avoid leaking memory.

Complete the following algorithm for the remove_front() method.

then add the prototype and write the method.

void erase ( );

This list class method will need to free up the memory associated with a list class, just as the destructor for the class should; in fact, the destructor for this class can simply call this method. This method can perform its task by repeatedly calling remove_front() as long as the list is not empty.

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

SQL Antipatterns Avoiding The Pitfalls Of Database Programming

Authors: Bill Karwin

1st Edition

1680508989, 978-1680508987

More Books

Students also viewed these Databases questions

Question

What are the determinants of cash cycle ? Explain

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago