Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct Node
- {
- int data;
- Node* next;
- };
- int sizell(Node* head);
- int GetNth(Node* head, unsigned index);
- void InsertNth(Node*& head, unsigned index, int element);
- void DeleteNth(Node*& head, unsigned index);
- void DeleteList(Node*& head);
- void printList(Node* head);
- Node* CopyList(Node*& head);
- void Append(Node*& a, Node*& b);
- void FrontBackSplit(Node* head, Node*& front, Node*& back);
- Node* SortedMerge(Node* a, Node* b);
- void RemoveDublicates(Node*& head);
- void MergeSort(Node*& head);
- void Reverse(Node*& head);
- int main()
- {
- Node* l1 = nullptr;
- InsertNth(l1, 0, 100);
- InsertNth(l1, 1, 100);
- InsertNth(l1, 2, 10);
- InsertNth(l1, 3, 100000);
- InsertNth(l1, 4, 1000);
- InsertNth(l1, 5, 1000);
- MergeSort(l1);
- Reverse(l1);
- printList(l1);
- return 0;
- }
- int sizell(Node* head)
- {
- int cnt = 0;
- Node* current = head;
- while (current != nullptr)
- {
- cnt++;
- current = current->next;
- }
- return cnt;
- }
- int GetNth(Node* head, unsigned index)
- {
- if (sizell(head) < index)
- {
- throw "Index out of range exception!";
- }
- Node* current = head;
- for (int i = 0; i < index; i++)
- {
- current = current->next;
- }
- return current->data;
- }
- void InsertNth(Node*& head, unsigned index, int element)
- {
- Node* n = new Node();
- n->data = element;
- if (head == nullptr || index == 0)
- {
- n->next = head;
- head = n;
- }
- else
- {
- Node* current = head;
- for (int i = 1; i < index && current->next != nullptr; i++)
- {
- current = current->next;
- }
- n->next = current->next;
- current->next = n;
- }
- }
- void DeleteNth(Node*& head, unsigned index)
- {
- if (index >= sizell(head))
- {
- throw "Index out of range exception!";
- }
- if (index == 0)
- {
- Node* todelete = head;
- head = head->next;
- delete todelete;
- return;
- }
- Node* current = head;
- Node* prev;
- for (int i = 0; i < index; i++)
- {
- prev = current;
- current = current->next;
- }
- prev->next = current->next;
- delete current;
- }
- void DeleteList(Node*& head)
- {
- Node* current = head;
- Node* todelete;
- while (current != nullptr)
- {
- todelete = current;
- current = current->next;
- delete todelete;
- }
- head = nullptr;
- }
- void printList(Node* head)
- {
- Node* current = head;
- while (current != nullptr)
- {
- cout << current->data << " ";
- current = current->next;
- }
- cout << endl;
- }
- Node* CopyList(Node*& head)
- {
- Node* copyl = nullptr;
- for (int i = 0; i < sizell(head); i++)
- {
- InsertNth(copyl, i, GetNth(head, i));
- }
- return copyl;
- }
- void Append(Node*& a, Node*& b)
- {
- int sizea = sizell(a);
- int sizeb = sizell(b);
- for (int i = 0; i < sizeb; i++)
- {
- InsertNth(a, sizea+i, GetNth(b, i));
- }
- b = nullptr;
- }
- Node* SortedMerge(Node* a, Node* b)
- {
- Node dummy;
- dummy.next = nullptr;
- Node* tail = &dummy;
- while (true)
- {
- if (a == nullptr)
- {
- tail->next = b;
- break;
- }
- if (b == nullptr)
- {
- tail->next = a;
- break;
- }
- if (a->data < b->data)
- {
- tail->next = a;
- tail = a;
- a=a->next;
- }
- else
- {
- tail->next = b;
- tail = b;
- b=b->next;
- }
- }
- return dummy.next;
- }
- void FrontBackSplit(Node* head, Node*& front, Node*& back)
- {
- if (head == nullptr)
- {
- front = nullptr;
- back = nullptr;
- }
- else
- {
- Node* slowptr = head;
- Node *fastptr = head->next;
- while (fastptr != nullptr)
- {
- fastptr = fastptr->next;
- if (fastptr != nullptr)
- {
- fastptr = fastptr->next;
- slowptr = slowptr->next;
- }
- }
- back = slowptr->next;
- slowptr->next = nullptr;
- front = head;
- }
- }
- void RemoveDublicates(Node*& head)
- {
- if (head == nullptr)
- {
- return;
- }
- Node *current = head;
- while (current->next != nullptr)
- {
- if (current->data == current->next->data)
- {
- Node* todelete = current->next;
- current->next = current->next->next;
- delete todelete;
- }
- else
- {
- current = current->next;
- }
- }
- }
- void Reverse(Node*& head)
- {
- Node* previous = nullptr;
- Node* current = head;
- Node* next;
- while (current != nullptr)
- {
- next = current->next;
- current->next = previous;
- previous = current;
- current = next;
- }
- head = previous;
- }
- void MergeSort(Node*& head)
- {
- if (head == nullptr || head->next == nullptr)
- {
- return;
- }
- Node* front;
- Node* back;
- FrontBackSplit(head, front, back);
- MergeSort(front);
- MergeSort(back);
- head = SortedMerge(front, back);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement