LinkedQueue

簡(jiǎn)書 賈小強(qiáng)
轉(zhuǎn)載請(qǐng)注明原創(chuàng)出處,謝謝!

package com.lab1.test1;

import java.util.Iterator;
import java.util.NoSuchElementException;

public class LinkedQueue<Item> implements Iterable<Item> {
    private int n;
    private Node first, last;

    private class Node {
        private Item item;
        private Node next;
    }

    @Override
    public Iterator<Item> iterator() {
        return new ListIterator();
    }

    private class ListIterator implements Iterator<Item> {
        Node current = first;

        @Override
        public boolean hasNext() {
            return current != null;
        }

        @Override
        public Item next() {
            Item item = current.item;
            current = current.next;
            return item;
        }

    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (Item item : this) {
            builder.append(item + " ");
        }
        return builder.toString();
    }

    private boolean isEmpty() {
        return first == null;
    }

    private int size() {
        return n;
    }

    private void push(Item item) {
        Node oldlast = last;
        last = new Node();
        last.item = item;
        if (isEmpty()) {
            first = last;
        } else {
            oldlast.next = last;
        }
        n++;
    }

    private Item pop() {
        if (isEmpty()) {
            throw new NoSuchElementException("empty stack exception");
        }
        Item item = first.item;
        first = first.next;
        if (isEmpty()) {
            last = null;
        }
        n--;
        return item;
    }

    public static void main(String[] args) {
        LinkedQueue<String> stack = new LinkedQueue<>();
        System.out.println(stack);
        System.out.println(stack.size());
        System.out.println(stack.isEmpty());

        stack.push("bill");
        stack.push("jack");
        stack.push("lucy");
        System.out.println(stack);
        System.out.println(stack.size());
        System.out.println(stack.isEmpty());

        stack.pop();
        stack.pop();
        System.out.println(stack);
        System.out.println(stack.size());
        System.out.println(stack.isEmpty());
    }

}

輸出


0
true
bill jack lucy 
3
false
lucy 
1
false

Happy learning !!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,323評(píng)論 25 708
  • 簡(jiǎn)書 賈小強(qiáng)轉(zhuǎn)載請(qǐng)注明原創(chuàng)出處,謝謝! Servlet是一種允許響應(yīng)請(qǐng)求的Java類。雖然Servlet可以響應(yīng)任...
    賈小強(qiáng)閱讀 10,698評(píng)論 1 44
  • 小時(shí)候我就是一個(gè)不愛學(xué)習(xí)的人,老師布置作業(yè)能少寫就少寫。上課時(shí)候逃課這些事情我都干過(guò)。記得我小學(xué)的時(shí)候很乖,學(xué)習(xí)成...
    河仙姑閱讀 184評(píng)論 2 1
  • 跑得像袋鼠 喵。 這張是抓拍,卻覺得比正兒八經(jīng)拍好看,所以有時(shí)候事情遠(yuǎn)沒有你想象的那么糟糕啦。
    豆沫不好喝閱讀 307評(píng)論 0 0
  • 文/2323 人這一生, 赤身裸體的來(lái),赤身裸體的走。 這中間,會(huì)經(jīng)過(guò)很多地方, 其中有一個(gè)地方,叫做 醫(yī)院。 如...
    王2323閱讀 257評(píng)論 0 0

友情鏈接更多精彩內(nèi)容