Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

provided List.h file: #ifndef DL_LIST_H #define DL_LIST_H #include namespace cop4530 { template class List { private: // nested Node class struct Node { T data;

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedprovided List.h file:

#ifndef DL_LIST_H #define DL_LIST_H #include  namespace cop4530 { template  class List { private: // nested Node class struct Node { T data; Node *prev; Node *next; Node(const T & d = T{}, Node *p = nullptr, Node *n = nullptr) : data{d}, prev{p}, next{n} {} Node(T && d, Node *p = nullptr, Node *n = nullptr) : data{std::move(d)}, prev{p}, next{n} {} }; public: /ested const_iterator class class const_iterator { public: const_iterator(); // default zero parameter constructor const T & operator*() const; // operator*() to return element // increment/decrement operators const_iterator & operator++(); const_iterator operator++(int); const_iterator & operator--(); const_iterator operator--(int); // comparison operators bool operator==(const const_iterator &rhs) const; bool operator!=(const const_iterator &rhs) const; protected: Node *current; // pointer to node in List T & retrieve() const; // retrieve the element refers to const_iterator(Node *p); // protected constructor friend class List; }; // nested iterator class class iterator : public const_iterator { public: iterator(); T & operator*(); const T & operator*() const; // increment/decrement operators iterator & operator++(); iterator operator++(int); iterator & operator--(); iterator operator--(int); protected: iterator(Node *p); friend class List; }; public: // constructor, desctructor, copy constructor List(); // default zero parameter constructor List(const List &rhs); // copy constructor List(List && rhs); // move constructor // num elements with value of val explicit List(int num, const T& val = T{}); // constructs with elements [start, end) List(const_iterator start, const_iterator end); ~List(); // destructor // copy assignment operator const List& operator=(const List &rhs); // move assignment operator List & operator=(List && rhs); // member functions int size() const; // number of elements bool empty() const; // check if list is empty void clear(); // delete all elements void reverse(); // reverse the order of the elements T &front(); // reference to the first element const T& front() const; T &back(); // reference to the last element const T & back() const; void push_front(const T & val); // insert to the beginning void push_front(T && val); // move version of insert void push_back(const T & val); // insert to the end void push_back(T && val); // move version of insert void pop_front(); // delete first element void pop_back(); // delete last element void remove(const T &val); // remove all elements with value = val // print out all elements. ofc is deliminitor void print(std::ostream& os, char ofc = ' ') const; iterator begin(); // iterator to first element const_iterator begin() const; iterator end(); // end marker iterator const_iterator end() const; iterator insert(iterator itr, const T& val); // insert val ahead of itr iterator insert(iterator itr, T && val); // move version of insert iterator erase(iterator itr); // erase one element iterator erase(iterator start, iterator end); // erase [start, end) private: int theSize; // number of elements Node *head; // head node Node *tail; // tail node void init(); // initialization }; // overloading comparison operators template  bool operator==(const List & lhs, const List &rhs); template  bool operator!=(const List & lhs, const List &rhs); // overloading output operator template  std::ostream & operator &l); // include the implementation file here #include "List.hpp" } // end of namespace 4530 #endif 

provided test_list file:

#include  #include  #include "List.h" using namespace std; using namespace cop4530; int main() { // typedef List::iterator ITRINT; // typedef List::iterator ITRSTR; List l1; // ITRINT itr; List l2; // ITRSTR itr2; const int num = 10; cout  l_t1(8, 5); List l_t2(l_t1.begin(), l_t1.end()); cout  l_t3 = std::move(l_t2); for (auto & x : l_t3) { cout  l2_t1(8, "2"); List l2_t2(l2_t1.begin(), l2_t1.end()); cout  

thanks!

Educational Objectives: Understanding generic programming and information hiding by developing generic containers. Getting familiar with the concept of class template and its usage. Use of nested (iterator) classes. Use of namespace. Operator overloading Statement of Work: Implement a doubly-linked list class template List and its associated iterators equirements 1. A header file List.h is provided, which contains the interfaces of the doubly-linked list class template List. In particular, it contains a 2. A driver program test_list.cpp has been included. It is used to test your implementation of the doubly-linked list class template for 3. You need to implement the member functions of the doubly-linked list class template List in a file named List.hpp. Note that, List hpp has nested Node structure, and two nested iterators class iterator and const iterator). You cannot change anything in the List.h file different data types (it tests Listint and Liststring>. Similarlv. vou cannot change anything in the test st.cpp file been included in the header file List h (towards the end of the file). As we have discussed in class, you should not try to compile List.hpp (or List h). (You should compile the provided driver program test_list.cpp to test your implementation of List, see below.) You need to implement all the member functions of List, Listiterator, and ListcT>.const iterator, and non-class global overloaded functions operator=-0 operator'-0 and operatore included in List h. The design of the List container follows the one presented in the textbook. It has three member variables, theSize, head, and tail. theSize records the number of elements in the list. The head and tail pointers point to the sentinel nodes. They represent the beginning and end markers. They do not store any real elements in the list. It is OK for you to use the code provided in the textbook. We describe the requirements of each function in the following (we may not write the function signatures in detail, please refer to the List.h file for the detailed function declaration) Member functions of nested const_iterator class const iteratorO: default zero-parameter constructor. Set pointer current to NULL (nullptr for ct+2011) operator*O: returns a reference to the corresponding element in the list by calling retrieve0 member function. operator++0, operator+(int), operator-0, operator-(int): prefix and postfix increment and decrement operators operator-0 and operator! 0: two iterators are equal if they refer to the same element. retrieve): return a reference to the corresponding element in the list. . const iterator Node p): one-parameter constructor. Set pointer current to the given node pointer p Member functions of nested iterator class iterator): default zero-parameter constructor operator O: returns a reference to the corresponding element in the list by calling retrieve0 member function. operator-t(), operatorTT(int). Operator--(), operator--(int): prefix and postfix ncrement and decrement operators . const_iterator(Node *p): one-parameter constructor. Member functions of List class template List): Default zero-parameter constructor. Call init) to initialize list member variables List(const List &rhs): Copy constructor. Create the new list using elements in existing list rhs List(List &&rhs): move constructor List(int num, const T & val- TO): Construct a list with num elements, all initialized with value val. e List(const_iterator start, const_iterator end): construct a List with elements from another list between start and end. Including the element referred to by the start iterator, but not the end iterator, that is [start, end) List0: destructor. You should properly reclaim memory (used by head and tail nodes) operator-(List &rhs): copy assignment operator operator-(List &&rhs): move assignment operator size: return the number of elements in the List. emptyO: return true if no element is in the list; otherwise, return false. clearO: delete all the elements in the list reverse0: reverse the order of the elements in the list. That is, the original first element becomes the last, while the original last becomes the first. front and back): return reference to the first and last element in the list, respectively push frontO and push back0, insert the new object as the first and last element into the list, respectively; and their move versions pop_frontO and pop back0, delete the first and last element in the list, respectively remove(const T & val): delete all nodes with value equal to val from the list. print(ostream &os, char ofc-print all elements in the list, using character ofc as the deliminator between elements in the list beginO: return iterator to the first element in the list endO: return iterator to the end marker of the list (tail) insert(iterator itr, const T & val): insert value val ahead of the node referred to by itr; and its move version erase(iterator itr): delete node referred to by itr. The return value is an iterator to the following node erase(iterator start, iterator end): delete all nodes between start and end (including start but not end), that is, all elements in the range [start, end) init0: initialize the member variables of list Non-class global functions operator-=(const ListT> & ins, const List T> & rhs): check if two lists contain the same sequence of elements. Two lists . are equal if they have the same number of elements and the elements at the corresponding position are equal. operator-const List & hs, const List & rhs): opposite of operator=0 operators(ostream& os, const List T> & ID: print out all elements in list I by calling List. Similarlv. vou cannot change anything in the test st.cpp file been included in the header file List h (towards the end of the file). As we have discussed in class, you should not try to compile List.hpp (or List h). (You should compile the provided driver program test_list.cpp to test your implementation of List, see below.) You need to implement all the member functions of List, Listiterator, and ListcT>.const iterator, and non-class global overloaded functions operator=-0 operator'-0 and operatore included in List h. The design of the List container follows the one presented in the textbook. It has three member variables, theSize, head, and tail. theSize records the number of elements in the list. The head and tail pointers point to the sentinel nodes. They represent the beginning and end markers. They do not store any real elements in the list. It is OK for you to use the code provided in the textbook. We describe the requirements of each function in the following (we may not write the function signatures in detail, please refer to the List.h file for the detailed function declaration) Member functions of nested const_iterator class const iteratorO: default zero-parameter constructor. Set pointer current to NULL (nullptr for ct+2011) operator*O: returns a reference to the corresponding element in the list by calling retrieve0 member function. operator++0, operator+(int), operator-0, operator-(int): prefix and postfix increment and decrement operators operator-0 and operator! 0: two iterators are equal if they refer to the same element. retrieve): return a reference to the corresponding element in the list. . const iterator Node p): one-parameter constructor. Set pointer current to the given node pointer p Member functions of nested iterator class iterator): default zero-parameter constructor operator O: returns a reference to the corresponding element in the list by calling retrieve0 member function. operator-t(), operatorTT(int). Operator--(), operator--(int): prefix and postfix ncrement and decrement operators . const_iterator(Node *p): one-parameter constructor. Member functions of List class template List): Default zero-parameter constructor. Call init) to initialize list member variables List(const List &rhs): Copy constructor. Create the new list using elements in existing list rhs List(List &&rhs): move constructor List(int num, const T & val- TO): Construct a list with num elements, all initialized with value val. e List(const_iterator start, const_iterator end): construct a List with elements from another list between start and end. Including the element referred to by the start iterator, but not the end iterator, that is [start, end) List0: destructor. You should properly reclaim memory (used by head and tail nodes) operator-(List &rhs): copy assignment operator operator-(List &&rhs): move assignment operator size: return the number of elements in the List. emptyO: return true if no element is in the list; otherwise, return false. clearO: delete all the elements in the list reverse0: reverse the order of the elements in the list. That is, the original first element becomes the last, while the original last becomes the first. front and back): return reference to the first and last element in the list, respectively push frontO and push back0, insert the new object as the first and last element into the list, respectively; and their move versions pop_frontO and pop back0, delete the first and last element in the list, respectively remove(const T & val): delete all nodes with value equal to val from the list. print(ostream &os, char ofc-print all elements in the list, using character ofc as the deliminator between elements in the list beginO: return iterator to the first element in the list endO: return iterator to the end marker of the list (tail) insert(iterator itr, const T & val): insert value val ahead of the node referred to by itr; and its move version erase(iterator itr): delete node referred to by itr. The return value is an iterator to the following node erase(iterator start, iterator end): delete all nodes between start and end (including start but not end), that is, all elements in the range [start, end) init0: initialize the member variables of list Non-class global functions operator-=(const ListT> & ins, const List T> & rhs): check if two lists contain the same sequence of elements. Two lists . are equal if they have the same number of elements and the elements at the corresponding position are equal. operator-const List & hs, const List & rhs): opposite of operator=0 operators(ostream& os, const List T> & ID: print out all elements in list I by calling List

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered 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

Students also viewed these Databases questions