Skip to content

Commit

Permalink
Fix iiitv#509: Add Queue [Java] (iiitv#516)
Browse files Browse the repository at this point in the history
* Add Queue [Java]

* remove unnecessary constructor

* update README.md with Queue row and remove not needed implements statement

* fix wrong class name

* add main function that demonstrates the use of the queue implementation

* fix main test example

* apply google code style to Queue.java
  • Loading branch information
sebasrp authored and singhpratyush committed Nov 2, 2017
1 parent 2943c71 commit 41ea7b4
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ Community (college) maintained list of Algorithms and Data Structures implementa
| [Linked List](https://en.wikipedia.org/wiki/Linked_list) | [:white_check_mark:](linked_list/linkedList.c.c) | | [:white_check_mark:](linked_list/LinkedList.java) | [:white_check_mark:](linked_list/linked_list.py) | [:white_check_mark:](linked_list/linked_list.go) | [:white_check_mark:](linked_list/linkedList.js) | [:white_check_mark:](linked_list/LinkedList.cs) |
| [Stack](https://en.wikipedia.org/wiki/Stack_(abstract_data_type)) | [:white_check_mark:](stack/stack.c) | | [:white_check_mark:](stack/Stack.java) | [:white_check_mark:](stack/stack.py) | [:white_check_mark:](stack/stack.go) | [:white_check_mark:](stack/stack.js) | [:white_check_mark:](stack/Stack.cs) |
| [Trie](https://en.wikipedia.org/wiki/Trie) | | [:white_check_mark:](trie/trie.cpp) | [:white_check_mark:](trie/Trie.java) | | [:white_check_mark:](trie/trie.go) | | [:white_check_mark:](trie/Trie.cs) | |
| [Queue](https://en.wikipedia.org/wiki/Queue_(abstract_data_type)) | | | [:white_check_mark:](queue/Queue.java) | | | | | |




Expand Down
143 changes: 143 additions & 0 deletions queue/Queue.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import java.util.NoSuchElementException;

/**
* Implements https://docs.oracle.com/javase/8/docs/api/java/util/Queue.html
*/
public class Queue<T> {

private int total;
// Linked Lists used to store the data
private Node first, last;

private class Node {

private T data;
private Node next;

public Node(T data) {
this.data = data;
}
}

/**
* Inserts the specified element into this queue if it is possible to do so immediately without
* violating capacity restrictions, returning true upon success and throwing an
* IllegalStateException if no space is currently available.
*
* @param element the element to add
* @return true upon success, throwing an IllegalStateException if no space is currently
* available.
*/
public boolean add(T element) {
Node newNode = new Node(element);

Node current = last;
last = newNode;

if (total == 0) {
first = last;
} else {
current.next = last;
}
total++;

return true;
}

/**
* Inserts the specified element into this queue if it is possible to do so immediately without
* violating capacity restrictions.
*
* @param element the element to add
* @return true if the element was added to this queue, else false
*/
public boolean offer(T element) {
try {
return add(element);
} catch (Exception ex) {
//failed to add, return false
return false;
}
}

/**
* Retrieves and removes the head of this queue.
* This method differs from poll only in that it throws an exception if this queue is empty.
*
* @return the head of this queue.
* @throws NoSuchElementException if this queue is empty
*/
public T remove() throws NoSuchElementException {
if (total == 0) {
throw new NoSuchElementException();
}
T element = first.data;
first = first.next;
total--;
if (total == 0) {
last = null;
}
return element;
}

/**
* Retrieves and removes the head of this queue, or returns null if this queue is empty.
* This method differs from peek only in that it throws an exception if this queue is empty.
*
* @return the head of this queue, or returns null if this queue is empty.
*/
public T poll() {
try {
return remove();
} catch (Exception ex) {
//failed to remove, return null
return null;
}
}

/**
* Retrieves, but does not remove, the head of this queue.
*
* @return the head of this queue
* @throws NoSuchElementException if this queue is empty
*/
public T element() throws NoSuchElementException {
if (total == 0) {
throw new NoSuchElementException();
}
return first.data;
}

/**
* Retrieves, but does not remove, the head of this queue, or returns null if this queue is empty.
*
* @return the head of this queue, or returns null if this queue is empty.
*/
public T peek() {
return total == 0 ? null : first.data;
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Node tmp = first;
while (tmp != null) {
sb.append(tmp.data).append(", ");
tmp = tmp.next;
}
return sb.toString();
}

public static void main(String[] args) {
Queue<Integer> queue = new Queue<>();
for (int i = 1; i <= 10; i++) { // Creates a dummy queue which contains integers from 1-10
queue.add(i);
}

System.out.println("Queue :");

while (queue.peek() != null) {
System.out.println(queue.poll());
}
}
}

0 comments on commit 41ea7b4

Please sign in to comment.