Write a list class, similar to the STL list class. To avoid conflicts with other
ID: 3687347 • Letter: W
Question
Write a list class, similar to the STL list class. To avoid conflicts with other classes (such as the STL list class), name the class LL (short for "linked list"). In this assignment we'll get started with a very limited set of member functions. In future assignments we'll add iterators and the additional member functions that iterators will allow us, we'll turn it into a templated class, and we'll add exception handling. For this assignment the LL class will include the following public members:
LL(): Creates an empty LL object.
bool empty() const: returns true if the LL object is empty, false otherwise.
size_type size() const: returns the number of items in the LL object.
void clear(): makes the LL object empty. Watch for memory leaks!
void pop_front(): removes the first item in the list. Watch for memory leaks!
void push_front(const int& x): insert x at the front of the LL object. i
nt& front(): returns a reference to the front item in the LL object.
const int& front() const: same, but for use by const LL objects.
The big three (of course). When copying or assigning a list, you must preserve the order of the items in the LL object.
void print() const: although nothing like this is present in any of the STL container classes, you'll need it here for testing purposes because we're not (yet) providing any way to iterate through an LL object.
size_type typedef
You should have a node struct nested inside your LL class, as demonstrated in the lecture examples. Something like this: class LL { public: . . . private: struct node { int data; node* next; }; node* list; };
Explanation / Answer
program1:
definition:
bool empty() const;
Returns a true value if the number of elements is zero, false otherwise.
iterator insert(iterator position, const T& x);
Insert the element x (type T is the type of a list element) into the list at the position specified by the iterator (before the element, if any, that was previously at the iterator's position). The return value is an iterator that specifies the position of the inserted element.
earse:
iterator erase(iterator position);
iterator erase(iterator first, iterator last);
Erase (remove) one element or a range of elements from a list. In the case of a range, this operation deletes elements from the first iterator position up to, but not including, the second iterator position. The returnediterator points to the element after the last one erased. For an alternate way to erase all elements, see the description of clear() below.
clear:
void clear();
Erase all elements from a list.
remove:
void remove (const T& value);
Erases all list elements that are equal to value. The equality operator (==) must be defined for T, the type of element stored in the list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.