《算法导论》学习笔记之Chapter10基本数据结构---栈的数组实现

来源:互联网 发布:淘宝神评论 编辑:程序博客网 时间:2024/04/30 00:59

下面用数组实现栈的代码:

public class Stack {/** * 本类是利用数组实现的栈类,实现的功能包括: 栈是否为空;是否为满;压入;弹出;peek;得到栈大小数;栈顶位置; *  * @author FX * @return */// 存放元素的数组int[] a;// 栈的最大长度private int size;// 栈顶位置private int top;// 构造方法public Stack(int size) {this.size = size;a = new int[size];top = -1;}public int getSize() {return size;}// 获取栈顶的位置public int getTop() {return top;}// 栈是否为空public boolean IsEmpty() {return top == -1;}// 栈是否为满public boolean IsFull() {return (top + 1) == size;}// 栈的压入public boolean push(int data) {if (IsFull()) {System.out.println("the stack is full!");return false;} else {top++;this.a[top] = data;return true;}}// 栈的弹出public int pop() throws Exception {if (IsEmpty()) {throw new Exception("the stack is empty!");} else {return this.a[top--];}}// 获取栈顶元素,但不弹出public int peek() {return this.a[top];}public static void main(String[] args) {// TODO Auto-generated method stubStack s = new Stack(5);s.push(0);s.push(1);s.push(2);s.push(3);s.push(4);// 注意下面一次压入操作,会返回false,因为此时栈已满,已有5个元素s.push(5);System.out.println("the top number is: " + s.peek());while (!s.IsEmpty()) {try {System.out.println(s.pop());} catch (Exception e) {e.printStackTrace();}}}}


代码输出结果:

the stack is full!
the top number is: 4
4
3
2
1
0

上面代码是一个数组实现一个栈的实现过程,更有意思的是,一个数组可以实现2个,乃至3个栈操作。下面分别给出自己在一个数组上实现2个和3个栈的实现:

2个栈实现思想:这个思路可以有三种:第一种栈1填充数组偶数位,栈2填充数组奇数位;第二种栈1和栈2均从数组中间开始向两边填充;第三种就是如下思路:

针对一个长度为n的数组来说,第一个栈从数组的0下标处开始压入,以后每压入一个元素下标+1;第二个栈则从数组的n-1下标开始压入,以后每压入一个元素下标-1;即栈1向右延伸,栈2向左延伸,当两个栈的元素之和<n时,可以持续压入,否则栈满。第三种思路的代码实现如下:


public class Stack2 {/** * 本类实现利用一个数组实现2个栈操作 *  * @author FX * @param args */// 数组int[] a;// 数组a的长度private int n;// 栈1的栈顶位置private int top1;// 栈2的栈顶位置private int top2;// 构造方法public Stack2(int n) {this.n = n;a = new int[n];top1 = -1;top2 = n;}public int getSize() {return n;}// 获取栈顶的位置public int getTop1() {return top1;}public int getTop2() {return top2;}// 栈是否为空,如果栈1和栈2都是空则为空public boolean IsEmpty() {return (top1 == -1) && (top2 == n);}// 栈是否为满,如果栈1的栈顶与栈2的栈顶已经挨着,则代表栈已满public boolean IsFull() {return (top1 + 1) == top2;}// 栈的压入public boolean push1(int data) {if (IsFull()) {System.out.println("the stack is full!");return false;} else {top1++;this.a[top1] = data;return true;}}public boolean push2(int data) {if (IsFull()) {System.out.println("the stack is full!");return false;} else {top2--;this.a[top2] = data;return true;}}// 栈的弹出public int pop1() throws Exception {if (IsEmpty()) {throw new Exception("the stack is empty!");} else {return this.a[top1--];}}public int pop2() throws Exception {if (IsEmpty()) {throw new Exception("the stack is empty!");} else {return this.a[top2++];}}// 获取栈顶元素,但不弹出public int peek1() {return this.a[top1];}public int peek2() {return this.a[top2];}public static void main(String[] args) throws Exception {// TODO Auto-generated method stubStack2 s = new Stack2(7);s.push1(0);s.push1(1);s.push1(2);s.push1(3);s.push1(4);s.push2(5);s.push2(6);s.push2(7);//这一行会出现the stack is full!的提示s.pop1();s.pop1();s.pop1();s.pop1();s.pop1();s.pop2();s.pop2();s.pop2();//这一行会出现n" java.lang.Exception: the stack is empty!错误提示s.pop2();}}

针对一个数组实现3个栈,思想:有一种较为简单的思路,就是按照固定比例进行分割,比如每个栈占数组总长度的1/3之类的比例,这样栈1分配空间为数组的前三分之一,栈2空间为数组的中间三分之一,栈3空间为数组的后三分之一,这个思路的实现与上述思路类似,就不再赘述。还有一种就是动态改变数组空间,这种思想代码实现较为复杂,我就在这里提供一下别人写的一篇博客学习吧。http://blog.csdn.net/qfikh/article/details/53130018


阅读全文
1 0
原创粉丝点击