From c8ee96d9d5a13cb01eef5ec710f190cdd9c77e2a Mon Sep 17 00:00:00 2001 From: asri71 Date: Tue, 5 Mar 2019 15:59:13 +0530 Subject: [PATCH 1/6] Adding Linked List based General queue implementation --- .../java/com/dataStructures/GeneralQueue.java | 142 ++++++++++++++++++ src/main/java/com/types/DataStructure.java | 31 ++++ src/main/java/com/types/Queue.java | 25 +++ .../com/dataStructures/GeneralQueueTest.java | 45 ++++++ 4 files changed, 243 insertions(+) create mode 100644 src/main/java/com/dataStructures/GeneralQueue.java create mode 100644 src/main/java/com/types/DataStructure.java create mode 100644 src/main/java/com/types/Queue.java create mode 100644 src/test/java/com/dataStructures/GeneralQueueTest.java diff --git a/src/main/java/com/dataStructures/GeneralQueue.java b/src/main/java/com/dataStructures/GeneralQueue.java new file mode 100644 index 000000000000..ebb92302d0c3 --- /dev/null +++ b/src/main/java/com/dataStructures/GeneralQueue.java @@ -0,0 +1,142 @@ +package src.main.java.com.dataStructures; + +import src.main.java.com.types.Queue; + +import java.util.Iterator; +import java.util.LinkedList; + +/** + * linkedList based implementation of queue. + * This implementation is not thread safe and need exclusive thread safety measures from the client. + * @param + */ +public class GeneralQueue implements Queue { + + private LinkedList queue; + private Iterator itr; + + //Overloaded constructor to create queue of specific size + public GeneralQueue() { + queue = new LinkedList<>(); + } + + @Override + public boolean add(T t) { + + if(queue == null) { + throw new NullPointerException(); + } + + queue.add(t); + return true; + } + + @Override + public boolean remove(T t) { + if(null == queue || queue.size() == 0){ + throw new NullPointerException(); + } + queue.remove(t); + return true; + } + + @Override + public boolean isEmpty() { + + if(null == queue || queue.size() == 0){ + return true; + } + + return false; + } + + @Override + public Iterator iterator() { + + if(queue == null) { + return null; + } + itr = queue.iterator(); + return itr; + } + + @Override + public boolean offer(T t) { + if(null == queue) { + return false; + } + + queue.add(t); + return true; + } + + @Override + public T poll() { + + if(queue == null || queue.isEmpty()){ + return null; + } + + return queue.pollFirst(); + } + + @Override + public T element() { + + if(queue == null || queue.isEmpty()) { + throw new NullPointerException(); + } + + return queue.peekFirst(); + } + + @Override + public T peek() { + if(null == queue || queue.size() == 0){ + return null; + } + + return queue.peekFirst(); + } + + @Override + public boolean hasNext() { + + if(itr.hasNext()){ + return true; + } + return false; + } + + @Override + public T next() { + + return itr.next(); + } + + @Override + public Object[] toArray() { + + Object[] elements = {}; + if(null == queue || queue.isEmpty()){ + return elements; + } + elements = new Object[queue.size()]; + for(int i=0;i + */ +public interface DataStructure extends Iterator { + + //Method to add element in the structure + public boolean add(T t); + + //Method to remove the given object from structure + public boolean remove(T o); + + //Method to get Iterator to parse on the given structure + public Iterator iterator(); + + //Method to check if structure is empty + public boolean isEmpty(); + + //Method to get all the elements of data structure in array + public Object[] toArray(); + + //Method to get the size or number of elements in structure + public int size(); + +} diff --git a/src/main/java/com/types/Queue.java b/src/main/java/com/types/Queue.java new file mode 100644 index 000000000000..a247b348a333 --- /dev/null +++ b/src/main/java/com/types/Queue.java @@ -0,0 +1,25 @@ +package src.main.java.com.types; + + +/** + * Interface to provide queue specific functionality to the implementing class + * This interface only defines the functionality which the queue implementing classes require. + * Any class having queue behaviour should implement this interface and override all of its methods + * @param + */ +public interface Queue extends DataStructure { + + //Method to add element + public boolean offer(T t); + + //Method to remove element + public T poll(); + + //Method to check element on head + public T peek(); + + //Method to check element on head. This throws exception on runtime if the queue is empty + public T element(); + + +} diff --git a/src/test/java/com/dataStructures/GeneralQueueTest.java b/src/test/java/com/dataStructures/GeneralQueueTest.java new file mode 100644 index 000000000000..4bb07a3f54c4 --- /dev/null +++ b/src/test/java/com/dataStructures/GeneralQueueTest.java @@ -0,0 +1,45 @@ +package src.test.java.com.dataStructures; + + +import org.junit.Assert; +import org.junit.Test; +import src.main.java.com.dataStructures.GeneralQueue; +import src.main.java.com.types.Queue; + +public class GeneralQueueTest { + + @Test + public void testGeneralQueue() { + + Queue myQueue = new GeneralQueue<>(); + myQueue.add(10); + myQueue.add(20); + myQueue.add(30); + myQueue.add(40); + myQueue.add(50); + + + Object[] myArray = myQueue.toArray(); + Assert.assertEquals(myArray.length, myQueue.size()); + + myQueue.remove(20); + Assert.assertEquals(myQueue.size(), 4); + + Boolean isEmpty = myQueue.isEmpty(); + Assert.assertEquals(Boolean.valueOf("false"), Boolean.valueOf(isEmpty)); + + myQueue.offer(60); + Assert.assertEquals(5, myQueue.size()); + + int polledElement = myQueue.poll(); + Assert.assertEquals(10, polledElement); + + int element = myQueue.element(); + Assert.assertEquals(30, element); + + myQueue.poll(); + int peekedElement = myQueue.peek(); + Assert.assertEquals(40, peekedElement); + + } +} From 320b5de0fee4c623e76635d0ba6fe31b00b67a50 Mon Sep 17 00:00:00 2001 From: asri71 Date: Tue, 5 Mar 2019 17:05:11 +0530 Subject: [PATCH 2/6] Making changes into method as per Oracle specifications --- .../java/com/dataStructures/GeneralQueue.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/dataStructures/GeneralQueue.java b/src/main/java/com/dataStructures/GeneralQueue.java index ebb92302d0c3..9098c71e70f9 100644 --- a/src/main/java/com/dataStructures/GeneralQueue.java +++ b/src/main/java/com/dataStructures/GeneralQueue.java @@ -4,6 +4,7 @@ import java.util.Iterator; import java.util.LinkedList; +import java.util.NoSuchElementException; /** * linkedList based implementation of queue. @@ -24,18 +25,23 @@ public GeneralQueue() { public boolean add(T t) { if(queue == null) { - throw new NullPointerException(); + throw new IllegalStateException(); + } + if(t == null){ + throw new NullPointerException(); } - queue.add(t); return true; } @Override public boolean remove(T t) { - if(null == queue || queue.size() == 0){ + if(null == queue){ throw new NullPointerException(); } + if(queue.isEmpty()) { + throw new NoSuchElementException(); + } queue.remove(t); return true; } @@ -65,7 +71,9 @@ public boolean offer(T t) { if(null == queue) { return false; } - + if(t == null){ + throw new NullPointerException(); + } queue.add(t); return true; } @@ -84,7 +92,7 @@ public T poll() { public T element() { if(queue == null || queue.isEmpty()) { - throw new NullPointerException(); + throw new NoSuchElementException(); } return queue.peekFirst(); From 5ce337fa54373fd984fe00b42e06a8300e5249b0 Mon Sep 17 00:00:00 2001 From: asri71 Date: Tue, 5 Mar 2019 17:08:54 +0530 Subject: [PATCH 3/6] Code changes to adhere to code best practices --- src/main/java/com/types/Queue.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/types/Queue.java b/src/main/java/com/types/Queue.java index a247b348a333..11cfe78b518b 100644 --- a/src/main/java/com/types/Queue.java +++ b/src/main/java/com/types/Queue.java @@ -1,6 +1,8 @@ package src.main.java.com.types; +import java.util.NoSuchElementException; + /** * Interface to provide queue specific functionality to the implementing class * This interface only defines the functionality which the queue implementing classes require. @@ -10,7 +12,7 @@ public interface Queue extends DataStructure { //Method to add element - public boolean offer(T t); + public boolean offer(T t) throws NullPointerException; //Method to remove element public T poll(); @@ -19,7 +21,7 @@ public interface Queue extends DataStructure { public T peek(); //Method to check element on head. This throws exception on runtime if the queue is empty - public T element(); + public T element() throws NoSuchElementException; } From 37b99ff6ddfc6f055d9c7916090d37515f559fca Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 6 Mar 2019 08:57:42 +0800 Subject: [PATCH 4/6] Update GeneralQueue.java --- .../java/com/dataStructures/GeneralQueue.java | 58 +++++++------------ 1 file changed, 22 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/dataStructures/GeneralQueue.java b/src/main/java/com/dataStructures/GeneralQueue.java index 9098c71e70f9..a141ab13bbc9 100644 --- a/src/main/java/com/dataStructures/GeneralQueue.java +++ b/src/main/java/com/dataStructures/GeneralQueue.java @@ -9,6 +9,7 @@ /** * linkedList based implementation of queue. * This implementation is not thread safe and need exclusive thread safety measures from the client. + * * @param */ public class GeneralQueue implements Queue { @@ -16,7 +17,9 @@ public class GeneralQueue implements Queue { private LinkedList queue; private Iterator itr; - //Overloaded constructor to create queue of specific size + /** + * Overloaded constructor to create queue of specific size + */ public GeneralQueue() { queue = new LinkedList<>(); } @@ -24,11 +27,11 @@ public GeneralQueue() { @Override public boolean add(T t) { - if(queue == null) { + if (queue == null) { throw new IllegalStateException(); } - if(t == null){ - throw new NullPointerException(); + if (t == null) { + throw new NullPointerException(); } queue.add(t); return true; @@ -36,10 +39,10 @@ public boolean add(T t) { @Override public boolean remove(T t) { - if(null == queue){ + if (null == queue) { throw new NullPointerException(); } - if(queue.isEmpty()) { + if (queue.isEmpty()) { throw new NoSuchElementException(); } queue.remove(t); @@ -48,18 +51,12 @@ public boolean remove(T t) { @Override public boolean isEmpty() { - - if(null == queue || queue.size() == 0){ - return true; - } - - return false; + return null == queue || queue.size() == 0; } @Override public Iterator iterator() { - - if(queue == null) { + if (queue == null) { return null; } itr = queue.iterator(); @@ -68,10 +65,10 @@ public Iterator iterator() { @Override public boolean offer(T t) { - if(null == queue) { + if (null == queue) { return false; } - if(t == null){ + if (t == null) { throw new NullPointerException(); } queue.add(t); @@ -80,9 +77,8 @@ public boolean offer(T t) { @Override public T poll() { - - if(queue == null || queue.isEmpty()){ - return null; + if (queue == null || queue.isEmpty()) { + return null; } return queue.pollFirst(); @@ -90,8 +86,7 @@ public T poll() { @Override public T element() { - - if(queue == null || queue.isEmpty()) { + if (queue == null || queue.isEmpty()) { throw new NoSuchElementException(); } @@ -100,8 +95,8 @@ public T element() { @Override public T peek() { - if(null == queue || queue.size() == 0){ - return null; + if (null == queue || queue.size() == 0) { + return null; } return queue.peekFirst(); @@ -109,28 +104,22 @@ public T peek() { @Override public boolean hasNext() { - - if(itr.hasNext()){ - return true; - } - return false; + return itr.hasNext(); } @Override public T next() { - return itr.next(); } @Override public Object[] toArray() { - Object[] elements = {}; - if(null == queue || queue.isEmpty()){ + if (null == queue || queue.isEmpty()) { return elements; } elements = new Object[queue.size()]; - for(int i=0;i Date: Wed, 6 Mar 2019 08:58:11 +0800 Subject: [PATCH 5/6] Update DataStructure.java --- src/main/java/com/types/DataStructure.java | 62 +++++++++++++++------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/types/DataStructure.java b/src/main/java/com/types/DataStructure.java index 99fa8710d373..e1ffe043ef98 100644 --- a/src/main/java/com/types/DataStructure.java +++ b/src/main/java/com/types/DataStructure.java @@ -6,26 +6,52 @@ * This interface is to define bacis functionality expected out of any implementation class * Since this is a data structure it should have the flexibility to contain any kind of object hence it has been made generic * Any implementation class need not to be thread safe or it could be depending on the implementation class how does it want to behave. + * * @param */ public interface DataStructure extends Iterator { - //Method to add element in the structure - public boolean add(T t); - - //Method to remove the given object from structure - public boolean remove(T o); - - //Method to get Iterator to parse on the given structure - public Iterator iterator(); - - //Method to check if structure is empty - public boolean isEmpty(); - - //Method to get all the elements of data structure in array - public Object[] toArray(); - - //Method to get the size or number of elements in structure - public int size(); - + /** + * Method to add element in the structure + * + * @param t element + * @return boolean + */ + boolean add(T t); + + /** + * Method to remove the given object from structure + * + * @param o element + * @return boolean + */ + boolean remove(T o); + + /** + * Method to get Iterator to parse on the given structure + * + * @return iterator + */ + Iterator iterator(); + + /** + * Method to check if structure is empty + * + * @return boolean + */ + boolean isEmpty(); + + /** + * Method to get all the elements of data structure in array + * + * @return arr + */ + Object[] toArray(); + + /** + * Method to get the size or number of elements in structure + * + * @return size + */ + int size(); } From 481be6290cadf4303225b68557685d058178c8c9 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 6 Mar 2019 08:58:35 +0800 Subject: [PATCH 6/6] Update Queue.java --- src/main/java/com/types/Queue.java | 44 +++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/types/Queue.java b/src/main/java/com/types/Queue.java index 11cfe78b518b..19c74e64a854 100644 --- a/src/main/java/com/types/Queue.java +++ b/src/main/java/com/types/Queue.java @@ -7,21 +7,39 @@ * Interface to provide queue specific functionality to the implementing class * This interface only defines the functionality which the queue implementing classes require. * Any class having queue behaviour should implement this interface and override all of its methods + * * @param */ public interface Queue extends DataStructure { - //Method to add element - public boolean offer(T t) throws NullPointerException; - - //Method to remove element - public T poll(); - - //Method to check element on head - public T peek(); - - //Method to check element on head. This throws exception on runtime if the queue is empty - public T element() throws NoSuchElementException; - - + /** + * Method to add element + * + * @param t element + * @return boolean + * @throws NullPointerException + */ + boolean offer(T t) throws NullPointerException; + + /** + * Method to remove element + * + * @return element + */ + T poll(); + + /** + * Method to check element on head + * + * @return element + */ + T peek(); + + /** + * Method to check element on head. This throws exception on runtime if the queue is empty + * + * @return element + * @throws NoSuchElementException + */ + T element() throws NoSuchElementException; }