Array Implementation Stack - Java Implementation

package struct;


//interface
interface IArrayStack{
	//The capacity of the stack
 	int length();
 	//Number of elements in the stack (stack size)
 	int size();
 	//Get the top element
 of the stack 	Object top();
 	//Determine whether the stack is empty
 	boolean isEmpty();
 	/ /
 Push 	Object pop();
 	//
 	Push Object push(Object value);
 	//Clear the stack
 	void clear();
}


//The StackImpl class that implements the interface
class StackImpl implements IArrayStack{
	static Object[] arr;//Array
 	private int top;//Mark the top position of the stack and indicate the capacity of the stack
 	private static int MAXSIZE = 10;//The maximum length of the array (constant)
 	//Construction method 
 	public StackImpl() {
 		arr = new Object[MAXSIZE];
 		top = 0;
 	}
 	// Find the stack capacity
 	public int length() {
 		return MAXSIZE;
 	}
 	// Find the number of elements in the stack, that is, the stack size
 	public int size(){
 		return top ;
 	}
 	//Get the top element
 of the stack 	public Object top() {
 		return arr[top];
 	}
 	//Check if the stack is empty
 	public boolean isEmpty() {
 		return (size() == 0);
 	}
 	//Pop the stack
 	public Object pop() {
 		if(isEmpty()){
 			System.out.println("The Stack is empty");
 			return -1;
		}
 		Object value = arr[top - 1];
 		top--;
 		arr[top -1] = null;
 		return value;
 	}
 	//Expand the stack (double each time)
 		public void expand(){
 			Object[] largerArr = new Object[size()*2];
 			for(int index = 0;index < top;index++){
 				largerArr[index] = arr[index];
 			}
 			arr = largerArr;
 			MAXSIZE = arr.length;
 		}
 	// Push the stack
 	public Object push(Object value) {
 		//If the length of the push element array is exceeded
 		if(top == arr.length){
 			expand();
 		}else{
 			arr[top] = value;
 			top++; }
 return arr;
 }
 //Print elements in the stack
 public static void print(){
 myPrint1(arr);
 }
	
											private static void myPrint1(Object[] obj){
		for(int i = 0;i < obj.length;i++){
			System.out.println(obj[i] + " ");
		}
	}
	//清空堆栈
	public void clear() {
		for(int i = top;i > 0;i--){
			arr[i] = null;
			top--;
		}
	}
}


// test function
public class ArrayStack {
	public static void main(String[] args) {
		IArrayStack stack = new StackImpl();
		System.out.println("==================栈中不存在元素测isEmpty函数================");
		System.out.println(stack.isEmpty());
		System.out.println("==================测length函数================");
		System.out.println( stack.length());
		System.out.println("==================测push及print函数================");
		stack.push("lemon");
		stack.push("hah");
		stack.push(1);
		stack.push(9);
		stack.push("lemon");
		stack.push("hah");
		stack.push(1);
		stack.push(9);
		stack.push("lemon");
		stack.push("hah");
		stack.push(1);
		stack.push(9);
		StackImpl.print();
		System.out.println("====================================");
 		System .out.println(stack.length());
 		System.out.println("==================Test top function========== ===== ");
 		System.out.println(stack.top());
 		System.out.println("==================Measure size function ===============");
 		System.out.println( stack.size());
 		System.out.println("============ =======Test pop function================");
 		stack.pop();
 		stack.pop();
 		System.out.println( stack.size ());
 		System.out.println("==================There is an element in the stack to measure the isEmpty function============== ==");
 		System.out.println(stack.isEmpty());
 		System.out.println("==================clear backside size function== =============");
 		stack.clear();
 		System.
out.println( stack.size());
	}
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325986663&siteId=291194637
Recommended