Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef Linked_List_H
- #define Linked_List_H
- #include <stdexcept>
- #include <exception>
- #include "Node.h"
- using namespace std;
- template <class T>
- class LinkedList
- {
- public:
- LinkedList();
- LinkedList(LinkedList&);
- LinkedList(const LinkedList&);
- ~LinkedList();
- LinkedList<T>& operator=(const LinkedList&);
- T at(const unsigned int) const;
- T at(const unsigned int);
- const Node<T> begin() const;
- const Node<T> end() const;
- void pop_back();
- void pop_front();
- void push_back(const T&);
- void insert_after(const T&);
- bool is_empty() const;
- unsigned int size() const;
- void clear();
- private:
- Node<T>* Back;
- Node<T>* Front;
- unsigned int Length;
- };
- template <class T>
- LinkedList<T>::LinkedList() :
- Back(0),
- Front(0),
- Length(0)
- {}
- template <class T>
- LinkedList<T>::LinkedList(LinkedList& rhs) :
- Back(rhs.end()),
- Front(rhs.begin()),
- Length(rhs.size())
- {}
- template <class T>
- LinkedList<T>::LinkedList(const LinkedList& rhs) :
- Back(rhs.end()),
- Front(rhs.begin()),
- Length(rhs.size())
- {}
- template <class T>
- LinkedList<T>::~LinkedList() { clear(); }
- template <class T>
- LinkedList<T>& LinkedList<T>::operator=(const LinkedList& rhs)
- {
- if (this != &rhs)
- {
- this.Back = rhs.end();
- this.Front = rhs.begin();
- this.Length = rhs.size();
- }
- return *this;
- }
- template <class T>
- T LinkedList<T>::at(const unsigned int Index) const
- {
- T *return_value = NULL;
- Node<T> *MyNode = nullptr;
- try
- {
- if(Index <= Length)
- {
- MyNode = Front;
- for(unsigned int i = 1 ; i <= Index ; ++i)
- {
- MyNode = MyNode->Next;
- }
- *return_value = MyNode->Cargo;
- }
- }
- catch(...)
- {
- delete MyNode;
- throw;
- }
- return *return_value;
- }
- template <class T>
- T LinkedList<T>::at(const unsigned int Index)
- {
- T return_value;
- Node<T> *MyNode = nullptr;
- try
- {
- if(Index <= Length)
- {
- MyNode = Front;
- for(unsigned int i = 1 ; i <= Index ; ++i)
- {
- MyNode = MyNode->Next;
- }
- return_value = MyNode->Cargo;
- }
- }
- catch(...)
- {
- delete MyNode;
- throw;
- }
- return return_value;
- }
- template <class T>
- const Node<T> LinkedList<T>::begin() const{ return Front; }
- template <class T>
- const Node<T> LinkedList<T>::end() const { return Back; }
- template <class T>
- void LinkedList<T>::pop_back()
- {
- if(!is_empty())
- {
- if(Length > 1)
- {
- Node<T> *TailNode = 0;
- try
- {
- TailNode = new Node<T>;
- TailNode = Back->Prev;
- TailNode->Next = NULL;
- Back = TailNode;
- --Length;
- }
- catch(...)
- {
- delete TailNode;
- throw;
- }
- }
- else if(Length == 1)
- {
- Back = NULL;
- Front = NULL;
- Length = 0;
- }
- }
- }
- template <class T>
- void LinkedList<T>::pop_front()
- {
- if(Length > 1)
- {
- Node<T> *NextNode = 0;
- try
- {
- NextNode = new Node<T>;
- NextNode = Front.Next;
- Front = NextNode;
- --Length;
- }
- catch(...)
- {
- delete NextNode;
- throw;
- }
- }
- else if (Length == 1)
- {
- Back = NULL;
- Front = NULL;
- Length = 0;
- }
- }
- template <class T>
- void LinkedList<T>::push_back(const T& Data)
- {
- Node<T> *NewNode = nullptr;
- try
- {
- NewNode = new Node<T>;
- NewNode->Cargo = Data;
- if(is_empty())
- {
- Front = NewNode;
- }
- else
- {
- Back->Next = NewNode;
- NewNode->Prev = Back;
- }
- Back = NewNode;
- ++Length;
- }
- catch(...)
- {
- delete NewNode;
- throw;
- }
- }
- template <class T>
- void LinkedList<T>::insert_after(const T& Data) {} /// todo
- template <class T>
- bool LinkedList<T>::is_empty() const
- {
- if(Length) { return false; }
- else { return true; }
- }
- template <class T>
- unsigned int LinkedList<T>::size() const
- {
- return Length;
- }
- template <class T>
- void LinkedList<T>::clear()
- {
- while(!is_empty()) { pop_back(); }
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment