Skip to content
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions hw04/ordered_list_linked_starter_code/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"files.associations": {
"*.tpp": "cpp"
}
}
4 changes: 2 additions & 2 deletions hw04/ordered_list_linked_starter_code/linked_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class LinkedList: public AbstractList<T>

private:

//TODO
Node<T> head;

};

#include "linked_list.tpp"
Expand Down
97 changes: 83 additions & 14 deletions hw04/ordered_list_linked_starter_code/linked_list.tpp
Original file line number Diff line number Diff line change
@@ -1,74 +1,143 @@
#include "linked_list.hpp"
#include "Node.hpp"
#include <algorithm>

template <typename T>
LinkedList<T>::LinkedList()
{
//TODO
head->next = nullptr;
}

template <typename T>
LinkedList<T>::~LinkedList()
{
//TODO
Node<T>* current = new Node<T>;
while(current != nullptr)
{
Node<T>* next = new Node<T>{current->next};
delete current;
current = next;
}
}

template <typename T>
LinkedList<T>::LinkedList(const LinkedList<T>& x)
{
//TODO
Node<T>* current = new Node<T>;
Node<T>* current_x = new Node<T>;
current = this.head;
current_x = x.head;

if(head == nullptr)
{
x.head.item = head.item;
return;
}

while(current != nullptr)
{
current = current -> next;
current_x -> next = new Node<T>{current->item};
}
}

template <typename T>
void LinkedList<T>::swap(LinkedList<T>& x, LinkedList<T>& y)
{
//TODO
std::swap(x.head, y.head);
}

template <typename T>
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& x)
{
//TODO
LinkedList<T> temp(x);
std::swap(temp.head, head);
return *this;
}

template <typename T>
bool LinkedList<T>::isEmpty() const
{
//TODO
return true;
if(head == nullptr)
return true;
else
return false;
}

template <typename T>
std::size_t LinkedList<T>::getLength() const
{
//TODO
return 0;
int count = 0;
Node<T>* temp = new Node<T>{head};
while(temp != nullptr)
{
temp = temp->next;
count++;
}
return count;
}

template <typename T>
bool LinkedList<T>::insert(std::size_t position, const T& item)
{
//TODO
return true;
Node<T>* current = new Node<T>;
Node<T>* slack = new Node<T>;
current = head;
int count{0};

while(count < position && slack != nullptr)
{
slack = current;
current = current -> next;
count++;
}

if(slack != nullptr)
{
Node<T> newNode = new Node<T>;
newNode.item = item;
slack -> next = newNode;
newNode -> next = current;
return true;
}else
{
return false;
}

}

template <typename T>
bool LinkedList<T>::remove(std::size_t position)
{
//TODO
Node<T>* current = new Node<T>;
Node<T>* slack = new Node<T>;
Node<T>* next_node = new Node<T>;
current = head;
int count{0};

while(count < position)
{
slack = current;
current = current -> next;
}

next_node = current ->next;
delete current;
slack -> next = next_node;

return true;
}

template <typename T>
void LinkedList<T>::clear()
{
//TODO
LinkedList::~LinkedList();
}

template <typename T>
T LinkedList<T>::getEntry(std::size_t position) const
{
//TODO
//TODO Not quiet sure what the purpose of this is? return head?
return T();
}

Expand Down