数据结构之栈

介绍了栈的基本操作

顺序栈:stackElement是对象数组,top指向的栈顶元素的下一个位置

链栈:Node top指向的是栈顶元素

 

 

IStack接口

package com.itheima.a_z;

public interface IStack {
	public void clear();//置空
	public boolean isEmpty();//判断是否为空
	public int length();//栈的长度
	public Object peek();//取栈顶元素
	public void push(Object x)throws Exception;//入栈
	public Object pop();//出栈
	public void display();//查看栈内元素
}

1.顺序栈的基本操作

package com.itheima.a_z;

public class SqStack implements IStack {

	// 对象数组
	private Object[] stackElement;
	// 在非空栈中,top始终指向栈顶元素的下一个存储位置
	// 在空栈中,top的值为0
	private int top;

	// 栈的构造函数,构造一个存储空间容量为maxSize的空栈
	public SqStack(int maxSize) {
		stackElement = new Object[maxSize];
	}

	// 栈置空
	@Override
	public void clear() {
		top = 0;
	}

	// 判栈是否为空
	@Override
	public boolean isEmpty() {
		return top == 0;
	}

	// 栈的元素个数
	@Override
	public int length() {
		return top;
	}

	// 取栈顶元素
	@Override
	public Object peek() {
		// 栈非空时
		if (!isEmpty()) {
			// 取出栈顶元素
			return stackElement[top - 1];
		} else {
			return null;
		}
	}

	// 入栈
	@Override
	public void push(Object x) throws Exception {
		// 栈满,抛出异常
		if (top == stackElement.length) {
			throw new Exception("栈已满");
		} else {
			stackElement[top] = x;
			++top;
		}
	}

	// 出栈
	@Override
	public Object pop() {
		// 非空时
		if (!isEmpty()) {
			--top;
			return stackElement[top];
		}
		return null;
	}

	// 打印栈中元素
	@Override
	public void display() {
		for (int i = top - 1; i >= 0; i--) {
			System.out.print(stackElement[i] + " ");
		}
	}
}

2.链栈

结点类

package com.itheima.a_z;

public class Node {

	// 存放结点的值
	private Object data;
	// 后继结点的引用
	private Node next;

	public Node() {
		super();
	}

	public Node(Object data) {
		super();
		this.data = data;
	}

	public Node(Object data, Node next) {
		super();
		this.data = data;
		this.next = next;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public Node getNext() {
		return next;
	}

	public void setNext(Node next) {
		this.next = next;
	}

}

实现类LinkStack

package com.itheima.a_z;

public class LinkStack implements IStack {

	// 栈顶元素的引用
	private Node top;

	// 将栈置空
	@Override
	public void clear() {
		top = null;
	}

	// 判断栈是否为空
	@Override
	public boolean isEmpty() {
		return top == null;
	}

	// 判断链栈的长度
	@Override
	public int length() {
		// 初始化,p指向栈顶元素,j为计数器
		Node p = top;
		int j = 0;
		// 非空时
		while (p != null) {
			// p指向下一个结点
			p = p.getNext();
			// 计数器+1
			++j;
		}
		return j;
	}

	// 取栈顶元素(top就是指向栈顶的)
	@Override
	public Object peek() {

		if (!isEmpty()) {
			return top.getData();
		} else {
			return null;
		}

	}

	// 入栈
	public void push(Object x) throws Exception {
		// 创建一个新结点
		Node p = new Node(x);
		// 将p的后继设为top
		p.setNext(top);
		// top指向栈顶
		top = p;

	}

	// 自己的测试类
	public Object test() {
		top = top.getNext();
		return top.getData();
	}

	// 出栈
	@Override
	public Object pop() {
		// 非空时
		if (!isEmpty()) {
			// top,p都是指向栈顶
			Node p = top;
			// top前移
			top = top.getNext();
			// 返回原栈顶元素,即删除的元素
			return p.getData();
		} else {
			return null;
		}

	}

	// 打印栈内元素
	@Override
	public void display() {
		// p指向栈顶
		Node p = top;
		while (p != null) {
			// 打印栈顶元素
			System.out.print(p.getData() + " ");
			// 栈顶前移
			p = p.getNext();
		}
		// 换行
		System.out.println();
	}

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值