栈Stack的实现【java】
In computer science, a stack is a last in, first out (LIFO) abstract data type and linear data structure. A stack can have any abstract data type as an element, but is characterized by only three fundamental operations: push, popand stack top. The push operation adds a new item to the top of the stack, or initializes the stack if it is empty. If the stack is full and does not contain enough space to accept the given item, the stack is then considered to be in an overflow state. The pop operation removes an item from the top of the stack. A pop either reveals previously concealed items, or results in an empty stack, but if the stack is empty then it goes into underflow state (It means no items are present in stack to be removed). The stack top operation gets the data from the top-most position and returns it to the user without deleting it. The same underflow state can also occur in stack top operation if stack is empty.
A stack is a restricted data structure, because only a small number of operations are performed on it. The nature of the pop and push operations also means that stack elements have a natural order. Elements are removed from the stack in the reverse order to the order of their addition: therefore, the lower elements are those that have been on the stack the longest.
---from wikipedia(http://en.wikipedia.org/wiki/Stack_(data_structure))
栈的java实现:
1 package com.matrix.stack;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 public class Stack {
7
8 // 栈的数据区
9 private List<Object> list;
10 // 栈顶指针
11 private int topPointer;
12
13 public Stack() {
14 this.list = new ArrayList();
15 this.topPointer = -1;
16 }
17
18 public Stack(List<Object> list, int topPointer) {
19 if (null == list) {
20 list = new ArrayList();
21 }
22 this.list = list;
23 this.topPointer = list.size() - 1;
24 }
25
26 /**
27 * 入栈方法
28 *
29 * @Description: TODO
30 * @param @param Object
31 * @return void
32 * @throws
33 * @author Matrix
34 */
35 public void push(Object Object) {
36 this.list.add(Object);
37 topPointer++;
38 }
39
40 /**
41 * 出栈方法
42 *
43 * @Description: TODO
44 * @param @return
45 * @return Object
46 * @throws
47 * @author Matrix
48 */
49 public Object pop() {
50 if (!isEmpty()) {
51 Object obj = this.list.remove(this.topPointer);
52 this.topPointer--;
53 return obj;
54 } else {
55 return null;
56 }
57 }
58
59 /**
60 * 判断栈是否为空
61 *
62 * @Description: TODO
63 * @param @return
64 * @return boolean
65 * @throws
66 * @author Matrix
67 */
68 public boolean isEmpty() {
69 if (topPointer == -1) {
70 return true;
71 } else {
72 return false;
73 }
74 }
75
76 /**
77 * 获取栈的大小
78 *
79 * @Description: TODO
80 * @param @return
81 * @return int
82 * @throws
83 * @author Matrix
84 */
85 public int getStackSize() {
86 return this.list.size();
87 }
88
89 /**
90 * 返回栈顶元素
91 *
92 * @Description: TODO
93 * @param @return
94 * @return Object
95 * @throws
96 * @author Matrix
97 */
98 public Object top() {
99 if (topPointer == -1) {
100 return null;
101 } else {
102 return list.get(topPointer);
103 }
104 }
105
106 /**
107 * 打印栈的内部数据信息
108 *
109 * @Description: TODO
110 * @param
111 * @return void
112 * @throws
113 * @author Matrix
114 */
115 public void printStack() {
116 if (this.list.size() > 0) {
117 System.out.println("The data int stack is:");
118 for (int i = 0; i < list.size(); i++) {
119 System.out.print(this.list.get(i).toString() + " ");
120 }
121 System.out.println();
122 } else {
123 System.out.println("The stack is empty!");
124 }
125 }
126
127 public List<Object> getList() {
128 return list;
129 }
130
131 public void setList(List<Object> list) {
132 this.list = list;
133 }
134
135 public int getTopPointer() {
136 return topPointer;
137 }
138
139 public void setTopPointer(int topPointer) {
140 this.topPointer = topPointer;
141 }
142 }
测试类
1 package com.matrix.stack;
2
3 import org.junit.Test;
4
5 public class StackTest {
6
7 @Test
8 public void stackTest(){
9 Stack stack= new Stack();
10 int i=5;
11 stack.push(i);
12 String str="Matrix";
13 stack.push(str);
14 double d=1.2345;
15 stack.push(d);
16 System.out.println(stack.top());
17 stack.printStack();
18 stack.pop();
19 stack.printStack();
20 stack.pop();
21 stack.printStack();
22 stack.pop();
23 stack.printStack();
24 }
25 }
一步一步往上爬......