From ee59e43d33f3384a052557a6cb1e36985067ecb7 Mon Sep 17 00:00:00 2001 From: souravpalitrana Date: Wed, 16 Jan 2019 02:02:08 +0600 Subject: [PATCH 1/9] Singly linked list implemented from scratch with basic add remove get functions --- LinkedList/.gitignore | 8 + LinkedList/LinkedListImplementation/build.xml | 73 + .../LinkedListImplementation/manifest.mf | 3 + .../nbproject/build-impl.xml | 1407 +++++++++++++++++ .../nbproject/genfiles.properties | 8 + .../nbproject/project.properties | 73 + .../nbproject/project.xml | 15 + .../LinkedListImplementation.java | 60 + .../SinglyLinkedList.java | 161 ++ .../linkedlistimplementation/SinglyNode.java | 23 + 10 files changed, 1831 insertions(+) create mode 100644 LinkedList/.gitignore create mode 100644 LinkedList/LinkedListImplementation/build.xml create mode 100644 LinkedList/LinkedListImplementation/manifest.mf create mode 100644 LinkedList/LinkedListImplementation/nbproject/build-impl.xml create mode 100644 LinkedList/LinkedListImplementation/nbproject/genfiles.properties create mode 100644 LinkedList/LinkedListImplementation/nbproject/project.properties create mode 100644 LinkedList/LinkedListImplementation/nbproject/project.xml create mode 100644 LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java create mode 100644 LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java create mode 100644 LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java 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/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..de83286 --- /dev/null +++ b/LinkedList/LinkedListImplementation/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/LinkedListImplementation/nbproject/genfiles.properties b/LinkedList/LinkedListImplementation/nbproject/genfiles.properties new file mode 100644 index 0000000..5047e0d --- /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=b4c9c6dc +nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.68.1.46 diff --git a/LinkedList/LinkedListImplementation/nbproject/project.properties b/LinkedList/LinkedListImplementation/nbproject/project.properties new file mode 100644 index 0000000..549fb45 --- /dev/null +++ b/LinkedList/LinkedListImplementation/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}/LinkedListImplementation.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=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} +# 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..653fda4 --- /dev/null +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.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 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 linkedList = new SinglyLinkedList(); + linkedList.add("1"); + linkedList.add("2"); + linkedList.add("3"); + linkedList.add("4"); + linkedList.add("5"); + + String value = linkedList.get(2); + System.out.println("The value is = " + value); + + linkedList.add(2,"6"); + value = linkedList.get(2); + System.out.println("The value is = " + value); + + linkedList.add(0,"7"); + value = linkedList.get(2); + System.out.println("The value is = " + value); + + linkedList.add(linkedList.size(), "8"); + value = linkedList.get(linkedList.size()); + System.out.println("The value is = " + value); + + linkedList.removeLast(); + value = linkedList.get(linkedList.size()); + System.out.println("The value is = " + value); + + linkedList.removeFirst(); + value = linkedList.get(0); + System.out.println("The value is = " + value); + + linkedList.remove(2); + value = linkedList.get(2); + System.out.println("The value is = " + value); + + } + +} diff --git a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java new file mode 100644 index 0000000..388d3f0 --- /dev/null +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java @@ -0,0 +1,161 @@ +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 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 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 and + // will set this node's next to new node and set the index node + // to new node's next + SinglyNode tempNode = head; + int counter = 1; + int LIMIT = index - 1; + while (counter <= LIMIT) { + if(counter == LIMIT) { + SinglyNode nextNode = tempNode.next; + SinglyNode newNode = new SinglyNode<>(nextNode, value); + tempNode.next = newNode; + } else { + tempNode = tempNode.next; + } + + counter++; + + } + 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); + lastNode.next = node; + lastNode = lastNode.next; + nodeCount++; + } + + public int size() { + return nodeCount; + } + + public E get(int index) { + + SinglyNode tempNode = head; + int counter = 1; + + if(index == 0) { + return tempNode.getItem(); + } else { + while (!tempNode.isLastNode()) { + counter++; + if(counter == index) { + tempNode = tempNode.getNext(); + break; + } else { + tempNode = tempNode.getNext(); + } + } + return tempNode.getItem(); + } + } + + public void removeFirst() { + head = head.next; + nodeCount--; + } + + public void removeLast() { + SinglyNode tempNode = head; + int counter = 1; + while (counter < nodeCount) { + if(counter == nodeCount - 1) { + tempNode.next = null; + break; + + } else { + tempNode = tempNode.next; + } + counter++; + + } + nodeCount--; + } + + public void remove(int index) { + if(nodeCount == 1 && index == 0) { + removeFirst(); + } else if(nodeCount == index) { + removeLast(); + } else { + SinglyNode tempNode = head; + int counter = 1; + while (counter < index) { + if(counter == index - 1) { + SinglyNode deleteNode = tempNode.next; + + tempNode.next = deleteNode.next; + break; + + } else { + tempNode = tempNode.next; + } + + counter++; + } + nodeCount--; + + } + } + + +} diff --git a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java new file mode 100644 index 0000000..c3d2c16 --- /dev/null +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java @@ -0,0 +1,23 @@ +package linkedlistimplementation; + +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; + } +} From 56f340dff18c310a9a8e6e1fe14f9862b90f5b54 Mon Sep 17 00:00:00 2001 From: souravpalitrana Date: Wed, 16 Jan 2019 02:29:07 +0600 Subject: [PATCH 2/9] Implemented basic stack with linked list --- .../StackWithSinglyLinkedList/build.xml | 73 + .../StackWithSinglyLinkedList/manifest.mf | 3 + .../nbproject/build-impl.xml | 1407 +++++++++++++++++ .../nbproject/genfiles.properties | 8 + .../nbproject/project.properties | 73 + .../nbproject/project.xml | 15 + .../stackwithsinglylinkedlist/SinglyNode.java | 33 + .../src/stackwithsinglylinkedlist/Stack.java | 77 + .../StackWithSinglyLinkedList.java | 39 + 9 files changed, 1728 insertions(+) create mode 100644 LinkedList/StackWithSinglyLinkedList/build.xml create mode 100644 LinkedList/StackWithSinglyLinkedList/manifest.mf create mode 100644 LinkedList/StackWithSinglyLinkedList/nbproject/build-impl.xml create mode 100644 LinkedList/StackWithSinglyLinkedList/nbproject/genfiles.properties create mode 100644 LinkedList/StackWithSinglyLinkedList/nbproject/project.properties create mode 100644 LinkedList/StackWithSinglyLinkedList/nbproject/project.xml create mode 100644 LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/SinglyNode.java create mode 100644 LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java create mode 100644 LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java diff --git a/LinkedList/StackWithSinglyLinkedList/build.xml b/LinkedList/StackWithSinglyLinkedList/build.xml new file mode 100644 index 0000000..1cff725 --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/build.xml @@ -0,0 +1,73 @@ + + + + + + + + + + + Builds, tests, and runs the project StackWithSinglyLinkedList. + + + diff --git a/LinkedList/StackWithSinglyLinkedList/manifest.mf b/LinkedList/StackWithSinglyLinkedList/manifest.mf new file mode 100644 index 0000000..328e8e5 --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/manifest.mf @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff --git a/LinkedList/StackWithSinglyLinkedList/nbproject/build-impl.xml b/LinkedList/StackWithSinglyLinkedList/nbproject/build-impl.xml new file mode 100644 index 0000000..3174777 --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/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/StackWithSinglyLinkedList/nbproject/genfiles.properties b/LinkedList/StackWithSinglyLinkedList/nbproject/genfiles.properties new file mode 100644 index 0000000..7cdfc98 --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/nbproject/genfiles.properties @@ -0,0 +1,8 @@ +build.xml.data.CRC32=c22e9069 +build.xml.script.CRC32=cde96998 +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=c22e9069 +nbproject/build-impl.xml.script.CRC32=c1a4c346 +nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.68.1.46 diff --git a/LinkedList/StackWithSinglyLinkedList/nbproject/project.properties b/LinkedList/StackWithSinglyLinkedList/nbproject/project.properties new file mode 100644 index 0000000..1f8a3be --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/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}/StackWithSinglyLinkedList.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=stackwithsinglylinkedlist.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/StackWithSinglyLinkedList/nbproject/project.xml b/LinkedList/StackWithSinglyLinkedList/nbproject/project.xml new file mode 100644 index 0000000..52ed1ac --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/nbproject/project.xml @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + StackWithSinglyLinkedList + + + + + + + + + diff --git a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/SinglyNode.java b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/SinglyNode.java new file mode 100644 index 0000000..555ba57 --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/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 stackwithsinglylinkedlist; + +/** + * + * @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/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java new file mode 100644 index 0000000..3587b93 --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java @@ -0,0 +1,77 @@ +/* + * 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 stackwithsinglylinkedlist; + +import java.util.EmptyStackException; + +/** + * + * @author SouravPalit + */ +public class Stack { + // 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 + */ + 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/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java new file mode 100644 index 0000000..0bfb717 --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java @@ -0,0 +1,39 @@ +/* + * 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 stackwithsinglylinkedlist; + +/** + * + * @author mac + */ +public class StackWithSinglyLinkedList { + + /** + * @param args the command line arguments + */ + public static void main(String[] args) { + // TODO code application logic here + + Stack myStack = new Stack(); + + myStack.push("1"); + myStack.push("2"); + myStack.push("3"); + myStack.push("4"); + myStack.push("5"); + + myStack.pop(); + + String value = myStack.peek(); + + System.out.println("Peek Value is = " + value); + + myStack.display(); + + } + +} From be7d9eed7bea284479975e6defb48246a95f920b Mon Sep 17 00:00:00 2001 From: souravpalitrana Date: Wed, 16 Jan 2019 02:42:09 +0600 Subject: [PATCH 3/9] Previous stack implementation bug fixed and queue implemented with singly linked list --- .../src/stackwithsinglylinkedlist/Queue.java | 78 +++++++++++++++++++ .../src/stackwithsinglylinkedlist/Stack.java | 14 ++-- .../StackWithSinglyLinkedList.java | 23 +++++- 3 files changed, 106 insertions(+), 9 deletions(-) create mode 100644 LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Queue.java diff --git a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Queue.java b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Queue.java new file mode 100644 index 0000000..f20271b --- /dev/null +++ b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/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 stackwithsinglylinkedlist; + +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/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java index 3587b93..c6b27c0 100644 --- a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java +++ b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java @@ -13,11 +13,9 @@ * @author SouravPalit */ public class Stack { - // 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 + //Node count will track the length of the stack private int nodeCount = 0; @@ -29,11 +27,11 @@ 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; + SinglyNode newNode = new SinglyNode<>(null, value); + SinglyNode tempNode = head; + newNode.next = head; + head = newNode; } nodeCount++; } diff --git a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java index 0bfb717..8cb2f48 100644 --- a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java +++ b/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java @@ -18,6 +18,27 @@ public class StackWithSinglyLinkedList { 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"); @@ -28,7 +49,7 @@ public static void main(String[] args) { myStack.pop(); - String value = myStack.peek(); + value = myStack.peek(); System.out.println("Peek Value is = " + value); From 8b38760bc5761ca590038fc2752c443953944da2 Mon Sep 17 00:00:00 2001 From: souravpalitrana Date: Wed, 16 Jan 2019 02:45:15 +0600 Subject: [PATCH 4/9] Project folder name changed as queue also implemented --- .../build.xml | 6 +++--- .../manifest.mf | 0 .../nbproject/build-impl.xml | 10 +++++----- .../nbproject/genfiles.properties | 8 ++++---- .../nbproject/project.properties | 4 ++-- .../nbproject/project.xml | 2 +- .../src/stackqueuewithsinglylinkedlist}/Queue.java | 2 +- .../stackqueuewithsinglylinkedlist}/SinglyNode.java | 2 +- .../src/stackqueuewithsinglylinkedlist}/Stack.java | 2 +- .../StackWithSinglyLinkedList.java | 2 +- 10 files changed, 19 insertions(+), 19 deletions(-) rename LinkedList/{StackWithSinglyLinkedList => StackQueueWithSinglyLinkedList}/build.xml (92%) rename LinkedList/{StackWithSinglyLinkedList => StackQueueWithSinglyLinkedList}/manifest.mf (100%) rename LinkedList/{StackWithSinglyLinkedList => StackQueueWithSinglyLinkedList}/nbproject/build-impl.xml (99%) rename LinkedList/{StackWithSinglyLinkedList => StackQueueWithSinglyLinkedList}/nbproject/genfiles.properties (67%) rename LinkedList/{StackWithSinglyLinkedList => StackQueueWithSinglyLinkedList}/nbproject/project.properties (94%) rename LinkedList/{StackWithSinglyLinkedList => StackQueueWithSinglyLinkedList}/nbproject/project.xml (89%) rename LinkedList/{StackWithSinglyLinkedList/src/stackwithsinglylinkedlist => StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist}/Queue.java (97%) rename LinkedList/{StackWithSinglyLinkedList/src/stackwithsinglylinkedlist => StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist}/SinglyNode.java (93%) rename LinkedList/{StackWithSinglyLinkedList/src/stackwithsinglylinkedlist => StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist}/Stack.java (97%) rename LinkedList/{StackWithSinglyLinkedList/src/stackwithsinglylinkedlist => StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist}/StackWithSinglyLinkedList.java (97%) diff --git a/LinkedList/StackWithSinglyLinkedList/build.xml b/LinkedList/StackQueueWithSinglyLinkedList/build.xml similarity index 92% rename from LinkedList/StackWithSinglyLinkedList/build.xml rename to LinkedList/StackQueueWithSinglyLinkedList/build.xml index 1cff725..72d542f 100644 --- a/LinkedList/StackWithSinglyLinkedList/build.xml +++ b/LinkedList/StackQueueWithSinglyLinkedList/build.xml @@ -7,8 +7,8 @@ - - Builds, tests, and runs the project StackWithSinglyLinkedList. + + Builds, tests, and runs the project StackQueueWithSinglyLinkedList. - + @@ -440,7 +440,7 @@ is divided into following sections: - + @@ -591,7 +591,7 @@ is divided into following sections: - + @@ -883,7 +883,7 @@ is divided into following sections: - + @@ -1365,7 +1365,7 @@ is divided into following sections: - + diff --git a/LinkedList/StackWithSinglyLinkedList/nbproject/genfiles.properties b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/genfiles.properties similarity index 67% rename from LinkedList/StackWithSinglyLinkedList/nbproject/genfiles.properties rename to LinkedList/StackQueueWithSinglyLinkedList/nbproject/genfiles.properties index 7cdfc98..6539f67 100644 --- a/LinkedList/StackWithSinglyLinkedList/nbproject/genfiles.properties +++ b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/genfiles.properties @@ -1,8 +1,8 @@ -build.xml.data.CRC32=c22e9069 -build.xml.script.CRC32=cde96998 +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=c22e9069 -nbproject/build-impl.xml.script.CRC32=c1a4c346 +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/StackWithSinglyLinkedList/nbproject/project.properties b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.properties similarity index 94% rename from LinkedList/StackWithSinglyLinkedList/nbproject/project.properties rename to LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.properties index 1f8a3be..d780708 100644 --- a/LinkedList/StackWithSinglyLinkedList/nbproject/project.properties +++ b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.properties @@ -24,7 +24,7 @@ debug.test.classpath=\ dist.archive.excludes= # This directory is removed when the project is cleaned: dist.dir=dist -dist.jar=${dist.dir}/StackWithSinglyLinkedList.jar +dist.jar=${dist.dir}/StackQueueWithSinglyLinkedList.jar dist.javadoc.dir=${dist.dir}/javadoc excludes= includes=** @@ -53,7 +53,7 @@ javadoc.splitindex=true javadoc.use=true javadoc.version=false javadoc.windowtitle= -main.class=stackwithsinglylinkedlist.StackWithSinglyLinkedList +main.class=stackqueuewithsinglylinkedlist.StackWithSinglyLinkedList manifest.file=manifest.mf meta.inf.dir=${src.dir}/META-INF mkdist.disabled=false diff --git a/LinkedList/StackWithSinglyLinkedList/nbproject/project.xml b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.xml similarity index 89% rename from LinkedList/StackWithSinglyLinkedList/nbproject/project.xml rename to LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.xml index 52ed1ac..2773fd8 100644 --- a/LinkedList/StackWithSinglyLinkedList/nbproject/project.xml +++ b/LinkedList/StackQueueWithSinglyLinkedList/nbproject/project.xml @@ -3,7 +3,7 @@ org.netbeans.modules.java.j2seproject - StackWithSinglyLinkedList + StackQueueWithSinglyLinkedList diff --git a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Queue.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Queue.java similarity index 97% rename from LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Queue.java rename to LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Queue.java index f20271b..1d6c393 100644 --- a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Queue.java +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Queue.java @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package stackwithsinglylinkedlist; +package stackqueuewithsinglylinkedlist; import java.util.EmptyStackException; diff --git a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/SinglyNode.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/SinglyNode.java similarity index 93% rename from LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/SinglyNode.java rename to LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/SinglyNode.java index 555ba57..2c96518 100644 --- a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/SinglyNode.java +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/SinglyNode.java @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package stackwithsinglylinkedlist; +package stackqueuewithsinglylinkedlist; /** * diff --git a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Stack.java similarity index 97% rename from LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java rename to LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Stack.java index c6b27c0..2e6247b 100644 --- a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/Stack.java +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/Stack.java @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package stackwithsinglylinkedlist; +package stackqueuewithsinglylinkedlist; import java.util.EmptyStackException; diff --git a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/StackWithSinglyLinkedList.java similarity index 97% rename from LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java rename to LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/StackWithSinglyLinkedList.java index 8cb2f48..1321037 100644 --- a/LinkedList/StackWithSinglyLinkedList/src/stackwithsinglylinkedlist/StackWithSinglyLinkedList.java +++ b/LinkedList/StackQueueWithSinglyLinkedList/src/stackqueuewithsinglylinkedlist/StackWithSinglyLinkedList.java @@ -4,7 +4,7 @@ * and open the template in the editor. */ -package stackwithsinglylinkedlist; +package stackqueuewithsinglylinkedlist; /** * From 9e75078b900fec4769d1b4ca43d81fa8e0678d93 Mon Sep 17 00:00:00 2001 From: souravpalitrana Date: Thu, 24 Jan 2019 01:42:40 +0600 Subject: [PATCH 5/9] Reversing linked list implemented --- LinkedList/ReverseSinglyLinkedList/build.xml | 73 + .../ReverseSinglyLinkedList/manifest.mf | 3 + .../nbproject/build-impl.xml | 1407 +++++++++++++++++ .../nbproject/genfiles.properties | 8 + .../nbproject/project.properties | 73 + .../nbproject/project.xml | 15 + .../ReverseSinglyLinkedList.java | 40 + .../SinglyLinkedList.java | 59 + .../reversesinglylinkedlist/SinglyNode.java | 23 + 9 files changed, 1701 insertions(+) create mode 100644 LinkedList/ReverseSinglyLinkedList/build.xml create mode 100644 LinkedList/ReverseSinglyLinkedList/manifest.mf create mode 100644 LinkedList/ReverseSinglyLinkedList/nbproject/build-impl.xml create mode 100644 LinkedList/ReverseSinglyLinkedList/nbproject/genfiles.properties create mode 100644 LinkedList/ReverseSinglyLinkedList/nbproject/project.properties create mode 100644 LinkedList/ReverseSinglyLinkedList/nbproject/project.xml create mode 100644 LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/ReverseSinglyLinkedList.java create mode 100644 LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyLinkedList.java create mode 100644 LinkedList/ReverseSinglyLinkedList/src/reversesinglylinkedlist/SinglyNode.java 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; + } +} From 39a42e8c783bfd4b6da96ed9bc9cecbbf2bd1e0b Mon Sep 17 00:00:00 2001 From: Sourav Palit Date: Sun, 16 Feb 2020 13:14:56 +0530 Subject: [PATCH 6/9] Add removeAt function and fix existing bug --- .../LinkedListImplementation.java | 88 +++++---- .../SinglyLinkedList.java | 176 +++++++++++------- .../linkedlistimplementation/SinglyNode.java | 8 +- 3 files changed, 166 insertions(+), 106 deletions(-) diff --git a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java index 653fda4..296ecb4 100644 --- a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java @@ -21,39 +21,61 @@ public class LinkedListImplementation { 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"); - - String value = linkedList.get(2); - System.out.println("The value is = " + value); - - linkedList.add(2,"6"); - value = linkedList.get(2); - System.out.println("The value is = " + value); - - linkedList.add(0,"7"); - value = linkedList.get(2); - System.out.println("The value is = " + value); - - linkedList.add(linkedList.size(), "8"); - value = linkedList.get(linkedList.size()); - System.out.println("The value is = " + value); - - linkedList.removeLast(); - value = linkedList.get(linkedList.size()); - System.out.println("The value is = " + value); - - linkedList.removeFirst(); - value = linkedList.get(0); - System.out.println("The value is = " + value); - - linkedList.remove(2); - value = linkedList.get(2); - System.out.println("The value is = " + value); + 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 index 388d3f0..43e5e5a 100644 --- a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyLinkedList.java @@ -4,7 +4,7 @@ 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; + private SinglyNode tail; //Node count will track the length of the linked list private int nodeCount = 0; @@ -17,15 +17,20 @@ public void add(E value) { if(head == null) { SinglyNode node = new SinglyNode<>(null, value); head = node; - lastNode = node; + tail = node; } else { SinglyNode node = new SinglyNode<>(null, value); - lastNode.next = node; - lastNode = lastNode.next; + 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. @@ -34,34 +39,26 @@ public void add(int index, E value) { if(index == 0) { // node count not zero and we want to add a new node at first. addFirst(value); - } else if(index > nodeCount) { + } 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 and - // will set this node's next to new node and set the index node - // to new node's next + // 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; - int counter = 1; - int LIMIT = index - 1; - while (counter <= LIMIT) { - if(counter == LIMIT) { - SinglyNode nextNode = tempNode.next; - SinglyNode newNode = new SinglyNode<>(nextNode, value); - tempNode.next = newNode; - } else { - tempNode = tempNode.next; - } - - counter++; - + + for (int i = 1; i < index; i++) { + tempNode = tempNode.next; } + + SinglyNode newNode = new SinglyNode(tempNode.next, value); + tempNode.next = newNode; + nodeCount++; } - - } } @@ -79,11 +76,15 @@ public void addFirst(E value) { public void addLast(E value) { SinglyNode node = new SinglyNode<>(null, value); - lastNode.next = node; - lastNode = lastNode.next; + tail.next = node; + tail = tail.next; nodeCount++; } + /** + * Return the size of the linked list + * @return + */ public int size() { return nodeCount; } @@ -93,69 +94,106 @@ public E get(int index) { SinglyNode tempNode = head; int counter = 1; - if(index == 0) { - return tempNode.getItem(); + if (index == 0) { + return tempNode.getValue(); } else { while (!tempNode.isLastNode()) { counter++; - if(counter == index) { + if (counter == index) { tempNode = tempNode.getNext(); break; } else { tempNode = tempNode.getNext(); } } - return tempNode.getItem(); + return tempNode.getValue(); } } - public void removeFirst() { - head = head.next; - nodeCount--; - } - - public void removeLast() { - SinglyNode tempNode = head; - int counter = 1; - while (counter < nodeCount) { - if(counter == nodeCount - 1) { - tempNode.next = null; - break; - - } else { + /** + * 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; } - counter++; - + + 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--; + nodeCount--; } - - public void remove(int index) { - if(nodeCount == 1 && index == 0) { - removeFirst(); - } else if(nodeCount == index) { - removeLast(); - } else { - SinglyNode tempNode = head; - int counter = 1; - while (counter < index) { - if(counter == index - 1) { - SinglyNode deleteNode = tempNode.next; - - tempNode.next = deleteNode.next; - break; - + + /** + * 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 { - tempNode = tempNode.next; + // 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; + } } - - counter++; - } - nodeCount--; - + 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 index c3d2c16..646e755 100644 --- a/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java +++ b/LinkedList/LinkedListImplementation/src/linkedlistimplementation/SinglyNode.java @@ -1,16 +1,16 @@ package linkedlistimplementation; public class SinglyNode { - public E item; + public E value; public SinglyNode next; public SinglyNode(SinglyNode node, E value) { this.next = node; - this.item = value; + this.value = value; } - public E getItem() { - return item; + public E getValue() { + return value; } public SinglyNode getNext() { From ccc879e122a5b82b74e9ccd2e29f980e5f69cc01 Mon Sep 17 00:00:00 2001 From: Sourav Palit Date: Sun, 16 Feb 2020 21:29:25 +0530 Subject: [PATCH 7/9] Add solution for removing duplicate from linked list --- .gitignore | 1 + .../LinkedListImplementation/.gitignore | 8 + .../nbproject/build-impl.xml | 2701 +++++++++-------- .../nbproject/genfiles.properties | 4 +- .../nbproject/project.properties | 8 +- LinkedList/RemoveDuplicates/.gitignore | 8 + LinkedList/RemoveDuplicates/build.xml | 73 + LinkedList/RemoveDuplicates/manifest.mf | 3 + .../RemoveDuplicates/nbproject/build-impl.xml | 1424 +++++++++ .../nbproject/genfiles.properties | 8 + .../nbproject/project.properties | 78 + .../RemoveDuplicates/nbproject/project.xml | 15 + .../removeduplicates/RemoveDuplicates.java | 41 + .../removeduplicates/SinglyLinkedList.java | 244 ++ .../src/removeduplicates/SinglyNode.java | 23 + 15 files changed, 3293 insertions(+), 1346 deletions(-) create mode 100644 .gitignore create mode 100644 LinkedList/LinkedListImplementation/.gitignore create mode 100644 LinkedList/RemoveDuplicates/.gitignore create mode 100644 LinkedList/RemoveDuplicates/build.xml create mode 100644 LinkedList/RemoveDuplicates/manifest.mf create mode 100644 LinkedList/RemoveDuplicates/nbproject/build-impl.xml create mode 100644 LinkedList/RemoveDuplicates/nbproject/genfiles.properties create mode 100644 LinkedList/RemoveDuplicates/nbproject/project.properties create mode 100644 LinkedList/RemoveDuplicates/nbproject/project.xml create mode 100644 LinkedList/RemoveDuplicates/src/removeduplicates/RemoveDuplicates.java create mode 100644 LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java create mode 100644 LinkedList/RemoveDuplicates/src/removeduplicates/SinglyNode.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ca76493 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/RemoveDuplicates/nbproject/private/ \ 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/nbproject/build-impl.xml b/LinkedList/LinkedListImplementation/nbproject/build-impl.xml index de83286..a910641 100644 --- a/LinkedList/LinkedListImplementation/nbproject/build-impl.xml +++ b/LinkedList/LinkedListImplementation/nbproject/build-impl.xml @@ -1,4 +1,5 @@ + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 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 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 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 - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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}" - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 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 - - - - - - - + + + + + + + + + + + + + + + + + + + + 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. - - - - - - - - - - - - - - - - - - - - - - + + 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 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 - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + 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. - - - + + + + + + + + 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 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 + + + + + + + - - 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 index 5047e0d..423d7be 100644 --- a/LinkedList/LinkedListImplementation/nbproject/genfiles.properties +++ b/LinkedList/LinkedListImplementation/nbproject/genfiles.properties @@ -4,5 +4,5 @@ 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=b4c9c6dc -nbproject/build-impl.xml.stylesheet.CRC32=5a01deb7@1.68.1.46 +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 index 549fb45..67c0170 100644 --- a/LinkedList/LinkedListImplementation/nbproject/project.properties +++ b/LinkedList/LinkedListImplementation/nbproject/project.properties @@ -27,6 +27,7 @@ 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= @@ -39,7 +40,8 @@ javac.source=1.8 javac.target=1.8 javac.test.classpath=\ ${javac.classpath}:\ - ${build.classes.dir} + ${build.classes.dir}:\ + ${kotlinc.classpath}: javac.test.processorpath=\ ${javac.test.classpath} javadoc.additionalparam= @@ -53,6 +55,7 @@ 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 @@ -60,7 +63,8 @@ mkdist.disabled=false platform.active=default_platform run.classpath=\ ${javac.classpath}:\ - ${build.classes.dir} + ${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: 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..29631a5 --- /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..061083a --- /dev/null +++ b/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java @@ -0,0 +1,244 @@ +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; + } + } + } + + 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; + } + + } + + 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; + } +} From ea6731a1af7bd5bbd41aceb88c54d44109ffd6c1 Mon Sep 17 00:00:00 2001 From: Sourav Palit Date: Sun, 16 Feb 2020 21:39:07 +0530 Subject: [PATCH 8/9] Add the complexity description --- .../src/removeduplicates/SinglyLinkedList.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java b/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java index 061083a..a3c8de7 100644 --- a/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java +++ b/LinkedList/RemoveDuplicates/src/removeduplicates/SinglyLinkedList.java @@ -183,6 +183,12 @@ public void remove(E item) { } } + /** + * 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<>(); @@ -204,6 +210,11 @@ public void removeDuplicatesUsingBuffer() { } + /** + * 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; From a8ea6f54020a247d9ef4716a6d0e1d632ecaf470 Mon Sep 17 00:00:00 2001 From: Sourav Palit Date: Thu, 16 Jul 2020 11:22:46 +0600 Subject: [PATCH 9/9] Add git ignore --- .gitignore | 4 +++- LinkedList/RemoveDuplicates/nbproject/project.properties | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index ca76493..cff400b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -/RemoveDuplicates/nbproject/private/ \ No newline at end of file +/RemoveDuplicates/nbproject/private/ +/RecursionProblems/nbproject/private/ +/RecursionProblems/build/ \ No newline at end of file diff --git a/LinkedList/RemoveDuplicates/nbproject/project.properties b/LinkedList/RemoveDuplicates/nbproject/project.properties index 29631a5..3268103 100644 --- a/LinkedList/RemoveDuplicates/nbproject/project.properties +++ b/LinkedList/RemoveDuplicates/nbproject/project.properties @@ -42,7 +42,7 @@ javac.target=1.8 javac.test.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ - ${kotlinc.classpath} + ${kotlinc.classpath}: javac.test.processorpath=\ ${javac.test.classpath} javadoc.additionalparam= @@ -65,7 +65,7 @@ platform.active=default_platform run.classpath=\ ${javac.classpath}:\ ${build.classes.dir}:\ - ${kotlinc.classpath} + ${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: