Skip to content
Merged
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
14 changes: 11 additions & 3 deletions header/forward_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,9 @@ class forward_lists{
bool operator!=(const Iterator& others)const{
return node != others.node;
}
bool operator==(const Iterator& others)const{
return node == others.node;
}
Node* get_raw()const{
return node;
}
Expand Down Expand Up @@ -716,7 +719,7 @@ class forward_lists{
}
void assign(std::initializer_list<T> arr){
clear();
head = new Node(T{});
head = _create_node(T{});
head->next = nullptr;
Node** curr = &head->next;
//size++;
Expand Down Expand Up @@ -1378,7 +1381,7 @@ class forward_lists{
* average case: O(n) jika pos ditengah list
* worst case: O(n) jika pos diakhir list(tail)
*/
void remove(std::size_t pos){
void remove(const std::size_t pos){
if(!head->next){
return;
}
Expand All @@ -1389,21 +1392,26 @@ class forward_lists{
//- 1 1 1 2 2
Node* prev = head;
Node* curr = head->next;
static_assert(std::is_same_v<decltype(curr), Node*>);
static_assert(std::is_same_v<decltype(tail), Node*>);

for(std::size_t i = 0;i < pos;i++){
prev = curr;
curr = curr->next;
}
//sekarang curr menunjuk pos
if(curr == tail){
head->next = nullptr;
curr = nullptr;
tail = curr;
tail = head;
}else{
prev->next = curr->next;
}
size--;
_destroy_node(curr);
if(!head->next){
tail = head;
size = 0;
}
}
public:
Expand Down