- 1.用java实现一个链栈
import java.util.Iterator;
import java.util.NoSuchElementException;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
//链表栈
public class LinkedStack<Item> implements Iterable<Item> {
private int n;
private Node first;
private class Node{
private Item item;
private Node next;
}
public LinkedStack(){
first=null;
n=0;
assert check();
}
public boolean isEmpty(){
return first == null;
}
public int size(){
return n;
}
//入栈
public void push(Item item){
Node oldfirst = first;
first = new Node();
first.item=item;
first.next=oldfirst;
n++;
assert check();
}
public Item pop(){
if(isEmpty()) throw new NoSuchElementException("stack underflow");
Item item = first.item;
first=first.next;
n--;
assert check();
return item;
}
public Item peek(){
if(isEmpty()) throw new NoSuchElementException("stack overflow");
return first.item;
}
//检查所有出错的情况
private boolean check(){
if(n<0) {return false;}
if(n==0){
if(first!=null) return false;
}
else if(n==1){
if(first == null) return false;
if(first.next!=null) return false;
}
else{
if(first == null) return false;
if(first.next == null) return false;
}
//检测是不是n个
int numberOfNodes = 0;
for(Node x=first; x!=null && numberOfNodes <= n;x=x.next){
numberOfNodes++;
}
if(numberOfNodes!=n) return false;
return true;
}
public String toString(){
StringBuilder s =new StringBuilder();
for(Item item:this){
s.append(item + " ");
}
return s.toString();
}
public Iterator<Item> iterator(){
return new LinkedIterator();
}
private class LinkedIterator implements Iterator<Item>{
private Node current = first;
public boolean hasNext(){
return current!=null;
}
public void remove(){
throw new UnsupportedOperationException();
}
public Item next(){
if(!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current=current.next;
return item;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedStack<String> stack = new LinkedStack<String>();
while(!StdIn.isEmpty()){
String item =StdIn.readString();
if(!item.equals("-"))
stack.push(item);
else if(!stack.isEmpty())
StdOut.print(stack.pop() + " ");
}
StdOut.println("(" + stack.size() + " ");
}
}
2.使用单链表实现一个通用的栈
每个栈元素的类型都是Item
import java.util.Iterator;
import java.util.NoSuchElementException;
import edu.princeton.cs.algs4.StdIn;
import edu.princeton.cs.algs4.StdOut;
public class Stack<Item> implements Iterable<Item>{
private Node<Item> first;
private int n;
private static class Node<Item>{
private Item item;
private Node<Item> next;
}
public Stack(){
first=null;
n=0;
}
public boolean isEmpty(){
return first == null;
}
public int size()
{
return n;
}
public void push(Item item){
Node<Item> oldfirst = first;
//first 指向创建的最新结点
first = new Node<Item>();
first.item=item;
first.next=oldfirst;
n++;
}
public Item pop(){
if(isEmpty()) throw new NoSuchElementException("stack overflow");
Item item=first.item;
first = first.next;
n--;
return item;
}
public Item peek(){
if(isEmpty()) throw new NoSuchElementException("stack overflow");
return first.item;
}
public String toString(){
StringBuilder s = new StringBuilder();
for(Item item : this){
s.append(item);
s.append(' ');
}
return s.toString();
}
public Iterator<Item> iterator(){
return new LinkedIterator(first);
}
private class LinkedIterator implements Iterator<Item>{
private Node<Item> current;
public LinkedIterator(Node<Item> first){
current = first;
}
public boolean hasNext(){
return current != null;
}
public void remove() {
throw new UnsupportedOperationException();
}
public Item next(){
if(!hasNext()) throw new NoSuchElementException();
Item item = current.item;
current = current.next;
return item;
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Stack<String> stack = new Stack<String>();
while(!StdIn.isEmpty()){
String item = StdIn.readString();
if(!item.equals("-"))
stack.push(item);
else if(!stack.isEmpty())
StdOut.print(stack.pop()+ " ");
}
StdOut.println("("+ stack.size()+" left on stack)");
}
}