[數據結構]-鏈表實現隊列node
若有問題,請指出呀。感謝。數據結構
package com.cn.jichu.day09; public class LinkedQueue<E> { /** * 首節點 */ private Node head; /** * 尾節點 */ private Node tail; /** * 當前棧中元素個數 */ private int size; public LinkedQueue(){ this.size = 0; } /** * 入列 * @param e */ public void enqueue(E e){ Node node = new Node(e); if(head == null && tail == null){ head = tail = node; size ++; return; } tail.next = node; tail = node; size ++; } /** * 出列 * @return */ public E dequeue(){ if(head==null){ throw new IllegalArgumentException("當前隊列無元素"); } if(head.equals(tail)){ System.out.println("head.equals(tail)"); E e = head.data; head = tail = null; size --; return e; } E e = head.data; head = head.next; size --; return e; } /** * 獲取隊列中的元素個數 * @return */ public int getSize(){ return size; } private class Node{ private Node next; private E data; public Node(E data) { this.data = data; } } @Override public String toString(){ StringBuilder ret = new StringBuilder(); ret.append("queue head["); Node node = head; while (node!=null){ ret.append(node.data + "-->"); node = node.next; } ret.append("null ] tail"); return ret.toString(); } }