diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cff400b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/RemoveDuplicates/nbproject/private/ +/RecursionProblems/nbproject/private/ +/RecursionProblems/build/ \ No newline at end of file diff --git a/LinkedList/.gitignore b/LinkedList/.gitignore new file mode 100644 index 0000000..d16077a --- /dev/null +++ b/LinkedList/.gitignore @@ -0,0 +1,8 @@ +**/nbproject/private/ +**/nbproject/Makefile-*.mk +**/nbproject/Package-*.bash +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/LinkedList/LinkedListImplementation/.gitignore b/LinkedList/LinkedListImplementation/.gitignore new file mode 100644 index 0000000..d16077a --- /dev/null +++ b/LinkedList/LinkedListImplementation/.gitignore @@ -0,0 +1,8 @@ +**/nbproject/private/ +**/nbproject/Makefile-*.mk +**/nbproject/Package-*.bash +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/LinkedList/LinkedListImplementation/build.xml b/LinkedList/LinkedListImplementation/build.xml new file mode 100644 index 0000000..c6e25b3 --- /dev/null +++ b/LinkedList/LinkedListImplementation/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project LinkedListImplementation. + + + diff --git a/LinkedList/LinkedListImplementation/manifest.mf b/LinkedList/LinkedListImplementation/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/LinkedList/LinkedListImplementation/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/LinkedList/LinkedListImplementation/nbproject/build-impl.xml b/LinkedList/LinkedListImplementation/nbproject/build-impl.xml new file mode 100644 index 0000000..a910641 --- /dev/null +++ b/LinkedList/LinkedListImplementation/nbproject/build-impl.xml @@ -0,0 +1,1424 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LinkedList/LinkedListImplementation/nbproject/genfiles.properties b/LinkedList/LinkedListImplementation/nbproject/genfiles.properties new file mode 100644 index 0000000..423d7be --- /dev/null +++ b/LinkedList/LinkedListImplementation/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=26e7a850 +build.xml.script.CRC32=beabae0e +build.xml.stylesheet.CRC32=8064a381@1.68.1.46 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=26e7a850 +nbproject/build-impl.xml.script.CRC32=3bea010e +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/LinkedList/LinkedListImplementation/nbproject/project.properties b/LinkedList/LinkedListImplementation/nbproject/project.properties new file mode 100644 index 0000000..67c0170 --- /dev/null +++ b/LinkedList/LinkedListImplementation/nbproject/project.properties @@ -0,0 +1,77 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/LinkedListImplementation.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +file.reference.kotlin-runtime.jar=/Users/souravpalit/Library/Application Support/NetBeans/8.2/kotlinc/lib\\kotlin-runtime.jar +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${kotlinc.classpath}: +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +kotlinc.classpath=${file.reference.kotlin-runtime.jar} +main.class=linkedlistimplementation.LinkedListImplementation +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${kotlinc.classpath}: +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/LinkedList/LinkedListImplementation/nbproject/project.xml b/LinkedList/LinkedListImplementation/nbproject/project.xml new file mode 100644 index 0000000..6fed159 --- /dev/null +++ b/LinkedList/LinkedListImplementation/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + LinkedListImplementation + + + + + + + + + diff --git a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java new file mode 100644 index 0000000..296ecb4 --- /dev/null +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java @@ -0,0 +1,82 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package linkedlistimplementation; + +import java.util.LinkedList; +import javafx.scene.Node; + +/** + * + * @author mac + */ +public class LinkedListImplementation { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + + SinglyLinkedList myLinkedList = new SinglyLinkedList(); + myLinkedList.add(2); + myLinkedList.add(4); + myLinkedList.add(5); + myLinkedList.add(6); + + myLinkedList.printList(); + + myLinkedList.add(4, 1); + System.out.println("After Adding new value(1) at index 4"); + myLinkedList.printList(); + + myLinkedList.add(5, 7); + System.out.println("After Adding new value(7) at index 5"); + myLinkedList.printList(); + + myLinkedList.add(2, 3); + System.out.println("After Adding new value(3) at index 2"); + myLinkedList.printList(); + + myLinkedList.remove(1); + System.out.println("After removing value(1)"); + myLinkedList.printList(); + + myLinkedList.remove(4); + System.out.println("After removing value(4)"); + myLinkedList.printList(); + + myLinkedList.remove(7); + System.out.println("After removing value(7)"); + myLinkedList.printList(); + + myLinkedList.add(9); + System.out.println("After adding value(9)"); + myLinkedList.printList(); + + myLinkedList.removeAt(2); + System.out.println("After removing value at index 2"); + myLinkedList.printList(); + + myLinkedList.removeAt(0); + System.out.println("After removing value from first"); + myLinkedList.printList(); + + myLinkedList.removeAt(myLinkedList.size() -1); + System.out.println("After removing value from last"); + myLinkedList.printList(); + + myLinkedList.removeAt(myLinkedList.size() -1); + System.out.println("After removing value from last"); + myLinkedList.printList(); + + myLinkedList.removeAt(myLinkedList.size() -1); + System.out.println("After removing value from last"); + myLinkedList.printList(); + + } + +} diff --git a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java new file mode 100644 index 0000000..43e5e5a --- /dev/null +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java @@ -0,0 +1,199 @@ +package linkedlistimplementation; + +public class SinglyLinkedList { + // Here we need head to traverse the linked list from starting to lastNode + private SinglyNode head; + //Last node will help us to add a new value at the last with out moving the + private SinglyNode tail; + //Node count will track the length of the linked list + private int nodeCount = 0; + + + /** + * Initializing head. First time both head and lastNode will be the same + * @param value + */ + public void add(E value) { + if(head == null) { + SinglyNode node = new SinglyNode<>(null, value); + head = node; + tail = node; + } else { + SinglyNode node = new SinglyNode<>(null, value); + tail.next = node; + tail = tail.next; + } + nodeCount++; + } + + /** + * Add value in a specific position + * @param index + * @param value + */ + public void add(int index, E value) { + if(nodeCount == 0) { + // Node count 0 means no node has been added yet. + add(value); + } else { + if(index == 0) { + // node count not zero and we want to add a new node at first. + addFirst(value); + } else if(index >= nodeCount) { + // if index is greater than node count we will add the node + // at the last position. We are not throwing exception + // when the index is greater than the linked list size + addLast(value); + } else { + // For adding a new node we will go before the target node (T-1) + // and create a new node which next node will be the last + // iterated node next (T-1 node's next). + SinglyNode tempNode = head; + + for (int i = 1; i < index; i++) { + tempNode = tempNode.next; + } + + SinglyNode newNode = new SinglyNode(tempNode.next, value); + tempNode.next = newNode; + + nodeCount++; + } + } + } + + public void addFirst(E value) { + if(nodeCount == 0) { + add(value); + } else { + SinglyNode node = new SinglyNode<>(null, value); + SinglyNode tempNodeFirst = head; + head = node; + head.next = tempNodeFirst; + nodeCount++; + } + } + + public void addLast(E value) { + SinglyNode node = new SinglyNode<>(null, value); + tail.next = node; + tail = tail.next; + nodeCount++; + } + + /** + * Return the size of the linked list + * @return + */ + public int size() { + return nodeCount; + } + + public E get(int index) { + + SinglyNode tempNode = head; + int counter = 1; + + if (index == 0) { + return tempNode.getValue(); + } else { + while (!tempNode.isLastNode()) { + counter++; + if (counter == index) { + tempNode = tempNode.getNext(); + break; + } else { + tempNode = tempNode.getNext(); + } + } + return tempNode.getValue(); + } + } + + /** + * Remove value from a specific position of the list + * @param index + */ + public void removeAt(int index) { + if (index < 0 || index >= nodeCount) return; + + if (nodeCount == 1) { + head = null; + tail = null; + } else if (index == 0) { + // That means we are deleting the first node. So need to change + // the head position + head = head.next; + } else { + SinglyNode tempNode = head; + + for (int i = 1; i < index; i++) { + tempNode = tempNode.next; + } + + tempNode.next = tempNode.next.next; + + if (tempNode.next == null) { + // That means we removed the last node. So need to change tail + // position + tail = tempNode; + } + } + nodeCount--; + } + + /** + * Remove a node from the list + * @param item + */ + public void remove(E item) { + if (nodeCount == 0) return; + + SinglyNode tempNode = head; + SinglyNode prevNode = null; + + while (tempNode != null) { + if (tempNode.value == item) { + if (nodeCount == 1) { + // Only one node present and we want to delete that. + head = null; + tail = null; + } else if (prevNode == null) { + // prevNode null means we found the node at the first node of + // the list. so we need to change the head position + head = tempNode.next; + } else { + // so our target node may present at the middle or at the end + prevNode.next = tempNode.next; + // Now if our target node is the last node in that case we need to + // change the last node position as it is deleted + if (prevNode.next == null) { + tail = prevNode; + } + } + nodeCount--; + break; + } else { + prevNode = tempNode; + tempNode = tempNode.next; + } + } + } + + /** + * Print existing linked list value + */ + public void printList() { + if (nodeCount == 0) { + System.out.println("Empty Linked List"); + } else { + SinglyNode tempNode = head; + + while (tempNode != null) { + System.out.println(tempNode.value); + tempNode = tempNode.next; + } + } + } + +} diff --git a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java new file mode 100644 index 0000000..646e755 --- /dev/null +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java @@ -0,0 +1,23 @@ +package linkedlistimplementation; + +public class SinglyNode { + public E value; + public SinglyNode next; + + public SinglyNode(SinglyNode node, E value) { + this.next = node; + this.value = value; + } + + public E getValue() { + return value; + } + + public SinglyNode getNext() { + return next; + } + + public boolean isLastNode() { + return next == null; + } +} diff --git a/LinkedList/RemoveDuplicates/.gitignore b/LinkedList/RemoveDuplicates/.gitignore new file mode 100644 index 0000000..d16077a --- /dev/null +++ b/LinkedList/RemoveDuplicates/.gitignore @@ -0,0 +1,8 @@ +**/nbproject/private/ +**/nbproject/Makefile-*.mk +**/nbproject/Package-*.bash +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/LinkedList/RemoveDuplicates/build.xml b/LinkedList/RemoveDuplicates/build.xml new file mode 100644 index 0000000..bef5c0b --- /dev/null +++ b/LinkedList/RemoveDuplicates/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project RemoveDuplicates. + + + diff --git a/LinkedList/RemoveDuplicates/manifest.mf b/LinkedList/RemoveDuplicates/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/LinkedList/RemoveDuplicates/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/LinkedList/RemoveDuplicates/nbproject/build-impl.xml b/LinkedList/RemoveDuplicates/nbproject/build-impl.xml new file mode 100644 index 0000000..4cc94be --- /dev/null +++ b/LinkedList/RemoveDuplicates/nbproject/build-impl.xml @@ -0,0 +1,1424 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LinkedList/RemoveDuplicates/nbproject/genfiles.properties b/LinkedList/RemoveDuplicates/nbproject/genfiles.properties new file mode 100644 index 0000000..98944b4 --- /dev/null +++ b/LinkedList/RemoveDuplicates/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=2d2358ec +build.xml.script.CRC32=44c922cb +build.xml.stylesheet.CRC32=8064a381@1.80.1.48 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=2d2358ec +nbproject/build-impl.xml.script.CRC32=e24b18fe +nbproject/build-impl.xml.stylesheet.CRC32=830a3534@1.80.1.48 diff --git a/LinkedList/RemoveDuplicates/nbproject/project.properties b/LinkedList/RemoveDuplicates/nbproject/project.properties new file mode 100644 index 0000000..3268103 --- /dev/null +++ b/LinkedList/RemoveDuplicates/nbproject/project.properties @@ -0,0 +1,78 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/RemoveDuplicates.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +file.reference.kotlin-runtime.jar=/Users/souravpalit/Library/Application Support/NetBeans/8.2/kotlinc/lib\\kotlin-runtime.jar +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.external.vm=true +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${kotlinc.classpath}: +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +kotlinc.classpath=${file.reference.kotlin-runtime.jar} +main.class=removeduplicates.RemoveDuplicates +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir}:\ + ${kotlinc.classpath}: +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/LinkedList/RemoveDuplicates/nbproject/project.xml b/LinkedList/RemoveDuplicates/nbproject/project.xml new file mode 100644 index 0000000..c7281ae --- /dev/null +++ b/LinkedList/RemoveDuplicates/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + RemoveDuplicates + + + + + + + + + diff --git a/LinkedList/RemoveDuplicates/src/removeduplicates/RemoveDuplicates.java b/LinkedList/RemoveDuplicates/src/removeduplicates/RemoveDuplicates.java new file mode 100644 index 0000000..b773d33 --- /dev/null +++ b/LinkedList/RemoveDuplicates/src/removeduplicates/RemoveDuplicates.java @@ -0,0 +1,41 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ +package removeduplicates; + +/** + * + * @author souravpalit + */ +public class RemoveDuplicates { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + SinglyLinkedList myLinkedList = new SinglyLinkedList(); + myLinkedList.add(1); + myLinkedList.add(2); + myLinkedList.add(3); + myLinkedList.add(3); + myLinkedList.add(4); + myLinkedList.add(5); + myLinkedList.add(6); + myLinkedList.add(4); + myLinkedList.add(2); + myLinkedList.add(6); + myLinkedList.add(7); + myLinkedList.add(7); + myLinkedList.add(1); + + myLinkedList.printList(); + + myLinkedList.removeDuplicatesNotUsingBuffer(); + System.out.println("After removing duplicates"); + myLinkedList.printList(); + + } + +} diff --git a/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java b/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java new file mode 100644 index 0000000..a3c8de7 --- /dev/null +++ b/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java @@ -0,0 +1,255 @@ +package removeduplicates; + +import java.util.HashMap; +import java.util.HashSet; + +public class SinglyLinkedList { + // Here we need head to traverse the linked list from starting to lastNode + private SinglyNode head; + //Last node will help us to add a new value at the last with out moving the + private SinglyNode tail; + //Node count will track the length of the linked list + private int nodeCount = 0; + + + /** + * Initializing head. First time both head and lastNode will be the same + * @param value + */ + public void add(E value) { + if(head == null) { + SinglyNode node = new SinglyNode<>(null, value); + head = node; + tail = node; + } else { + SinglyNode node = new SinglyNode<>(null, value); + tail.next = node; + tail = tail.next; + } + nodeCount++; + } + + /** + * Add value in a specific position + * @param index + * @param value + */ + public void add(int index, E value) { + if(nodeCount == 0) { + // Node count 0 means no node has been added yet. + add(value); + } else { + if(index == 0) { + // node count not zero and we want to add a new node at first. + addFirst(value); + } else if(index >= nodeCount) { + // if index is greater than node count we will add the node + // at the last position. We are not throwing exception + // when the index is greater than the linked list size + addLast(value); + } else { + // For adding a new node we will go before the target node (T-1) + // and create a new node which next node will be the last + // iterated node next (T-1 node's next). + SinglyNode tempNode = head; + + for (int i = 1; i < index; i++) { + tempNode = tempNode.next; + } + + SinglyNode newNode = new SinglyNode(tempNode.next, value); + tempNode.next = newNode; + + nodeCount++; + } + } + } + + public void addFirst(E value) { + if(nodeCount == 0) { + add(value); + } else { + SinglyNode node = new SinglyNode<>(null, value); + SinglyNode tempNodeFirst = head; + head = node; + head.next = tempNodeFirst; + nodeCount++; + } + } + + public void addLast(E value) { + SinglyNode node = new SinglyNode<>(null, value); + tail.next = node; + tail = tail.next; + nodeCount++; + } + + /** + * Return the size of the linked list + * @return + */ + public int size() { + return nodeCount; + } + + public E get(int index) { + + SinglyNode tempNode = head; + int counter = 1; + + if (index == 0) { + return tempNode.getValue(); + } else { + while (!tempNode.isLastNode()) { + counter++; + if (counter == index) { + tempNode = tempNode.getNext(); + break; + } else { + tempNode = tempNode.getNext(); + } + } + return tempNode.getValue(); + } + } + + /** + * Remove value from a specific position of the list + * @param index + */ + public void removeAt(int index) { + if (index < 0 || index >= nodeCount) return; + + if (nodeCount == 1) { + head = null; + tail = null; + } else if (index == 0) { + // That means we are deleting the first node. So need to change + // the head position + head = head.next; + } else { + SinglyNode tempNode = head; + + for (int i = 1; i < index; i++) { + tempNode = tempNode.next; + } + + tempNode.next = tempNode.next.next; + + if (tempNode.next == null) { + // That means we removed the last node. So need to change tail + // position + tail = tempNode; + } + } + nodeCount--; + } + + /** + * Remove a node from the list + * @param item + */ + public void remove(E item) { + if (nodeCount == 0) return; + + SinglyNode tempNode = head; + SinglyNode prevNode = null; + + while (tempNode != null) { + if (tempNode.value == item) { + if (nodeCount == 1) { + // Only one node present and we want to delete that. + head = null; + tail = null; + } else if (prevNode == null) { + // prevNode null means we found the node at the first node of + // the list. so we need to change the head position + head = tempNode.next; + } else { + // so our target node may present at the middle or at the end + prevNode.next = tempNode.next; + // Now if our target node is the last node in that case we need to + // change the last node position as it is deleted + if (prevNode.next == null) { + tail = prevNode; + } + } + nodeCount--; + break; + } else { + prevNode = tempNode; + tempNode = tempNode.next; + } + } + } + + /** + * The best way to remove duplicate from the linked list is using a map where + * you will insert the visited node and while iterating if you found a value + * already exists in the map then you need to delete that. But here though + * time complexity is O(n) but space complexity is O(n) + */ + public void removeDuplicatesUsingBuffer() { + HashSet map = new HashSet<>(); + + SinglyNode current = head; + SinglyNode prev = null; + + while (current != null) { + + if (map.contains(current.value)) { + // Remove this node + prev.next = current.next; + } else { + map.add(current.value); + prev = current; + } + + current = current.next; + } + + } + + /** + * If you could not use extra memory like map to check where a value is present + * then you need to use to loop like bubble sort. In that case time complexity + * will be O(n^2) but space complexity O(1) + */ + public void removeDuplicatesNotUsingBuffer() { + SinglyNode current = head; + + while (current != null) { + SinglyNode runner = current.next; + SinglyNode prev = current; + + while (runner != null) { + if (current.value == runner.value) { + prev.next = runner.next; + } else { + prev = runner; + } + + runner = runner.next; + } + + current = current.next; + } + } + + /** + * Print existing linked list value + */ + public void printList() { + if (nodeCount == 0) { + System.out.println("Empty Linked List"); + } else { + SinglyNode tempNode = head; + + while (tempNode != null) { + System.out.println(tempNode.value); + tempNode = tempNode.next; + } + } + } + +} diff --git a/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyNode.java b/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyNode.java new file mode 100644 index 0000000..757005a --- /dev/null +++ b/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyNode.java @@ -0,0 +1,23 @@ +package removeduplicates; + +public class SinglyNode { + public E value; + public SinglyNode next; + + public SinglyNode(SinglyNode node, E value) { + this.next = node; + this.value = value; + } + + public E getValue() { + return value; + } + + public SinglyNode getNext() { + return next; + } + + public boolean isLastNode() { + return next == null; + } +} diff --git a/LinkedList/ReverseSinglyLinkedList/build.xml b/LinkedList/ReverseSinglyLinkedList/build.xml new file mode 100644 index 0000000..928ca59 --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project ReverseSinglyLinkedList. + + + diff --git a/LinkedList/ReverseSinglyLinkedList/manifest.mf b/LinkedList/ReverseSinglyLinkedList/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/LinkedList/ReverseSinglyLinkedList/nbproject/build-impl.xml b/LinkedList/ReverseSinglyLinkedList/nbproject/build-impl.xml new file mode 100644 index 0000000..45b162e --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/nbproject/build-impl.xml @@ -0,0 +1,1407 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LinkedList/ReverseSinglyLinkedList/nbproject/genfiles.properties b/LinkedList/ReverseSinglyLinkedList/nbproject/genfiles.properties new file mode 100644 index 0000000..3b134fa --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=0b14902e +build.xml.script.CRC32=de05e794 +build.xml.stylesheet.CRC32=8064a381@1.68.1.46 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=0b14902e +nbproject/build-impl.xml.script.CRC32=268bf36f +nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.68.1.46 diff --git a/LinkedList/ReverseSinglyLinkedList/nbproject/project.properties b/LinkedList/ReverseSinglyLinkedList/nbproject/project.properties new file mode 100644 index 0000000..fd064dd --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/nbproject/project.properties @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/ReverseSinglyLinkedList.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=reversesinglylinkedlist.ReverseSinglyLinkedList +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/LinkedList/ReverseSinglyLinkedList/nbproject/project.xml b/LinkedList/ReverseSinglyLinkedList/nbproject/project.xml new file mode 100644 index 0000000..110e429 --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + ReverseSinglyLinkedList + + + + + + + + + diff --git a/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/ReverseSinglyLinkedList.java b/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/ReverseSinglyLinkedList.java new file mode 100644 index 0000000..f745c7c --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/ReverseSinglyLinkedList.java @@ -0,0 +1,40 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package reversesinglylinkedlist; + +/** + * + * @author mac + */ +public class ReverseSinglyLinkedList { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + SinglyLinkedList linkedList = new SinglyLinkedList(); + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + linkedList.add("4"); + linkedList.add("5"); + + System.out.println("Before Reverse"); + linkedList.display(); + linkedList.reverse(); + System.out.println("After Reverse"); + linkedList.display(); + + System.out.println("After adding a new value 6 and 7"); + + linkedList.add("6"); + linkedList.add("7"); + linkedList.display(); + } + +} diff --git a/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyLinkedList.java b/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyLinkedList.java new file mode 100644 index 0000000..d42e3a0 --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyLinkedList.java @@ -0,0 +1,59 @@ +package reversesinglylinkedlist; + +public class SinglyLinkedList { + // Here we need head to traverse the linked list from starting to lastNode + private SinglyNode head; + //Last node will help us to add a new value at the last with out moving the + private SinglyNode lastNode; + //Node count will track the length of the linked list + private int nodeCount = 0; + + + /** + * Initializing head. First time both head and lastNode will be the same + * @param value + */ + public void add(E value) { + if(head == null) { + SinglyNode node = new SinglyNode<>(null, value); + head = node; + lastNode = node; + } else { + SinglyNode node = new SinglyNode<>(null, value); + lastNode.next = node; + lastNode = lastNode.next; + } + nodeCount++; + } + + + public void reverse() { + + SinglyNode prev = null; + SinglyNode current = head; + SinglyNode next = null; + + // Current head will be the lastNode + lastNode = current; + + while(current != null) { + next = current.next; + current.next = prev; + prev = current; + current = next; + } + head = prev; + } + + + public void display() { + if(head == null) return; + SinglyNode tempNode = head; + + while(tempNode != null) { + System.out.println(tempNode.item); + tempNode = tempNode.getNext(); + } + } + +} diff --git a/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyNode.java b/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyNode.java new file mode 100644 index 0000000..b2e7e32 --- /dev/null +++ b/LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyNode.java @@ -0,0 +1,23 @@ +package reversesinglylinkedlist; + +public class SinglyNode { + public E item; + public SinglyNode next; + + public SinglyNode(SinglyNode node, E value) { + this.next = node; + this.item = value; + } + + public E getItem() { + return item; + } + + public SinglyNode getNext() { + return next; + } + + public boolean isLastNode() { + return next == null; + } +} diff --git a/LinkedList/StackQueueWithSinglyLinkedList/build.xml b/LinkedList/StackQueueWithSinglyLinkedList/build.xml new file mode 100644 index 0000000..72d542f --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project StackQueueWithSinglyLinkedList. + + + diff --git a/LinkedList/StackQueueWithSinglyLinkedList/manifest.mf b/LinkedList/StackQueueWithSinglyLinkedList/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/LinkedList/StackQueueWithSinglyLinkedList/nbproject/build-impl.xml b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/build-impl.xml new file mode 100644 index 0000000..0bd7eba --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/build-impl.xml @@ -0,0 +1,1407 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/LinkedList/StackQueueWithSinglyLinkedList/nbproject/genfiles.properties b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/genfiles.properties new file mode 100644 index 0000000..6539f67 --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=66060602 +build.xml.script.CRC32=0ab09b61 +build.xml.stylesheet.CRC32=8064a381@1.68.1.46 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=66060602 +nbproject/build-impl.xml.script.CRC32=7fa626b6 +nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.68.1.46 diff --git a/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.properties b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.properties new file mode 100644 index 0000000..d780708 --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.properties @@ -0,0 +1,73 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# Files in build.classes.dir which should be excluded from distribution jar +dist.archive.excludes= +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/StackQueueWithSinglyLinkedList.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.8 +javac.target=1.8 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=stackqueuewithsinglylinkedlist.StackWithSinglyLinkedList +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff --git a/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.xml b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.xml new file mode 100644 index 0000000..2773fd8 --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + StackQueueWithSinglyLinkedList + + + + + + + + + diff --git a/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Queue.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Queue.java new file mode 100644 index 0000000..1d6c393 --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Queue.java @@ -0,0 +1,78 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package stackqueuewithsinglylinkedlist; + +import java.util.EmptyStackException; + +/** + * + * @author mac + */ +public class Queue { + // Here we need head to traverse the linked list from starting to lastNode + private SinglyNode head; + //Last node will help us to add a new value at the last with out moving the + private SinglyNode lastNode; + //Node count will track the length of the Queue + private int nodeCount = 0; + + + /** + * Initializing head. First time both head and lastNode will be the same + * @param value + */ + private void add(E value) { + if(head == null) { + SinglyNode node = new SinglyNode<>(null, value); + head = node; + lastNode = node; + } else { + SinglyNode node = new SinglyNode<>(null, value); + lastNode.next = node; + lastNode = lastNode.next; + } + nodeCount++; + } + + public void removeFirst() { + head = head.next; + nodeCount--; + } + + public void push(E value) { + add(value); + } + + public void pop() { + if(nodeCount != 0) { + removeFirst(); + } + } + + public E peek() throws EmptyStackException { + if(nodeCount > 0) { + return head.getItem(); + } else { + + throw new EmptyStackException(); + } + } + + public void display() { + if(head == null) return; + SinglyNode tempNode = head; + + while(!tempNode.isLastNode()) { + System.out.println(tempNode.item); + tempNode = tempNode.getNext(); + } + // Printing the last item + System.out.println(tempNode.item); + } + +} + diff --git a/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/SinglyNode.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/SinglyNode.java new file mode 100644 index 0000000..2c96518 --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/SinglyNode.java @@ -0,0 +1,33 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package stackqueuewithsinglylinkedlist; + +/** + * + * @author SouravPalit + */ +public class SinglyNode { + public E item; + public SinglyNode next; + + public SinglyNode(SinglyNode node, E value) { + this.next = node; + this.item = value; + } + + public E getItem() { + return item; + } + + public SinglyNode getNext() { + return next; + } + + public boolean isLastNode() { + return next == null; + } +} diff --git a/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Stack.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Stack.java new file mode 100644 index 0000000..2e6247b --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Stack.java @@ -0,0 +1,75 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package stackqueuewithsinglylinkedlist; + +import java.util.EmptyStackException; + +/** + * + * @author SouravPalit + */ +public class Stack { + + private SinglyNode head; + //Node count will track the length of the stack + private int nodeCount = 0; + + + /** + * Initializing head. First time both head and lastNode will be the same + * @param value + */ + private void add(E value) { + if(head == null) { + SinglyNode node = new SinglyNode<>(null, value); + head = node; + } else { + SinglyNode newNode = new SinglyNode<>(null, value); + SinglyNode tempNode = head; + newNode.next = head; + head = newNode; + } + nodeCount++; + } + + public void removeFirst() { + head = head.next; + nodeCount--; + } + + public void push(E value) { + add(value); + } + + public void pop() { + if(nodeCount != 0) { + removeFirst(); + } + } + + public E peek() throws EmptyStackException { + if(nodeCount > 0) { + return head.getItem(); + } else { + + throw new EmptyStackException(); + } + } + + public void display() { + if(head == null) return; + SinglyNode tempNode = head; + + while(!tempNode.isLastNode()) { + System.out.println(tempNode.item); + tempNode = tempNode.getNext(); + } + // Printing the last item + System.out.println(tempNode.item); + } + +} diff --git a/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/StackWithSinglyLinkedList.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/StackWithSinglyLinkedList.java new file mode 100644 index 0000000..1321037 --- /dev/null +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/StackWithSinglyLinkedList.java @@ -0,0 +1,60 @@ +/* + * To change this license header, choose License Headers in Project Properties. + * To change this template file, choose Tools | Templates + * and open the template in the editor. + */ + +package stackqueuewithsinglylinkedlist; + +/** + * + * @author mac + */ +public class StackWithSinglyLinkedList { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + + System.out.println("Printing Queue Values......"); + + Queue myQueue = new Queue(); + + myQueue.push("1"); + myQueue.push("2"); + myQueue.push("3"); + myQueue.push("4"); + myQueue.push("5"); + + myQueue.pop(); + + String value = myQueue.peek(); + + System.out.println("Peek Value is = " + value); + + myQueue.display(); + + + System.out.println("Printing Stack Values....."); + + Stack myStack = new Stack(); + + myStack.push("1"); + myStack.push("2"); + myStack.push("3"); + myStack.push("4"); + myStack.push("5"); + + myStack.pop(); + + value = myStack.peek(); + + System.out.println("Peek Value is = " + value); + + myStack.display(); + + } + +}