数组实现堆栈——Java实现
1 package struct; 2 3 4 //接口 5 interface IArrayStack{ 6 //栈的容量 7 int length(); 8 //栈中元素个数(栈大小) 9 int size(); 10 //取栈顶元素 11 Object top(); 12 //判断栈是否为空 13 boolean isEmpty(); 14 //入栈 15 Object pop(); 16 //出栈 17 Object push(Object value); 18 //清空栈 19 void clear(); 20 } 21 22 23 //实现接口的StackImpl类 24 class StackImpl implements IArrayStack{ 25 static Object[] arr;//数组 26 private int top;//标记栈顶位置,并且表示栈的容量大小 27 private static int MAXSIZE = 10;//数组的最大长度(常量) 28 //构造方法 29 public StackImpl(){ 30 arr = new Object[MAXSIZE]; 31 top = 0; 32 } 33 //求堆栈容量 34 public int length() { 35 return MAXSIZE; 36 } 37 //求堆栈中元素的个数,即堆栈大小 38 public int size(){ 39 return top; 40 } 41 //取栈顶元素 42 public Object top() { 43 return arr[top]; 44 } 45 //判断堆栈是否为空 46 public boolean isEmpty() { 47 return (size() == 0); 48 } 49 //出栈 50 public Object pop() { 51 if(isEmpty()){ 52 System.out.println("The Stack is empty"); 53 return -1; 54 } 55 Object value = arr[top - 1]; 56 top--; 57 arr[top -1] = null; 58 return value; 59 } 60 //对栈进行扩容(每次扩容一倍) 61 public void expand(){ 62 Object[] largerArr = new Object[size()*2]; 63 for(int index = 0;index < top;index++){ 64 largerArr[index] = arr[index]; 65 } 66 arr = largerArr; 67 MAXSIZE = arr.length; 68 } 69 //入栈 70 public Object push(Object value) { 71 //如果超过入栈元素数组长度 72 if(top == arr.length){ 73 expand(); 74 }else{ 75 arr[top] = value; 76 top++; 77 } 78 return arr; 79 } 80 //打印堆栈中元素 81 public static void print(){ 82 myPrint1(arr); 83 } 84 private static void myPrint1(Object[] obj){ 85 for(int i = 0;i < obj.length;i++){ 86 System.out.println(obj[i] + " "); 87 } 88 } 89 //清空堆栈 90 public void clear() { 91 for(int i = top;i > 0;i--){ 92 arr[i] = null; 93 top--; 94 } 95 } 96 } 97 98 99 //测试函数 100 public class ArrayStack { 101 public static void main(String[] args) { 102 IArrayStack stack = new StackImpl(); 103 System.out.println("==================栈中不存在元素测isEmpty函数================"); 104 System.out.println(stack.isEmpty()); 105 System.out.println("==================测length函数================"); 106 System.out.println( stack.length()); 107 System.out.println("==================测push及print函数================"); 108 stack.push("lemon"); 109 stack.push("hah"); 110 stack.push(1); 111 stack.push(9); 112 stack.push("lemon"); 113 stack.push("hah"); 114 stack.push(1); 115 stack.push(9); 116 stack.push("lemon"); 117 stack.push("hah"); 118 stack.push(1); 119 stack.push(9); 120 StackImpl.print(); 121 System.out.println("==================扩容后测length函数================"); 122 System.out.println(stack.length()); 123 System.out.println("==================测top函数================"); 124 System.out.println(stack.top()); 125 System.out.println("==================测size函数================"); 126 System.out.println( stack.size()); 127 System.out.println("==================测pop函数================"); 128 stack.pop(); 129 stack.pop(); 130 System.out.println( stack.size()); 131 System.out.println("==================栈中存在元素测isEmpty函数================"); 132 System.out.println(stack.isEmpty()); 133 System.out.println("==================clear后侧size函数================"); 134 stack.clear(); 135 System.out.println( stack.size()); 136 } 137 }