Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stack>
- using namespace std;
- struct Node
- {
- int data;
- Node* next;
- };
- bool contains(Node* head, int el);
- int sizell(Node* head);
- void printList(Node* head);
- void DeleteNth(Node*& head, unsigned index);
- void MergeSort(Node*& head);
- void PushBack(Node*& head, int element);
- int GetNth(Node* head, unsigned index); // 1
- void InsertNth(Node*& head, unsigned index, int element); // 2
- void DeleteList(Node*& head); // 3
- Node* CopyList(Node*& head); // 4
- void Append(Node*& a, Node*& b); // 5
- void FrontBackSplit(Node* head, Node*& front, Node*& back); // 6
- void RemoveDublicates(Node*& head); // 7
- void MoveNode(Node*& a, Node*& b); // 8
- void AlternatingSplit(Node* head, Node*& a, Node*& b); // 9
- void AlternatingSplit2(Node* head, Node*& a, Node*& b); // 10
- Node* ShuffleMerge(Node* a, Node* b); // 11
- void Shuffle(Node*& head); // 12
- Node* SortedMerge(Node* a, Node* b); // 13
- Node* SortedUnion(Node* a, Node* b); // 14
- Node* SortedIntersect(Node* a, Node* b); // 15
- Node* SortedDifference(Node* a, Node* b); // 16
- void Reverse(Node*& head); // 17
- void ReverseWithStack(Node*& head); // 18
- int main()
- {
- Node* l1 = nullptr;
- Node* l2 = nullptr;
- /*InsertNth(l1, 0, 1);
- InsertNth(l1, 1, 10);
- InsertNth(l1, 2, 25);
- InsertNth(l1, 3, 45);
- InsertNth(l1, 4, 55);
- InsertNth(l1, 5, 65);
- InsertNth(l1, 6, 75);
- InsertNth(l1, 7, 80);
- InsertNth(l2, 0, 10);
- InsertNth(l2, 1, 20);
- InsertNth(l2, 2, 30);
- InsertNth(l2, 3, 40);
- InsertNth(l2, 4, 50);
- InsertNth(l2, 5, 60);
- InsertNth(l2, 6, 70);
- InsertNth(l2, 7, 80);*/
- InsertNth(l1, 0, 1);
- InsertNth(l1, 1, 2);
- InsertNth(l1, 2, 4);
- InsertNth(l1, 3, 4);
- InsertNth(l1, 4, 5);
- InsertNth(l1, 5, 6);
- InsertNth(l1, 6, 7);
- InsertNth(l1, 7, 8);
- InsertNth(l2, 0, 2);
- InsertNth(l2, 1, 4);
- InsertNth(l2, 2, 7);
- InsertNth(l2, 3, 40);
- InsertNth(l2, 4, 50);
- InsertNth(l2, 5, 60);
- InsertNth(l2, 6, 70);
- InsertNth(l2, 7, 80);
- printList(l1);
- ReverseWithStack(l1);
- printList(l1);
- return 0;
- }
- int sizell(Node* head)
- {
- int cnt = 0;
- Node* current = head;
- while (current != nullptr)
- {
- cnt++;
- current = current->next;
- }
- return cnt;
- }
- bool contains(Node* head, int el)
- {
- Node* current = head;
- while(current != nullptr)
- {
- if (current->data == el)
- {
- return true;
- }
- current = current->next;
- }
- return false;
- }
- 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 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);
- }
- void MoveNode(Node*& a, Node*& b)
- {
- if (b == nullptr)
- {
- return;
- }
- Node* temp = b->next;
- b->next = a;
- a = b;
- b = temp;
- }
- void AlternatingSplit(Node* head, Node*& a, Node*& b)
- {
- a = nullptr;
- b = nullptr;
- if (head == nullptr)
- {
- return;
- }
- while (true)
- {
- if (head != nullptr)
- {
- MoveNode(a, head);
- }
- else
- {
- break;
- }
- if (head != nullptr)
- {
- MoveNode(b, head);
- }
- else
- {
- break;
- }
- }
- }
- void AlternatingSplit2(Node* head, Node*& a, Node*& b)
- {
- a = nullptr;
- b = nullptr;
- if (head == nullptr)
- {
- return;
- }
- int i = 0;
- Node* current = head;
- while (true)
- {
- if (current != nullptr)
- {
- InsertNth(a, i, current->data);
- }
- else
- {
- break;
- }
- current = current->next;
- if (current != nullptr)
- {
- InsertNth(b, i, current->data);
- }
- else
- {
- break;
- }
- current = current->next;
- i++;
- }
- }
- void PushBack(Node*& head, int element)
- {
- InsertNth(head, sizell(head), element);
- }
- Node* ShuffleMerge(Node* a, Node* b)
- {
- Node* result = nullptr;
- Node* currentA = a;
- Node* currentB = b;
- while (true)
- {
- if (currentA != nullptr)
- {
- PushBack(result, currentA->data);
- currentA = currentA->next;
- }
- if (currentB != nullptr)
- {
- PushBack(result, currentB->data);
- currentB = currentB->next;
- }
- if (currentA == nullptr && currentB == nullptr)
- {
- break;
- }
- }
- return result;
- }
- void Shuffle(Node*& head)
- {
- Node* a;
- Node* b;
- FrontBackSplit(head, a, b);
- head = ShuffleMerge(a, b);
- }
- Node* SortedUnion(Node* a, Node* b)
- {
- Node* unionl = SortedMerge(a, b);
- RemoveDublicates(unionl);
- return unionl;
- }
- Node* SortedIntersectSortedUnion(Node* a, Node* b)
- {
- Node* intersect = nullptr;
- Node* current = a;
- while (current != nullptr)
- {
- if (contains(b, current->data) && !contains(intersect, current->data))
- {
- PushBack(intersect, current->data);
- }
- current = current->next;
- }
- return intersect;
- }
- Node* SortedDifference(Node* a, Node* b)
- {
- Node* diff = nullptr;
- Node* current = a;
- while (current != nullptr)
- {
- if (!contains(b, current->data) && !contains(diff, current->data))
- {
- PushBack(diff, current->data);
- }
- current = current->next;
- }
- return diff;
- }
- 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 ReverseWithStack(Node*& head)
- {
- stack<int> st;
- Node* current = head;
- while (current != nullptr)
- {
- st.push(current->data);
- current = current->next;
- }
- current = head;
- while (current != nullptr)
- {
- current->data = st.top();
- st.pop();
- current = current->next;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement