Java实现栈与队列

以下基于链表结构实现。

stack:

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

/**
 * Stack,即栈,特性是先进后出;
 *
 * @author zhkp
 */
public class Stack<Item> implements Iterable<Item> {
    /**
     * 链表结构;
     */
    private class Node {
        Item item;
        Node next;
    }

    private Node first;
    private int N;

    /**
     * 判断栈是否为空;
     *
     * @return 为空则返回true,不为空则返回false。
     */
    public boolean isEmpty() {
        return N == 0;
    }

    /**
     * 获取栈当前装载量;
     *
     * @return 栈装载量。
     */
    public int size() {
        return N;
    }

    /**
     * 添加物品入栈;
     *
     * @param item 被添加的物品。
     */
    public void push(Item item) {
        Node temp = new Node();
        temp.item = item;
        temp.next = first;
        first = temp;
        N++;
    }

    /**
     * 弹出栈顶物品;
     *
     * @return 栈顶物品。
     * @throws NoSuchElementException 栈为空时抛出NoSuchElementException。
     */
    public Item pop() throws NoSuchElementException {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        Item temp = first.item;
        first = first.next;
        N--;
        return temp;
    }

    /**
     * 获取栈迭代器;
     *
     * @return 栈迭代器。
     */
    public Iterator<Item> iterator() {
        return new StackIterator();
    }

    /**
     * 栈迭代器;
     * 顺序:从栈顶到栈底;
     */
    private class StackIterator implements Iterator<Item> {
        private Node temp = first;

        public boolean hasNext() {
            return temp != null;
        }

        public Item next() throws NoSuchElementException {
            if (temp == null) {
                throw new NoSuchElementException();
            }
            Item item = temp.item;
            temp = temp.next;
            return item;
        }

        public void remove() throws UnsupportedOperationException {
        }
    }

    /**
     * 测试样例
     */
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();

        if (stack.isEmpty()) {
            System.out.println("Stack is empty.");
        }

        for (int i = 0; i < 10; i++) {
            stack.push(i);
        }

        for (Integer i : stack) {
            System.out.println(i);
        }

        System.out.println("Stack's size is " + stack.size() + ".");

        while (stack.size() > 0) {
            System.out.println(stack.pop());
        }
    }
}

queue:

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

/**
 * Queue,即队列,特性是先进先出;
 *
 * @author zhkp
 */
public class Queue<Item> implements Iterable<Item> {
    /**
     * 链表结构;
     */
    private class Node {
        Item item;
        Node next;
    }

    private Node first;
    private Node last;
    private int N;

    /**
     * 判断队列是否为空;
     *
     * @return 为空则返回true,不为空则返回false。
     */
    public boolean isEmpty() {
        return N == 0;
    }

    /**
     * 获取队列当前装载量;
     *
     * @return 队列装载量。
     */
    public int size() {
        return N;
    }

    /**
     * 添加物品进入队列;
     *
     * @param item 被添加的物品。
     */
    public void enqueue(Item item) {
        Node temp = new Node();
        temp.item = item;
        if (isEmpty()) {
            last = temp;
            first = temp;
        } else {
            last.next = temp;
            last = last.next;
        }
        N++;
    }

    /**
     * 删除并返回队列头的物品;
     *
     * @return 队列头的物品。
     * @throws NoSuchElementException 栈为空时抛出NoSuchElementException。
     */
    public Item dequeue() throws NoSuchElementException {
        if (isEmpty()) {
            throw new NoSuchElementException();
        }
        Item temp = first.item;
        first = first.next;
        N--;
        if (isEmpty()) {
            last = first;
        }
        return temp;
    }

    /**
     * 获取队列迭代器;
     *
     * @return 队列迭代器。
     */
    public Iterator<Item> iterator() {
        return new QueueIterator();
    }

    /**
     * 队列迭代器;
     * 顺序:从队列头到队列尾;
     */
    private class QueueIterator implements Iterator<Item> {
        private Node temp = first;

        public boolean hasNext() {
            return temp != null;
        }

        public Item next() throws NoSuchElementException {
            if (temp == null) {
                throw new NoSuchElementException();
            }
            Item item = temp.item;
            temp = temp.next;
            return item;
        }

        public void remove() throws UnsupportedOperationException {
        }
    }

    /**
     * 测试样例
     */
    public static void main(String[] args) {
        Queue<Integer> queue = new Queue<>();

        if (queue.isEmpty()) {
            System.out.println("Queue is empty.");
        }

        for (int i = 0; i < 10; i++) {
            queue.enqueue(i);
        }

        for (Integer i : queue) {
            System.out.println(i);
        }

        System.out.println("Queue's size is " + queue.size() + ".");

        while (queue.size() > 0) {
            System.out.println(queue.dequeue());
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值