📄 mystack.java
字号:
public class MyStack
{
/**
* Creates a new stack that can hold 1000 elements
*
*/
Object[] mystack;
int Capacity = 0;
int max = 1000;
int size;
public MyStack ( )
{
mystack = new Object [max];
}
/**
*
* @param initialSize The maximum number of elements to store in the stack
*/
public MyStack ( int initialSize )
{
mystack = new Object [initialSize];
size = initialSize;
}
/**
* Remove all elements from the stack
*
*/
public void makeEmpty ()
{
for (int i=0; i < mystack.length; i++)
{
pop();
}
}
/**
* @return is the stack empty?
*/
public boolean isEmpty ()
{
if (Capacity == 0)
return true;
else
return false;
}
/**
* @return the number of elements in the stack
*/
public int size ()
{
return Capacity;
}
/**
* Returns the top element on the stack and removes it.
* @throws MyStackEmptyException
* @return the top element on the stack
*/
public Object pop ( ) throws MyStackEmptyException
{
Object temp=null;
//System.out.println("before pop: "+ Capacity);
if (Capacity == 0)
{
throw new MyStackEmptyException();
}
else
{
temp = mystack[Capacity - 1];
mystack[Capacity - 1] = null;
Capacity--;
}
return temp;
}
/**
* 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 (Capacity == 0)
throw new MyStackEmptyException();
return mystack[Capacity - 1];
}
/**
* Push a new element onto the Stack
* @throws MyStackFullException
*/
public void push ( Object o ) throws MyStackFullException
{
//System.out.println("before pop: "+ Capacity);
if (size == 0)
{
size = max;
}
if (Capacity >= size)
{
throw new MyStackFullException();
}
else
{
mystack[Capacity]= o;
Capacity++;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -