Answered step by step
Verified Expert Solution
Question
1 Approved Answer
please i need an explanation for this code like a report // C++ program to sort link list // using insertion sort #include using namespace
please i need an explanation for this code like a report
// C++ program to sort link list // using insertion sort #includeusing namespace std; struct Node { int val; struct Node* next; Node(int x) { val = x; next = NULL; } }; class LinkedlistIS { public: Node* head; Node* sorted; void push(int val) { /* allocate node */ Node* newnode = new Node(val); /* link the old list off the new node */ newnode->next = head; /* move the head to point to the new node */ head = newnode; } // function to sort a singly linked list using insertion // sort void insertionSort(Node* headref) { // Initialize sorted linked list sorted = NULL; Node* current = headref; // Traverse the given linked list and insert every // node to sorted while (current != NULL) { // Store next for next iteration Node* next = current->next; // insert current in sorted linked list sortedInsert(current); // Update current current = next; } // Update head_ref to point to sorted linked list head = sorted; } /* * function to insert a new_node in a list. Note that * this function expects a pointer to head_ref as this * can modify the head of the input linked list * (similar to push()) */ void sortedInsert(Node* newnode) { /* Special case for the head end */ if (sorted == NULL || sorted->val >= newnode->val) { newnode->next = sorted; sorted = newnode; } else { Node* current = sorted; /* Locate the node before the point of insertion */ while (current->next != NULL && current->next->val < newnode->val) { current = current->next; } newnode->next = current->next; current->next = newnode; } } /* Function to print linked list */ void printlist(Node* head) { while (head != NULL) { cout << head->val << " "; head = head->next; } } }; // Driver program to test above functions int main() { LinkedlistIS list; list.head = NULL; list.push(5); list.push(20); list.push(4); list.push(3); list.push(30); cout << "Linked List before sorting" << endl; list.printlist(list.head); cout << endl; list.insertionSort(list.head); cout << "Linked List After sorting" << endl; list.printlist(list.head); }
2. Quick Sort
// C++ program for Quick Sort on Singly Linked List #include#include using namespace std; /* a node of the singly linked list */ struct Node { int data; struct Node* next; }; /* A utility function to insert a node at the beginning of * linked list */ void push(struct Node** head_ref, int new_data) { /* allocate node */ struct Node* new_node = new Node; /* put in the data */ new_node->data = new_data; /* link the old list off the new node */ new_node->next = (*head_ref); /* move the head to point to the new node */ (*head_ref) = new_node; } /* A utility function to print linked list */ void printList(struct Node* node) { while (node != NULL) { printf("%d ", node->data); node = node->next; } printf(" "); } // Returns the last node of the list struct Node* getTail(struct Node* cur) { while (cur != NULL && cur->next != NULL) cur = cur->next; return cur; } // Partitions the list taking the last element as the pivot struct Node* partition(struct Node* head, struct Node* end, struct Node** newHead, struct Node** newEnd) { struct Node* pivot = end; struct Node *prev = NULL, *cur = head, *tail = pivot; // During partition, both the head and end of the list // might change which is updated in the newHead and // newEnd variables while (cur != pivot) { if (cur->data < pivot->data) { // First node that has a value less than the // pivot - becomes the new head if ((*newHead) == NULL) (*newHead) = cur; prev = cur; cur = cur->next; } else // If cur node is greater than pivot { // Move cur node to next of tail, and change // tail if (prev) prev->next = cur->next; struct Node* tmp = cur->next; cur->next = NULL; tail->next = cur; tail = cur; cur = tmp; } } // If the pivot data is the smallest element in the // current list, pivot becomes the head if ((*newHead) == NULL) (*newHead) = pivot; // Update newEnd to the current last node (*newEnd) = tail; // Return the pivot node return pivot; } // here the sorting happens exclusive of the end node struct Node* quickSortRecur(struct Node* head, struct Node* end) { // base condition if (!head || head == end) return head; Node *newHead = NULL, *newEnd = NULL; // Partition the list, newHead and newEnd will be // updated by the partition function struct Node* pivot = partition(head, end, &newHead, &newEnd); // If pivot is the smallest element - no need to recur // for the left part. if (newHead != pivot) { // Set the node before the pivot node as NULL struct Node* tmp = newHead; while (tmp->next != pivot) tmp = tmp->next; tmp->next = NULL; // Recur for the list before pivot newHead = quickSortRecur(newHead, tmp); // Change next of last node of the left half to // pivot tmp = getTail(newHead); tmp->next = pivot; } // Recur for the list after the pivot element pivot->next = quickSortRecur(pivot->next, newEnd); return newHead; } // The main function for quick sort. This is a wrapper over // recursive function quickSortRecur() void quickSort(struct Node** headRef) { (*headRef) = quickSortRecur(*headRef, getTail(*headRef)); return; } // Driver code int main() { struct Node* a = NULL; push(&a, 5); push(&a, 20); push(&a, 4); push(&a, 3); push(&a, 30); cout << "Linked List before sorting "; printList(a); quickSort(&a); cout << "Linked List after sorting "; printList(a); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started