fork download
  1. #include <iostream>
  2. #include <new>
  3. #include <cstdlib>
  4. #include <algorithm>
  5.  
  6. //
  7. //
  8. //
  9.  
  10. template <typename T>
  11. class Deque
  12. {
  13. public:
  14.  
  15. Deque();
  16. Deque(const Deque & rhs);
  17. ~Deque();
  18.  
  19. Deque & operator= (const Deque & rhs);
  20.  
  21. Deque(Deque && rhs) : theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail };
  22. Deque & operator= (Deque && rhs);
  23.  
  24.  
  25. T & Deque<T>::operator[](int n);
  26.  
  27. Iterator begin();
  28. const_iterator begin() const;
  29. Iterator end();
  30. const_iterator end() const;
  31.  
  32. int size() const;
  33. bool isEmpty() const;
  34.  
  35. void clear();
  36.  
  37. T & left();
  38. const T & left() const;
  39. T & right();
  40. const T & right() const;
  41. void pushLeft(const T & x);
  42. void pushLeft(T && x);
  43. void pushRight(const T & x);
  44. void pushRight(T && x);
  45. T & popLeft();
  46. T & popRight();
  47. bool Deque<T>::contains(const T&);
  48.  
  49. struct Node
  50. {
  51. T data;
  52. Node *prev;
  53. Node *next;
  54.  
  55. Node(const T & d = T{}, Node * p = NULL, Node * n = NULL) :
  56. data{ d }, prev{ p }, next{ n } {}
  57.  
  58. Node(T && d, Node * p = NULL, Node * n = NULL)
  59. : data{ std::move(d) }, prev{ p }, next{ n } {}
  60. };
  61.  
  62. class const_iterator
  63. {
  64. public:
  65. const_iterator() : current{ NULL } { }
  66.  
  67. const T & operator* () const;
  68. const_iterator & operator++ ();
  69. const_iterator & operator-- (); //!
  70. const_iterator operator++ (int);
  71. const_iterator operator-- (int); //!
  72.  
  73.  
  74. bool operator== (const const_iterator & rhs) const;
  75. bool operator!= (const const_iterator & rhs) const;
  76.  
  77. protected:
  78.  
  79. Node *current;
  80.  
  81. T & retrieve() const;
  82.  
  83. const_iterator(Node *p) : current{ p } { }
  84.  
  85. friend class Deque<T>;
  86. };
  87.  
  88. class Iterator : public const_iterator
  89. {
  90. public:
  91. Iterator();
  92.  
  93. T & operator* ();
  94. const T & operator* () const;
  95.  
  96. Iterator & operator++ ();
  97. Iterator & operator-- (); //!
  98.  
  99. Iterator operator++ (int);
  100. Iterator operator-- (int); //!
  101.  
  102. protected:
  103. Iterator(Node *p) : const_iterator{ p } { }
  104.  
  105. friend class Deque<T>;
  106. };
  107.  
  108. private:
  109. // Insert x before itr.
  110. Iterator insert(Iterator itr, const T & x);
  111.  
  112. // Insert x before itr.
  113. Iterator insert(Iterator itr, T && x);
  114.  
  115. // Erase item at itr.
  116. Iterator erase(Iterator itr);
  117.  
  118. Iterator erase(Iterator from, Iterator to);
  119.  
  120. int theSize;
  121. Node *head;
  122. Node *tail;
  123.  
  124. void init();
  125. };
  126.  
  127. template<typename T>
  128. inline const T & Deque<T>::const_iterator::operator*() const
  129. {
  130. return retrieve();
  131. }
  132.  
  133. template<typename T>
  134. inline const_iterator & Deque<T>::const_iterator::operator++()
  135. {
  136. current = current->next;
  137. return *this;
  138. }
  139.  
  140. template<typename T>
  141. inline const_iterator & Deque<T>::const_iterator::operator--()
  142. {
  143. current = current->prev;
  144. return *this;
  145. }
  146.  
  147. template<typename T>
  148. inline const_iterator Deque<T>::const_iterator::operator++(int)
  149. {
  150. const_iterator old = *this;
  151. ++(*this);
  152. return old;
  153. }
  154.  
  155. template<typename T>
  156. inline const_iterator Deque<T>::const_iterator::operator--(int)
  157. {
  158. const_iterator old = *this;
  159. --(*this);
  160. return old;
  161. }
  162.  
  163. template<typename T>
  164. inline bool Deque<T>::const_iterator::operator==(const const_iterator & rhs) const
  165. {
  166. return current == rhs.current;
  167. }
  168.  
  169. template<typename T>
  170. inline bool Deque<T>::const_iterator::operator!=(const const_iterator & rhs) const
  171. {
  172. return !(*this == rhs);
  173. }
  174.  
  175. template<typename T>
  176. inline T & Deque<T>::const_iterator::retrieve() const
  177. {
  178. return current->data;
  179. }
  180.  
  181. template<typename T>
  182. inline Deque<T>::Iterator::Iterator()
  183. {
  184.  
  185. }
  186.  
  187. template<typename T>
  188. inline T & Deque<T>::Iterator::operator*()
  189. {
  190. return const_iterator::retrieve();
  191. }
  192.  
  193. template<typename T>
  194. inline const T & Deque<T>::Iterator::operator*() const
  195. {
  196. return const_iterator::operator*();
  197. }
  198.  
  199. template<typename T>
  200. inline Iterator & Deque<T>::Iterator::operator++()
  201. {
  202. this->current = this->current->next;
  203. return *this;
  204. }
  205.  
  206. template<typename T>
  207. inline Iterator & Deque<T>::Iterator::operator--()
  208. {
  209. this->current = this->current->prev;
  210. return *this;
  211. }
  212.  
  213. template<typename T>
  214. inline Iterator Deque<T>::Iterator::operator++(int)
  215. {
  216. Iterator old = *this;
  217. ++(*this);
  218. return old;
  219. }
  220.  
  221. template<typename T>
  222. inline Iterator Deque<T>::Iterator::operator--(int)
  223. {
  224. Iterator old = *this;
  225. --(*this);
  226. return old;
  227. }
  228.  
  229. template<typename T>
  230. inline Deque<T>::Deque()
  231. {
  232. init();
  233. }
  234.  
  235. template<typename T>
  236. inline Deque<T>::~Deque()
  237. {
  238. clear();
  239. delete head;
  240. delete tail;
  241. }
  242.  
  243. template<typename T>
  244. inline Deque & Deque<T>::operator=(const Deque & rhs)
  245. {
  246. if (this == &rhs)
  247. return *this;
  248. clear();
  249. for (const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr)
  250. pushRight(*itr)
  251.  
  252. return *this;
  253. }
  254.  
  255. template<typename T>
  256. inline Deque<T>::Deque(Deque && rhs)
  257. {
  258. rhs.theSize = 0;
  259. rhs.head = NULL;
  260. rhs.tail = NULL;
  261. }
  262.  
  263. template<typename T>
  264. inline Deque & Deque<T>::operator=(Deque && rhs)
  265. {
  266. std::swap(theSize, rhs.theSize);
  267. std::swap(head, rhs.head);
  268. std::swap(tail, rhs.tail);
  269.  
  270. return *this;
  271. }
  272.  
  273. template<typename T>
  274. inline T & Deque<T>::operator[](int n)
  275. {
  276. Iterator itr = begin();
  277.  
  278. for (int i = 0; i < n; i++)
  279. {
  280. itr.current = itr.current->next;
  281. }
  282.  
  283. return itr.current->data;
  284. }
  285.  
  286. template<typename T>
  287. inline Iterator Deque<T>::begin()
  288. {
  289. return{ head->next };
  290. }
  291.  
  292. template<typename T>
  293. inline const_iterator Deque<T>::begin() const
  294. {
  295. return{ head->next };
  296. }
  297.  
  298. template<typename T>
  299. inline Iterator Deque<T>::end()
  300. {
  301. return{ tail->prev }; //changed to -> prev
  302. }
  303.  
  304. template<typename T>
  305. inline const_iterator Deque<T>::end() const
  306. {
  307. return{ tail->prev };
  308. }
  309.  
  310. template<typename T>
  311. inline int Deque<T>::size() const
  312. {
  313. return theSize;
  314. }
  315.  
  316. template<typename T>
  317. inline bool Deque<T>::isEmpty() const
  318. {
  319. return size() == 0;
  320. }
  321.  
  322. template<typename T>
  323. inline void Deque<T>::clear()
  324. {
  325. while (!isEmpty())
  326. popLeft();
  327. }
  328.  
  329. template<typename T>
  330. inline T & Deque<T>::left()
  331. {
  332. return *begin();
  333. }
  334.  
  335. template<typename T>
  336. inline const T & Deque<T>::left() const
  337. {
  338. return *begin();
  339. }
  340.  
  341. template<typename T>
  342. inline T & Deque<T>::right()
  343. {
  344. return *end(); // deleted "--*end"
  345. }
  346.  
  347. template<typename T>
  348. inline const T & Deque<T>::right() const
  349. {
  350. return *end();
  351. }
  352.  
  353. template<typename T>
  354. inline void Deque<T>::pushLeft(const T & x)
  355. {
  356. insert(begin(), x);
  357. }
  358.  
  359. template<typename T>
  360. inline void Deque<T>::pushLeft(T && x)
  361. {
  362. insert(begin(), std::move(x)); // changed std::move(x)) to x
  363. }
  364.  
  365. template<typename T>
  366. inline void Deque<T>::pushRight(const T & x)
  367. {
  368. insert(end(), x);
  369. }
  370.  
  371. template<typename T>
  372. inline void Deque<T>::pushRight(T && x)
  373. {
  374. insert(end(), std::move(x));
  375. }
  376.  
  377. template<typename T>
  378. inline T & Deque<T>::popLeft()
  379. {
  380. return *begin(); erase(begin());
  381. }
  382.  
  383. template<typename T>
  384. inline T & Deque<T>::popRight()
  385. {
  386. return *end(); erase(end()); // changed --end to end
  387. }
  388.  
  389. template<typename T>
  390. inline bool Deque<T>::contains(const T &)
  391. {
  392. // stuff here
  393. }
  394.  
  395. template<typename T>
  396. inline Iterator Deque<T>::insert(Iterator itr, const T & x)
  397. {
  398. Node *p = itr.current;
  399. theSize++;
  400. return{ p->prev = p->prev->next = new Node{ x, p->prev, p } };
  401. }
  402.  
  403. template<typename T>
  404. inline Iterator Deque<T>::insert(Iterator itr, T && x)
  405. {
  406. Node *p = itr.current;
  407. theSize++;
  408. return{ p->prev = p->prev->next = new Node{ std::move(x), p->prev, p } };
  409. }
  410.  
  411. template<typename T>
  412. inline Iterator Deque<T>::erase(Iterator itr)
  413. {
  414. Node *p = itr.current;
  415. Iterator retVal{ p->next };
  416. p->prev->next = p->next;
  417. p->next->prev = p->prev;
  418. delete p;
  419. theSize--;
  420.  
  421. return retVal;
  422. }
  423.  
  424. template<typename T>
  425. inline Iterator Deque<T>::erase(Iterator from, Iterator to)
  426. {
  427. for (Iterator itr = from; itr != to; )
  428. itr = erase(itr);
  429.  
  430. return to;
  431. }
  432.  
  433. template<typename T>
  434. inline void Deque<T>::init()
  435. {
  436. theSize = 0;
  437. head = new Node;
  438. tail = new Node;
  439. head->next = tail;
  440. tail->prev = head;
  441. }
  442.  
  443. template<typename T>
  444. inline Deque<T>::Deque(const Deque & rhs)
  445. {
  446. init();
  447. *this = rhs;
  448. }
  449.  
  450.  
  451. int main() {
  452. // your code goes here
  453. return 0;
  454. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:25:9: error: extra qualification 'Deque<T>::' on member 'operator[]' [-fpermissive]
     T & Deque<T>::operator[](int n);
         ^
prog.cpp:25:35: error: explicit specialization of 'T& Deque<T>::operator[](int)' must be introduced by 'template <>'
     T & Deque<T>::operator[](int n);
                                   ^
prog.cpp:27:5: error: 'Iterator' does not name a type
     Iterator begin();
     ^
prog.cpp:28:5: error: 'const_iterator' does not name a type
     const_iterator begin() const;
     ^
prog.cpp:29:5: error: 'Iterator' does not name a type
     Iterator end();
     ^
prog.cpp:30:5: error: 'const_iterator' does not name a type
     const_iterator end() const;
     ^
prog.cpp:47:10: error: extra qualification 'Deque<T>::' on member 'contains' [-fpermissive]
     bool Deque<T>::contains(const T&);
          ^
prog.cpp:47:37: error: explicit specialization of 'bool Deque<T>::contains(const T&)' must be introduced by 'template <>'
     bool Deque<T>::contains(const T&);
                                     ^
prog.cpp: In constructor 'Deque<T>::Deque(Deque<T>&&)':
prog.cpp:21:84: error: expected '{' at end of input
     Deque(Deque && rhs) : theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail };
                                                                                    ^
prog.cpp: At global scope:
prog.cpp:134:8: error: 'const_iterator' does not name a type
 inline const_iterator & Deque<T>::const_iterator::operator++()
        ^
prog.cpp:141:8: error: 'const_iterator' does not name a type
 inline const_iterator & Deque<T>::const_iterator::operator--()
        ^
prog.cpp:148:8: error: 'const_iterator' does not name a type
 inline const_iterator Deque<T>::const_iterator::operator++(int)
        ^
prog.cpp:156:8: error: 'const_iterator' does not name a type
 inline const_iterator Deque<T>::const_iterator::operator--(int)
        ^
prog.cpp:200:8: error: 'Iterator' does not name a type
 inline Iterator & Deque<T>::Iterator::operator++()
        ^
prog.cpp:207:8: error: 'Iterator' does not name a type
 inline Iterator & Deque<T>::Iterator::operator--()
        ^
prog.cpp:214:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::Iterator::operator++(int)
        ^
prog.cpp:222:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::Iterator::operator--(int)
        ^
prog.cpp:244:8: error: invalid use of template-name 'Deque' without an argument list
 inline Deque & Deque<T>::operator=(const Deque & rhs)
        ^
prog.cpp:256:8: error: redefinition of 'Deque<T>::Deque(Deque<T>&&)'
 inline Deque<T>::Deque(Deque && rhs)
        ^
prog.cpp:21:5: note: 'Deque<T>::Deque(Deque<T>&&)' previously declared here
     Deque(Deque && rhs) : theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail };
     ^
prog.cpp:264:8: error: invalid use of template-name 'Deque' without an argument list
 inline Deque & Deque<T>::operator=(Deque && rhs)
        ^
prog.cpp:274:38: error: no 'T& Deque<T>::operator[](int)' member function declared in class 'Deque<T>'
 inline T & Deque<T>::operator[](int n)
                                      ^
prog.cpp:287:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::begin()
        ^
prog.cpp:293:8: error: 'const_iterator' does not name a type
 inline const_iterator Deque<T>::begin() const
        ^
prog.cpp:299:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::end()
        ^
prog.cpp:305:8: error: 'const_iterator' does not name a type
 inline const_iterator Deque<T>::end() const
        ^
prog.cpp: In member function 'T& Deque<T>::left()':
prog.cpp:332:19: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
     return *begin();
                   ^
prog.cpp:332:19: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
prog.cpp: In member function 'const T& Deque<T>::left() const':
prog.cpp:338:19: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
     return *begin();
                   ^
prog.cpp: In member function 'T& Deque<T>::right()':
prog.cpp:344:17: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
     return *end(); // deleted "--*end"
                 ^
prog.cpp: In member function 'const T& Deque<T>::right() const':
prog.cpp:350:17: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
     return *end();
                 ^
prog.cpp: In member function 'void Deque<T>::pushLeft(const T&)':
prog.cpp:356:18: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
     insert(begin(), x);
                  ^
prog.cpp: In member function 'void Deque<T>::pushLeft(T&&)':
prog.cpp:362:18: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
     insert(begin(), std::move(x)); // changed std::move(x)) to x
                  ^
prog.cpp: In member function 'void Deque<T>::pushRight(const T&)':
prog.cpp:368:16: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
     insert(end(), x);
                ^
prog.cpp: In member function 'void Deque<T>::pushRight(T&&)':
prog.cpp:374:16: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
     insert(end(), std::move(x));
                ^
prog.cpp: In member function 'T& Deque<T>::popLeft()':
prog.cpp:380:19: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
     return *begin(); erase(begin());
                   ^
prog.cpp:380:34: error: there are no arguments to 'begin' that depend on a template parameter, so a declaration of 'begin' must be available [-fpermissive]
     return *begin(); erase(begin());
                                  ^
prog.cpp: In member function 'T& Deque<T>::popRight()':
prog.cpp:386:17: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
     return *end(); erase(end());  // changed --end to end
                 ^
prog.cpp:386:30: error: there are no arguments to 'end' that depend on a template parameter, so a declaration of 'end' must be available [-fpermissive]
     return *end(); erase(end());  // changed --end to end
                              ^
prog.cpp: At global scope:
prog.cpp:390:41: error: no 'bool Deque<T>::contains(const T&)' member function declared in class 'Deque<T>'
 inline bool Deque<T>::contains(const T &)
                                         ^
prog.cpp:396:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::insert(Iterator itr, const T & x)
        ^
prog.cpp:404:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::insert(Iterator itr, T && x)
        ^
prog.cpp:412:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::erase(Iterator itr)
        ^
prog.cpp:425:8: error: 'Iterator' does not name a type
 inline Iterator Deque<T>::erase(Iterator from, Iterator to)
        ^
stdout
Standard output is empty