From 446cfdc1d27b1cd0c088e01377d9647ceed19947 Mon Sep 17 00:00:00 2001 From: kuroyukihime <78816968+KhushiChaudhary744@users.noreply.github.com> Date: Fri, 1 Oct 2021 02:10:31 +0530 Subject: [PATCH 1/2] Add files via upload --- .../Lists/detect_and_create_loop.jav | 115 ++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 DataStructures/Lists/detect_and_create_loop.jav diff --git a/DataStructures/Lists/detect_and_create_loop.jav b/DataStructures/Lists/detect_and_create_loop.jav new file mode 100644 index 000000000000..ba805e183867 --- /dev/null +++ b/DataStructures/Lists/detect_and_create_loop.jav @@ -0,0 +1,115 @@ + +import java.util.*; +import java.util.Scanner; +public class LinkedList { + + static Node head; // head of list + + static class Node // Linked list Node + { + int data; //to store value + Node next; //pointer + Node(int d) + { + data = d; + next = null; + } + } + + static int countNodes(Node ptr) //Function to count the number of nodes present + { + int count = 0; + while (ptr != null) + { + ptr = ptr.next; + count++; + } + return count; + } + + static public void push(int new_data) // Function to inserts a new Node at front of the list + { + + Node new_node = new Node(new_data); //Allocate a pointer/node and store the data + + new_node.next = head; // make next of new Node as head + + head = new_node; // Move the head to point to new Node. + } + + static void printList( Node head, int total_nodes) //function to traverse through the list and print all data values + { + Node curr = head; + int count = 0; + while (count < total_nodes) + { + count++; + System.out.print(curr.data + " "); + curr = curr.next; + } + } + + static Node makeloop( Node head_ref, int k) + { + Node temp = head_ref; + int count = 1; + while (count < k) //traverrse the list till point is found + { + temp = temp.next; + count++; + } + + Node connected_point = temp; + + while (temp.next != null) // traverse remaining nodes + temp = temp.next; + + temp.next = connected_point; //connect last node to k-th element + return head_ref; + } + + static boolean detectLoop(Node h) //Function to detect loop, retuens true if loop is in linked list else returns false. + { + HashSet traverse = new HashSet(); + while (n != null) { + + if (traverse.contains(n)) //if the hash a;ready contains a record of the node encountered true is returned as a loop isdetected + return true; + + traverse.add(n); + n = n.next; + } + return false; + } + + public static void main(String[] args) + { + LinkedList l = new LinkedList(); + + Scanner sc = new Scanner(System.in); + + print("Enter elements in the list, to stop entering press any alphabetical key"); + while(true) + { + try{ + i = sc.nextInt(); + l.push(i); + } + catch (Exception e) { + System.out.println("Creating loop for run"); + } + } + System.out.println("Enter the location to generate loop"); + int k = sc.nextInt() + System.out.print("Given list"); + printList(head, total_nodes); + head = makeloop(head, k); + System.out.print( "Modified list with loop"); + printList(head, total_nodes); + + if (detectLoop(head)) + System.out.println("Loop found"); + else + System.out.println("No Loop"); + } +} From 654b08c90fc988e091b49c61f2e8cb9a79eed82e Mon Sep 17 00:00:00 2001 From: kuroyukihime <78816968+KhushiChaudhary744@users.noreply.github.com> Date: Fri, 1 Oct 2021 10:52:36 +0530 Subject: [PATCH 2/2] Update detect_and_create_loop.jav --- .../Lists/detect_and_create_loop.jav | 200 +++++++++--------- 1 file changed, 96 insertions(+), 104 deletions(-) diff --git a/DataStructures/Lists/detect_and_create_loop.jav b/DataStructures/Lists/detect_and_create_loop.jav index ba805e183867..a4f391457d13 100644 --- a/DataStructures/Lists/detect_and_create_loop.jav +++ b/DataStructures/Lists/detect_and_create_loop.jav @@ -1,115 +1,107 @@ - import java.util.*; import java.util.Scanner; public class LinkedList { - static Node head; // head of list - - static class Node // Linked list Node - { - int data; //to store value - Node next; //pointer - Node(int d) - { - data = d; - next = null; - } - } - - static int countNodes(Node ptr) //Function to count the number of nodes present - { - int count = 0; - while (ptr != null) - { - ptr = ptr.next; - count++; - } - return count; - } - - static public void push(int new_data) // Function to inserts a new Node at front of the list - { - - Node new_node = new Node(new_data); //Allocate a pointer/node and store the data - - new_node.next = head; // make next of new Node as head - - head = new_node; // Move the head to point to new Node. - } - - static void printList( Node head, int total_nodes) //function to traverse through the list and print all data values + static Node head; // head of list + + static class Node // Linked list Node + { + int data; //to store value + Node next; //pointer + Node(int d) { + data = d; + next = null; + } + } + + static int countNodes(Node ptr) //Function to count the number of nodes present + { + int count = 0; + while (ptr != null) { + ptr = ptr.next; + count++; + } + return count; + } + + static public void push(int new_data) // Function to inserts a new Node at front of the list + { + + Node new_node = new Node(new_data); //Allocate a pointer/node and store the data + + new_node.next = head; // make next of new Node as head + + head = new_node; // Move the head to point to new Node. + } + + static void printList(Node head, int total_nodes) //function to traverse through the list and print all data values { - Node curr = head; - int count = 0; - while (count < total_nodes) - { - count++; - System.out.print(curr.data + " "); - curr = curr.next; - } + Node curr = head; + int count = 0; + while (count < total_nodes) { + count++; + System.out.print(curr.data + " "); + curr = curr.next; + } } - static Node makeloop( Node head_ref, int k) - { - Node temp = head_ref; - int count = 1; - while (count < k) //traverrse the list till point is found - { - temp = temp.next; - count++; - } - - Node connected_point = temp; - - while (temp.next != null) // traverse remaining nodes - temp = temp.next; - - temp.next = connected_point; //connect last node to k-th element - return head_ref; - } - - static boolean detectLoop(Node h) //Function to detect loop, retuens true if loop is in linked list else returns false. - { - HashSet traverse = new HashSet(); - while (n != null) { - - if (traverse.contains(n)) //if the hash a;ready contains a record of the node encountered true is returned as a loop isdetected - return true; - - traverse.add(n); - n = n.next; - } - return false; - } - - public static void main(String[] args) - { - LinkedList l = new LinkedList(); + static Node makeloop(Node head_ref, int k) { + Node temp = head_ref; + int count = 1; + while (count < k) //traverrse the list till point is found + { + temp = temp.next; + count++; + } + + Node connected_point = temp; + + while (temp.next != null) // traverse remaining nodes + temp = temp.next; + + temp.next = connected_point; //connect last node to k-th element + return head_ref; + } + + static boolean detectLoop(Node h) //Function to detect loop, retuens true if loop is in linked list else returns false. + { + HashSet < Node > traverse = new HashSet < Node > (); + while (n != null) { + + if (traverse.contains(n)) //if the hash a;ready contains a record of the node encountered true is returned as a loop isdetected + return true; + + traverse.add(n); + n = n.next; + } + return false; + } + + public static void main(String[] args) { + LinkedList l = new LinkedList(); Scanner sc = new Scanner(System.in); - - print("Enter elements in the list, to stop entering press any alphabetical key"); - while(true) - { - try{ - i = sc.nextInt(); - l.push(i); - } - catch (Exception e) { - System.out.println("Creating loop for run"); - } - } + + print("Enter elements in the list, to stop entering press any alphabetical key"); + while (true) { + try { + i = sc.nextInt(); + l.push(i); + } catch (Exception e) { + System.out.println("Creating loop for run"); + } + } System.out.println("Enter the location to generate loop"); - int k = sc.nextInt() - System.out.print("Given list"); - printList(head, total_nodes); - head = makeloop(head, k); - System.out.print( "Modified list with loop"); - printList(head, total_nodes); - - if (detectLoop(head)) - System.out.println("Loop found"); - else - System.out.println("No Loop"); - } + int k = sc.nextInt() + System.out.print("Given list"); + printList(head, total_nodes); + head = makeloop(head, k); + System.out.print("Modified list with loop"); + printList(head, total_nodes); + + if (detectLoop(head)) + System.out.println("Loop found"); + else + System.out.println("No Loop"); + } }