CSE 330 Lab 5 List Implement class List that provides the li

CSE 330 Lab 5: List

Implement class List that provides the list functionality needed by the test code List_test.cpp given below. When you do this, you should not use STL list class.

First get the simpler test program test.cpp given below working, then get List_test.cpp working.

Since List is a template container class, it should be implemented in one file: List.h. Complete List.h given below.

I am done with mine but it will not suceed with the final tests! any help will be appriciated!

Solution

#include using namespace std; // forward declaration of classes defined in this header template class Link; template class List_iterator; template class List { public: typedef List_iterator iterator; List(); List(const List & l); ~List(); bool empty() const; unsigned int size() const; T & back() const; T & front() const; void push_front(const T & x); void push_back(const T & x); void pop_front(); void pop_back(); iterator begin() const; iterator end() const; void insert(iterator pos, const T & x); // insert x before pos void erase(iterator & pos); // pos must be valid after erase() returns List operator=(const List & l); protected: Link * first_link; Link * last_link; unsigned int my_size; }; template List::List() { first_link = 0; last_link = 0; my_size = 0; } template List::List(const List & l) { first_link = 0; last_link = 0; my_size = 0; for (Link * current = l.first_link; current != 0; current = current -> next_link) push_back(current -> value); } template typename List::iterator List::begin() const { return iterator(first_link); } // Your code goes here ... template class Link { private: Link(const T & x): value(x), next_link(0), prev_link(0) {} T value; Link * next_link; Link * prev_link; friend class List; friend class List_iterator; }; template class List_iterator { public: typedef List_iterator iterator; List_iterator(Link * source_link): current_link(source_link) { } List_iterator(): current_link(0) { } List_iterator(List_iterator * source_iterator): current_link(source_iterator.current_link) { } T & operator*(); // dereferencing operator iterator operator=(const iterator & rhs); bool operator==(const iterator & rhs) const; bool operator!=(const iterator & rhs) const; iterator & operator++(); // pre-increment, ex. ++it iterator operator++(int); // post-increment, ex. it++ iterator & operator--(); // pre-decrement iterator operator--(int); // post-decrement protected: Link * current_link; friend class List; }; template T & List_iterator::operator*() { return current_link -> value; } template List_iterator & List_iterator::operator++() // pre-increment { current_link = current_link -> next_link; return *this; } // more code goes here #endif // List_test.cpp #include #include #include \"List.h\" using namespace std; int main() { List l; assert(l.size() == 0); assert(l.empty()); l.push_front(44); // list = 44 assert(!l.empty()); assert(l.front() == 44); assert(l.back() == 44); l.push_front(33); // list = 33, 44 assert(l.size() == 2); assert(l.front() == 33); assert(l.back() == 44); l.push_front(22); // list = 22, 33, 44 List::iterator it = l.begin(); l.insert(it, 11); // list = 11, 22, 33, 44 it = l.begin(); assert(l.front() == 11); assert(*it == 11); assert(*++it == 22); assert(*++it == 33); assert(*++it == 44); it = l.begin(); ++it; ++it; ++it; l.insert(it, 38); // list = 11, 22, 33, 38, 44 List::iterator it2 = l.begin(); assert(*it2 == 11); assert(*++it2 == 22); assert(*++it2 == 33); assert(*++it2 == 38); assert(*++it2 == 44); l.pop_front(); // list = 22, 33, 38, 44 it2 = l.begin(); assert(*it2 == 22); assert(*++it2 == 33); assert(*++it2 == 38); assert(*++it2 == 44); l.pop_back(); //list = 22, 33, 38 List copy = l; //copy = 22, 33, 38 assert(copy.size() == 3); List::iterator it3 = copy.begin(); assert(*it3 == 22); assert(*++it3 == 33); copy.erase(it3); //copy = 22, 38 assert(copy.size() == 2); it3 = copy.begin(); assert(*it3 == 22); assert(*++it3 == 38); cout << \"SUCCESS\ \"; } // test.cpp - a simple test program for List.h #include #include \"List.h\" using namespace std; int main() { List l; l.push_back(44); // list = 44 l.push_back(33); // list = 44, 33 l.push_back(11); // list = 44, 33, 11 l.push_back(22); // list = 44, 33, 11, 22 List m(l); List::iterator itr(m.begin()); while (itr != m.end()) { cout << *itr << endl; itr++; } }
CSE 330 Lab 5: List Implement class List that provides the list functionality needed by the test code List_test.cpp given below. When you do this, you should no

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site