1. 程式人生 > >[數據結構]-鏈表實現隊列

[數據結構]-鏈表實現隊列

ram @param str ++ pen 隊列 enqueue exc err

[數據結構]-鏈表實現隊列

如有問題,請指出呀。感謝。

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(); } }

[數據結構]-鏈表實現隊列