From 0174dad7057382b66bbc550e8a32e4136662aab6 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Thu, 28 Nov 2019 13:52:24 +0100 Subject: [PATCH] rename Operations on Datastructures -> operations_on_datastructures (#655) --- .../Array Left Rotation.cpp | 0 .../Array Right Rotation.cpp | 0 .../Circular Linked List.cpp | 0 .../Circular Queue Using Array.cpp | 0 .../Intersection_of_2_arrays.cpp | 0 .../Reverse a Linked List using Recusion.cpp | 0 .../Union_of_2_arrays.cpp | 0 .../selectionSortLinkedList.cpp | 316 +++++++++--------- 8 files changed, 158 insertions(+), 158 deletions(-) rename {Operations on Datastructures => operations_on_datastructures}/Array Left Rotation.cpp (100%) rename {Operations on Datastructures => operations_on_datastructures}/Array Right Rotation.cpp (100%) rename {Operations on Datastructures => operations_on_datastructures}/Circular Linked List.cpp (100%) rename {Operations on Datastructures => operations_on_datastructures}/Circular Queue Using Array.cpp (100%) rename {Operations on Datastructures => operations_on_datastructures}/Intersection_of_2_arrays.cpp (100%) rename {Operations on Datastructures => operations_on_datastructures}/Reverse a Linked List using Recusion.cpp (100%) rename {Operations on Datastructures => operations_on_datastructures}/Union_of_2_arrays.cpp (100%) rename {Operations on Datastructures => operations_on_datastructures}/selectionSortLinkedList.cpp (97%) diff --git a/Operations on Datastructures/Array Left Rotation.cpp b/operations_on_datastructures/Array Left Rotation.cpp similarity index 100% rename from Operations on Datastructures/Array Left Rotation.cpp rename to operations_on_datastructures/Array Left Rotation.cpp diff --git a/Operations on Datastructures/Array Right Rotation.cpp b/operations_on_datastructures/Array Right Rotation.cpp similarity index 100% rename from Operations on Datastructures/Array Right Rotation.cpp rename to operations_on_datastructures/Array Right Rotation.cpp diff --git a/Operations on Datastructures/Circular Linked List.cpp b/operations_on_datastructures/Circular Linked List.cpp similarity index 100% rename from Operations on Datastructures/Circular Linked List.cpp rename to operations_on_datastructures/Circular Linked List.cpp diff --git a/Operations on Datastructures/Circular Queue Using Array.cpp b/operations_on_datastructures/Circular Queue Using Array.cpp similarity index 100% rename from Operations on Datastructures/Circular Queue Using Array.cpp rename to operations_on_datastructures/Circular Queue Using Array.cpp diff --git a/Operations on Datastructures/Intersection_of_2_arrays.cpp b/operations_on_datastructures/Intersection_of_2_arrays.cpp similarity index 100% rename from Operations on Datastructures/Intersection_of_2_arrays.cpp rename to operations_on_datastructures/Intersection_of_2_arrays.cpp diff --git a/Operations on Datastructures/Reverse a Linked List using Recusion.cpp b/operations_on_datastructures/Reverse a Linked List using Recusion.cpp similarity index 100% rename from Operations on Datastructures/Reverse a Linked List using Recusion.cpp rename to operations_on_datastructures/Reverse a Linked List using Recusion.cpp diff --git a/Operations on Datastructures/Union_of_2_arrays.cpp b/operations_on_datastructures/Union_of_2_arrays.cpp similarity index 100% rename from Operations on Datastructures/Union_of_2_arrays.cpp rename to operations_on_datastructures/Union_of_2_arrays.cpp diff --git a/Operations on Datastructures/selectionSortLinkedList.cpp b/operations_on_datastructures/selectionSortLinkedList.cpp similarity index 97% rename from Operations on Datastructures/selectionSortLinkedList.cpp rename to operations_on_datastructures/selectionSortLinkedList.cpp index e5b840234..52363ceff 100644 --- a/Operations on Datastructures/selectionSortLinkedList.cpp +++ b/operations_on_datastructures/selectionSortLinkedList.cpp @@ -1,159 +1,159 @@ -#include -using namespace std; - -//node defined -class node -{ -public: - int data; - node *link; - node(int d) - { - data = d; - link = NULL; - } -}; - -//printing the linked list -void print(node *head) -{ - node *current = head; - while (current != NULL) - { - cout << current->data << " "; - current = current->link; - } - cout << endl; -} - -//creating the linked list with 'n' nodes -node *createlist(int n) -{ - node *head = NULL; - node *t = NULL; - for (int i = 0; i < n; i++) - { - node *temp = NULL; - int num; - cin >> num; - temp = new node(num); - if (head == NULL) - { - head = temp; - t = temp; - continue; - } - if (t->link == NULL) - t->link = temp; - t = temp; - } - return head; -} - -//performing selection sort on the linked list in an iterative manner -void my_selection_sort_linked_list(node *&head) -{ - node *min = head; //throughout the algorithm 'min' is used to denote the node with min value out of all the nodes left for scanning - //while scanning if we find a node 'X' with value lesser than min, - //then we update the pointers in such a way that 'X' becomes the predecessor of 'min' - node *current = min->link; // 'current' refers to the current node we are scanning - node *previous = min; //'previous' refers to the node that is previous to the current node - node *temp = NULL; // 'temp' in this algo is used to point to the last node of the sorted part of the linked list. - //eg. If at any time instance the state of the linked list is suppose 1->2->5->3->8->NULL - //then, we see that "1->2" is the sorted part of the LL, and therefore temp will be pointing to the last node of the sorted part,i.e,'2' - //We keep on arranging the Linked list in such a way that after each iteration the node with 'min' value is placed at its correct position. - //Eg. Let suppose initially we have 5->4->1->3->2->NULL - //After 1st iteration : 1->4->5->3->2->NULL and so on - - while (min->link != NULL) //so that all the nodes are scanned or until there exists a node - { - //pick the first node from the unsorted part and assume that it is the minimum and then start scanning from the next node - - while (current != NULL) //suppose you choose the min node to be X, then scan starts from the (X+1)th node until its NULL. current = (X+1)th node and min = X - { - if (current->data < min->data) //if the current node is smaller than the presumed node 'min' - { - if (temp == NULL) //temp stays null for the first iteration, therefore it symbolizes that we are scanning for the first time - { - if (previous == min) //if the 'previous' is pointing to the 'min' node - { - //Update the pointers - head = current; //update the head pointer with the current node - min->link = current->link; - current->link = previous; - min = current; - current = previous->link; - } - else //if the 'previous' is not pointing to the 'min' node - { - //Update the pointers - head = current; //update the head pointer with the current node - previous->link = current->link; - current->link = min; - min = current; - current = previous->link; - } - } - else //if 'temp' is not NULL, i.e., its not the 1st iteration - { - temp->link = current; - previous->link = current->link; - current->link = min; - min = current; - current = previous->link; - } - } - else //if the current node is greater than min, just move the previous and the current pointer a step further - { - previous = previous->link; - current = current->link; - } - } - - //update the pointers. Set 'temp' to the last node in the sorted part. Make 'min' move a step further so that 'min' points to the 1st node of the unsorted part - //start the iteration again - temp = min; - min = min->link; - previous = min; - current = min->link; - } -} - -// Test cases: - -// enter the no. of nodes : 5 -// 8 9 3 1 4 -// original list is : 8 9 3 1 4 -// sorted list is : 1 3 4 8 9 - -// enter the no. of nodes : 3 -// -1 -2 -3 -// original list is : -1 -2 -3 -// sorted list is : -3 -2 -1 - -// enter the no. of nodes : 8 -// 8 7 6 5 4 3 2 1 -// original list is : 8 7 6 5 4 3 2 1 -// sorted list is : 1 2 3 4 5 6 7 8 - -// enter the no. of nodes : 6 -// 5 3 4 1 -2 -4 -// original list is : 5 3 4 1 -2 -4 -// sorted list is : -4 -2 1 3 4 5 - -int main() -{ - node *head = NULL; - int n; - cout << "enter the no. of nodes : "; //taking input from user about the number of nodes in linked list - cin >> n; - if (n == 0) - return 0; - head = createlist(n); //creating the list - cout << "original list is : "; - print(head); //printing the original linked list - my_selection_sort_linked_list(head); //applying selection sort - cout << "sorted list is : "; - print(head); //printing the sorted linked list - return 0; +#include +using namespace std; + +//node defined +class node +{ +public: + int data; + node *link; + node(int d) + { + data = d; + link = NULL; + } +}; + +//printing the linked list +void print(node *head) +{ + node *current = head; + while (current != NULL) + { + cout << current->data << " "; + current = current->link; + } + cout << endl; +} + +//creating the linked list with 'n' nodes +node *createlist(int n) +{ + node *head = NULL; + node *t = NULL; + for (int i = 0; i < n; i++) + { + node *temp = NULL; + int num; + cin >> num; + temp = new node(num); + if (head == NULL) + { + head = temp; + t = temp; + continue; + } + if (t->link == NULL) + t->link = temp; + t = temp; + } + return head; +} + +//performing selection sort on the linked list in an iterative manner +void my_selection_sort_linked_list(node *&head) +{ + node *min = head; //throughout the algorithm 'min' is used to denote the node with min value out of all the nodes left for scanning + //while scanning if we find a node 'X' with value lesser than min, + //then we update the pointers in such a way that 'X' becomes the predecessor of 'min' + node *current = min->link; // 'current' refers to the current node we are scanning + node *previous = min; //'previous' refers to the node that is previous to the current node + node *temp = NULL; // 'temp' in this algo is used to point to the last node of the sorted part of the linked list. + //eg. If at any time instance the state of the linked list is suppose 1->2->5->3->8->NULL + //then, we see that "1->2" is the sorted part of the LL, and therefore temp will be pointing to the last node of the sorted part,i.e,'2' + //We keep on arranging the Linked list in such a way that after each iteration the node with 'min' value is placed at its correct position. + //Eg. Let suppose initially we have 5->4->1->3->2->NULL + //After 1st iteration : 1->4->5->3->2->NULL and so on + + while (min->link != NULL) //so that all the nodes are scanned or until there exists a node + { + //pick the first node from the unsorted part and assume that it is the minimum and then start scanning from the next node + + while (current != NULL) //suppose you choose the min node to be X, then scan starts from the (X+1)th node until its NULL. current = (X+1)th node and min = X + { + if (current->data < min->data) //if the current node is smaller than the presumed node 'min' + { + if (temp == NULL) //temp stays null for the first iteration, therefore it symbolizes that we are scanning for the first time + { + if (previous == min) //if the 'previous' is pointing to the 'min' node + { + //Update the pointers + head = current; //update the head pointer with the current node + min->link = current->link; + current->link = previous; + min = current; + current = previous->link; + } + else //if the 'previous' is not pointing to the 'min' node + { + //Update the pointers + head = current; //update the head pointer with the current node + previous->link = current->link; + current->link = min; + min = current; + current = previous->link; + } + } + else //if 'temp' is not NULL, i.e., its not the 1st iteration + { + temp->link = current; + previous->link = current->link; + current->link = min; + min = current; + current = previous->link; + } + } + else //if the current node is greater than min, just move the previous and the current pointer a step further + { + previous = previous->link; + current = current->link; + } + } + + //update the pointers. Set 'temp' to the last node in the sorted part. Make 'min' move a step further so that 'min' points to the 1st node of the unsorted part + //start the iteration again + temp = min; + min = min->link; + previous = min; + current = min->link; + } +} + +// Test cases: + +// enter the no. of nodes : 5 +// 8 9 3 1 4 +// original list is : 8 9 3 1 4 +// sorted list is : 1 3 4 8 9 + +// enter the no. of nodes : 3 +// -1 -2 -3 +// original list is : -1 -2 -3 +// sorted list is : -3 -2 -1 + +// enter the no. of nodes : 8 +// 8 7 6 5 4 3 2 1 +// original list is : 8 7 6 5 4 3 2 1 +// sorted list is : 1 2 3 4 5 6 7 8 + +// enter the no. of nodes : 6 +// 5 3 4 1 -2 -4 +// original list is : 5 3 4 1 -2 -4 +// sorted list is : -4 -2 1 3 4 5 + +int main() +{ + node *head = NULL; + int n; + cout << "enter the no. of nodes : "; //taking input from user about the number of nodes in linked list + cin >> n; + if (n == 0) + return 0; + head = createlist(n); //creating the list + cout << "original list is : "; + print(head); //printing the original linked list + my_selection_sort_linked_list(head); //applying selection sort + cout << "sorted list is : "; + print(head); //printing the sorted linked list + return 0; } \ No newline at end of file