Java版本
1.使用链表的方式来实现栈
这个Stack的实现版本使用了Java里面的泛型、迭代器的功能。例如,Stack实现了Iterable接口,这使得Stack能够支持Java中的for-each功能。
public interface Iterable<E>
{
Iterator<E> iterator();
}
可以这样使用Stack:
Stack<String> stack = new Stack<>(); // 使用Java7 的菱形运算符
stack.push( "Java" );
stack.push( "C++" );
stack.push( "C" );
for( String elem : stack )
System.out.println( elem );
Stack的完整实现:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Stack<T> implements Iterable<T>
{
private int count;
private Node<T> first;
private static class Node<T>
{
private T elem;
private Node<T> next;
}
public void push( T elem )
{
Node<T> temp = new Node<>(); // 使用Java7 的菱形运算符
temp.elem = elem;
temp.next = first;
first = temp;
count++;
}
public String toString()
{
StringBuilder s = new StringBuilder();
s.append( "[ " );
for( T elem : this )
s.append( elem + " " );
s.append( "]" );
return s.toString();
}
public T peek()
{
if( isEmpty() )
throw new NoSuchElementException( "Stack underflow" );
return first.elem;
}
public T pop()
{
if( isEmpty() )
throw new NoSuchElementException( "Stack underflow" );
T elem = first.elem;
first = first.next;
count--;
return elem;
}
public boolean isEmpty()
{
return first == null;
}
public int size()
{
return count;
}
private class ListIterator implements Iterator<T>
{
private Node<T> current = first;
@Override
public boolean hasNext()
{
return current != null;
}
@Override
public T next()
{
if( !hasNext() )
throw new NoSuchElementException();
T elem = current.elem;
current = current.next;
return elem;
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}
@Override
public Iterator<T> iterator()
{
return new ListIterator();
}
}
使用动态数组来实现
import java.util.Iterator;
import java.util.NoSuchElementException;
public class ResizingArrayStack<T> implements Iterable<T>
{
private T[] data;
private int count;
@SuppressWarnings("unchecked")
public ResizingArrayStack()
{
data = ( T[] )new Object[2];
}
public boolean isEmpty()
{
return count == 0;
}
public void push( T elem )
{
if( count >= data.length )
resize( data.length * 2 );
data[count++] = elem;
}
@SuppressWarnings("unchecked")
public void resize( int newSize )
{
T[] newData = ( T[] )new Object[newSize];
for( int i = 0; i < count; i++ )
newData[i] = data[i];
data = newData;
}
public T pop()
{
if( isEmpty() )
throw new NoSuchElementException( "Stack underflow" );
T elem = data[count - 1];
data[count - 1] = null;
count--;
if( count > 0 && count == data.length / 4 )
resize( data.length / 2 );
return elem;
}
public T peek()
{
if( isEmpty() )
throw new NoSuchElementException( "Stack underflow" );
return data[count - 1];
}
public int size()
{
return count;
}
public Iterator<T> iterator()
{
return new ReverseArrayIterator();
}
private class ReverseArrayIterator implements Iterator<T>
{
private int c = count;
@Override
public boolean hasNext()
{
return c > 0;
}
@Override
public T next()
{
if( !hasNext() )
throw new NoSuchElementException();
return data[--c];
}
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
}
}
2、C++版本
待续...