-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Modifications to be compliant with deep copy of structures. #15
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,16 +37,17 @@ class LinkedList{ | |
// everytime the list suffer changes | ||
bool isCached; | ||
|
||
ListNode<T>* getNode(int index); | ||
ListNode<T>* getNode(int index); | ||
|
||
public: | ||
LinkedList(); | ||
LinkedList(const LinkedList<T> &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<T> & operator =(const LinkedList<T> &rhs); | ||
|
||
//virtual operator LinkedList<T>(); | ||
|
||
}; | ||
|
||
// Initialize LinkedList with false values | ||
template<typename T> | ||
LinkedList<T>::LinkedList() | ||
LinkedList<T>::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<typename T> | ||
LinkedList<T>::LinkedList(const LinkedList<T> &rhs) : LinkedList() | ||
{ | ||
*this = rhs; | ||
} | ||
|
||
// Clear Nodes and free Memory | ||
|
@@ -161,7 +166,7 @@ ListNode<T>* LinkedList<T>::getNode(int index){ | |
} | ||
|
||
template<typename T> | ||
int LinkedList<T>::size(){ | ||
int LinkedList<T>::size() const{ | ||
return _size; | ||
} | ||
|
||
|
@@ -187,7 +192,7 @@ bool LinkedList<T>::add(int index, T _t){ | |
} | ||
|
||
template<typename T> | ||
bool LinkedList<T>::add(T _t){ | ||
bool LinkedList<T>::add(T& _t){ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if it's a primitive (int, char, float...) type, and it gets by reference? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no problem at all. Show me an example that doesn't work. |
||
|
||
ListNode<T> *tmp = new ListNode<T>(); | ||
tmp->data = _t; | ||
|
@@ -245,15 +250,15 @@ T LinkedList<T>::pop(){ | |
|
||
if(_size >= 2){ | ||
ListNode<T> *tmp = getNode(_size - 2); | ||
T ret = tmp->next->data; | ||
T ret = tmp->next->data; | ||
delete(tmp->next); | ||
tmp->next = NULL; | ||
last = tmp; | ||
_size--; | ||
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<T>::clear(){ | |
shift(); | ||
} | ||
|
||
template<typename T> | ||
LinkedList<T>& LinkedList<T>::operator =(const LinkedList<T>& rhs) { | ||
|
||
clear(); | ||
|
||
ListNode<T> * x = rhs.root; | ||
// copy | ||
for(int i=0; i < rhs.size(); i++) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please follow the pattern (Open bracket on the same line) |
||
T tmp = T(x->data); | ||
|
||
add(tmp); | ||
x = x->next; | ||
} | ||
|
||
return *this; | ||
} | ||
|
||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this will cause incompatibility with major usage.. In the past, it was by reference, but was changed back to the Type in order to support pointers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pointers can be passed by reference. This is the safest method. Without that you are making copies of variables without sense. Trust on compiler :)