Skip to content

Commit

Permalink
Merge pull request ivanseidel#19 from NitrofMtl/NitrofMtl-patch-1
Browse files Browse the repository at this point in the history
add [] operator support
  • Loading branch information
ivanseidel authored Apr 16, 2019
2 parents fc2db43 + 4d9c0f3 commit 816c39e
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions LinkedList.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class LinkedList{

public:
LinkedList();
LinkedList(int sizeIndex, T _t); //initiate list size and default value
~LinkedList();

/*
Expand Down Expand Up @@ -100,6 +101,11 @@ class LinkedList{
*/
virtual void sort(int (*cmp)(T &, T &));

// add support to array brakets [] operator
inline T& operator[](int index);
inline T& operator[](size_t& i) { return this->get(i); }
inline const T& operator[](const size_t& i) const { return this->get(i); }

};

// Initialize LinkedList with false values
Expand Down Expand Up @@ -171,6 +177,13 @@ int LinkedList<T>::size(){
return _size;
}

template<typename T>
LinkedList<T>::LinkedList(int sizeIndex, T _t){
for (int i = 0; i < sizeIndex; i++){
add(_t);
}
}

template<typename T>
bool LinkedList<T>::add(int index, T _t){

Expand Down Expand Up @@ -232,6 +245,12 @@ bool LinkedList<T>::unshift(T _t){
return true;
}


template<typename T>
T& LinkedList<T>::operator[](int index) {
return getNode(index)->data;
}

template<typename T>
bool LinkedList<T>::set(int index, T _t){
// Check if index position is in bounds
Expand Down

0 comments on commit 816c39e

Please sign in to comment.