Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 38 additions & 15 deletions LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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&);
Copy link
Owner

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

Copy link
Author

@anmaped anmaped Oct 19, 2016

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 :)

/*
Adds a T object in the start of the LinkedList;
Increment _size;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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){
Copy link
Owner

Choose a reason for hiding this comment

The 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?

Copy link
Author

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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++)
{
Copy link
Owner

Choose a reason for hiding this comment

The 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