Guest User

Const Correct? 2

a guest
Jan 26th, 2013
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #ifndef Linked_List_H
  2. #define Linked_List_H
  3.  
  4. #include <stdexcept>
  5.  
  6. #include <exception>
  7.  
  8. #include "Node.h"
  9.  
  10. using namespace std;
  11.  
  12. template <class T>
  13. class LinkedList
  14. {
  15.   public:
  16.     LinkedList();
  17.     LinkedList(LinkedList&);
  18.     LinkedList(const LinkedList&);
  19.     ~LinkedList();
  20.     LinkedList<T>& operator=(const LinkedList&);
  21.     T at(const unsigned int) const;
  22.     T at(const unsigned int);
  23.     const Node<T> begin() const;
  24.     const Node<T> end() const;
  25.     void pop_back();
  26.     void pop_front();
  27.     void push_back(const T&);
  28.     void insert_after(const T&);
  29.     bool is_empty() const;
  30.     unsigned int size() const;
  31.     void clear();
  32.  
  33.   private:
  34.     Node<T>* Back;
  35.     Node<T>* Front;
  36.     unsigned int Length;
  37. };
  38.  
  39. template <class T>
  40. LinkedList<T>::LinkedList() :
  41.   Back(0),
  42.   Front(0),
  43.   Length(0)
  44. {}
  45.  
  46. template <class T>
  47. LinkedList<T>::LinkedList(LinkedList& rhs) :
  48.   Back(rhs.end()),
  49.   Front(rhs.begin()),
  50.   Length(rhs.size())
  51. {}
  52.  
  53. template <class T>
  54. LinkedList<T>::LinkedList(const LinkedList& rhs) :
  55.   Back(rhs.end()),
  56.   Front(rhs.begin()),
  57.   Length(rhs.size())
  58. {}
  59.  
  60. template <class T>
  61. LinkedList<T>::~LinkedList() { clear(); }
  62.  
  63. template <class T>
  64. LinkedList<T>& LinkedList<T>::operator=(const LinkedList& rhs)
  65. {
  66.   if (this != &rhs)
  67.   {
  68.     this.Back = rhs.end();
  69.     this.Front = rhs.begin();
  70.     this.Length = rhs.size();
  71.   }
  72.  
  73.   return *this;
  74. }
  75.  
  76. template <class T>
  77. T LinkedList<T>::at(const unsigned int Index) const
  78. {
  79.   T *return_value = NULL;
  80.  
  81.   Node<T> *MyNode = nullptr;
  82.  
  83.   try
  84.   {
  85.     if(Index <= Length)
  86.     {
  87.       MyNode = Front;
  88.  
  89.       for(unsigned int i = 1 ; i <= Index ; ++i)
  90.       {
  91.         MyNode = MyNode->Next;
  92.       }
  93.  
  94.       *return_value = MyNode->Cargo;
  95.     }
  96.   }
  97.   catch(...)
  98.   {
  99.     delete MyNode;
  100.     throw;
  101.   }
  102.  
  103.   return *return_value;
  104. }
  105.  
  106. template <class T>
  107. T LinkedList<T>::at(const unsigned int Index)
  108. {
  109.   T return_value;
  110.  
  111.   Node<T> *MyNode = nullptr;
  112.  
  113.   try
  114.   {
  115.     if(Index <= Length)
  116.     {
  117.       MyNode = Front;
  118.  
  119.       for(unsigned int i = 1 ; i <= Index ; ++i)
  120.       {
  121.         MyNode = MyNode->Next;
  122.       }
  123.  
  124.       return_value = MyNode->Cargo;
  125.     }
  126.   }
  127.   catch(...)
  128.   {
  129.     delete MyNode;
  130.     throw;
  131.   }
  132.  
  133.   return return_value;
  134. }
  135.  
  136. template <class T>
  137. const Node<T> LinkedList<T>::begin() const{ return Front; }
  138.  
  139. template <class T>
  140. const Node<T> LinkedList<T>::end() const { return Back; }
  141.  
  142. template <class T>
  143. void LinkedList<T>::pop_back()
  144. {
  145.   if(!is_empty())
  146.   {
  147.     if(Length > 1)
  148.     {
  149.       Node<T> *TailNode = 0;
  150.  
  151.       try
  152.       {
  153.         TailNode = new Node<T>;
  154.         TailNode = Back->Prev;
  155.         TailNode->Next = NULL;
  156.         Back = TailNode;
  157.         --Length;
  158.       }
  159.       catch(...)
  160.       {
  161.         delete TailNode;
  162.         throw;
  163.       }
  164.     }
  165.     else if(Length == 1)
  166.     {
  167.       Back = NULL;
  168.       Front = NULL;
  169.       Length = 0;
  170.     }
  171.   }
  172. }
  173.  
  174. template <class T>
  175. void LinkedList<T>::pop_front()
  176. {
  177.   if(Length > 1)
  178.   {
  179.     Node<T> *NextNode = 0;
  180.  
  181.     try
  182.     {
  183.       NextNode = new Node<T>;
  184.       NextNode = Front.Next;
  185.       Front = NextNode;
  186.       --Length;
  187.     }
  188.     catch(...)
  189.     {
  190.       delete NextNode;
  191.       throw;
  192.     }
  193.   }
  194.   else if (Length == 1)
  195.   {
  196.     Back = NULL;
  197.     Front = NULL;
  198.     Length = 0;
  199.   }
  200. }
  201.  
  202. template <class T>
  203. void LinkedList<T>::push_back(const T& Data)
  204. {
  205.   Node<T> *NewNode = nullptr;
  206.  
  207.   try
  208.   {
  209.     NewNode = new Node<T>;
  210.  
  211.     NewNode->Cargo = Data;
  212.  
  213.     if(is_empty())
  214.     {
  215.       Front = NewNode;
  216.     }
  217.     else
  218.     {
  219.       Back->Next = NewNode;
  220.       NewNode->Prev = Back;
  221.     }
  222.  
  223.     Back = NewNode;
  224.  
  225.     ++Length;
  226.   }
  227.   catch(...)
  228.   {
  229.     delete NewNode;
  230.     throw;
  231.   }
  232. }
  233.  
  234. template <class T>
  235. void LinkedList<T>::insert_after(const T& Data) {} /// todo
  236.  
  237. template <class T>
  238. bool LinkedList<T>::is_empty() const
  239. {
  240.   if(Length) { return false; }
  241.   else       { return true; }
  242. }
  243.  
  244. template <class T>
  245. unsigned int LinkedList<T>::size() const
  246. {
  247.   return Length;
  248. }
  249.  
  250. template <class T>
  251. void LinkedList<T>::clear()
  252. {
  253.   while(!is_empty()) { pop_back(); }
  254. }
  255.  
  256. #endif
Advertisement
Add Comment
Please, Sign In to add comment