数据结构怎么玩目录(一)--栈

栈是基本简单的数据结构之一.所谓基本,是数据结构中有着众多在此基础上搭建起来的结构;所谓简单,是栈只有一个入口,而且实现起来也非常的简单.

栈有一个特性,是"后进先出",这是规定好的,有这种特性的数据结构叫做栈.日常中的例子也很多,比如弹夹,第一个压入弹夹的子弹是最后一个弹出来的.即栈就一个入口.

下面是我写的一个栈的结构,包括了简单的压入栈操作,弹出栈操作等等.

  1 package com.bao.structure.stack;
  2 
  3 
  4 /**
  5  * 使用数组实现栈.
  6  * 
  7  * @title ArrayStack.java
  8  * @package com.bao.structure.stack.impl
  9  * @author Eason
 10  * @version v1.0, 2013-6-18
 11  * @param <E>
 12  * @see com.bao.structure.stack.ArrayStackX
 13  */
 14 @Deprecated
 15 public class ArrayStack<E> implements Stack<E> {
 16 
 17     private E[] items;
 18     private int capacity;
 19     private int top;
 20     private static final int DEFAULT_CAPACITY = 10;
 21 
 22     public ArrayStack() {
 23         this(DEFAULT_CAPACITY);
 24     }
 25 
 26     @SuppressWarnings("unchecked")
 27     public ArrayStack(int capacity) {
 28         items = (E[]) new Object[capacity];
 29         this.capacity = capacity;
 30         top = -1;
 31     }
 32 
 33     /**
 34      * Make the stack logically empty.<br />
 35      * 清空数组数据.
 36      */
 37     public void clear() {
 38         for (int i = 0; i < capacity; i++) {
 39             items[i] = null;
 40         }
 41         //capacity = 0;
 42         top = -1;
 43     }
 44 
 45     /**
 46      * Test if the stack is logically empty.
 47      * 
 48      * @return true if empty, false otherwise.
 49      */
 50     public boolean empty() {
 51         return top == -1;
 52     }
 53 
 54     /**
 55      * Get the most recently inserted item in the stack.<br />
 56      * throw ArrayIndexOutOfBoundsException if the stack is empty.
 57      * 
 58      * @return E the most recently inserted element.
 59      */
 60     public E peek() {
 61         if (empty()) {
 62             throw new ArrayIndexOutOfBoundsException("Stack underflow.");
 63         }
 64 
 65         return items[top];
 66     }
 67 
 68     /**
 69      * remove the most recently inserted element from the stack. <br />
 70      * throw ArrayIndexOutOfBoundsException if the stack is empty.
 71      * 
 72      * @return E the most recently inserted element.
 73      */
 74     public E pop() {
 75         if (empty()) {
 76             throw new ArrayIndexOutOfBoundsException("Stack underflow.");
 77         }
 78 
 79         return items[top--];
 80     }
 81 
 82     /**
 83      * Insert a new element into the stack.
 84      */
 85     public void push(E e) {
 86         if (top + 1 == capacity) {
 87             extendArray();
 88         }
 89         items[++top] = e;
 90     }
 91 
 92     /**
 93      * Internal method to extend the array.<br />
 94      * 扩展数组.扩大一倍多一,为房子容量为0的情况.
 95      */
 96     @SuppressWarnings("unchecked")
 97     private void extendArray() {
 98         E[] newDoubleArray = (E[]) new Object[capacity * 2 + 1];
 99         for (int i = 0; i < capacity; i++) {
100             newDoubleArray[i] = items[i];
101         }
102         items = newDoubleArray;
103         capacity = items.length;
104     }
105 
106     public String toString() {
107         StringBuffer result = new StringBuffer("[");
108 
109         for (int i = top; i > -1; i--) {
110             result.append(items[i]).append(" ");
111         }
112         result.append("]");
113         return result.toString();
114     }
115 
116 }
ArrayStack.java

在这个结构中,我使用数组实现的栈结构,同时定义数组的长度.不过当数组长度不足时,需要注意此时应该讲长度也修改,故我修改了这种方式,即下面一个升级版.

  1 package com.bao.structure.stack;
  2 
  3 
  4 /**
  5  * 使用数组实现栈.升级版<br />
  6  * 删除数组长度的变量,防止在进行操作时没用同时修改而出错.
  7  * 
  8  * @title ArrayStack.java
  9  * @package com.bao.structure.stack.impl
 10  * @author Eason
 11  * @version v1.0, 2013-6-18
 12  * @param <E>
 13  * @see com.bao.structure.stack.ArrayStack
 14  */
 15 public class ArrayStackX<E> implements Stack<E> {
 16 
 17     private E[] items;
 18     private int top;
 19     private static final int DEFAULT_CAPACITY = 10;
 20 
 21     public ArrayStackX() {
 22         this(DEFAULT_CAPACITY);
 23     }
 24 
 25     @SuppressWarnings("unchecked")
 26     public ArrayStackX(int capacity) {
 27         items = (E[]) new Object[capacity];
 28         top = -1;
 29     }
 30 
 31     /**
 32      * Make the stack logically empty.<br />
 33      * 清空数组数据.
 34      */
 35     public void clear() {
 36         for (int i = 0; i < items.length; i++) {
 37             items[i] = null;
 38         }
 39         //items = (E[]) new Object[0];
 40         top = -1;
 41     }
 42 
 43     /**
 44      * Test if the stack is logically empty.
 45      * 
 46      * @return true if empty, false otherwise.
 47      */
 48     public boolean empty() {
 49         return top == -1;
 50     }
 51 
 52     /**
 53      * Get the most recently inserted item in the stack.<br />
 54      * throw ArrayIndexOutOfBoundsException if the stack is empty.
 55      * 
 56      * @return E the most recently inserted element.
 57      */
 58     public E peek() {
 59         if (empty()) {
 60             throw new ArrayIndexOutOfBoundsException("Stack underflow.");
 61         }
 62 
 63         return items[top];
 64     }
 65 
 66     /**
 67      * remove the most recently inserted element from the stack. <br />
 68      * throw ArrayIndexOutOfBoundsException if the stack is empty.
 69      * 
 70      * @return E the most recently inserted element.
 71      */
 72     public E pop() {
 73         if (empty()) {
 74             throw new ArrayIndexOutOfBoundsException("Stack underflow.");
 75         }
 76 
 77         return items[top--];
 78     }
 79 
 80     /**
 81      * Insert a new element into the stack.
 82      */
 83     public void push(E e) {
 84         if (top + 1 == items.length) {
 85             extendArray();
 86         }
 87         items[++top] = e;
 88     }
 89 
 90     /**
 91      * Internal method to extend the array.<br />
 92      * 扩展数组.扩大一倍多一,为房子容量为0的情况.
 93      */
 94     private void extendArray() {
 95         @SuppressWarnings("unchecked")
 96         E[] newDoubleArray = (E[]) new Object[items.length * 2 + 1];
 97         for (int i = 0; i < items.length; i++) {
 98             newDoubleArray[i] = items[i];
 99         }
100         items = newDoubleArray;
101     }
102 
103     public String toString() {
104         StringBuffer result = new StringBuffer("[");
105 
106         for (int i = top; i > -1; i--) {
107             result.append(items[i]).append(" ");
108         }
109 
110         result.append("]");
111         return result.toString();
112     }
113 
114 }
ArrayStackX.java

如果有new hand,可以自己看一下,不要被这么多代码吓着,其实还是蛮简单的.

栈也有很多应用.比如单词逆序,即将一个单词以从右向左的顺序显示.在此我也写出来,不过大家可以想想,然后再看,非常简单的.

 1 package intelligence.stack;
 2 
 3 import com.bao.structure.stack.ArrayStackX;
 4 import com.bao.structure.stack.Stack;
 5 
 6 /**
 7  * 单词逆序.
 8  *
 9  * @title:         WordReverse.java
10  * @package:     intelligence.stack
11  * @author:        Eason
12  * @date:        2013-6-18
13  * @version:    v1.0
14  */
15 public class WordReverse {
16 
17     public static String reverse(String arg) {
18 
19         String result = "";
20 
21         if (arg == null) {
22             throw new NullPointerException("arg is null.");
23         }
24 
25         Stack<Character> stack = new ArrayStackX<Character>();
26 
27         for (int i = 0; i < arg.length(); i++) {
28             stack.push(arg.charAt(i));
29         }
30 
31         while (!stack.empty()) {
32             Character ch = stack.pop();
33             result = result + ch;
34         }
35 
36         return result;
37     }
38 
39     public static void main(String[] args) {
40 
41         String reverseWord = WordReverse.reverse("goodgoodgoodgoodgoodgoodgoodgoodgoodgood");
42         System.out.println(reverseWord);
43 
44     }
45 
46 }
WordReverse

好了,今天的栈就到这里,如果有些写的不正确的,请各位高手指正,毕竟小弟才疏学浅.谢谢

 

posted @ 2013-06-30 16:05  葛一凡  阅读(183)  评论(0编辑  收藏  举报