#1 Added operator overload, [], to Linked List

This commit is contained in:
Luke Else 2022-11-23 20:44:21 +00:00
parent ee0556730d
commit 596d863a25
2 changed files with 8 additions and 1 deletions

View File

@ -9,7 +9,7 @@ namespace Datastructures {
LinkedList(); LinkedList();
LinkedList(T value); LinkedList(T value);
~LinkedList(); ~LinkedList();
T operator[](int index); T& operator[](int index);
void append(T value); void append(T value);
bool insert(T value, int index); bool insert(T value, int index);
bool remove(int index); bool remove(int index);
@ -43,6 +43,12 @@ namespace Datastructures {
template <typename T> template <typename T>
LinkedList<T>::~LinkedList() {} LinkedList<T>::~LinkedList() {}
template <typename T>
T& LinkedList<T>::operator[](int index) {
//Return the value from a given index. (Will return null pointer if out of range)
return this->getIndex(index)->value;
}
template <typename T> template <typename T>
void LinkedList<T>::append(T value) { void LinkedList<T>::append(T value) {
mCount++; mCount++;

View File

@ -7,6 +7,7 @@ int main() {
list.append(200); list.append(200);
list.insert(20, 2); list.insert(20, 2);
std::cout << list.count() << std::endl; //3 std::cout << list.count() << std::endl; //3
std::cout << list[0] << list[1] << list[2] << std::endl; //520020
list.remove(0); list.remove(0);
int x = list.find(10); int x = list.find(10);
std::cout << list.count() << std::endl; //2 std::cout << list.count() << std::endl; //2