Advertisement
Guest User

Untitled

a guest
Dec 18th, 2010
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.03 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template<class T>
  6. class UberList
  7. {
  8.     friend class Iter;
  9.     friend class CIter;
  10.     private:
  11.    
  12.     struct Node
  13.     {
  14.         friend class Iter;
  15.         friend class CIter;
  16.         T data;
  17.        
  18.         Node *prev;
  19.         Node *next;
  20.        
  21.         Node()
  22.             :data(0), prev(0), next(0)
  23.         {
  24.         }
  25.        
  26.         Node(const T& d, Node *p, Node *n)
  27.             : data(d), prev(p), next(n)
  28.         {
  29.         }
  30.        
  31.     };
  32.    
  33.     public:
  34.     class Empty
  35.     {
  36.     };
  37.     class BadIter
  38.     {
  39.     };
  40.    
  41.     class CIter
  42.     {
  43.        
  44.         friend class UberList;
  45.  
  46.         public:
  47.        
  48.         CIter() { ccurr = NULL; }
  49.         CIter(const Node *n)
  50.         {
  51.             ccurr = n;
  52.         }
  53.         CIter(const Iter& it)
  54.         {
  55.             ccurr = it.curr;
  56.         }
  57.        
  58.        
  59.         const T& operator*()
  60.         {
  61.             const T& temp = ccurr->data;
  62.             return temp;
  63.         }
  64.        
  65.        
  66.         CIter& operator++()
  67.         {
  68.             if (ccurr == 0)
  69.                 throw BadIter();
  70.                
  71.             ccurr = ccurr->next;
  72.             return *this;
  73.         }
  74.        
  75.         CIter& operator--()
  76.         {
  77.             if(ccurr == 0)
  78.                 throw BadIter();
  79.                
  80.             ccurr = ccurr->prev;
  81.             return *this;
  82.         }
  83.        
  84.         bool operator==(const CIter& it)
  85.         {
  86.             return ccurr == it.ccurr;
  87.         }
  88.         bool operator!=(const CIter& it)
  89.         {
  90.             return ccurr != it.ccurr;
  91.         }
  92.        
  93.        
  94.         private:
  95.         Node const *ccurr;
  96.     };
  97.    
  98.     class Iter
  99.     {
  100.         friend class UberList;
  101.         friend class CIter;
  102.        
  103.        
  104.        
  105.         public:
  106.        
  107.        
  108.         Iter() { curr = NULL; }
  109.         Iter(Node *n)
  110.         {
  111.             curr = n;
  112.         }
  113.        
  114.         T& operator*()
  115.         {
  116.             return curr->data;
  117.         }
  118.        
  119.        
  120.         Iter& operator++()
  121.         {
  122.             if (curr == 0)
  123.                 throw BadIter();
  124.                
  125.             curr = curr->next;
  126.             return *this;
  127.         }
  128.        
  129.         Iter& operator--()
  130.         {
  131.             if(curr == 0)
  132.                 throw BadIter();
  133.                
  134.             curr = curr->prev;
  135.             return *this;
  136.         }
  137.        
  138.        
  139.         bool operator==(const Iter& it)
  140.         {
  141.             return curr == it.curr;
  142.         }
  143.         bool operator!=(const Iter& it)
  144.         {
  145.             return curr != it.curr;
  146.         }
  147.            
  148.         private:
  149.         Node *curr;
  150.     };
  151.    
  152.    
  153.    
  154.     UberList()
  155.         :length(0), head(0), tail(0)
  156.     {
  157.     }
  158.    
  159.     UberList (const UberList<T>& OldList);
  160.     ~UberList();
  161.    
  162.    
  163.     int size()
  164.     {
  165.         return length;
  166.     }
  167.    
  168.     bool empty()
  169.     {
  170.         return length == 0;
  171.     }
  172.    
  173.     UberList<T> operator= (const UberList<T>& OL)
  174.     {
  175.         UberList<T> NL = new (OL);
  176.         return NL;
  177.     }
  178.    
  179.     void pushBack(const T& elem);
  180.     void popBack();
  181.     void pushFront(const T& elem);
  182.     void popFront();
  183.     void clear();
  184.    
  185.     Iter begin();
  186.     Iter end();
  187.     CIter begin() const;
  188.     CIter end() const;
  189.  
  190.     Iter erase(typename UberList<T>::Iter curr);
  191.     void insertBefore(const T& elem, Iter curr);
  192.     void insertAfter(const T& elem, Iter curr);
  193.    
  194.     private:
  195.     int length;
  196.     Node *head;
  197.     Node *tail;
  198. };
  199.  
  200.  
  201. template<class T>
  202. void UberList<T>::clear()
  203. {
  204.     while(head != 0)
  205.     {
  206.         popBack();
  207.     }
  208. }
  209.  
  210. template<class T>
  211. UberList<T>::UberList (const UberList<T>& OldList)
  212. {
  213.     head = 0;
  214.     tail = 0;
  215.     length = 0;
  216.     for (UberList<int>::CIter it = OldList.begin(); it != OldList.end(); ++it)
  217.     {
  218.         pushBack(*it);
  219.     }
  220. }
  221. template<class T>
  222. typename UberList<T>::Iter UberList<T>::begin()
  223. {
  224.     Iter it;
  225.     it.curr = head;
  226.     return it;
  227. }
  228.  
  229. template<class T>
  230. typename UberList<T>::Iter UberList<T>::end()
  231. {
  232.     Iter it;
  233.     it.curr = tail->next;
  234.     return it;
  235. }
  236.  
  237. template<class T>
  238. typename UberList<T>::CIter UberList<T>::begin() const
  239. {
  240.     CIter it;
  241.     it.ccurr = head;
  242.     return it;
  243. }
  244.  
  245. template<class T>
  246. typename UberList<T>::CIter UberList<T>::end() const
  247. {
  248.     CIter it;
  249.     it.ccurr = tail->next;
  250.     return it;
  251. }
  252.  
  253.  
  254. template<class T>
  255. UberList<T>::~UberList()
  256. {
  257.     while(head != 0)
  258.     {
  259.         popBack();
  260.     }
  261. }
  262.  
  263. template<class T>
  264. void UberList<T>::pushFront(const T& elem)
  265. {
  266.     if(empty())
  267.     {
  268.         head = tail = new Node(elem, 0, 0);
  269.     }
  270.     else
  271.     {
  272.         head = head -> prev = new Node (elem, 0, head);
  273.     }
  274. }
  275.  
  276.  
  277. template<class T>
  278. void UberList<T>::pushBack(const T& elem)
  279. {
  280.     if(empty())
  281.     {
  282.        
  283.         head = tail = new Node(elem, 0, 0);
  284.     }
  285.     else
  286.     {
  287.        
  288.         tail = tail -> next = new Node(elem, tail, 0);
  289.     }
  290.     ++length;
  291.  
  292. }
  293.  
  294. template<class T>
  295. void UberList<T>::popFront()
  296. {
  297.     if(empty())
  298.         throw Empty();
  299.        
  300.     if(length == 1)
  301.     {
  302.         delete head;
  303.         head = tail = 0;
  304.         --length;
  305.     }
  306.     else
  307.     {
  308.         head = head->next;
  309.         delete head->prev;
  310.         head->prev = 0;
  311.         --length;
  312.     }
  313. }
  314.  
  315. template<class T>
  316. void UberList<T>::popBack()
  317. {
  318.    
  319.     if(empty())
  320.         throw Empty();
  321.    
  322.    
  323.     if(length == 1)
  324.     {
  325.         delete head;
  326.         head = tail = 0;
  327.         --length;
  328.     }
  329.     else
  330.     {
  331.        
  332.         tail = tail->prev;
  333.        
  334.         delete tail->next;
  335.        
  336.         tail->next = 0;
  337.         --length;
  338.     }
  339. }
  340.  
  341. template<class T>
  342. typename UberList<T>::Iter UberList<T>::erase(typename UberList<T>::Iter curr)
  343. {
  344.     if(curr.curr == 0)
  345.         throw BadIter();
  346.     if(curr.curr == head)
  347.     {
  348.         popFront();
  349.     }
  350.     else if(curr.curr == tail)
  351.     {
  352.         popBack();
  353.     }
  354.     else
  355.     {
  356.        
  357.         curr.curr->prev->next = curr.curr->next;
  358.        
  359.         curr.curr->next->prev = curr.curr->prev;
  360.        
  361.         Node *t = curr.curr->next;
  362.  
  363.         delete curr.curr;
  364.         return Iter(t);
  365.    
  366.     }
  367.  
  368. }
  369.  
  370. template<class T>
  371. void UberList<T>::insertAfter(const T& elem, Iter curr)
  372. {
  373.     if(curr.curr == 0)
  374.         throw BadIter();
  375.        
  376.     if(curr.curr == head)
  377.     {
  378.         pushBack(elem);
  379.     }
  380.     else
  381.     {
  382.         curr.curr->next = curr.curr->next->prev = new Node (elem, curr.curr, curr.curr->next);
  383.         ++length;
  384.     }
  385. }
  386.  
  387. template<class T>
  388. void UberList<T>::insertBefore(const T& elem, Iter curr)
  389. {
  390.     if(curr.curr == 0)
  391.         throw BadIter();
  392.     if(curr.curr == head)
  393.     {
  394.         pushFront(elem);
  395.     }
  396.     else
  397.     {
  398.    
  399.         curr.curr->prev = curr.curr->prev->next = new Node(elem, curr.curr->prev, curr.curr);
  400.         ++length;
  401.     }
  402. }
  403.  
  404. #include <iostream>
  405.  
  406. int main()
  407. {
  408.    
  409.     int a;
  410.     UberList<int> ulist;
  411.     while (cin >> a)
  412.     {
  413.         ulist.pushBack(a);
  414.     }
  415.    
  416.     int size = ulist.size();
  417.     cout << endl;
  418.    
  419.     cout << size << endl;
  420.  
  421.  
  422.     for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
  423.     {
  424.         cout << *it << " ";
  425.     }
  426.    
  427.     cout << endl;
  428.    
  429.     UberList<int>::Iter it2 = ulist.begin();
  430.     ++it2;
  431.    
  432.     int b = 15;
  433.     ulist.insertBefore(b, it2);
  434.    
  435.     for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
  436.     {
  437.         cout << *it << " ";
  438.     }
  439.    
  440.     cout << endl;
  441.    
  442.     ulist.erase(it2);
  443.     for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
  444.     {
  445.         cout << *it << " ";
  446.     }
  447.     cout << endl;
  448.    
  449.     b = 22;
  450.    
  451.     it2 = ulist.begin();
  452.     it2++;
  453.    
  454.     ulist.insertAfter(b, it2);
  455.    
  456.     for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
  457.     {
  458.         cout << *it << " ";
  459.     }
  460.    
  461.     cout << endl;
  462.    
  463.     ulist.pushFront(b);
  464.     for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
  465.     {
  466.         cout << *it << " ";
  467.     }
  468.     cout << endl;
  469.    
  470.     ulist.popFront();
  471.    
  472.     for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
  473.     {
  474.         cout << *it << " ";
  475.     }
  476.     cout << endl;
  477.    
  478.     UberList<int> ulist2 = ulist;
  479.    
  480.     cout << "newlist:" << endl;
  481.     for (UberList<int>::Iter it = ulist2.begin(); it != ulist2.end(); ++it)
  482.     {
  483.         cout << *it << " ";
  484.     }
  485.     cout << endl;
  486.    
  487.     cout << "citer:" << endl;
  488.     for (UberList<int>::CIter it = ulist.begin(); it != ulist.end(); ++it)
  489.     {
  490.         cout << *it << " ";
  491.     }
  492.     cout << endl;
  493.    
  494.     ulist.clear();
  495.     ulist.pushBack(b);
  496.     for (UberList<int>::Iter it = ulist.begin(); it != ulist.end(); ++it)
  497.     {
  498.         cout << *it << " ";
  499.     }
  500.     cout << endl;
  501.    
  502.    
  503.  
  504.     return 0;
  505. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement