本文记录用Java来实现包(Bag)、队列(Queue)、堆栈(Stack)
一、包(Bag)
特点: 只进不出
package com.hef.algorithms.fundamentals.bags_queues_stacks;
import java.util.Iterator;
/**
* FILO first in last out
* @param <Item>
*/
public class Bag<Item> implements Iterable<Item>{
// first node in list
private Node first;
private class Node{
Item item;
Node next;
}
public void add(Item item){
// same as push() in Stack
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
}
@Override
public Iterator<Item> iterator() {
return new ReverseIterator();
}
private class ReverseIterator implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
}
}
二、队列(Queue)
先进先出
package com.hef.algorithms.fundamentals.bags_queues_stacks;
import java.util.Iterator;
/**
* FIFO
* @param <Item>
*/
public class Queue<Item> implements Iterable<Item>{
// link to least recently added node
private Node first;
// link to most recently added node
private Node last;
// number of items on the queue
private int N;
private class Node {
// nested class to define nodes
Item item;
Node next;
}
public boolean isEmpty() { return first == null; }
public int size() { return N; }
public void enqueue(Item item){
// Add item to the end of the list
Node oldlast = last;
last = new Node();
last.item = item;
last.next = null;
if (isEmpty()) first = last;
else oldlast.next = last;
N ++;
}
public Item dequeue(){
// Remove item from the beginning of the list
Item item = first.item;
first = first.next;
if (isEmpty()) last = null;
N--;
return item;
}
@Override
public Iterator<Item> iterator() {
return new QueueIterator();
}
private class QueueIterator implements Iterator<Item> {
private Node current = first;
@Override
public boolean hasNext() {
return current != null;
}
@Override
public Item next() {
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args) {
Queue<String> queue = new Queue<>();
queue.dequeue();
}
}
三、堆栈(Stack)
package com.hef.algorithms.fundamentals.bags_queues_stacks;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Stack<Item> implements Iterable<Item>{
// top of stack (most recently added node)
private Node first;
// number of items
private int N;
private class Node{
Item item;
Node next;
}
public boolean isEmpty() { return first == null; }
public int size() { return N; }
public void push(Item item){
// Add item to top of stack
Node oldfirst = first;
first = new Node();
first.item = item;
first.next = oldfirst;
N ++;
}
public boolean hasNext(){
return N > 0;
}
public Item pop(){
if(isEmpty()) throw new NoSuchElementException("overflow");
// Remove item from top of stack
Item item = first.item;
first = first.next;
N --;
return item;
}
public Item peek(){
if (first == null){
throw new NoSuchElementException("Stack underflow");
}else {
return first.item;
}
}
@Override
public Iterator<Item> iterator() {
return new ListIterator();
}
private class ListIterator implements Iterator<Item> {
private int totalSize = N;
private Node current = first;
@Override
public boolean hasNext() {
if (totalSize == N){
return current != null;
}else{
throw new ConcurrentModificationException();
}
}
@Override
public Item next() {
if(totalSize == N){
Item item = current.item;
current = current.next;
return item;
}else {
throw new ConcurrentModificationException();
}
}
}
public static void main(String[] args) {
Stack<String> stack = new Stack<>();
stack.push("a");
stack.push("b");
stack.push("c");
System.out.println("size: " + stack.size());
System.out.println(stack.pop());
Stack<String>[] stacks = (Stack<String>[]) new Stack[5];
}
}