From 48fb9afce690e74a71299540b54dcd15e1505adc Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Wed, 26 May 2021 10:36:06 +0530 Subject: [PATCH 1/3] Create binarysearch.java --- binarysearch.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 binarysearch.java diff --git a/binarysearch.java b/binarysearch.java new file mode 100644 index 0000000..421f93b --- /dev/null +++ b/binarysearch.java @@ -0,0 +1,25 @@ +class BinarySearchExample{ + public static void binarySearch(int arr[], int first, int last, int key){ + int mid = (first + last)/2; + while( first <= last ){ + if ( arr[mid] < key ){ + first = mid + 1; + }else if ( arr[mid] == key ){ + System.out.println("Element is found at index: " + mid); + break; + }else{ + last = mid - 1; + } + mid = (first + last)/2; + } + if ( first > last ){ + System.out.println("Element is not found!"); + } + } + public static void main(String args[]){ + int arr[] = {10,20,30,40,50}; + int key = 30; + int last=arr.length-1; + binarySearch(arr,0,last,key); + } +} From f5ebd4fd8bd2b0a84dd0616cea712180dc733cf0 Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Wed, 26 May 2021 10:40:04 +0530 Subject: [PATCH 2/3] Create printlinkedlist.java --- printlinkedlist.java | 67 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 printlinkedlist.java diff --git a/printlinkedlist.java b/printlinkedlist.java new file mode 100644 index 0000000..d8be732 --- /dev/null +++ b/printlinkedlist.java @@ -0,0 +1,67 @@ +public class SinglyLinkedList { + //Represent a node of the singly linked list + class Node{ + int data; + Node next; + + public Node(int data) { + this.data = data; + this.next = null; + } + } + + //Represent the head and tail of the singly linked list + public Node head = null; + public Node tail = null; + + //addNode() will add a new node to the list + public void addNode(int data) { + //Create a new node + Node newNode = new Node(data); + + //Checks if the list is empty + if(head == null) { + //If list is empty, both head and tail will point to new node + head = newNode; + tail = newNode; + } + else { + //newNode will be added after tail such that tail's next will point to newNode + tail.next = newNode; + //newNode will become new tail of the list + tail = newNode; + } + } + + //display() will display all the nodes present in the list + public void display() { + //Node current will point to head + Node current = head; + + if(head == null) { + System.out.println("List is empty"); + return; + } + System.out.println("Nodes of singly linked list: "); + while(current != null) { + //Prints each node by incrementing pointer + System.out.print(current.data + " "); + current = current.next; + } + System.out.println(); + } + + public static void main(String[] args) { + + SinglyLinkedList sList = new SinglyLinkedList(); + + //Add nodes to the list + sList.addNode(1); + sList.addNode(2); + sList.addNode(3); + sList.addNode(4); + + //Displays the nodes present in the list + sList.display(); + } +} From d3d06c696a4e1d7a44bc1ef75c139b5de68020ca Mon Sep 17 00:00:00 2001 From: Ayush Parikh Date: Sun, 30 May 2021 09:34:06 +0530 Subject: [PATCH 3/3] Delete binarysearch.java --- binarysearch.java | 25 ------------------------- 1 file changed, 25 deletions(-) delete mode 100644 binarysearch.java diff --git a/binarysearch.java b/binarysearch.java deleted file mode 100644 index 421f93b..0000000 --- a/binarysearch.java +++ /dev/null @@ -1,25 +0,0 @@ -class BinarySearchExample{ - public static void binarySearch(int arr[], int first, int last, int key){ - int mid = (first + last)/2; - while( first <= last ){ - if ( arr[mid] < key ){ - first = mid + 1; - }else if ( arr[mid] == key ){ - System.out.println("Element is found at index: " + mid); - break; - }else{ - last = mid - 1; - } - mid = (first + last)/2; - } - if ( first > last ){ - System.out.println("Element is not found!"); - } - } - public static void main(String args[]){ - int arr[] = {10,20,30,40,50}; - int key = 30; - int last=arr.length-1; - binarySearch(arr,0,last,key); - } -}