Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- class DoubleNode {
- public:
- DoubleNode(int data) : data(data) {}
- int data;
- std::shared_ptr<DoubleNode> previous = nullptr;
- std::shared_ptr<DoubleNode> next = nullptr;
- int index = 0;
- };
- class DoublyLinkedListOfIntegers {
- public:
- std::shared_ptr<DoubleNode> pFirstNode = nullptr;
- std::shared_ptr<DoubleNode> pLastNode = nullptr;
- int count = 0;
- void add(int data) {
- std::shared_ptr<DoubleNode> newNode{ std::make_shared<DoubleNode>(data) };
- if (!pFirstNode) {
- pFirstNode = newNode;
- } else {
- pLastNode->next = newNode;
- newNode->previous = pLastNode;
- }
- newNode->index = count;
- pLastNode = newNode;
- ++count;
- }
- static void mergeSort(DoublyLinkedListOfIntegers& linkedList) {
- int amount = linkedList.count;
- int left, middle, right;
- for (int step = 1; step < amount; step *= 2) {
- for (int start = 0; start < amount; start += step * 2) {
- left = start;
- middle = std::min(start + step - 1, amount - 1);
- right = std::min(start + 2 * step - 1, amount - 1);
- merge(linkedList, left, middle, right);
- }
- }
- }
- static void merge(DoublyLinkedListOfIntegers& linkedList, int left, int middle, int right) {
- int leftAmount = middle - left + 1;
- int rightAmount = right - middle;
- DoublyLinkedListOfIntegers leftList;
- DoublyLinkedListOfIntegers rightList;
- std::shared_ptr<DoubleNode> listNode{ linkedList.pFirstNode };
- for (int i = 0; i < left; ++i) {
- listNode = listNode->next;
- }
- std::shared_ptr<DoubleNode> leftListNode{ listNode };
- for (int i = 0; i < leftAmount; ++i) {
- leftList.add(listNode->data);
- listNode = listNode->next;
- }
- for (int j = 0; j < rightAmount; ++j) {
- rightList.add(listNode->data);
- listNode = listNode->next;
- }
- int indexForLeftAmount = 0;
- int indexForRightAmount = 0;
- listNode = leftListNode;
- leftListNode = leftList.pFirstNode;
- std::shared_ptr<DoubleNode> rightListNode = rightList.pFirstNode;
- int k = left;
- while (indexForLeftAmount < leftAmount && indexForRightAmount < rightAmount) {
- if (leftListNode->data <= rightListNode->data) {
- listNode->data = leftListNode->data;
- leftListNode = leftListNode->next;
- ++indexForLeftAmount;
- listNode = listNode->next;
- } else {
- listNode->data = rightListNode->data;
- rightListNode = rightListNode->next;
- ++indexForRightAmount;
- listNode = listNode->next;
- }
- ++k;
- }
- while (indexForLeftAmount < leftAmount) {
- listNode->data = leftListNode->data;
- leftListNode = leftListNode->next;
- listNode = listNode->next;
- ++indexForLeftAmount;
- ++k;
- }
- while (indexForRightAmount < rightAmount) {
- listNode->data = rightListNode->data;
- rightListNode = rightListNode->next;
- listNode = listNode->next;
- ++indexForRightAmount;
- ++k;
- }
- }
- class Iterator {
- public:
- Iterator(std::shared_ptr<DoubleNode> node) : current(node) {}
- Iterator& operator++() {
- current = current->next;
- return *this;
- }
- bool operator != (const Iterator& other) const {
- return current != other.current;
- }
- int operator*() const {
- return current->data;
- }
- private:
- std::shared_ptr<DoubleNode> current;
- };
- Iterator begin() {
- return Iterator(pFirstNode);
- }
- Iterator end() {
- return Iterator(nullptr);
- }
- };
- int main(int argc, char* argv[]) {
- int value;
- DoublyLinkedListOfIntegers linkedList;
- while (std::cin >> value) {
- linkedList.add(value);
- }
- DoublyLinkedListOfIntegers::mergeSort(linkedList);
- for (int node : linkedList) {
- std::cout << node << " ";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement