Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- template<class T>
- class UberList
- {
- friend class Iter;
- friend class CIter;
- private:
- struct Node
- {
- friend class Iter;
- friend class CIter;
- T data;
- Node *prev;
- Node *next;
- Node()
- :data(0), prev(0), next(0)
- {
- }
- Node(const T& d, Node *p, Node *n)
- : data(d), prev(p), next(n)
- {
- }
- };
- public:
- class Empty
- {
- };
- class BadIter
- {
- };
- class CIter
- {
- friend class UberList;
- public:
- CIter() { ccurr = NULL; }
- CIter(const Node *n)
- {
- ccurr = n;
- }
- CIter(const Iter& it)
- {
- ccurr = it.curr;
- }
- const T& operator*()
- {
- const T& temp = ccurr->data;
- return temp;
- }
- CIter& operator++()
- {
- if (ccurr == 0)
- throw BadIter();
- ccurr = ccurr->next;
- return *this;
- }
- CIter& operator--()
- {
- if(ccurr == 0)
- throw BadIter();
- ccurr = ccurr->prev;
- return *this;
- }
- bool operator==(const CIter& it)
- {
- return ccurr == it.ccurr;
- }
- bool operator!=(const CIter& it)
- {
- return ccurr != it.ccurr;
- }
- private:
- Node const *ccurr;
- };
- class Iter
- {
- friend class UberList;
- friend class CIter;
- public:
- Iter() { curr = NULL; }
- Iter(Node *n)
- {
- curr = n;
- }
- T& operator*()
- {
- return curr->data;
- }
- Iter& operator++()
- {
- if (curr == 0)
- throw BadIter();
- curr = curr->next;
- return *this;
- }
- Iter& operator--()
- {
- if(curr == 0)
- throw BadIter();
- curr = curr->prev;
- return *this;
- }
- bool operator==(const Iter& it)
- {
- return curr == it.curr;
- }
- bool operator!=(const Iter& it)
- {
- return curr != it.curr;
- }
- private:
- Node *curr;
- };
- UberList()
- :length(0), head(0), tail(0)
- {
- }
- UberList (const UberList<T>& OldList);
- ~UberList();
- int size()
- {
- return length;
- }
- bool empty()
- {
- return length == 0;
- }
- UberList<T> operator= (const UberList<T>& OL)
- {
- UberList<T> NL = new (OL);
- return NL;
- }
- void pushBack(const T& elem);
- void popBack();
- void pushFront(const T& elem);
- void popFront();
- void clear();
- Iter begin();
- Iter end();
- CIter begin() const;
- CIter end() const;
- Iter erase(typename UberList<T>::Iter curr);
- void insertBefore(const T& elem, Iter curr);
- void insertAfter(const T& elem, Iter curr);
- private:
- int length;
- Node *head;
- Node *tail;
- };
- template<class T>
- void UberList<T>::clear()
- {
- while(head != 0)
- {
- popBack();
- }
- }
- template<class T>
- UberList<T>::UberList (const UberList<T>& OldList)
- {
- head = 0;
- tail = 0;
- length = 0;
- for (UberList<int>::CIter it = OldList.begin(); it != OldList.end(); ++it)
- {
- pushBack(*it);
- }
- }
- template<class T>
- typename UberList<T>::Iter UberList<T>::begin()
- {
- Iter it;
- it.curr = head;
- return it;
- }
- template<class T>
- typename UberList<T>::Iter UberList<T>::end()
- {
- Iter it;
- it.curr = tail->next;
- return it;
- }
- template<class T>
- typename UberList<T>::CIter UberList<T>::begin() const
- {
- CIter it;
- it.ccurr = head;
- return it;
- }
- template<class T>
- typename UberList<T>::CIter UberList<T>::end() const
- {
- CIter it;
- it.ccurr = tail->next;
- return it;
- }
- template<class T>
- UberList<T>::~UberList()
- {
- while(head != 0)
- {
- popBack();
- }
- }
- template<class T>
- void UberList<T>::pushFront(const T& elem)
- {
- if(empty())
- {
- head = tail = new Node(elem, 0, 0);
- }
- else
- {
- head = head -> prev = new Node (elem, 0, head);
- }
- }
- template<class T>
- void UberList<T>::pushBack(const T& elem)
- {
- if(empty())
- {
- head = tail = new Node(elem, 0, 0);
- }
- else
- {
- tail = tail -> next = new Node(elem, tail, 0);
- }
- ++length;
- }
- template<class T>
- void UberList<T>::popFront()
- {
- if(empty())
- throw Empty();
- if(length == 1)
- {
- delete head;
- head = tail = 0;
- --length;
- }
- else
- {
- head = head->next;
- delete head->prev;
- head->prev = 0;
- --length;
- }
- }
- template<class T>
- void UberList<T>::popBack()
- {
- if(empty())
- throw Empty();
- if(length == 1)
- {
- delete head;
- head = tail = 0;
- --length;
- }
- else
- {
- tail = tail->prev;
- delete tail->next;
- tail->next = 0;
- --length;
- }
- }
- template<class T>
- typename UberList<T>::Iter UberList<T>::erase(typename UberList<T>::Iter curr)
- {
- if(curr.curr == 0)
- throw BadIter();
- if(curr.curr == head)
- {
- popFront();
- }
- else if(curr.curr == tail)
- {
- popBack();
- }
- else
- {
- curr.curr->prev->next = curr.curr->next;
- curr.curr->next->prev = curr.curr->prev;
- Node *t = curr.curr->next;
- delete curr.curr;
- return Iter(t);
- }
- }
- template<class T>
- void UberList<T>::insertAfter(const T& elem, Iter curr)
- {
- if(curr.curr == 0)
- throw BadIter();
- if(curr.curr == head)
- {
- pushBack(elem);
- }
- else
- {
- curr.curr->next = curr.curr->next->prev = new Node (elem, curr.curr, curr.curr->next);
- ++length;
- }
- }
- template<class T>
- void UberList<T>::insertBefore(const T& elem, Iter curr)
- {
- if(curr.curr == 0)
- throw BadIter();
- if(curr.curr == head)
- {
- pushFront(elem);
- }
- else
- {
- curr.curr->prev = curr.curr->prev->next = new Node(elem, curr.curr->prev, curr.curr);
- ++length;
- }
- }
- #include <iostream>
- int main()
- {
- int a;
- UberList<int> ulist;
- while (cin >> a)
- {
- ulist.pushBack(a);
- }
- int size = ulist.size();
- cout << endl;
- cout << size << endl;
- for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- UberList<int>::Iter it2 = ulist.begin();
- ++it2;
- int b = 15;
- ulist.insertBefore(b, it2);
- for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- ulist.erase(it2);
- for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- b = 22;
- it2 = ulist.begin();
- it2++;
- ulist.insertAfter(b, it2);
- for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- ulist.pushFront(b);
- for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- ulist.popFront();
- for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- UberList<int> ulist2 = ulist;
- cout << "newlist:" << endl;
- for (UberList<int>::Iter it = ulist2.begin(); it != ulist2.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- cout << "citer:" << endl;
- for (UberList<int>::CIter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- ulist.clear();
- ulist.pushBack(b);
- for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
- {
- cout << *it << " ";
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement