-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[REVIEW] Добавяне на reverse и sort(mergesort) функции в списъците #11
base: main
Are you sure you want to change the base?
[REVIEW] Добавяне на reverse и sort(mergesort) функции в списъците #11
Conversation
2f54bb2
to
67f8093
Compare
@@ -100,6 +101,71 @@ class DoubleLinkedList { | |||
deleteFirst(front->data); | |||
} | |||
|
|||
void split(I& list, I& left, I& right) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тази функция може да е публична и да унищожава оригиналния списък и да създав два нови със съответните вериги в тях.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ако приемем да работим със списъци вместо с итератори, първият параметър може да се пропусне и да се използва this.
slow.ptr->next = nullptr; | ||
} | ||
|
||
I merge(I left, I right) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Тази функция може да е публична, да приема два списъка с препратка, след което да им „краде“ веригите като ги слива. Ако резултатът е текущият списък, първо трябва да се изтрие текущото му съдържание,ако има такова. Ако резултатът е нов списък, тогава функцията би следвало да е статична.
return left; | ||
|
||
if(left.get() <= right.get()) { | ||
I recResult = merge(left.next(), right); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Рекурсията е излишна. Идеята на функцията е да стане с константна допълнителна памет, а така става с линейна допълнителна памет заради стековите рамки.
} | ||
} | ||
|
||
I mergeSort(I list) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Използва се като статична функция, макар че не е. Ако следваме идеята горните функции също да не са статични, то тогава би трябвало:
split
да запълва два подадени празни списъка, като изпразва текущия.merge
да слива два подадени списъка в текущия, като изпразва попадените.mergeSort
би трябвало да ползва два временни списъка и да работи върху списъка, за който се извиква метода. Това ще стане по естествен начин,тъй като така ще работи извикването наmerge
.
void sort() { | ||
front = mergeSort(begin()).ptr; | ||
|
||
I it = begin(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Частта със задаването на back
ще отпадне, ако merge
работи директно със списъци.
@@ -94,6 +95,62 @@ class LinkedList { | |||
deleteFirst(front->data); | |||
} | |||
|
|||
void split(I list, I& left, I& right) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Виж коментарите за двойносвързания списък.
list.reverse(); | ||
|
||
CHECK(!list.empty()); | ||
CHECK_EQ(list.begin().get(), val); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Да се провери, че списъкът е само от един елемент.
No description provided.