From 9411d5be56f444e750e19a052cff25f8e22365e0 Mon Sep 17 00:00:00 2001 From: zacharyjones123 Date: Tue, 18 Apr 2017 07:57:17 -0700 Subject: [PATCH] In this commit I have: Added JavaDoc to every package except for "heaps" --- .classpath | 7 + .gitignore | 1 + .project | 17 ++ BinarySearch.java | 74 ++++++ BinaryToDecimal.java | 32 +++ BubbleSort.java | 50 ++++ DecimalToBinary.java | 31 +++ DecimalToOctal.java | 31 +++ Factorial.java | 19 +- FindingPrimes.java | 21 +- HeapSort.java | 359 +++++++++++++------------- InsertionSort.java | 49 ++++ InsertionSortInteger.java | 55 ++++ LinearSearch.java | 19 +- MergeSort.java | 216 ++++++++-------- Quicksort.java | 33 ++- ReverseString.java | 47 ++++ SelectionSort.java | 52 ++++ bfs.java | 22 +- countwords.java | 13 + data_structures/BinaryTree.java | 124 +++++++-- data_structures/DoublyLinkedList.java | 111 ++++++-- data_structures/PriorityQueues.java | 75 +++++- data_structures/Queues.java | 82 +++++- data_structures/SinglyLinkedList.java | 118 ++++++--- data_structures/Stacks.java | 135 ++++++++-- data_structures/heaps/MaxHeap.java | 8 +- data_structures/heaps/MinHeap.java | 8 +- dfs.java | 22 +- 29 files changed, 1406 insertions(+), 425 deletions(-) create mode 100644 .classpath create mode 100644 .gitignore create mode 100644 .project create mode 100644 BinarySearch.java create mode 100644 BinaryToDecimal.java create mode 100644 BubbleSort.java create mode 100644 DecimalToBinary.java create mode 100644 DecimalToOctal.java create mode 100644 InsertionSort.java create mode 100644 InsertionSortInteger.java create mode 100644 ReverseString.java create mode 100644 SelectionSort.java diff --git a/.classpath b/.classpath new file mode 100644 index 00000000..67d5f6d3 --- /dev/null +++ b/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..ae3c1726 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/.project b/.project new file mode 100644 index 00000000..ea54703d --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + Java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/BinarySearch.java b/BinarySearch.java new file mode 100644 index 00000000..25814197 --- /dev/null +++ b/BinarySearch.java @@ -0,0 +1,74 @@ +import java.util.Scanner; + +/** + * Implements a Binary Search in Java + * + * @author unknown + */ +class BinarySearch +{ + /** + * This method implements the Binary Search + * + * @param array The array to make the binary search + * @param key The number you are looking for + * @param lb The lower bound + * @param up The upper bound + * @return the location of the key + **/ + public static int BS(int array[], int key, int lb, int ub) + { if (lb>ub) + { + return -1; + } + + int mid=(lb+ub)/2; + + if (keyarray[mid]) + { + return (BS(array, key, mid+1, ub)); + } + else + { + return mid; + } + } + + + /** + * This is the main method of Binary Search + * + * @author Unknown + * @param args Command line parameters + */ + + public static void main(String[] args) + { + Scanner input=new Scanner(System.in); + int array[]=new int[10] ; + int key; + + //Input + System.out.println("Enter an array of 10 numbers : "); + for (int i=0; i<10 ;i++ ) + { + array[i]=input.nextInt(); + } + System.out.println("Enter the number to be searched : "); + key=input.nextInt(); + + int index=BS(array, key, 0, 9); + if (index!=-1) + { + System.out.println("Number found at index number : " + index); + } + else + { + System.out.println("Not found"); + } + } +} diff --git a/BinaryToDecimal.java b/BinaryToDecimal.java new file mode 100644 index 00000000..f7357d3c --- /dev/null +++ b/BinaryToDecimal.java @@ -0,0 +1,32 @@ +import java.util.Scanner; + +/** + * This class converts a Binary number to a Decimal number + * + * @author Unknown + * + */ +class BinaryToDecimal +{ + + /** + * Main Method + * + * @param args Command line arguments + */ + public static void main(String args[]) + { + Scanner sc=new Scanner(System.in); + int n,k,d,s=0,c=0; + n=sc.nextInt(); + k=n; + while(k!=0) + { + d=k%10; + s+=d*(int)Math.pow(2,c++); + k/=10; + } + System.out.println("Binary number:"+n); + System.out.println("Decimal equivalent:"+s); + } +} diff --git a/BubbleSort.java b/BubbleSort.java new file mode 100644 index 00000000..ac4015f1 --- /dev/null +++ b/BubbleSort.java @@ -0,0 +1,50 @@ +import java.util.Scanner; + +/** + * This class implements BubbleSort + * + * @author Unknown + * + */ + +class BubbleSort +{ + /** + * Main Method + * + * @param args Command line arguments + */ + public static void main(String[] args) + { + int array[]=new int[6]; + Scanner input=new Scanner(System.in); + + //Input + System.out.println("Enter any 6 Numbers for Unsorted Array : "); + for(int i=0; i<6; i++) + { + array[i]=input.nextInt(); + } + + //Sorting + for(int i=0; i<5; i++) + { + for(int j=i+1; j<6; j++) + { + if(array[j]>array[i]) + { + int temp=array[j]; + array[j]=array[i]; + array[i]=temp; + } + } + } + + //Output + for(int i=0; i<6; i++) + { + System.out.print(array[i]+"\t"); + } + + } +} diff --git a/DecimalToBinary.java b/DecimalToBinary.java new file mode 100644 index 00000000..e88c7d97 --- /dev/null +++ b/DecimalToBinary.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +/** + * This class converts a Decimal number to a Binary number + * + * @author Unknown + * + */ +class DecimalToBinary +{ + /** + * Main Method + * + * @param args Command Line Arguments + */ + public static void main(String args[]) + { + Scanner sc=new Scanner(System.in); + int n,k,s=0,c=0,d; + n=sc.nextInt(); + k=n; + while(k!=0) + { + d=k%2; + s=s+d*(int)Math.pow(10,c++); + k/=2; + }//converting decimal to binary + System.out.println("Decimal number:"+n); + System.out.println("Binary equivalent:"+s); + } +} diff --git a/DecimalToOctal.java b/DecimalToOctal.java new file mode 100644 index 00000000..f0590316 --- /dev/null +++ b/DecimalToOctal.java @@ -0,0 +1,31 @@ +import java.util.Scanner; + +/** + * This class converts Decimal numbers to Octal Numbers + * + * @author Unknown + * + */ +class Decimal_Octal +{ + /** + * Main Method + * + * @param args Command line Arguments + */ + public static void main(String[] args) + { + Scanner sc=new Scanner(System.in); + int n,k,d,s=0,c=0; + n=sc.nextInt(); + k=n; + while(k!=0) + { + d=k%8; + s+=d*(int)Math.pow(10,c++); + k/=8; + } + System.out.println("Decimal number:"+n); + System.out.println("Octal equivalent:"+s); + } +} diff --git a/Factorial.java b/Factorial.java index f675ba6a..134ec2f5 100644 --- a/Factorial.java +++ b/Factorial.java @@ -1,7 +1,19 @@ import java.util.Scanner; +/** + * This program will print out the factorial of any non-negative + * number that you input into it. + * + * @author Unknown + * + */ public class Factorial{ + /** + * The main method + * + * @param args Command line arguments + */ public static void main(String[] args){ Scanner input = new Scanner(System.in); //Prompt user to enter integer @@ -20,7 +32,12 @@ public class Factorial{ } } - //Factorial method + /** + * Recursive Factorial Method + * + * @param n The number to factorial + * @return The factorial of the number + */ public static long factorial(int n){ if (n==0){ diff --git a/FindingPrimes.java b/FindingPrimes.java index ecf3f7b4..f7cc14d7 100644 --- a/FindingPrimes.java +++ b/FindingPrimes.java @@ -1,12 +1,27 @@ -/* - * The Sieve of Eratosthenes is an algorithm used to find prime numbers, up to a given value. +/** + * The Sieve of Eratosthenes is an algorithm use to find prime numbers, + * up to a given value. * Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif -*/ + * (This illustration is also in the github repository) + * + * @author Unknown + * + */ public class FindingPrimes{ + /** + * The Main method + * + * @param args Command line arguments + */ public static void main(String args[]){ SOE(20); //Example: Finds all the primes up to 20 } + /** + * The method implementing the Sieve of Eratosthenes + * + * @param n Number to perform SOE on + */ public static void SOE(int n){ boolean sieve[] = new boolean[n]; diff --git a/HeapSort.java b/HeapSort.java index 7dd420a1..6b3a3fdc 100644 --- a/HeapSort.java +++ b/HeapSort.java @@ -1,185 +1,174 @@ -import java.util.Scanner; - -/** - * Heap Sort Algorithm. Implements MinHeap - * - */ -public class HeapSort { - /** - * array to store heap. - */ - private int[] heap; - /** - * size of heap. - */ - private int size; - - /** - * Constructor. - * - * @param heap - * array of unordered integers - */ - public HeapSort(int[] heap) { - this.setHeap(heap); - this.setSize(heap.length); - } - - /** - * Sets this.size with {@code length). - * - * @param length - * integer length of heap - */ - private void setSize(int length) { - this.size = length; - } - - /** - * Sets Heap with {@code heap}. - * - * @param heap - * array of unordered elements - */ - private void setHeap(int[] heap) { - this.heap = heap; - } - - /** - * Swaps index of {@code first} with {@code second}. - * - * @param first - * index to swap {@code second} with - * @param second - * index to swap {@code first} with - */ - private void swap(int first, int second) { - int temp = this.heap[first]; - this.heap[first] = this.heap[second]; - this.heap[second] = temp; - } - - /** - * Heapifies subtree from {@code top} as root to {@code last} as last child. - * - * @param rootIndex - * index of root - * @param lastChild - * index of last child - */ - private void heapSubtree(int rootIndex, int lastChild) { - int leftIndex = rootIndex * 2 + 1; - int rightIndex = rootIndex * 2 + 2; - int root = this.heap[rootIndex]; - if (rightIndex <= lastChild) { // if has right and left children - int left = this.heap[leftIndex]; - int right = this.heap[rightIndex]; - if (left < right && left < root) { - this.swap(leftIndex, rootIndex); - this.heapSubtree(leftIndex, lastChild); - } else if (right < root) { - this.swap(rightIndex, rootIndex); - this.heapSubtree(rightIndex, lastChild); - } - } else if (leftIndex <= lastChild) { // if no right child, but has left child - int left = this.heap[leftIndex]; - if (left < root) { - this.swap(leftIndex, rootIndex); - this.heapSubtree(leftIndex, lastChild); - } - } - } - - /** - * Makes heap with {@code root} as root. - * - * @param root - * index of root of heap - */ - private void makeMinHeap(int root) { - int leftIndex = root * 2 + 1; - int rightIndex = root * 2 + 2; - boolean hasLeftChild = leftIndex < this.heap.length; - boolean hasRightChild = rightIndex < this.heap.length; - if (hasRightChild) { //if has left and right - this.makeMinHeap(leftIndex); - this.makeMinHeap(rightIndex); - this.heapSubtree(root, this.heap.length - 1); - } else if (hasLeftChild) { - this.heapSubtree(root, this.heap.length - 1); - } - } - - /** - * Gets the root of this.heap. - * - * @return root of this.heap - */ - private int getRoot() { - this.swap(0, this.size - 1); - this.size--; - this.heapSubtree(0, this.size - 1); - return this.heap[this.size]; // return old root - } - - /** - * Sorts this.heap with heap sort; displays ordered elements to console. - * - * @return {@code sorted} array of sorted elements - */ - public final int[] sort() { - this.makeMinHeap(0); // make min heap using index 0 as root. - int[] sorted = new int[this.size]; - int index = 0; - while (this.size > 0) { - int min = this.getRoot(); - sorted[index] = min; - index++; - } - return sorted; - } - - /** - * Gets input to sort. - * - * @return unsorted array of integers to sort - */ - public static int[] getInput() { - final int numElements = 6; - int[] unsorted = new int[numElements]; - Scanner input = new Scanner(System.in); - System.out.println("Enter any 6 Numbers for Unsorted Array : "); - for (int i = 0; i < numElements; i++) { - unsorted[i] = input.nextInt(); - } - input.close(); - return unsorted; - } - - /** - * Prints elements in heap. - * - * @param heap - * array representing heap - */ - public static void printData(int[] heap) { - System.out.println("Sorted Elements:"); - for (int i = 0; i < heap.length; i++) { - System.out.print(" " + heap[i] + " "); - } - } - - /** - * Main method. - * - * @param args - * the command line arguments - */ - public static void main(String[] args) { - int[] heap = getInput(); - HeapSort data = new HeapSort(heap); - int[] sorted = data.sort(); - printData(sorted); - } - -} +import java.util.Scanner; + +/** + * Heap Sort Algorithm + * Implements MinHeap + * + * @author Unknown + * + */ +public class HeapSort { + /** Array to store heap */ + private int[] heap; + /** The size of the heap */ + private int size; + + /** + * Constructor + * + * @param heap array of unordered integers + */ + public HeapSort(int[] heap) { + this.setHeap(heap); + this.setSize(heap.length); + } + + /** + * Setter for variable size + * + * @param length new size + */ + private void setSize(int length) { + this.size = length; + } + + /** + * Setter for variable heap + * + * @param heap array of unordered elements + */ + private void setHeap(int[] heap) { + this.heap = heap; + } + + /** + * Swaps index of first with second + * + * @param first First index to switch + * @param second Second index to switch + */ + private void swap(int first, int second) { + int temp = this.heap[first]; + this.heap[first] = this.heap[second]; + this.heap[second] = temp; + } + + /** + * Heapifies subtree from top as root to last as last child + * + * @param rootIndex index of root + * @param lastChild index of last child + */ + private void heapSubtree(int rootIndex, int lastChild) { + int leftIndex = rootIndex * 2 + 1; + int rightIndex = rootIndex * 2 + 2; + int root = this.heap[rootIndex]; + if (rightIndex <= lastChild) { // if has right and left children + int left = this.heap[leftIndex]; + int right = this.heap[rightIndex]; + if (left < right && left < root) { + this.swap(leftIndex, rootIndex); + this.heapSubtree(leftIndex, lastChild); + } else if (right < root) { + this.swap(rightIndex, rootIndex); + this.heapSubtree(rightIndex, lastChild); + } + } else if (leftIndex <= lastChild) { // if no right child, but has left child + int left = this.heap[leftIndex]; + if (left < root) { + this.swap(leftIndex, rootIndex); + this.heapSubtree(leftIndex, lastChild); + } + } + } + + /** + * Makes heap with root as root + * + * @param root index of root of heap + */ + private void makeMinHeap(int root) { + int leftIndex = root * 2 + 1; + int rightIndex = root * 2 + 2; + boolean hasLeftChild = leftIndex < this.heap.length; + boolean hasRightChild = rightIndex < this.heap.length; + if (hasRightChild) { //if has left and right + this.makeMinHeap(leftIndex); + this.makeMinHeap(rightIndex); + this.heapSubtree(root, this.heap.length - 1); + } else if (hasLeftChild) { + this.heapSubtree(root, this.heap.length - 1); + } + } + + /** + * Gets the root of heap + * + * @return root of heap + */ + private int getRoot() { + this.swap(0, this.size - 1); + this.size--; + this.heapSubtree(0, this.size - 1); + return this.heap[this.size]; // return old root + } + + /** + * Sorts heap with heap sort; displays ordered elements to console. + * + * @return sorted array of sorted elements + */ + public final int[] sort() { + this.makeMinHeap(0); // make min heap using index 0 as root. + int[] sorted = new int[this.size]; + int index = 0; + while (this.size > 0) { + int min = this.getRoot(); + sorted[index] = min; + index++; + } + return sorted; + } + + /** + * Gets input to sort + * + * @return unsorted array of integers to sort + */ + public static int[] getInput() { + final int numElements = 6; + int[] unsorted = new int[numElements]; + Scanner input = new Scanner(System.in); + System.out.println("Enter any 6 Numbers for Unsorted Array : "); + for (int i = 0; i < numElements; i++) { + unsorted[i] = input.nextInt(); + } + input.close(); + return unsorted; + } + + /** + * Prints elements in heap + * + * @param heap array representing heap + */ + public static void printData(int[] heap) { + System.out.println("Sorted Elements:"); + for (int i = 0; i < heap.length; i++) { + System.out.print(" " + heap[i] + " "); + } + } + + /** + * Main method + * + * @param args the command line arguments + */ + public static void main(String[] args) { + int[] heap = getInput(); + HeapSort data = new HeapSort(heap); + int[] sorted = data.sort(); + printData(sorted); + } + +} diff --git a/InsertionSort.java b/InsertionSort.java new file mode 100644 index 00000000..772b5a77 --- /dev/null +++ b/InsertionSort.java @@ -0,0 +1,49 @@ +import java.util.Scanner; + +/** + * This class implements Insertion Sort + * + * @author Unknown + * + */ +class InsertionSort +{ + /** + * Main Method + * + * @param args Command line arguments + */ + public static void main(String[] args) + { + int array[]=new int[6]; + Scanner input=new Scanner(System.in); + + //Input + System.out.println("Enter any 6 Numbers for Unsorted Array : "); + for(int i=0; i<6; i++) + { + array[i]=input.nextInt(); + } + + //Sorting + for(int i=0; i<6; i++) + { + int temp=array[i]; + int j=i-1; + while(j>=0 && temp 0; lastSortedIndex--){ + + //If our extracted element is smaller than element to the right, switch them. + if (sortedArray[lastSortedIndex-1] > extractedElement){ + //move the element to the left of extractedElement to the right in sortedArray + sortedArray[lastSortedIndex] = sortedArray[lastSortedIndex-1]; + //And move the current extractedElement to the left one (since it's smaller). + sortedArray[lastSortedIndex-1] = extractedElement; + } + else{ + //insert element where it is. + sortedArray[lastSortedIndex] = extractedElement; + //Terminating loop since element is in the right spot. + break; + } + + } + + } + + return sortedArray; + + } + +} diff --git a/LinearSearch.java b/LinearSearch.java index 82078593..f74f4ad0 100644 --- a/LinearSearch.java +++ b/LinearSearch.java @@ -1,7 +1,10 @@ import java.util.Scanner; public class LinearSearch{ - //Main method + /** + * The main method + * @param args Command line arguments + */ public static void main(String[] args){ Scanner input = new Scanner(System.in); @@ -27,7 +30,13 @@ public class LinearSearch{ } - //Linear search method + /** + * Linear search method + * + * @param list List to be searched + * @param key Key being searched for + * @return Location of the key + */ public static int linearsearch(int[] list, int key){ for (int i = 0; i< list.length; i++){ @@ -37,7 +46,11 @@ public class LinearSearch{ } return -1; } - //Helper method + /** + * Helper Method + * + * @param a array to be printed + */ public static void printarray(int[] a){ System.out.print("The array is: "); for( int d: a){ diff --git a/MergeSort.java b/MergeSort.java index 08479ff7..f574281d 100644 --- a/MergeSort.java +++ b/MergeSort.java @@ -1,107 +1,109 @@ -import java.util.Scanner; - -/** - * Merge Sort - * - */ -public class MergeSort { - private int[] array; - private int[] tempMergArr; - private int length; - - /** - * Sorts {@code inputArr} with merge sort algorithm. - * - * @param inputArr - */ - public final void sort(int inputArr[]) { - this.array = inputArr; - this.length = inputArr.length; - this.tempMergArr = new int[this.length]; - this.mergeSort(0, this.length - 1); - } - - /** - * Partitions Array into recursively smaller pieces. - * - * @param lowerIndex - * lower bound to include in the first partition - * @param higherIndex - * upper bound to include in the third partition - */ - private void mergeSort(int lowerIndex, int higherIndex) { - if (lowerIndex < higherIndex) { - int middle = lowerIndex + (higherIndex - lowerIndex) / 2; - // Below step sorts the left side of the array - this.mergeSort(lowerIndex, middle); - // Below step sorts the right side of the array - this.mergeSort(middle + 1, higherIndex); - // Now merge both sides - this.mergeParts(lowerIndex, middle, higherIndex); - } - } - - /** - * Merges partitions. - * - * @param lowerIndex - * @param middle - * @param higherIndex - */ - private void mergeParts(int lowerIndex, int middle, int higherIndex) { - for (int i = lowerIndex; i <= higherIndex; i++) { - this.tempMergArr[i] = this.array[i]; - } - int i = lowerIndex; - int j = middle + 1; - int k = lowerIndex; - while (i <= middle && j <= higherIndex) { - if (this.tempMergArr[i] <= this.tempMergArr[j]) { - this.array[k] = this.tempMergArr[i]; - i++; - } else { - this.array[k] = this.tempMergArr[j]; - j++; - } - k++; - } - while (i <= middle) { - this.array[k] = this.tempMergArr[i]; - k++; - i++; - } - - } - - /** - * Gets input to sort. - * - * @return unsorted array of integers to sort - */ - public static int[] getInput() { - final int numElements = 6; - int[] unsorted = new int[numElements]; - Scanner input = new Scanner(System.in); - System.out.println("Enter any 6 Numbers for Unsorted Array : "); - for (int i = 0; i < numElements; i++) { - unsorted[i] = input.nextInt(); - } - input.close(); - return unsorted; - } - - /** - * Main Method. - * - * @param args - */ - public static void main(String args[]) { - int[] inputArr = getInput(); - MergeSort mergeSort = new MergeSort(); - mergeSort.sort(inputArr); - for (int i : inputArr) { - System.out.print(i); - System.out.print(" "); - } - } -} +import java.util.Scanner; + +/** + * This class implements MergeSort + * @author Unknown + * + */ +public class MergeSort { + /** Array for mergeSort*/ + private int[] array; + /** Temp Merge Array*/ + private int[] tempMergArr; + /** Length of the array*/ + private int length; + + /** + * Sorts inputArr with merge sort algorithm + * + * @param inputArr Array to be sorted + */ + public final void sort(int inputArr[]) { + this.array = inputArr; + this.length = inputArr.length; + this.tempMergArr = new int[this.length]; + this.mergeSort(0, this.length - 1); + } + + /** + * Partitions Array into recursively smaller pieces + * + * @param lowerIndex lower bound to include in the first partition + * @param higherIndex upper bound to include in the third partition + */ + private void mergeSort(int lowerIndex, int higherIndex) { + if (lowerIndex < higherIndex) { + int middle = lowerIndex + (higherIndex - lowerIndex) / 2; + // Below step sorts the left side of the array + this.mergeSort(lowerIndex, middle); + // Below step sorts the right side of the array + this.mergeSort(middle + 1, higherIndex); + // Now merge both sides + this.mergeParts(lowerIndex, middle, higherIndex); + } + } + + /** + * Merges partitions + * + * @param lowerIndex The lower index + * @param middle The middle index + * @param higherIndex The higher index + */ + private void mergeParts(int lowerIndex, int middle, int higherIndex) { + for (int i = lowerIndex; i <= higherIndex; i++) { + this.tempMergArr[i] = this.array[i]; + } + int i = lowerIndex; + int j = middle + 1; + int k = lowerIndex; + while (i <= middle && j <= higherIndex) { + if (this.tempMergArr[i] <= this.tempMergArr[j]) { + this.array[k] = this.tempMergArr[i]; + i++; + } else { + this.array[k] = this.tempMergArr[j]; + j++; + } + k++; + } + while (i <= middle) { + this.array[k] = this.tempMergArr[i]; + k++; + i++; + } + + } + + /** + * Gets input to sort + * + * @return unsorted array of integers to sort + */ + public static int[] getInput() { + final int numElements = 6; + int[] unsorted = new int[numElements]; + Scanner input = new Scanner(System.in); + System.out.println("Enter any 6 Numbers for Unsorted Array : "); + for (int i = 0; i < numElements; i++) { + unsorted[i] = input.nextInt(); + } + input.close(); + return unsorted; + } + + /** + * Main Method + * + * @param args Command line arguments + */ + public static void main(String args[]) { + int[] inputArr = getInput(); + MergeSort mergeSort = new MergeSort(); + mergeSort.sort(inputArr); + for (int i : inputArr) { + System.out.print(i); + System.out.print(" "); + } + } +} diff --git a/Quicksort.java b/Quicksort.java index f693c42b..5b5896f8 100644 --- a/Quicksort.java +++ b/Quicksort.java @@ -1,7 +1,19 @@ import java.util.Scanner; +/** + * Implementation of QuickSort + * + * @author Unknown + * + */ + public class Quicksort{ + /** + * The main method + * + * @param args Command line arguments + */ public static void main(String[] args){ Scanner input = new Scanner(System.in); int[] array; @@ -28,7 +40,13 @@ public class Quicksort{ System.out.println(); } - //Quicksort Method + /** + * QuickSort method + * + * @param ar Array to perform QuickSort + * @param start Start of the array + * @param end End of the array + */ public static void quicksort(int[] ar, int start, int end){ int[] array; @@ -54,7 +72,13 @@ public class Quicksort{ } } - //Helper methods + /** + * Helper Method 1 - Swaps elements of an array + * + * @param ar Array to be used + * @param index1 Index 1 to be switched with Index 2 + * @param index2 Index 2 to be switched with Index 1 + */ public static void swap(int[] ar, int index1, int index2){ int temp = ar[index1]; @@ -62,6 +86,11 @@ public class Quicksort{ ar[index2] = temp; } + /** + * Helper Method 2 - Prints the elements of an array + * + * @param array Array to be printed + */ public static void printarray(int[] array){ for (int data : array){ diff --git a/ReverseString.java b/ReverseString.java new file mode 100644 index 00000000..e8a75b3e --- /dev/null +++ b/ReverseString.java @@ -0,0 +1,47 @@ +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * This method produces a reversed version of a string + * + * @author Unknown + * + */ +class ReverseString +{ + + /** + * This method reverses the string str and returns it + * @param str String to be reversed + * @return Reversed string + */ + static String reverseString(String str) + { + String reverse=" "; + if(str.length()==1) + { + return str; + } + else + { + reverse=reverse+str.charAt(str.length()-1)+reverseString(str.substring(0,str.length()-1)); + return reverse; + } + } + + /** + * Main Method + * + * @param args Command line arguments + * @throws IOException Exception thrown because of BufferedReader + */ + public static void main(String args[]) throws IOException + { + BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); + System.out.println("Enter the string"); + String srr=br.readLine(); + System.out.println("Reverse="+reverseString(srr)); + } +} + \ No newline at end of file diff --git a/SelectionSort.java b/SelectionSort.java new file mode 100644 index 00000000..d8e1e003 --- /dev/null +++ b/SelectionSort.java @@ -0,0 +1,52 @@ +import java.util.Scanner; + +/** + * This class implements Selection Sort + * + * @author Unknown + * + */ + +class SelectionSort +{ + /** + * Main method + * + * @param args Command line arguments + */ + public static void main(String[] args) + { + int array[]=new int[6]; + Scanner input=new Scanner(System.in); + + //Input + System.out.println("Enter any 6 Numbers for Unsorted Array : "); + for(int i=0; i<6; i++) + { + array[i]=input.nextInt(); + } + + //Sorting + for(int i=0; i<6; i++) + { + int min=i; + for(int j=i+1; j<6; j++) + { + if(array[j] st=new LinkedList<>(); //operational stack + Queue st=new Queue<>(); //operational stack st.add(source); //assigning source while(!st.isEmpty()){ b[st.peek()]=(byte)0; //assigning waiting status @@ -26,6 +39,11 @@ public class App{ } + /** + * The main method + * + * @param args Command line arguments + */ public static void main(String args[]){ Scanner in=new Scanner(System.in); int vertices=in.nextInt(),source=in.nextInt(); diff --git a/countwords.java b/countwords.java index a1d4d72d..2eebe786 100644 --- a/countwords.java +++ b/countwords.java @@ -1,6 +1,19 @@ import java.util.Scanner; + +/** + * You enter a string into this program, and it will return how + * many words were in that particular string + * + * @author Unknown + * + */ class CountTheWords { + /** + * The main method + * + * @param args Command line arguments + */ public static void main(String args[]) { System.out.println("Enter the string"); diff --git a/data_structures/BinaryTree.java b/data_structures/BinaryTree.java index 90a6d980..b26525cf 100644 --- a/data_structures/BinaryTree.java +++ b/data_structures/BinaryTree.java @@ -1,11 +1,35 @@ -//A binary tree is a data structure in which an element has two successors(children) -//The left child is usually smaller than the parent, and the right child is usually bigger +/** + * This entire class is used to build a Binary Tree data structure. + * There is the Node Class and the Tree Class, both explained below. + * + * @author Unknown + * + */ + + +/** + * This class implements the nodes that will go on the Binary Tree. + * They consist of the data in them, the node to the left, the node + * to the right, and the parent from which they came from. + * + * @author Unknown + * + */ class Node{ + /** Data for the node */ public int data; + /** The Node to the left of this one */ public Node left; + /** The Node to the right of this one */ public Node right; + /** The parent of this node */ public Node parent; + /** + * Constructor of Node + * + * @param value Value to put in the node + */ public Node(int value){ data = value; left = null; @@ -14,13 +38,33 @@ class Node{ } } + +/** + * A binary tree is a data structure in which an element + * has two successors(children). The left child is usually + * smaller than the parent, and the right child is usually + * bigger. + * + * @author Unknown + * + */ class Tree{ + /** The root of the Binary Tree */ private Node root; + /** + * Constructor + */ public Tree(){ root = null; } - //Returns the node if it finds it, otherwise returns the parent + + /** + * Method to find a Node with a certain value + * + * @param key Value being looked for + * @return The node if it finds it, otherwise returns the parent + */ public Node find(int key){ Node current = root; Node last = root; @@ -37,7 +81,11 @@ class Tree{ return last; } - //Inserts the given value + /** + * Inserts certain value into the Binary Tree + * + * @param value Value to be inserted + */ public void put(int value){ Node newNode = new Node(value); if(root == null) @@ -60,7 +108,12 @@ class Tree{ } } - //Deletes the given value + /** + * Deletes a given value from the Binary Tree + * + * @param value Value to be deleted + * @return If the value was deleted + */ public boolean remove(int value){ //temp is the node to be deleted Node temp = find(value); @@ -84,34 +137,34 @@ class Tree{ //Two children else if(temp.left != null && temp.right != null){ - Node succesor = findSuccesor(temp); + Node successor = findSuccessor(temp); //The left tree of temp is made the left tree of the successor - succesor.left = temp.left; - succesor.left.parent = succesor; + successor.left = temp.left; + successor.left.parent = successor; //If the successor has a right child, the child's grandparent is it's new parent - if(succesor.right != null && succesor.parent != temp){ - succesor.right.parent = succesor.parent; - succesor.parent.left = succesor.right; - succesor.right = temp.right; - succesor.right.parent = succesor; + if(successor.right != null && successor.parent != temp){ + successor.right.parent = successor.parent; + successor.parent.left = successor.right; + successor.right = temp.right; + successor.right.parent = successor; } if(temp == root){ - succesor.parent = null; - root = succesor; + successor.parent = null; + root = successor; return true; } //If you're not deleting the root else{ - succesor.parent = temp.parent; + successor.parent = temp.parent; //This if/else assigns the new node to be either the left or right child of the parent if(temp.parent.data < temp.data) - temp.parent.right = succesor; + temp.parent.right = successor; else - temp.parent.left = succesor; + temp.parent.left = successor; return true; } } @@ -148,8 +201,14 @@ class Tree{ } } - //Move right once and go left down the tree as far as you can - public Node findSuccesor(Node n){ + /** + * This method finds the Successor to the Node given. + * Move right once and go left down the tree as far as you can + * + * @param n Node that you want to find the Successor of + * @return The Successor of the node + */ + public Node findSuccessor(Node n){ if(n.right == null) return n; Node current = n.right; @@ -161,11 +220,20 @@ class Tree{ return parent; } + /** + * Returns the root of the Binary Tree + * + * @return the root of the Binary Tree + */ public Node getRoot(){ return root; } - //Prints leftChild - root - rightChild + /** + * Prints leftChild - root - rightChild + * + * @param localRoot The local root of the binary tree + */ public void inOrder(Node localRoot){ if(localRoot != null){ inOrder(localRoot.left); @@ -173,7 +241,12 @@ class Tree{ inOrder(localRoot.right); } } - //Prints root - leftChild - rightChild + + /** + * Prints root - leftChild - rightChild + * + * @param localRoot The local root of the binary tree + */ public void preOrder(Node localRoot){ if(localRoot != null){ System.out.print(localRoot.data + " "); @@ -181,7 +254,12 @@ class Tree{ preOrder(localRoot.right); } } - //Prints rightChild - leftChild - root + + /** + * Prints rightChild - leftChild - root + * + * @param localRoot The local root of the binary tree + */ public void postOrder(Node localRoot){ if(localRoot != null){ postOrder(localRoot.left); diff --git a/data_structures/DoublyLinkedList.java b/data_structures/DoublyLinkedList.java index f8411994..c3229d9c 100644 --- a/data_structures/DoublyLinkedList.java +++ b/data_structures/DoublyLinkedList.java @@ -1,19 +1,38 @@ -/* - * A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes. - * With a linked list you do not need to predetermine it's size as it grows and shrinks as it is edited. - * This is an example of a double ended, doubly linked list. - * Each link references the next link and the previous one. -*/ -class LinkedList{ - private Link head; //Head refers to the front of the list - private Link tail; //Tail refers to the back of the list +/** + * This class implements a DoublyLinkedList. This is done using the classes + * LinkedList and Link. + * + * A linked list is simplar to an array, it holds values. However, + * links in a linked list do not have indees. With a linked list + * you do not need to predetermine it's size as it grows and shrinks + * as it is edited. This is an example of a double ended, doubly + * linked list. Each link references the next link and the previous + * one. + * + * @author Unknown + * + */ - public LinkedList(){ +class DoublyLinkedList{ + /** Head refers to the front of the list */ + private Link head; + /** Tail refers to the back of the list */ + private Link tail; + + /** + * Constructor + */ + public DoublyLinkedList(){ head = null; tail = null; } - public void insertHead(int x){ //Insert an element at the head + /** + * Insert an element at the head + * + * @param x Element to be inserted + */ + public void insertHead(int x){ Link newLink = new Link(x); //Create a new link with a value attached to it if(isEmpty()) //Set the first element added to be the tail tail = newLink; @@ -23,6 +42,11 @@ class LinkedList{ head = newLink; // newLink(head) <--> oldhead } + /** + * Insert an element at the tail + * + * @param x Element to be inserted + */ public void insertTail(int x){ Link newLink = new Link(x); newLink.next = null; // currentTail(tail) newlink --> @@ -31,7 +55,12 @@ class LinkedList{ tail = newLink; // oldTail <--> newLink(tail) --> } - public Link deleteHead(){ //Delete the element at the head + /** + * Delete the element at the head + * + * @return The new head + */ + public Link deleteHead(){ Link temp = head; head = head.next; // oldHead <--> 2ndElement(head) head.previous = null; // oldHead --> 2ndElement(head) nothing pointing at old head so will be removed @@ -40,6 +69,11 @@ class LinkedList{ return temp; } + /** + * Delete the element at the tail + * + * @return The new tail + */ public Link deleteTail(){ Link temp = tail; tail = tail.previous; // 2ndLast(tail) <--> oldTail --> null @@ -47,6 +81,12 @@ class LinkedList{ return temp; } + /** + * Delete the element from somewhere in the list + * + * @param x element to be deleted + * @return Link deleted + */ public Link delete(int x){ Link current = head; @@ -66,6 +106,11 @@ class LinkedList{ return current; } + /** + * Inserts element and reorders + * + * @param x Element to be added + */ public void insertOrdered(int x){ Link newLink = new Link(x); Link current = head; @@ -86,10 +131,18 @@ class LinkedList{ } } - public boolean isEmpty(){ //Returns true if list is empty + /** + * Returns true if list is empty + * + * @return true if list is empty + */ + public boolean isEmpty(){ return(head == null); } + /** + * Prints contents of the list + */ public void display(){ //Prints contents of the list Link current = head; while(current!=null){ @@ -100,24 +153,44 @@ class LinkedList{ } } +/** + * This class is used to implement the nodes of the + * linked list. + * + * @author Unknown + * + */ class Link{ + /** Value of node */ public int value; - public Link next; //This points to the link in front of the new link - public Link previous; //This points to the link behind the new link + /** This points to the link in front of the new link */ + public Link next; + /** This points to the link behind the new link */ + public Link previous; + /** + * Constructor + * + * @param value Value of node + */ public Link(int value){ this.value = value; } + /** + * Displays the node + */ public void displayLink(){ System.out.print(value+" "); } -} -//Example -public class DoublyLinkedList{ + /** + * Main Method + * + * @param args Command line arguments + */ public static void main(String args[]){ - LinkedList myList = new LinkedList(); + DoublyLinkedList myList = new DoublyLinkedList(); myList.insertHead(13); myList.insertHead(7); diff --git a/data_structures/PriorityQueues.java b/data_structures/PriorityQueues.java index a0c1af1a..acb6b552 100644 --- a/data_structures/PriorityQueues.java +++ b/data_structures/PriorityQueues.java @@ -1,21 +1,40 @@ -/* +/** + * This class implements a PriorityQueue. + * * A priority queue adds elements into positions based on their priority. * So the most important elements are placed at the front/on the top. * In this example I give numbers that are bigger, a higher priority. - * Queues in theory have no fixed size but when using an array implementation it does. + * Queues in theory have no fixed size but when using an array + * implementation it does. + * + * @author Unknown + * */ class PriorityQueue{ + /** The max size of the queue */ private int maxSize; + /** The array for the queue */ private int[] queueArray; + /** How many items are in the queue */ private int nItems; - public PriorityQueue(int size){ //Constructor + /** + * Constructor + * + * @param size Size of the queue + */ + public PriorityQueue(int size){ maxSize = size; queueArray = new int[size]; nItems = 0; } - public void insert(int value){ //Inserts an element in it's appropriate place + /** + * Inserts an element in it's appropriate place + * + * @param value Value to be inserted + */ + public void insert(int value){ if(nItems == 0){ queueArray[0] = value; } @@ -30,28 +49,64 @@ class PriorityQueue{ nItems++; } - public int remove(){ //Remove the element from the front of the queue + /** + * Remove the element from the front of the queue + * + * @return The element removed + */ + public int remove(){ return queueArray[--nItems]; } - public int peek(){ //Checks what's at the front of the queue + /** + * Checks what's at the front of the queue + * + * @return element at the front of the queue + */ + public int peek(){ return queueArray[nItems-1]; } - public boolean isEmpty(){ //Returns true is the queue is empty + /** + * Returns true if the queue is empty + * + * @return true if the queue is empty + */ + public boolean isEmpty(){ return(nItems == 0); } - public boolean isFull(){ //Returns true is the queue is full + /** + * Returns true if the queue is full + * + * @return true if the queue is full + */ + public boolean isFull(){ return(nItems == maxSize); } - public int getSize(){ //Returns the number of elements in the queue + /** + * Returns the number of elements in the queue + * + * @return number of elements in the queue + */ + public int getSize(){ return nItems; } } -//Example + +/** + * This class implements the PriorityQueue class above. + * + * @author Unknown + * + */ public class PriorityQueues{ + /** + * Main method + * + * @param args Command Line Arguments + */ public static void main(String args[]){ PriorityQueue myQueue = new PriorityQueue(4); myQueue.insert(10); diff --git a/data_structures/Queues.java b/data_structures/Queues.java index c7725417..84638cb2 100644 --- a/data_structures/Queues.java +++ b/data_structures/Queues.java @@ -1,16 +1,31 @@ -/* +/** + * This implements Queues by using the class Queue. + * * A queue data structure functions the same as a real world queue. * The elements that are added first are the first to be removed. - * New elements are added to the back/rear of the queue. + * New elements are added to the back/rear of the queue. + * + * @author Unknown + * */ class Queue{ + /** Max size of the queue */ private int maxSize; + /** The array representing the queue */ private int[] queueArray; + /** Front of the queue */ private int front; + /** Rear of the queue */ private int rear; + /** How many items are in the queue */ private int nItems; - public Queue(int size){ //Constructor + /** + * Constructor + * + * @param size Size of the new queue + */ + public Queue(int size){ maxSize = size; queueArray = new int[size]; front = 0; @@ -18,7 +33,13 @@ class Queue{ nItems = 0; } - public boolean insert(int x){ //Inserts an element at the rear of the queue + /** + * Inserts an element at the rear of the queue + * + * @param x element to be added + * @return True if the element was added successfully + */ + public boolean insert(int x){ if(isFull()) return false; if(rear == maxSize-1) //If the back of the queue is the end of the array wrap around to the front @@ -29,6 +50,11 @@ class Queue{ return true; } + /** + * Remove an element from the front of the queue + * + * @return the new front of the queue + */ public int remove(){ //Remove an element from the front of the queue if(isEmpty()){ System.out.println("Queue is empty"); @@ -42,28 +68,64 @@ class Queue{ return temp; } - public int peekFront(){ //Checks what's at the front of the queue + /** + * Checks what's at the front of the queue + * + * @return element at the front of the queue + */ + public int peekFront(){ return queueArray[front]; } - public int peekRear(){ //Checks what's at the rear of the queue + /** + * Checks what's at the rear of the queue + * + * @return element at the rear of the queue + */ + public int peekRear(){ return queueArray[rear]; } - public boolean isEmpty(){ //Returns true is the queue is empty + /** + * Returns true if the queue is empty + * + * @return true if the queue is empty + */ + public boolean isEmpty(){ return(nItems == 0); } - public boolean isFull(){ //Returns true is the queue is full + /** + * Returns true if the queue is full + * + * @return true if the queue is full + */ + public boolean isFull(){ return(nItems == maxSize); } - public int getSize(){ //Returns the number of elements in the queue + /** + * Returns the number of elements in the queue + * + * @return number of elements in the queue + */ + public int getSize(){ return nItems; } } -//Example + +/** + * This class is the example for the Queue class + * + * @author Unknown + * + */ public class Queues{ + /** + * Main method + * + * @param args Command line arguments + */ public static void main(String args[]){ Queue myQueue = new Queue(4); myQueue.insert(10); diff --git a/data_structures/SinglyLinkedList.java b/data_structures/SinglyLinkedList.java index 75660c73..4523739b 100644 --- a/data_structures/SinglyLinkedList.java +++ b/data_structures/SinglyLinkedList.java @@ -1,58 +1,78 @@ -/* - * A linked list is similar to an array, it holds values. However, links in a linked list do not have indexes. - * With a linked list you do not need to predetermine it's size as it grows and shrinks as it is edited. - * This is an example of a singly linked list. Elements can only be added/removed at the head/front of the list. -*/ -class LinkedList{ - private Link head; //Head refers to the front of the list +/** + * This class implements a SinglyLinked List. This is done + * using SinglyLinkedList class and a LinkForLinkedList Class. + * + * A linked list is implar to an array, it hold values. + * However, links in a linked list do not have indexes. With + * a linked list you do not need to predetermine it's size as + * it gorws and shrinks as it is edited. This is an example of + * a singly linked list. Elements can only be added/removed + * at the head/front of the list. + * + * @author Unknown + * + */ +class SinglyLinkedList{ + /**Head refered to the front of the list */ + private LinkForLinkedList head; - public LinkedList(){ + /** + * Constructor of SinglyLinkedList + */ + public SinglyLinkedList(){ head = null; } - public void insertHead(int x){ //Insert an element at the head - Link newLink = new Link(x); //Create a new link with a value attached to it + /** + * This method inserts an element at the head + * + * @param x Element to be added + */ + public void insertHead(int x){ + LinkForLinkedList newLink = new LinkForLinkedList(x); //Create a new link with a value attached to it newLink.next = head; //Set the new link to point to the current head head = newLink; //Now set the new link to be the head } - public Link deleteHead(){ //Delete the element at the head - Link temp = head; + /** + * This method deletes an element at the head + * + * @return The element deleted + */ + public LinkForLinkedList deleteHead(){ + LinkForLinkedList temp = head; head = head.next; //Make the second element in the list the new head, the Java garbage collector will later remove the old head return temp; } - public boolean isEmpty(){ //Returns true if list is empty + /** + * Checks if the list is empty + * + * @return true is list is empty + */ + public boolean isEmpty(){ return(head == null); } - public void display(){ //Prints contents of the list - Link current = head; + /** + * Prints contents of the list + */ + public void display(){ + LinkForLinkedList current = head; while(current!=null){ current.displayLink(); current = current.next; } System.out.println(); } -} - -class Link{ - public int value; - public Link next; //This is what the link will point to - - public Link(int valuein){ - value = valuein; - } - - public void displayLink(){ - System.out.print(value+" "); - } -} - -//Example -public class SinglyLinkedList{ + + /** + * Main method + * + * @param args Command line arguments + */ public static void main(String args[]){ - LinkedList myList = new LinkedList(); + SinglyLinkedList myList = new SinglyLinkedList(); System.out.println(myList.isEmpty()); //Will print true @@ -66,4 +86,36 @@ public class SinglyLinkedList{ myList.display(); // 7(head) --> 5 } +} + +/** + * This class is the nodes of the SinglyLinked List. + * They consist of a vlue and a pointer to the node + * after them. + * + * @author Unknown + * + */ +class LinkForLinkedList{ + /** The value of the node */ + public int value; + /** Point to the next node */ + public LinkForLinkedList next; //This is what the link will point to + + /** + * Constructor + * + * @param valuein Value to be put in the node + */ + public LinkForLinkedList(int valuein){ + value = valuein; + } + + /** + * Prints out the value of the node + */ + public void displayLink(){ + System.out.print(value+" "); + } + } \ No newline at end of file diff --git a/data_structures/Stacks.java b/data_structures/Stacks.java index 18139147..dbb2a621 100644 --- a/data_structures/Stacks.java +++ b/data_structures/Stacks.java @@ -1,62 +1,131 @@ -/* - *A stack is exactly what it sounds like. An element gets added to top of the stack and only the element on the top may be removed. - *This is an example of an array implementation of a Stack. So an element can only be added/removed from the end of the array. - *In theory stacks have no fixed size, but with an array implementation it does. -*/ +import java.util.ArrayList; + +/** + * This class implements a Stack using two different implementations. + * Stack is used with a regular array and Stack2 uses an ArrayList. + * + * A stack is exactly what it sounds like. An element gets added to the top of + * the stack and only the element on the top may be removed. This is an example + * of an array implementation of a Stack. So an element can only be added/removed + * from the end of the array. In theory stack have no fixed size, but with an + * array implementation it does. + * + * @author Unknown + * + */ class Stack{ + /** The max size of the Stack */ private int maxSize; + /** The array representation of the Stack */ private int[] stackArray; + /** The top of the stack */ private int top; - public Stack(int size){ //Constructor + /** + * Constructor + * + * @param size Size of the Stack + */ + public Stack(int size){ maxSize = size; stackArray = new int[maxSize]; top = -1; } - public void push(int value){ //Adds an element to the top of the stack + /** + * Adds an element to the top of the stack + * + * @param value The element added + */ + public void push(int value){ top++; stackArray[top] = value; } - public int pop(){ //Removes the top element of the stack and returns the value you've removed + /** + * Removes the top element of the stack and returns the value you've removed + * + * @return value popped off the Stack + */ + public int pop(){ return stackArray[top--]; } - public int peek(){ //Returns the element at the top of the stack + /** + * Returns the element at the top of the stack + * + * @return element at the top of the stack + */ + public int peek(){ return stackArray[top]; } - public boolean isEmpty(){ //Returns true if the stack is empty + /** + * Returns true if the stack is empty + * + * @return true if the stack is empty + */ + public boolean isEmpty(){ return(top == -1); } - public boolean isFull(){ //Returns true if the stack is full + /** + * Returns true if the stack is full + * + * @return true if the stack is full + */ + public boolean isFull(){ return(top+1 == maxSize); } + + /** + * Deletes everything in the Stack + * + * Doesn't delete elements in the array + * but if you call push method after calling + * makeEmpty it will overwrite previous + * values + */ public void makeEmpty(){ //Doesn't delete elements in the array but if you call top = -1; //push method after calling makeEmpty it will overwrite previous values } } - -/* This is ArrayList Implementation of stack , Where size is not - a problem we can extend the stack as much as we want*/ +/** + * This is an ArrayList Implementation of stack, Where size is not + * a problem we can extend the stack as much as we want. + * + * @author Unknown + * + */ class Stack2{ - - + /** ArrayList representation of the stack */ ArrayList stackList; - Stack2(){ //constructor + /** + * Constructor + */ + Stack2(){ stackList=new ArrayList<>(); } - - void push(int value){ //adds value to the end of list which is the top for stack + /** + * Adds value to the end of list which + * is the top for stack + * + * @param value value to be added + */ + void push(int value){ stackList.add(value); } - int pop(){ //pops last element of list which is indeed the top for Stack + /** + * Pops last element of list which is indeed + * the top for Stack + * + * @return Element popped + */ + int pop(){ if(!isEmpty()){ // checks for an empty Stack @@ -71,7 +140,12 @@ class Stack2{ } - boolean isEmpty(){ //checks for empty Stack + /** + * Checks for empty Stack + * + * @return true if stack is empty + */ + boolean isEmpty(){ if(stackList.isEmpty()) return true; @@ -79,13 +153,28 @@ class Stack2{ } - int peek(){ //top element of stack + /** + * Top element of stack + * + * @return top element of stack + */ + int peek(){ return stackList.get(stackList.size()-1); } } -//Example +/** + * This class implements the Stack and Stack2 created above + * + * @author Unknown + * + */ public class Stacks{ + /** + * Main method + * + * @param args Command line arguments + */ public static void main(String args[]){ Stack myStack = new Stack(4); //Declare a stack of maximum size 4 //Populate the stack diff --git a/data_structures/heaps/MaxHeap.java b/data_structures/heaps/MaxHeap.java index 6768d487..16ba7cff 100644 --- a/data_structures/heaps/MaxHeap.java +++ b/data_structures/heaps/MaxHeap.java @@ -84,7 +84,13 @@ public class MaxHeap implements Heap { @Override public void deleteElement(int elementIndex) { - if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap"); + if (maxHeap.isEmpty()) + try { + throw new EmptyHeapException("Attempt to delete an element from an empty heap"); + } catch (EmptyHeapException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } if ((elementIndex > maxHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range"); // The last element in heap replaces the one to be deleted maxHeap.set(elementIndex - 1, getElement(maxHeap.size())); diff --git a/data_structures/heaps/MinHeap.java b/data_structures/heaps/MinHeap.java index cd484d79..5793bb92 100644 --- a/data_structures/heaps/MinHeap.java +++ b/data_structures/heaps/MinHeap.java @@ -87,7 +87,13 @@ public class MinHeap implements Heap { @Override public void deleteElement(int elementIndex) { - if (isempty(maxHeap)) throw new EmptyHeapException("Attempt to delete an element from an empty heap"); + if (minHeap.isEmpty()) + try { + throw new EmptyHeapException("Attempt to delete an element from an empty heap"); + } catch (EmptyHeapException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } if ((elementIndex > minHeap.size()) && (elementIndex <= 0)) throw new IndexOutOfBoundsException("Index out of heap range"); // The last element in heap replaces the one to be deleted minHeap.set(elementIndex - 1, getElement(minHeap.size())); diff --git a/dfs.java b/dfs.java index 52e64272..023e1d5f 100644 --- a/dfs.java +++ b/dfs.java @@ -1,8 +1,21 @@ import java.util.*; -public class App{ - +/** + * Implementation of a Depth First Search + * + * @author Unknown + * + */ + +public class dfs{ + /** + * Implementation in code of a DFS + * + * @param a structure to be DFS'ed + * @param vertices The vertices + * @param source The source + */ public static void dfs(byte [][] a,int vertices,int source){ //passing adjacency matrix and no of vertices byte []b=new byte[vertices]; //flag container containing status of each vertices Arrays.fill(b,(byte)-1); //status initialization @@ -27,6 +40,11 @@ public class App{ } + /** + * The main method + * + * @param args Command line arguments + */ public static void main(String args[]){ Scanner in=new Scanner(System.in); int vertices=in.nextInt(),source=in.nextInt();