#include <iostream> #include <new> #include <cstdlib> #include <algorithm> // // // template <typename T> class Deque { public: Deque(); Deque(const Deque & rhs); ~Deque(); Deque & operator= (const Deque & rhs); Deque(Deque && rhs) : theSize{ rhs.theSize }, head{ rhs.head }, tail{ rhs.tail }; Deque & operator= (Deque && rhs); T & Deque<T>::operator[](int n); Iterator begin(); const_iterator begin() const; Iterator end(); const_iterator end() const; int size() const; bool isEmpty() const; void clear(); T & left(); const T & left() const; T & right(); const T & right() const; void pushLeft(const T & x); void pushLeft(T && x); void pushRight(const T & x); void pushRight(T && x); T & popLeft(); T & popRight(); bool Deque<T>::contains(const T&); struct Node { T data; Node *prev; Node *next; Node(const T & d = T{}, Node * p = NULL, Node * n = NULL) : data{ d }, prev{ p }, next{ n } {} Node(T && d, Node * p = NULL, Node * n = NULL) : data{ std::move(d) }, prev{ p }, next{ n } {} }; class const_iterator { public: const_iterator() : current{ NULL } { } const T & operator* () const; const_iterator & operator++ (); const_iterator & operator-- (); //! const_iterator operator++ (int); const_iterator operator-- (int); //! bool operator== (const const_iterator & rhs) const; bool operator!= (const const_iterator & rhs) const; protected: Node *current; T & retrieve() const; const_iterator(Node *p) : current{ p } { } friend class Deque<T>; }; class Iterator : public const_iterator { public: Iterator(); T & operator* (); const T & operator* () const; Iterator & operator++ (); Iterator & operator-- (); //! Iterator operator++ (int); Iterator operator-- (int); //! protected: Iterator(Node *p) : const_iterator{ p } { } friend class Deque<T>; }; private: // Insert x before itr. Iterator insert(Iterator itr, const T & x); // Insert x before itr. Iterator insert(Iterator itr, T && x); // Erase item at itr. Iterator erase(Iterator itr); Iterator erase(Iterator from, Iterator to); int theSize; Node *head; Node *tail; void init(); }; template<typename T> inline const T & Deque<T>::const_iterator::operator*() const { return retrieve(); } template<typename T> inline const_iterator & Deque<T>::const_iterator::operator++() { current = current->next; return *this; } template<typename T> inline const_iterator & Deque<T>::const_iterator::operator--() { current = current->prev; return *this; } template<typename T> inline const_iterator Deque<T>::const_iterator::operator++(int) { const_iterator old = *this; ++(*this); return old; } template<typename T> inline const_iterator Deque<T>::const_iterator::operator--(int) { const_iterator old = *this; --(*this); return old; } template<typename T> inline bool Deque<T>::const_iterator::operator==(const const_iterator & rhs) const { return current == rhs.current; } template<typename T> inline bool Deque<T>::const_iterator::operator!=(const const_iterator & rhs) const { return !(*this == rhs); } template<typename T> inline T & Deque<T>::const_iterator::retrieve() const { return current->data; } template<typename T> inline Deque<T>::Iterator::Iterator() { } template<typename T> inline T & Deque<T>::Iterator::operator*() { return const_iterator::retrieve(); } template<typename T> inline const T & Deque<T>::Iterator::operator*() const { return const_iterator::operator*(); } template<typename T> inline Iterator & Deque<T>::Iterator::operator++() { this->current = this->current->next; return *this; } template<typename T> inline Iterator & Deque<T>::Iterator::operator--() { this->current = this->current->prev; return *this; } template<typename T> inline Iterator Deque<T>::Iterator::operator++(int) { Iterator old = *this; ++(*this); return old; } template<typename T> inline Iterator Deque<T>::Iterator::operator--(int) { Iterator old = *this; --(*this); return old; } template<typename T> inline Deque<T>::Deque() { init(); } template<typename T> inline Deque<T>::~Deque() { clear(); delete head; delete tail; } template<typename T> inline Deque & Deque<T>::operator=(const Deque & rhs) { if (this == &rhs) return *this; clear(); for (const_iterator itr = rhs.begin(); itr != rhs.end(); ++itr) pushRight(*itr) return *this; } template<typename T> inline Deque<T>::Deque(Deque && rhs) { rhs.theSize = 0; rhs.head = NULL; rhs.tail = NULL; } template<typename T> inline Deque & Deque<T>::operator=(Deque && rhs) { std::swap(theSize, rhs.theSize); std::swap(head, rhs.head); std::swap(tail, rhs.tail); return *this; } template<typename T> inline T & Deque<T>::operator[](int n) { Iterator itr = begin(); for (int i = 0; i < n; i++) { itr.current = itr.current->next; } return itr.current->data; } template<typename T> inline Iterator Deque<T>::begin() { return{ head->next }; } template<typename T> inline const_iterator Deque<T>::begin() const { return{ head->next }; } template<typename T> inline Iterator Deque<T>::end() { return{ tail->prev }; //changed to -> prev } template<typename T> inline const_iterator Deque<T>::end() const { return{ tail->prev }; } template<typename T> inline int Deque<T>::size() const { return theSize; } template<typename T> inline bool Deque<T>::isEmpty() const { return size() == 0; } template<typename T> inline void Deque<T>::clear() { while (!isEmpty()) popLeft(); } template<typename T> inline T & Deque<T>::left() { return *begin(); } template<typename T> inline const T & Deque<T>::left() const { return *begin(); } template<typename T> inline T & Deque<T>::right() { return *end(); // deleted "--*end" } template<typename T> inline const T & Deque<T>::right() const { return *end(); } template<typename T> inline void Deque<T>::pushLeft(const T & x) { insert(begin(), x); } template<typename T> inline void Deque<T>::pushLeft(T && x) { insert(begin(), std::move(x)); // changed std::move(x)) to x } template<typename T> inline void Deque<T>::pushRight(const T & x) { insert(end(), x); } template<typename T> inline void Deque<T>::pushRight(T && x) { insert(end(), std::move(x)); } template<typename T> inline T & Deque<T>::popLeft() { return *begin(); erase(begin()); } template<typename T> inline T & Deque<T>::popRight() { return *end(); erase(end()); // changed --end to end } template<typename T> inline bool Deque<T>::contains(const T &) { // stuff here } template<typename T> inline Iterator Deque<T>::insert(Iterator itr, const T & x) { Node *p = itr.current; theSize++; return{ p->prev = p->prev->next = new Node{ x, p->prev, p } }; } template<typename T> inline Iterator Deque<T>::insert(Iterator itr, T && x) { Node *p = itr.current; theSize++; return{ p->prev = p->prev->next = new Node{ std::move(x), p->prev, p } }; } template<typename T> inline Iterator Deque<T>::erase(Iterator itr) { Node *p = itr.current; Iterator retVal{ p->next }; p->prev->next = p->next; p->next->prev = p->prev; delete p; theSize--; return retVal; } template<typename T> inline Iterator Deque<T>::erase(Iterator from, Iterator to) { for (Iterator itr = from; itr != to; ) itr = erase(itr); return to; } template<typename T> inline void Deque<T>::init() { theSize = 0; head = new Node; tail = new Node; head->next = tail; tail->prev = head; } template<typename T> inline Deque<T>::Deque(const Deque & rhs) { init(); *this = rhs; } int main() { // your code goes here return 0; }
Standard input is empty
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)
^
Standard output is empty