package com.stack.sym; public interface MyStack<T> { /* * 属性都是public static final。 方法都是:public abstract. */ boolean isEmpty();// 判断栈是否为空 void clear();// 清空栈 int length();// 计算栈的长度 boolean push(T data);// 把数据压入到栈 T pop();// 从栈中取出数据 } package com.stack.sym; public class MyArrayStack<T> implements MyStack<T> { private Object[] objs = new Object[16]; private int size = 0; @Override public void clear() { for (int i = 0; i < objs.length; i++) { objs[i]=null; } size=0; } @Override public boolean isEmpty() { return 0==size; } @Override public int length() { return size; } @Override public T pop() { if(size==0){ return null; } return (T) objs[--size]; } @Override public boolean push(T data) { /* * 询问是否需要扩容 */ if (size>=objs.length) { resize(); } objs[size++]=data; return true; } public void resize(){ Object[]temp=new Object[objs.length*3/2+1]; for (int i = 0; i < temp.length; i++) { temp[i]=objs[i]; objs[i]=null; } objs=temp; } @Override public String toString() { StringBuffer sb = new StringBuffer(); sb.append("MyArrayStack: ["); for (int i = 0; i < size; i++) { sb.append(objs[i].toString()); if (i != size - 1) { sb.append(", "); } } sb.append("]"); return sb.toString(); } } package com.stack.sym; public class Person { Person(int age, String sex) { this.age = age; this.sex = sex; } private int age; private String sex; } package com.stack.sym; public class TestStack { /** * @param args */ public static void main(String[] args) { MyStack<Person> myStack = new MyArrayStack<Person>(); myStack.push(new Person(2, "nv")); myStack.push(new Person(5, "man")); myStack.push(new Person(6, "man")); myStack.pop(); myStack.toString(); System.out.println(myStack.length());//打印为2 System.out.println(convert(3456,4).toString()); } /* * 将10进制正整数num转换为n进制 */ private static String convert(int mun,int n){ MyStack<Integer>myStack=new MyArrayStack<Integer>(); Integer result=mun; while(true){ myStack.push(result%n); result=result/n; if(result%n==0){ break; } } StringBuffer sBuffer=new StringBuffer(); while(myStack.pop()!=null){ sBuffer.append(myStack.pop()); } return sBuffer.toString(); } /* * 遍历字符串的每一个char, 将char与栈顶元素比较. 如果char和栈顶元素配对, 则char不入栈, * 否则将char入栈. 当遍历完成时栈为空说明字符串是合法的. */ public static boolean isMatch(String str){ MyStack<Character>myStack=new MyArrayStack<Character>() ; char []arr=str.toCharArray(); for (char c:arr){ Character temp=myStack.pop(); if(temp==null){ myStack.push(c); } else if(temp=='['&&c==']'){ }else if (temp=='{'&&c=='}') { }else { myStack.push(temp); myStack.push(c); } } return myStack.isEmpty(); } /* * 输入行中字符'#'表示退格, '@'表示之前的输入全都无效. * 使用栈保存输入的字符, 如果遇到'#'就将栈顶出栈, 如果遇到@就清空栈. * 输入完成时将栈中所有字符出栈后反转就是输入的结果: */ public static String lineEdit(String input){ MyStack<Character>myStack=new MyArrayStack<Character>(); char[] arr = input.toCharArray(); for (char c : arr) { if (c == '#') { myStack.pop(); } else if (c == '@') { myStack.clear(); } else { myStack.push(c); } } StringBuilder sb = new StringBuilder(); Character temp = null; while ((temp = myStack.pop()) != null) { sb.append(temp); } // 反转字符串 sb.reverse(); return sb.toString(); } }