聶永的博客

          記錄工作/學習的點點滴滴。

          JAVA Stack(棧)學習代碼

          近日了解一下棧的概念,LIFO,后進先出,順手寫了幾個實現,添加泛型支持,特貼于此:

          /**
          * 定義一個不可變棧
          *
          * @author xiaomin
          *
          * @param <T>
          */
          public class DefaultStack<T extends Serializable> implements Stack<T> {

          private int size;

          private Object[] values;

          private int curr;

          public DefaultStack(int size) {
          this.size = size;

          curr = -1;

          values = new Object[size];
          }

          @Override
          public void push(T t) {
          if ((size - 1) == curr) {
          throw new UnsupportedOperationException("The Stack is full now !");
          }

          curr++;
          values[curr] = t;
          }

          @SuppressWarnings("unchecked")
          @Override
          public T pop() {
          if (curr < 0) {
          throw new UnsupportedOperationException("The Stack is empty now !");
          }

          return (T) values[curr--];
          }

          @Override
          public boolean end() {
          return curr == -1;
          }
          }
          有三個實現:
          /**
          * 定義一個鏈接結構的棧
          *
          * @author xiaomin
          *
          * @param <T>
          */

          public class LinkStack<T extends Serializable> implements Stack<T> {

          /**
          * 定義一個節點
          *
          * @author xiaomin
          *
          */

          private class Node implements Serializable {
          private static final long serialVersionUID = 1L;

          private T value;
          private Node pre;

          // 構造一個空值
          public Node() {
          value = null;
          pre = null;
          }

          public Node(T value, Node pre) {
          this.value = value;
          this.pre = top;
          }
          }

          private Node top = new Node();

          @Override
          public void push(T t) {
          top = new Node(t, top);
          }

          @Override
          public T pop() {
          T value = top.value;

          if (!end())
          top = top.pre;

          return value;
          }

          @Override
          public boolean end() {
          return top.value == null && top.pre == null;
          }
          }

          /**
          * 定義一個不可變棧
          *
          * @author xiaomin
          *
          * @param <T>
          */

          public class DefaultStack<T extends Serializable> implements Stack<T> {

          private int size;

          private Object[] values;

          private int curr;

          public DefaultStack(int size) {
          this.size = size;

          curr = -1;

          values = new Object[size];
          }

          @Override
          public void push(T t) {
          if ((size - 1) == curr) {
          throw new UnsupportedOperationException("The Stack is full now !");
          }

          curr++;
          values[curr] = t;
          }

          @SuppressWarnings("unchecked")
          @Override
          public T pop() {
          if (curr < 0) {
          throw new UnsupportedOperationException("The Stack is empty now !");
          }

          return (T) values[curr--];
          }

          @Override
          public boolean end() {
          return curr == -1;
          }
          }

          /**
          * 定義一個容量可變的棧
          *
          * @author xiaomin
          *
          * @param <T>
          */

          public class ListStack<T extends Serializable> implements Stack<T> {

          private List<T> list = null;

          public ListStack() {
          list = new ArrayList<T>();
          }

          public ListStack(int size) {
          list = new ArrayList<T>(size);
          }

          @Override
          public void push(T t) {
          list.add(t);
          }

          @Override
          public T pop() {
          if (list.isEmpty()) {
          throw new EmptyStackException();
          }

          return list.remove(list.size() - 1);
          }

          @Override
          public boolean end() {
          return list.isEmpty();
          }
          }

          測試代碼如下:
           public static void main(String[] args) {
          System.out.println("link stack ...\n");

          Stack<Integer> linkStack = new LinkStack<Integer>();
          int times = 10;

          testMethod(linkStack, times);

          System.out.println("\ndefault stack ...\n");
          Stack<Integer> defaultStack = new DefaultStack<Integer>(times);

          testMethod(defaultStack, times);

          System.out.println("\nlist stack with enouch space...\n");
          Stack<Integer> listStack = new ListStack<Integer>();

          testMethod(listStack, times*2);
          }

          private static void testMethod(Stack<Integer> linkStack, int times) {
          Random random = new Random(47);

          for (int i = 0; i < times; i++) {
          int value = i * random.nextInt(times);

          System.out.println("節點 " + (i + 1) + " : " + value);

          linkStack.push(value);
          }

          System.out.println("*********************************");

          int nums = times;
          while (!linkStack.end()) {
          System.out.println("節點 " + (nums--) + " : " + linkStack.pop());
          }
          }

          以上代碼簡單易懂,這里不做過多解釋。

          posted on 2010-10-05 11:36 nieyong 閱讀(2250) 評論(0)  編輯  收藏 所屬分類: Java

          公告

          所有文章皆為原創,若轉載請標明出處,謝謝~

          新浪微博,歡迎關注:

          導航

          <2010年10月>
          262728293012
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          統計

          常用鏈接

          留言簿(58)

          隨筆分類(130)

          隨筆檔案(151)

          個人收藏

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 抚顺县| 屯留县| 湖口县| 阿合奇县| 阳江市| 吉林市| 大竹县| 赣榆县| 叙永县| 屯昌县| 平塘县| 林芝县| 西充县| 桐梓县| 宜兰县| 寿阳县| 鄯善县| 科技| 罗江县| 江安县| 台北市| 肃北| 深圳市| 龙州县| 盖州市| 潼关县| 即墨市| 浦江县| 措美县| 龙江县| 东方市| 策勒县| 射阳县| 蓝山县| 会宁县| 陈巴尔虎旗| 德庆县| 筠连县| 河池市| 文登市| 津市市|