Skip to content

Commit

Permalink
Updates the beta version of linked list.
Browse files Browse the repository at this point in the history
  • Loading branch information
ax-6 committed Mar 30, 2024
1 parent 426d3fc commit 376e756
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 64 deletions.
56 changes: 11 additions & 45 deletions compiler/linked_list/linked_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,6 @@ Compiler::LinkedList<DataType>::~LinkedList() {
}
}

template <typename DataType>
Compiler::LinkedList<DataType>::Node::Node(Node* prev, Node* next,
DataType data)
: location(prev, next), data(data) {}
template <typename DataType>
Compiler::LinkedList<DataType>::Node::~Node() = default;

template <typename DataType>
typename Compiler::LinkedList<DataType>::Node*
Compiler::LinkedList<DataType>::Node::GetPrev() const {
return this->location.first;
}
template <typename DataType>
typename Compiler::LinkedList<DataType>::Node*
Compiler::LinkedList<DataType>::Node::GetNext() const {
return this->location.second;
}
template <typename DataType>
DataType Compiler::LinkedList<DataType>::Node::GetData() {
return this->data;
}
template <typename DataType>
void Compiler::LinkedList<DataType>::Node::SetPrev(Node* prev) {
this->location.first = prev;
}
template <typename DataType>
void Compiler::LinkedList<DataType>::Node::SetNext(Node* next) {
this->location.second = next;
}
template <typename DataType>
void Compiler::LinkedList<DataType>::Node::SetData(DataType data) {
this->data = data;
}

template <typename DataType>
Compiler::LinkedList<DataType>::Iterator::Iterator(Node* node) : node_(node) {}
template <typename DataType>
Expand All @@ -73,48 +39,48 @@ Compiler::LinkedList<DataType>::Iterator::operator++() {
template <typename DataType>
typename Compiler::LinkedList<DataType>::Iterator&
Compiler::LinkedList<DataType>::Iterator::operator--() {
node_ = node_->prev;
node_ = node_->location.first;
return *this;
}
template <typename DataType>
typename Compiler::LinkedList<DataType>::Iterator&
Compiler::LinkedList<DataType>::Iterator::operator+=(std::size_t n) {
for (std::size_t i = 0; i < n; ++i) {
node_ = node_->next;
node_ = node_->location.second;
}
return *this;
}
template <typename DataType>
typename Compiler::LinkedList<DataType>::Iterator&
Compiler::LinkedList<DataType>::Iterator::operator-=(std::size_t n) {
for (std::size_t i = 0; i < n; ++i) {
node_ = node_->prev;
node_ = node_->location.first;
}
return *this;
}
template <typename DataType>
typename Compiler::LinkedList<DataType>::Iterator
Compiler::LinkedList<DataType>::Iterator::operator+(std::size_t n) const {
Iterator newIterator(*this);
newIterator += n;
return newIterator;
Iterator new_iterator(*this);
new_iterator += n;
return new_iterator;
}
template <typename DataType>
typename Compiler::LinkedList<DataType>::Iterator
Compiler::LinkedList<DataType>::Iterator::operator-(std::size_t n) const {
Iterator newIterator(*this);
newIterator -= n;
return newIterator;
Iterator new_iterator(*this);
new_iterator -= n;
return new_iterator;
}
template <typename DataType>
bool Compiler::LinkedList<DataType>::Iterator::operator==(
const Iterator& other) const {
return node_ == other.node_;
return *this->node_ == other.node_;
}
template <typename DataType>
bool Compiler::LinkedList<DataType>::Iterator::operator!=(
const Iterator& other) const {
return !(*this == other);
return !(*this->node_ == other->node_);
}

template <typename DataType>
Expand Down
20 changes: 1 addition & 19 deletions compiler/linked_list/linked_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,7 @@ class Compiler::LinkedList {
LinkedList& operator=(const LinkedList&) = delete;
LinkedList& operator=(LinkedList&&) noexcept = delete;

class Node {
public:
Node(Node* prev, Node* next, DataType data);
~Node();

Node(const Node&) = default;
Node(Node&&) noexcept = default;
Node& operator=(const Node&) = default;
Node& operator=(Node&&) noexcept = default;

Node* GetPrev() const;
Node* GetNext() const;
DataType GetData();

void SetPrev(Node* prev);
void SetNext(Node* next);
void SetData(DataType data);

private:
struct Node {
Pair<Node*, Node*> location;
DataType data;
};
Expand Down

0 comments on commit 376e756

Please sign in to comment.