1.給出對應於stack的資料類型介面
package dataStructures;
public interface Stack
{
public boolean empty(); //是否空棧
public Object peek(); //返回棧頂元素
public void push(Object theObject);//
public Object pop(); //
}
2.實現子類
1)/** a stack class derived from ArrayLinearList */
package dataStructures;
import java.util.*;
public class DerivedArrayStack extends ArrayLinearList
implements Stack
{
// constructors
/** create a stack with the given initial capacity */
public DerivedArrayStack(int initialCapacity)
{super(initialCapacity);}
/** create a stack with initial capacity 10 */
public DerivedArrayStack()
{this(10);}
// methods
/** @return true iff stack is empty */
public boolean empty()
{return isEmpty();}
/** @return top element of stack
* @throws EmptyStackException when the stack is empty */
public Object peek()
{
if (empty())
throw new EmptyStackException();
return get(size() - 1);
}
/** add theElement to the top of the stack */
public void push(Object theElement)
{add(size(), theElement);}
/** remove top element of stack and return it
* @throws EmptyStackException when the stack is empty */
public Object pop()
{
if (empty())
throw new EmptyStackException();
return remove(size() - 1);
}
/** test program */
public static void main(String [] args)
{
int x;
DerivedArrayStack s = new DerivedArrayStack(3);
// add a few elements
s.push(new Integer(1));
s.push(new Integer(2));
s.push(new Integer(3));
s.push(new Integer(4));
// delete all elements
while (!s.empty())
{
System.out.println("Top element is " + s.peek());
System.out.println("Removed the element " + s.pop());
}
}
}
2)類arrayStack
/** a stack class that uses a one-dimensional array */
package dataStructures;
import java.util.EmptyStackException;
import utilities.*;
public class ArrayStack implements Stack
{
// data members
int top; // current top of stack
Object [] stack; // element array
// constructors
/** create a stack with the given initial capacity
* @throws IllegalArgumentException when initialCapacity < 1 */
public ArrayStack(int initialCapacity)
{
if (initialCapacity < 1)
throw new IllegalArgumentException
("initialCapacity must be >= 1");
stack = new Object [initialCapacity];
top = -1;
}
/** create a stack with initial capacity 10 */
public ArrayStack()
{this(10);}
// methods
/** @return true iff stack is empty */
public boolean empty()
{return top == -1;}
/** @return top element of stack
* @throws EmptyStackException when the stack is empty */
public Object peek()
{
if (empty())
throw new EmptyStackException();
return stack[top];
}
/** add theElement to the top of the stack */
public void push(Object theElement)
{
// increase array size if necessary
if (top == stack.length - 1)
stack = ChangeArrayLength.changeLength1D(stack, 2 * stack.length);
// put theElement at the top of the stack
stack[++top] = theElement;
}
/** remove top element of stack and return it
* @throws EmptyStackException when the stack is empty */
public Object pop()
{
if (empty())
throw new EmptyStackException();
Object topElement = stack[top];
stack[top--] = null; // enable garbage collection
return topElement;
}
/** test program */
public static void main(String [] args)
{
int x;
ArrayStack s = new ArrayStack(3);
// add a few elements
s.push(new Integer(1));
s.push(new Integer(2));
s.push(new Integer(3));
s.push(new Integer(4));
// delete all elements
while (!s.empty())
{
System.out.println("Top element is " + s.peek());
System.out.println("Removed the element " + s.pop());
}
}
}
3.DerivedLinkedStack
/** a stack class derived from Chain */
package dataStructures;
import java.util.*;
public class DerivedLinkedStack extends Chain
implements Stack
{
// constructors
/** included only for compatibility with other stack classes */
public DerivedLinkedStack(int initialCapacity)
{super();}
public DerivedLinkedStack()
{this(0);}
// methods
/** @return true iff stack is empty */
public boolean empty()
{return isEmpty();}
/** @return top element of stack
* @throws EmptyStackException when the stack is empty */
public Object peek()
{
if (empty())
throw new EmptyStackException();
return get(0);
}
/** add theElement to the top of the stack */
public void push(Object theElement)
{add(0, theElement);}
/** remove top element of stack and return it
* @throws EmptyStackException when the stack is empty */
public Object pop()
{
if (empty())
throw new EmptyStackException();
// remove and return top element
return remove(0);
}
/** test program */
public static void main(String [] args)
{
int x;
DerivedLinkedStack s = new DerivedLinkedStack(3);
// add a few elements
s.push(new Integer(1));
s.push(new Integer(2));
s.push(new Integer(3));
s.push(new Integer(4));
// delete all elements
while (!s.empty())
{
System.out.println("Top element is " + s.peek());
System.out.println("Removed the element " + s.pop());
}
}
}
4. DerivedVectorStack
/** a stack class derived from Vector */
package dataStructures;
import java.util.*;
public class DerivedVectorStack extends Vector
implements Stack
{
// constructors
/** create a stack with the given initial capacity */
public DerivedVectorStack(int initialCapacity)
{super(initialCapacity);}
/** create a stack with initial capacity 10 */
public DerivedVectorStack()
{// use default capacity of 10
this(10);
}
// methods
/** @return true iff stack is empty */
public boolean empty()
{return isEmpty();}
/** @return top element of stack
* @throws EmptyStackException when the stack is empty */
public Object peek()
{
if (empty())
throw new EmptyStackException();
return get(size() - 1);
}
/** add theElement to the top of the stack */
public void push(Object theElement)
{add(size(), theElement);}
/** remove top element of stack and return it
* @throws EmptyStackException when the stack is empty */
public Object pop()
{
if (empty())
throw new EmptyStackException();
return remove(size() - 1);
}
/** test program */
public static void main(String [] args)
{
int x;
DerivedVectorStack s = new DerivedVectorStack(3);
// add a few elements
s.push(new Integer(1));
s.push(new Integer(2));
s.push(new Integer(3));
s.push(new Integer(4));
// delete all elements
while (!s.empty())
{
System.out.println("Top element is " + s.peek());
System.out.println("Removed the element " + s.pop());
}
}
}
5)DerivedArrayStackWithCatch
/** a stack class derived from ArrayLinearList */
/** this implementation catches exceptions thrown by
* ArrayLinearList */
package dataStructures;
import java.util.*;
public class DerivedArrayStackWithCatch extends ArrayLinearList
implements Stack
{
// constructors
/** create a stack with the given initial capacity */
public DerivedArrayStackWithCatch(int initialCapacity)
{super(initialCapacity);}
/** create a stack with initial capacity 10 */
public DerivedArrayStackWithCatch()
{this(10);}
// methods
/** @return true iff stack is empty */
public boolean empty()
{return isEmpty();}
/** @return top element of stack
* @throws EmptyStackException when the stack is empty */
public Object peek()
{
try {return get(size() - 1);}
catch (IndexOutOfBoundsException e)
{
throw new EmptyStackException();
}
}
/** add theElement to the top of the stack */
public void push(Object theElement)
{add(size(), theElement);}
/** remove top element of stack and return it
* @throws EmptyStackException when the stack is empty */
public Object pop()
{
try
{return remove(size() - 1);}
catch (IndexOutOfBoundsException e)
{
throw new EmptyStackException();
}
}
/** test program */
public static void main(String [] args)
{
int x;
DerivedArrayStackWithCatch s = new DerivedArrayStackWithCatch(3);
// add a few elements
s.push(new Integer(1));
s.push(new Integer(2));
s.push(new Integer(3));
s.push(new Integer(4));
// delete all elements
while (!s.empty())
{
System.out.println("Top element is " + s.peek());
System.out.println("Removed the element " + s.pop());
}
}
}