[Data Structure] Chain Queue of Queue

public class LinkQueue<T> {
    // Define the inner class Node, the Node instance represents the node of the chain queue
    private class Node {
        // save the node data
        private T data;
        // a reference to the next node
        private Node next;
        // constructor with no arguments
        public Node() {

        }
        // initialize the constructor for all properties
        public Node(T data, Node next) {
            this.data = data;
            this.next = next;
        }
    }
    // Save the head node of the chain queue
    private Node front;
    // Save the tail node of the chain queue
    private Node rear;
    // Save the number of nodes already contained in the chain queue
    private int size;
    // create empty chain queue
    public LinkQueue() {
        // empty chain queue, front and rear are null
        front = null;
        rear  = null;
    }
    // Create a chain queue with the specified data element, the chain queue has only one element
    public LinkQueue(T element) {
        front = new Node(element, null);
        // There is only one node, both front and rear point to this node
        rear = front;
        size++;
    }
    // return the length of the chain queue
    public int length() {
        return size;
    }
    // add the new element to the queue
    public void add(T element) {
        // If the chain queue is still an empty chain queue
        if (front == null) {
            front = new Node(element, null);
            // There is only one node, front and rear all point to this node
            rear = front;
        } else {
            // create new node
            Node newNode = new Node(element, null);
            // Let the next of the tail node point to the new node
            rear.next = newNode;
            // use the new node as the new tail node
            rear = newNode;
        }
        size++;
    }
    // delete the element on the front side of the queue
    public T remove() {
        Node oldFront = front;
        front = front.next;
        oldFront.next = null;
        size--;
        return oldFront.data;
    }
    // access the last element in the chained queue
    public T element() {
        return rear.data;
    }
    // Determine if the chained queue is empty
    public boolean empty() {
        return size == 0;
    }
    // clear the chain
    public void clear() {
        // Assign the front and rear nodes to null
        front = null;
        rear  = null;
        size  = 0;
    }
    public String toString() {
        if (empty()) {
            return "[]";
        } else {
            StringBuilder sb = new StringBuilder("[");
            for (Node current = front; current != null; current = current.next) {
                sb.append(current.data.toString() + ", ");
            }
            int len = sb.length();
            return sb.delete(len - 2, len).append("]").toString();
        }
    }
}
public class LinkQueueTest {
    public static void main(String[] args) {
        LinkQueue<String> queue = new LinkQueue<String>("aaaa");
        queue.add("bbbb");
        queue.add("cccc");
        System.out.println("Chain queue initialization element: " + queue.toString());
        queue.remove();
        System.out.println("Queue after deleting an element: " + queue.toString());
        queue.add("dddd");
        System.out.println("Queue after adding elements again: " + queue);
        queue.remove();
        queue.add("eeee");
        System.out.println(queue);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325788826&siteId=291194637