Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package randomid;
- import java.util.NoSuchElementException;
- /** Queue data structure */
- public class ListQueue<T> {
- /** Queue node data structure */
- private class Node<T> {
- Node<T> next;
- T element;
- public Node( T element,Node<T> next ) {
- this.element = element;
- this.next = next;
- }
- }
- /** Head of the queue */
- private Node<T> head = null;
- /** Tail of the queue */
- private Node<T> tail = null;
- /** Enqueue a new node at the tail (end) of the queue */
- public void enqueue( T t ) {
- Node<T> newNode = new Node<>( t, null );
- if( isEmpty( ) ) {
- head = tail = newNode;
- } else {
- tail.next = newNode;
- tail = newNode;
- }
- }
- /** Dequeue the node from the head (top) of the queue */
- public T dequeue( ) throws NoSuchElementException {
- if( isEmpty( ) ) {
- throw new NoSuchElementException( );
- }
- T element = head.element;
- head = head.next;
- return element;
- }
- /** Checks if the queue is empty */
- public boolean isEmpty( ) {
- return head == null;
- }
- }
Add Comment
Please, Sign In to add comment