Advertisement
Guest User

List.h

a guest
Nov 15th, 2019
1,412
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.82 KB | None | 0 0
  1. #ifndef LIST_H
  2. #define LIST_H
  3. /* List.h
  4. *
  5. * doubly-linked, double-ended list with Iterator interface
  6. * Project UID c1f28c309e55405daf00c565d57ff9ad
  7. * EECS 280 Project 4
  8. */
  9.  
  10. #include <iostream>
  11. #include <cassert> //assert
  12. #include <cstddef> //NULL
  13.  
  14. using namespace std;
  15.  
  16.  
  17. template <typename T>
  18. class List {
  19. //OVERVIEW: a doubly-linked, double-ended list with Iterator interface
  20. public:
  21.  
  22. //EFFECTS: returns true if the list is empty
  23. bool empty() const;
  24.  
  25. //EFFECTS: returns the number of elements in this List
  26. int size() const;
  27.  
  28. //REQUIRES: list is not empty
  29. //EFFECTS: Returns the first element in the list by reference
  30. T & front();
  31.  
  32. //REQUIRES: list is not empty
  33. //EFFECTS: Returns the last element in the list by reference
  34. T & back();
  35.  
  36. //EFFECTS: inserts datum into the front of the list
  37. void push_front(const T &datum);
  38.  
  39. //EFFECTS: inserts datum into the back of the list
  40. void push_back(const T &datum);
  41.  
  42. //REQUIRES: list is not empty
  43. //MODIFIES: may invalidate list iterators
  44. //EFFECTS: removes the item at the front of the list
  45. void pop_front();
  46.  
  47. //REQUIRES: list is not empty
  48. //MODIFIES: may invalidate list iterators
  49. //EFFECTS: removes the item at the back of the list
  50. void pop_back();
  51.  
  52. //MODIFIES: may invalidate list iterators
  53. //EFFECTS: removes all items from the list
  54. void clear();
  55.  
  56. List(); //constructor
  57.  
  58. ~List(); //destructor
  59.  
  60. List(List &x);
  61.  
  62. List& operator= (List &x);
  63. // You should add in a default constructor, destructor, copy constructor,
  64. // and overloaded assignment operator, if appropriate. If these operations
  65. // will work correctly without defining these, you can omit them. A user
  66. // of the class must be able to create, copy, assign, and destroy Lists
  67.  
  68. private:
  69. //a private type
  70. struct Node {
  71. Node *next;
  72. Node *prev;
  73. T datum;
  74. };
  75.  
  76. //REQUIRES: list is empty
  77. //EFFECTS: copies all nodes from other to this
  78. void copy_all(const List<T> &other);
  79.  
  80. Node *first; // points to first Node in list, or nullptr if list is empty
  81. Node *last; // points to last Node in list, or nullptr if list is empty
  82.  
  83. public:
  84. ////////////////////////////////////////
  85.  
  86. class Iterator {
  87.  
  88. //OVERVIEW: Iterator interface to List
  89. friend class List;
  90. public:
  91. Iterator();
  92.  
  93. Iterator(Iterator &x) {
  94. node_ptr = x.node_ptr;
  95. }
  96.  
  97. Iterator& operator= (Iterator &x) {
  98. node_ptr = x.node_ptr;
  99. }
  100.  
  101.  
  102. // You should add in a default constructor, destructor, copy constructor,
  103. // and overloaded assignment operator, if appropriate. If these operations
  104. // will work correctly without defining these, you can omit them. A user
  105. // of the class must be able to create, copy, assign, and destroy Iterators.
  106.  
  107.  
  108. T& operator* ();
  109.  
  110. Iterator &operator++();
  111.  
  112. bool operator==(Iterator rhs) const;
  113.  
  114. bool operator!=(Iterator rhs) const;
  115.  
  116. // Your iterator should implement the following public operators: *,
  117. // ++ (prefix), default constructor, == and !=.
  118.  
  119. public:
  120. // This operator will be used to test your code. Do not modify it.
  121. // Requires that the current element is dereferenceable.
  122. Iterator& operator--() {
  123. assert(node_ptr);
  124. node_ptr = node_ptr->prev;
  125. return *this;
  126. }
  127.  
  128. private:
  129. Node *node_ptr; //current Iterator position is a List node
  130. // add any additional necessary member variables here
  131. // construct an Iterator at a specific position
  132. Iterator(Node *p);
  133.  
  134.  
  135. };//List::Iterator
  136. ////////////////////////////////////////
  137.  
  138. // return an Iterator pointing to the first element
  139.  
  140.  
  141. //REQUIRES: i is a valid, dereferenceable iterator associated with this list
  142. //MODIFIES: may invalidate other list iterators
  143. //EFFECTS: Removes a single element from the list container
  144. void erase(Iterator i);
  145.  
  146. //REQUIRES: i is a valid iterator associated with this list
  147. //EFFECTS: inserts datum before the element at the specified position.
  148. void insert(Iterator i, const T &datum);
  149.  
  150.  
  151. // return an Iterator pointing to "past the end"
  152.  
  153. Iterator begin() {
  154. return Iterator(first);
  155. }
  156. Iterator end() {
  157. return Iterator();
  158. }
  159.  
  160. };//List
  161.  
  162. template <typename T>
  163. List<T>::Iterator::Iterator() {
  164. node_ptr = nullptr;
  165. }
  166.  
  167. template <typename T>
  168. void List<T>::erase(Iterator i) {
  169. assert(!empty());
  170. if(i.node_ptr == first) {
  171. pop_front();
  172. }
  173. else if(i.node_ptr == last) {
  174. pop_back();
  175. }
  176. else {
  177. ((*(i.node_ptr)).prev)->next =(*(i.node_ptr)).next;
  178. ((*(i.node_ptr)).next)->prev =(*(i.node_ptr)).prev;
  179. delete i.node_ptr;
  180. }
  181. }
  182.  
  183. template <typename T>
  184. void List<T>::insert(Iterator i, const T &datum) {
  185. if(i.node_ptr == first) {
  186. push_front(datum);
  187. }
  188. else {
  189. Node *added = new Node();
  190. added->next = i.node_ptr;
  191. added->prev =(*(i.node_ptr)).prev;
  192. added->datum = datum;
  193. (i.node_ptr)->prev = added;
  194. ((*(i.node_ptr)).prev)->next = added;
  195. }
  196. }
  197.  
  198. template <typename T>
  199. T & List<T>::Iterator::operator*() {
  200. assert(node_ptr); // check whether this is a past-the-end iterator
  201. return node_ptr->datum;
  202. }
  203.  
  204. template <typename T>
  205. typename List<T>::Iterator & List<T>::Iterator::operator++() {
  206. assert(node_ptr); // check whether this is a past-the-end iterator
  207. node_ptr = node_ptr->next;
  208. return *this;
  209. }
  210.  
  211. template <typename T>
  212. bool List<T>::Iterator::operator==(Iterator rhs) const {
  213. return node_ptr == rhs.node_ptr;
  214. }
  215.  
  216. template <typename T>
  217. bool List<T>::Iterator::operator!=(Iterator rhs) const {
  218. return node_ptr != rhs.node_ptr;
  219. }
  220.  
  221. template <class T>
  222. List<T>::List(List &x): List<T>() {
  223. for(Node *ptr = x.first; ptr; ptr = ptr->next) {
  224. push_back(ptr->datum);
  225. }
  226. }
  227.  
  228. template <class T>
  229. List<T>& List<T>::operator= (List &x) {
  230. if(this == &x) {
  231. return *this;
  232. }
  233. while(!empty()) {
  234. pop_front();
  235. }
  236. for(Node *ptr = x.first; ptr; ptr = ptr->next) {
  237. push_back(ptr->datum);
  238. }
  239. return *this;
  240. }
  241.  
  242. template <class T>
  243. List<T>::~List() {
  244. while(!empty()) {
  245. pop_front();
  246. }
  247. }
  248.  
  249. template <class T>
  250. List<T>::List() {
  251. first = nullptr;
  252. last = nullptr;
  253. }
  254.  
  255. template <class T>
  256. bool List<T>::empty() const {
  257. return size() == 0;
  258. }
  259.  
  260. template <class T>
  261. void List<T>::copy_all(const List<T> &other) {
  262. assert(empty());
  263. for(Node *ptr = other.first; ptr; ptr = ptr->next) {
  264. push_back(ptr->datum);
  265. }
  266. }
  267.  
  268. template <class T>
  269. int List<T>::size() const {
  270. if(first == nullptr) {
  271. return 0;
  272. }
  273. int i = 0;
  274. for(Node *ptr = first; ptr != nullptr; ptr = ptr->next) {
  275. i++;
  276. }
  277. return i;
  278. }
  279.  
  280. template <class T>
  281. T & List<T>::front() {
  282. assert(!empty());
  283. return first->datum;
  284. }
  285.  
  286. template <class T>
  287. T & List<T>::back() {
  288. assert(!empty());
  289. return last->datum;
  290. }
  291.  
  292. template <class T>
  293. void List<T>::push_front(const T &datum) {
  294. Node *p = new Node;
  295. p->datum = datum;
  296. p->next = first;
  297. p->prev = nullptr;
  298. if(first != nullptr) {
  299. first->prev = p;
  300. }
  301. else { //empty list
  302. last = p;
  303. }
  304. first = p;
  305. }
  306.  
  307. template <class T>
  308. void List<T>::push_back(const T &datum) {
  309. Node *p = new Node;
  310. p->datum = datum;
  311. p->next = nullptr;
  312. p->prev = last;
  313. if(last) {
  314. last->next = p;
  315. }
  316. else {
  317. first = p;
  318. }
  319. last = p;
  320. }
  321.  
  322. template <class T>
  323. void List<T>::pop_front() {
  324. assert(!empty());
  325. if(size() == 1) {
  326. delete first;
  327. first = nullptr;
  328. last = nullptr;
  329. }
  330. else {
  331. Node *newFirst = first->next;
  332. delete first;
  333. first = newFirst;
  334. if(!empty()) {
  335. first->prev = nullptr;
  336. }
  337. }
  338. }
  339.  
  340. template <class T>
  341. void List<T>::pop_back() {
  342. assert(!empty());
  343. if(size() == 1) {
  344. delete last;
  345. first = nullptr;
  346. last = nullptr;
  347. }
  348. else {
  349. Node *newLast = last->prev;
  350. delete last;
  351. last = newLast;
  352. if(!empty()) {
  353. last->next = nullptr;
  354. }
  355. }
  356. }
  357.  
  358. template <class T>
  359. void List<T>::clear() {
  360. while(!empty()) {
  361. pop_front();
  362. }
  363. }
  364.  
  365.  
  366. ////////////////////////////////////////////////////////////////////////////////
  367. // Add your member function implementations below or in the class above
  368. // (your choice). Do not change the public interface of List, although you
  369. // may add the Big Three if needed. Do add the public member functions for
  370. // Iterator.
  371.  
  372.  
  373. #endif // Do not remove this. Write all your code above this line.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement