Skip to content

Commit 4ba34f5

Browse files
authored
Merge pull request #35 from yusuf601/main
feat: add two overloads splice after
2 parents baf7450 + 7dc98b6 commit 4ba34f5

File tree

4 files changed

+87
-28
lines changed

4 files changed

+87
-28
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ FetchContent_MakeAvailable(googletest)
2626

2727
# ==== Testing ====
2828
include(CTest)
29+
set(CMAKE_CTEST_ARGUMENTS "--timeout 5")
2930
enable_testing()
3031

3132
add_executable(forward_list_test

header/forward_list.hpp

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -613,26 +613,67 @@ class forward_lists{
613613
size++;
614614
}
615615
void splice_after(const Iterator pos,forward_lists& others){
616+
if(this == &others){
617+
return;
618+
}
616619
Node* src = pos.get_raw();//pos object saat ini
617620
Node* begin = others.head->next ; //begin object lain
618621
Node* end = others.tail; //end object lain
619622

620-
//lingking node pertama others ke object saat ini
621-
Node* moved = src->next;
622-
moved->next = begin->next; //tunjuk node setelah begin
623-
624-
begin->next = moved; //linking begin ke node setelah iterator
625-
//linking node terakhir object lain ke src->next
626623
end->next = src->next;
624+
src->next = begin;
625+
//update tail
626+
if(src == tail){
627+
tail = end;
628+
}
629+
//update size
630+
size += others.size;
631+
//kosong object lain
632+
others.head->next = nullptr; //harus menunjuk nullptr
633+
others.tail = others.head;//reset tail,tail menunjuk head(dummy node)
634+
others.size = 0;
635+
}
636+
void splice_after(const Iterator pos,forward_lists&& others){ //global reference
637+
if(this == &others){
638+
return;
639+
}
640+
Node* src = pos.get_raw();//pos object saat ini
641+
Node* begin = others.head->next ; //begin object lain
642+
Node* end = others.tail; //end object lain
627643

628-
//linking node pertama object saat ini ke head object lain
644+
end->next = src->next;
629645
src->next = begin;
646+
//update tail
647+
if(src == tail){
648+
tail = end;
649+
}
650+
//update size
630651
size += others.size;
652+
//kosong object lain
653+
others.head->next = nullptr; //harus menunjuk nullptr
654+
others.tail = others.head;//reset tail,tail menunjuk head(dummy node)
631655
others.size = 0;
632656
}
633-
// void splice_after(const Iterator pos,forward_lists& others,const Iterator first,const Iterator last){
634-
// Node* curr = pos.get_raw();
635-
// }
657+
void splice_after(const Iterator pos,forward_lists& others,const Iterator first,const Iterator last){
658+
Node* curr = pos.get_raw();
659+
Node* start = others.head->next;
660+
Node* other_begin = first.get_raw()->next;
661+
Node* other_end = last.get_raw();
662+
//linking other_end to curr->next
663+
other_end->next =curr->next;
664+
//lingking curr->next with to others_begin
665+
curr->next = other_begin;
666+
667+
//update size
668+
//karena linking dimulai dari node setelah posisi first
669+
size += std::distance(next(first,last));
670+
others.size -= size;
671+
672+
//kosongkan object lain
673+
674+
tail = first.get_raw();
675+
676+
}
636677
public:
637678
/**
638679
* @brief method untuk print semua node list

implementation/implementation.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44
int main(){
55
std::cout << "Pop Front" << std::endl;
66
forward_lists<int>flst = {1,2,3};
7-
flst.pop_front();
8-
flst.pop_front();
9-
std::cout << "harus nya sisa 3" << std::endl;
10-
std::cout << "ini tail " << flst.back() << std::endl;
7+
forward_lists<int>fa = {10,20,30};
8+
flst.splice_after(flst.begin(),fa);
9+
// flst.pop_front();
10+
// flst.pop_front();
11+
std::cout << "ini splice" << std::endl;
12+
// std::cout << "ini tail " << flst.back() << std::endl;
1113
flst.print_all(flst.begin(),flst.end()); //3
1214
//initializer list
1315
std::cout << "initializer list constructor" << std::endl;

test/testing.cpp

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,32 @@ TEST(Assign_test,Assign_testIII){
213213
}
214214
EXPECT_EQ(actual,expectations);
215215
}
216-
// TEST(splice_test,splice_testI){
217-
// forward_lists<int>fl = {1,2,3};
218-
// forward_lists<int>list = {10,20,30};
219-
// fl.splice_after(fl.begin(),list);
220-
// std::vector<int>expectations = {1,10,20,30,2,3};
221-
// std::vector<int>actual;
222-
// for(auto x: fl){
223-
// actual.push_back(x);
224-
// }
225-
// EXPECT_EQ(fl.get_size(),6);
226-
// EXPECT_EQ(list.get_size(),0);
227-
// EXPECT_TRUE(!list.is_empty());
228-
// EXPECT_EQ(actual,expectations);
229-
// }
216+
TEST(splice_test,splice_testI){
217+
forward_lists<int>fl = {1,2,3};
218+
forward_lists<int>list = {10,20,30};
219+
fl.splice_after(fl.begin(),list);
220+
std::vector<int>expectations = {1,10,20,30,2,3};
221+
std::vector<int>actual;
222+
for(auto x: fl){
223+
actual.push_back(x);
224+
}
225+
EXPECT_EQ(fl.get_size(),6);
226+
EXPECT_EQ(list.get_size(),0);
227+
EXPECT_TRUE(list.is_empty());
228+
EXPECT_EQ(actual,expectations);
229+
}
230+
TEST(splice_test,splice_testII){
231+
forward_lists<int>fl = {100,200,300};
232+
forward_lists<int>lists = {20,30,40};
233+
EXPECT_EQ(fl.get_size(),3);
234+
EXPECT_EQ(lists.get_size(),3);
235+
fl.splice_after(fl.begin(),lists,lists.begin());
236+
std::vector<int>expectations = {100,30,200,300};
237+
std::vector<int>actual;
238+
for(auto x: fl){
239+
actual.push_back(x);
240+
}
241+
EXPECT_EQ(fl.get_size(),4);
242+
EXPECT_EQ(lists.get_size(),2);
243+
EXPECT_EQ(actual,expectations);
244+
}

0 commit comments

Comments
 (0)