数据结构学习笔记——stack实现(数组篇)

一 栈:是一种表,限制插入和删除只能在一个位置,也即是表的末端(也是栈的顶)进行。

      基本操作:push 和 pop。

二 栈的数组实现

      运用数组来存储元素,和栈操作先关的是theArray(一个数组实例)和topOfStack(指向栈顶元素,对于空栈,它的值是-1)。

      push操作:将某个元素 item 推入栈中,使得 topOfStack 增1然后置 theArray[topOfStack] = item.

      pop操作: 将栈顶严肃弹出,我们置返回值为 theArray[topOfStack],然后 topOfStack 减1.

三 时间复杂度

     显然都是常数时间运行,O(1)。

java代码实现:

  1 package com.xuyunyu.demo;
  2 /**
  3  * 
  4  * generic type class MyStack to implement the stack with array
  5  * @author Administrator
  6  *
  7  * @param <AnyType>
  8  */
  9 public class MyStack<AnyType>
 10 {
 11     /**
 12      * the capacity of the stack
 13      */
 14     private static  final int DEFAULT_CAPACITY = 10;
 15     /**
 16      * the array to store the elements
 17      */
 18     private AnyType[] theItems;
 19     /**
 20      * the pointer to point to the top of the stack
 21      */
 22     private int TopOfStack;
 23     /**
 24      * constructor
 25      * 
 26      * to initial the stack
 27      */
 28     public MyStack ()
 29     {
 30         clear();
 31     }
 32     /**
 33      * clear the stack
 34      */
 35     public void clear()
 36     {
 37         theItems = ( AnyType [] ) new Object[DEFAULT_CAPACITY];
 38         TopOfStack = -1;
 39     }
 40     /**
 41      * get the capacity of the stack
 42      * @return
 43      */
 44     public int size()
 45     {
 46         return DEFAULT_CAPACITY;
 47     }
 48     /**
 49      * get the actual counts of 
 50      * the element stored in the stack
 51      * @return
 52      */
 53     public int getCountOfElement()
 54     {
 55         return TopOfStack + 1;
 56     }
 57     /**
 58      * get the top element of the stack
 59      * @return
 60      */
 61     public AnyType getTopElement()
 62     {
 63         return theItems[TopOfStack];
 64     }
 65     /**
 66      * judge if the stack is empty
 67      * @return true if the stack is empty,false or not
 68      */
 69     public boolean isEmpty()
 70     {
 71         return TopOfStack == -1;
 72     }
 73     /**
 74      * judge if the stack is full
 75      * @return true if the stack is full,false or not 
 76      */
 77     public boolean isFull()
 78     {
 79         return (TopOfStack + 1) == theItems.length;
 80     }
 81     /**
 82      * push an item to the stack
 83      * the TopOfStack INC ,and then store the item
 84      * @param item
 85      */
 86     public void push(AnyType item)
 87     {
 88         if(isFull())
 89             throw new IndexOutOfBoundsException();        
 90         TopOfStack++;
 91         theItems[TopOfStack] = item;
 92     }
 93     /**
 94      * return the item on the top and then make one decrement of the TopOfStack
 95      * @return
 96      */
 97     public AnyType pop()
 98     {
 99         if(isEmpty() )
100             throw new IndexOutOfBoundsException();
101         
102         AnyType ItemPulled = theItems[TopOfStack];
103         theItems[TopOfStack] = null;
104         TopOfStack--;        
105         return ItemPulled;
106     }
107     
108     
109 
110 }

注意:这是一个泛型类,更加具有复用性。

四 测试代码

 1 package com.xuyunyu.demo;
 2 
 3 
 4 public class test_MyStack
 5 {
 6     public static void main(String[] args)
 7     {
 8         MyStack<Integer> stack = new MyStack<Integer>();
 9         System.out.println("counts of the stack: " + stack.getCountOfElement());
10         System.out.println("size of the stack: " + stack.size());
11         //System.out.println("top element of the stack: " + stack.getTopElement());
12         
13         stack.push(17);
14         System.out.println("counts of the stack: " + stack.getCountOfElement());
15         System.out.println("top element of the stack: " + stack.getTopElement());
16         
17         stack.push(30);
18         System.out.println("counts of the stack: " + stack.getCountOfElement());
19         System.out.println("top element of the stack: " + stack.getTopElement());
20         
21         stack.clear();
22         System.out.println("counts of the stack: " + stack.getCountOfElement());
23         
24         System.out.println("push to the stack:" );
25         for (int i = 0; i < stack.size(); i++)
26         {    
27             System.out.print(" " + i);
28             stack.push(i);
29         }
30         System.out.println();
31         
32         System.out.println("pop from the stack:" );
33         for (int i = 0; i < stack.size(); i++)
34         {
35             System.out.print(" " + stack.pop());
36         }
37         
38     }
39 
40 }

期待输出如下:

counts of the stack: 0
size of the stack: 10
counts of the stack: 1
top element of the stack: 17
counts of the stack: 2
top element of the stack: 30
counts of the stack: 0
push to the stack
 0 1 2 3 4 5 6 7 8 9
pop from the stack
 9 8 7 6 5 4 3 2 1 0

结论:

显然实现了stack的后进先出的性质。

栈可以应用到平衡符号,中缀到后缀的转换以及方法调用上。详情可参考Mark Allen Weisss的《数据结构与算法分析——java语言描述》。 本文部分内容页参考此书。

 

posted @ 2013-10-14 21:24  加油好男儿  阅读(365)  评论(0编辑  收藏  举报