链式实现:
在栈的一段添加和删除元素,在栈中维护一个指向栈顶的结点和一个count变量指示栈的大小:
private LinearNode top; //指向栈顶
private int count;//标记栈的大小
每次出栈和压栈在链表的表头:(也可以再表尾,实现方式不一样而已)
top--->元素1--->元素2--->元素3.........
实现(附带测试main):
LinkedStack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
package Stack; import Bag.LinearNode; //为了重点来实现算法,将异常情况直接打印出然后退出程序,不再声明异常类 public class LinkedStack implements StackADT { private LinearNode top; //指向栈顶 private int count; //标记栈的大小 public static void main(String[] args){ LinkedStack stack = new LinkedStack(); System.out.println( "将0到10依次压栈" ); for ( int i = 0 ;i < 10 ;i++) stack.push(i); System.out.println( "连续执行5次出栈操作" ); for ( int i = 0 ;i < 5 ;i++) stack.pop(); System.out.println( "栈为空吗?: " + stack.isEmpty()); System.out.println( "栈的大小为: " + stack.size()); System.out.println( "栈顶元素为: " + stack.top.getElement()); System.out.println( "栈顶元素为: " + stack.peek()); } public LinkedStack() { top = null ; count = 0 ; } public int size() { return count; } public boolean isEmpty() { return (size() == 0 ); } public void push(Object element) { LinearNode node = new LinearNode(element); node.setNext(top); top = node; count++; } public Object pop() { if (isEmpty()) { System.out.println( "stack is empty!" ); System.exit( 1 ); } Object result = top.getElement(); top = top.getNext(); count--; return result; } public Object peek() { Object result = top.getElement(); return result; } } |
运行结果:
将0到10依次压栈
连续执行5次出栈操作
栈为空吗?: false
栈的大小为: 5
栈顶元素为: 4
栈顶元素为: 4
数组实现:
栈底总是数组下标为0的位置,入栈出栈从数组下标的最后一个元素开始:
1
2
|
private Object[] contents; private int top; //top标记下一个入栈的位置,同时也表示栈的容量大小,跟链式实现的count比较一下!!! |
实现(附带测试main):
ArrayStack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
package Stack; public class ArrayStack implements StackADT { private Object[] contents; private int top; //top标记下一个入栈的位置,同时也表示栈的容量大小,跟链式实现的count比较一下!!! private static int SIZE = 10 ; public ArrayStack() { contents = new Object[SIZE]; top = 0 ; } public void expand(){ //借助于申请一个辅助空间,每次扩展容量一倍 Object[] larger = new Object[size()* 2 ]; for ( int index = 0 ;index < top;index++) larger[index] = contents[index]; contents = larger; } public int size() { return top; } public boolean isEmpty() { return (size() == 0 ); } public void push(Object element) { //if(isEmpty()) //expand(); if (top == contents.length) expand(); contents[top] = element; top++; } public Object pop() { if (isEmpty()) { System.out.println( "stack is empty!" ); System.exit( 1 ); } Object result = contents[top- 1 ]; contents[top- 1 ] = null ; //出栈 top--; return result; /*书上这样写简便一点::: * top--; * Object result = contents[top]; * contents[top] = null;*/ } public Object peek() { Object result; if (isEmpty()) result = null ; else result = contents[top- 1 ]; return result; } public static void main(String[] args) { ArrayStack stack = new ArrayStack(); System.out.println( "将0到24依次压栈,然后连续10次出栈" ); for ( int i = 0 ;i < 25 ;i++) stack.push(i); for ( int i = 0 ;i < 10 ;i++) stack.pop(); System.out.println( "栈的大小为: " + stack.size()); System.out.println( "栈为空吗?: " + stack.isEmpty()); System.out.println( "栈顶元素为: " + stack.peek()); } } |
运行结果:
将0到24依次压栈,然后连续10次出栈
栈的大小为: 15
栈为空吗?: false
栈顶元素为: 14
使用集合LinkedList来模拟栈
方法
java的泛型可以让LinkedList模拟存储各种数据类型的栈,包括int,double,String,Object等等,介绍一下几种用到的API接口:
入栈
1
|
void addFirst(E e); // 将指定元素插入此列表的开头 |
获取栈顶元素
1
|
E getFirst(); // 返回此列表的第一个元素 |
出栈
1
|
E removeFirst(); // 移除并返回此列表第一个元素 |
判栈空
1
|
boolean isEmpty(); // 判断栈空 |
示例代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
import java.util.LinkedList; import java.util.NoSuchElementException; public class SimulateStack { private LinkedList<Integer> stack = new LinkedList<Integer>(); public boolean isEmpty() { return this .stack.isEmpty(); } public void push( int data) { this .stack.addFirst(data); } public int pop() throws NoSuchElementException{ return this .stack.removeFirst(); } public int getTop() throws NoSuchElementException{ return this .stack.getFirst(); } public static void main(String args[]) { SimulateStack s = new SimulateStack(); s.push( 1 ); s.push( 2 ); s.push( 3 ); while (! s.isEmpty()) { int data = s.getTop(); System.out.println(data); s.pop(); } } } |