以下基于链表结构实现。
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());
}
}
}
3009

被折叠的 条评论
为什么被折叠?



