From 7e5f87a09f28d362782885b081b06b4434f2684e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Pedro?= Date: Mon, 17 Oct 2016 13:45:27 +0100 Subject: [PATCH] Modifications to be compliant with deep copy of structures. --- LinkedList.h | 53 +++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 15 deletions(-) diff --git a/LinkedList.h b/LinkedList.h index 371b14a..d55328a 100644 --- a/LinkedList.h +++ b/LinkedList.h @@ -37,16 +37,17 @@ class LinkedList{ // everytime the list suffer changes bool isCached; - ListNode* getNode(int index); + ListNode* getNode(int index); public: LinkedList(); + LinkedList(const LinkedList &rhs); ~LinkedList(); /* Returns current size of LinkedList */ - virtual int size(); + virtual int size() const; /* Adds a T object in the specified index; Unlink and link the LinkedList correcly; @@ -57,7 +58,7 @@ class LinkedList{ Adds a T object in the end of the LinkedList; Increment _size; */ - virtual bool add(T); + virtual bool add(T&); /* Adds a T object in the start of the LinkedList; Increment _size; @@ -87,26 +88,30 @@ class LinkedList{ Return Element if accessible, else, return false; */ - virtual T get(int index); + virtual T get(int index); /* Clear the entire array */ virtual void clear(); + virtual LinkedList & operator =(const LinkedList &rhs); + + //virtual operator LinkedList(); + }; // Initialize LinkedList with false values template -LinkedList::LinkedList() +LinkedList::LinkedList() : root(NULL), last(NULL), _size(0), lastNodeGot(NULL), lastIndexGot(0), isCached(false) { - root=NULL; - last=NULL; - _size=0; - lastNodeGot = root; - lastIndexGot = 0; - isCached = false; +} + +template +LinkedList::LinkedList(const LinkedList &rhs) : LinkedList() +{ + *this = rhs; } // Clear Nodes and free Memory @@ -161,7 +166,7 @@ ListNode* LinkedList::getNode(int index){ } template -int LinkedList::size(){ +int LinkedList::size() const{ return _size; } @@ -187,7 +192,7 @@ bool LinkedList::add(int index, T _t){ } template -bool LinkedList::add(T _t){ +bool LinkedList::add(T& _t){ ListNode *tmp = new ListNode(); tmp->data = _t; @@ -245,7 +250,7 @@ T LinkedList::pop(){ if(_size >= 2){ ListNode *tmp = getNode(_size - 2); - T ret = tmp->next->data; + T ret = tmp->next->data; delete(tmp->next); tmp->next = NULL; last = tmp; @@ -253,7 +258,7 @@ T LinkedList::pop(){ return ret; }else{ // Only one element left on the list - T ret = root->data; + T ret = root->data; delete(root); root = NULL; last = NULL; @@ -322,4 +327,22 @@ void LinkedList::clear(){ shift(); } +template +LinkedList& LinkedList::operator =(const LinkedList& rhs) { + + clear(); + + ListNode * x = rhs.root; + // copy + for(int i=0; i < rhs.size(); i++) + { + T tmp = T(x->data); + + add(tmp); + x = x->next; + } + + return *this; +} + #endif