📄 mystack.java
字号:
/**
* MyStack.java
* @version 1.0.0
* @Hao Chen
*0236407
*/
/**
* An implementation of the Stack ADT.
* In this implementation, we do not grow the stack
* once it is full. A better implementation would do so.
*/
import java.awt.*;
public class MyStack
{
/**
* Creates a new stack that can hold 1000 elements
*
*/
private int top=-1;
private int cap;
private Object elem[];
public final static int max=1000;
public MyStack ( )
{
this(max);
}
/**
*
* @param initialSize The maximum number of elements to store in the stack
*/
public MyStack ( int initialSize )
{
cap = initialSize;
elem= new Object[initialSize];
}
/**
* Remove all elements from the stack
*
*/
public void makeEmpty ()
{
for(int i=0; i<cap;i++){
elem[i]=null;
}
top=-1;
}
/**
* @return is the stack empty?
*/
public boolean isEmpty ()
{
return (top<0);
}
/**
* @return the number of elements in the stack
*/
public int size ()
{
return (top+1);
}
/**
* Returns the top element on the stack and removes it.
* @throws MyStackEmptyException
* @return the top element on the stack
*/
public Object pop ( ) throws MyStackEmptyException
{
if(isEmpty()){
throw new MyStackEmptyException("32stack is empty.");}
Object k= elem[top];
elem[top--]=null;
return k;
}
/**
* This method returns the top element without removing it from
* the stack.
* @throws MyStackEmptyException
* @return the top element on the stack
*/
public Object top ( ) throws MyStackEmptyException
{
if(isEmpty()){
throw new MyStackEmptyException("2313stack is empty.");}
return elem[top];
}
/**
* Push a new element onto the Stack
* @throws MyStackFullException
*/
public void push ( Object o ) throws MyStackFullException
{
if(size()==cap){
throw new MyStackFullException("stack is full.");}
elem[++top]=o;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -