标签:style blog class code java color
用Array来实现Stack,语言c#。
欢迎大家批评指正。
关键点:
public class Stack<T> { T[] a = new T[1]; int size = 0; public bool IsEmpty() { return size == 0; } public void Push(T data) { if (size == a.Length) { resize(a.Length * 2); } a[size++] = data; } public T Pop() { if (size == 0) { throw new Exception("The stack is Empty"); } T result = a[--size]; a[size] = default(T); if (size == a.Length / 4) { resize(a.Length / 2); } return result; } private void resize(int newLength) { T[] b = new T[newLength]; for (int i = 0; i < size; i++) { b[i] = a[i]; } a = b; } }
标签:style blog class code java color
原文地址:http://www.cnblogs.com/jeffrey517/p/3712978.html