聶永的博客

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

          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)

          個人收藏

          最新隨筆

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 千阳县| 汪清县| 自治县| 屯门区| 漾濞| 德化县| 蓬溪县| 娄烦县| 五莲县| 云梦县| 云南省| 丹阳市| 远安县| 湘阴县| 双牌县| 岳西县| 黄梅县| 大余县| 锦屏县| 新丰县| 扶沟县| 福建省| 清河县| 昌江| 红原县| 调兵山市| 伽师县| 湖南省| 山西省| 额敏县| 汝州市| 张掖市| 栖霞市| 冕宁县| 沧州市| 通辽市| 临江市| 垦利县| 洪江市| 岳阳县| 大悟县|