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

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

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 stub
		Stack 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 stub
		Stack2 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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值