Java数据结构——栈

1. 栈的定义及用法

1.1 定义

  1. (stack)是限定仅在表尾进行插入或删除操作的线性表。
  2. 来说,表尾被称为:栈顶(top),表头被称为:栈底(bottom)
  3. 不含元素的空表被称为空栈

1.2 特点

  1. 后进先出
  2. 简称:LIFO结构

1.3 图例

在这里插入图片描述


2. C语言下的栈

2.1 栈的定义及初始

#define STACK_SIZE = 10;
typedef struct {
	SElemType *base;
	SElemType *top;
	int stacksize;
}SqStack;
Status InitStack(SqStack &S) {
	S.base = (SElemType*)malloc(STACK_SIZE*sizeof(SElemType));
	if(!S.base) return -2;
	S.top = S.base;
	S.stacksize = STACK_SIZE;
	return OK;
}//InitStack

2.2 取栈顶元素

Status GetTop(SqStack S, SElemType &e) {
	if(S.top==S.base) return ERROR;
	e = *(S.top-1);
	return OK;
}//GetTop

2.3 入栈

Status Push(SqStack &S, SElemType e) {
	if((S.top-S.base)>=S.stacksize) { //栈满 
		S.base = (SElemType*)realloc(S.base,
					(STACK_SIZE+10)*sizeof(SElemType));
		if(!S.base) return -2;
		S.top = S.base + S.stacksize;
		S.stacksize += 10; 
	}
	*S.top++ = e;
	return OK;
}//Push

2.4 出栈

Status Pop(SqStack &S, SElemType &e) {
	if(S.top==S.base) return ERROR;
	e = *--S.top;
	return OK;
}//Pop

2.5 判断空栈

Status StackEmpty(SqStack &S) {
	if(S.top==S.base) {
		printf("栈空\n");
		return OK;
	}
	else return ERROR;
}//StackEmpty

3. Java语言下的栈

3.1 构造一个空栈

	/**
	 * 构造一个空栈
	 */
	SqStack() {
		this.stacksize = 0;
		this.data = new int[STACK_SIZE];
	}//of SqStack

3.2 便历,输出,重写toString()

	/**
	 * 便历,输出,重写toString()
	 * 
	 * @author NoBug
	 * @return result data数组里面的值,以字符串形式输出
	 */
	public String toString() {
		String result = "";
		for (int i = 0; i < this.stackSize; i++) {
			result += data[i] + " ";
		}

		return result;
	}// of toString

3.3 入栈push

	/**
	 * 入栈push
	 * 
	 * @param elem 表示待入栈的元素
	 * @return 成功或失败
	 */
	public boolean push(int elem) {
		if (this.stackSize == STACK_SIZE) {
			System.out.println("Stack full.");
			return false;
		} // Stack full
		this.data[this.stackSize] = elem;
		this.stackSize++;

		return true;
	}// of push

2.4 出栈pop

	/**
	 * 出栈pop
	 * 
	 * @author NoBug
	 * @return 删除栈顶,并返回栈顶值
	 */
	public int pop() {
		if (this.stackSize == 0) {
			System.out.println("StackEmpty.");
			return '\0';
		} // of StackEmpty

		int elem = data[this.stackSize - 1];
		this.stackSize--;

		return elem;
	}// of pop

3.5 判断栈是否为空

	/**
	 * 判断栈是否为空
	 * 
	 * @author NoBug
	 * @return true or false 空或者不空
	 */
	public boolean isEmpty() {
		if (this.stackSize == 0) {

			return true;
		} // Empty
		else {

			return false;
		}
	}// of isEmpty

4. Java源码

4.1 SqStack类

package datastructure.stack;

/**
 * 线性栈
 * 
 * @author NoBug
 * @version 2.0
 * @time 2022/3/9
 * 
 */
public class SqStack {

	/** 栈最大长度 */
	public static final int STACK_SIZE = 15;

	/** 栈实际长度 */
	public int stackSize;

	/** 栈工作空间 */
	public int[] data;

	/**
	 * 构造一个空栈
	 */
	SqStack() {
		this.stackSize = 0;
		this.data = new int[STACK_SIZE];
	}// of SqStack

	/**
	 * 便历,输出,重写toString()
	 * 
	 * @author NoBug
	 * @return result data数组里面的值,以字符串形式输出
	 */
	public String toString() {
		String result = "";
		for (int i = 0; i < this.stackSize; i++) {
			result += data[i] + " ";
		}

		return result;
	}// of toString

	/**
	 * 入栈push
	 * 
	 * @param elem 表示待入栈的元素
	 * @return 成功或失败
	 */
	public boolean push(int elem) {
		if (this.stackSize == STACK_SIZE) {
			System.out.println("Stack full.");
			return false;
		} // Stack full
		this.data[this.stackSize] = elem;
		this.stackSize++;

		return true;
	}// of push

	/**
	 * 出栈pop
	 * 
	 * @author NoBug
	 * @return 删除栈顶,并返回栈顶值
	 */
	public int pop() {
		if (this.stackSize == 0) {
			System.out.println("StackEmpty.");
			return '\0';
		} // of StackEmpty

		int elem = data[this.stackSize - 1];
		this.stackSize--;

		return elem;
	}// of pop

	/**
	 * 判断栈是否为空
	 * 
	 * @author NoBug
	 * @return true or false 空或者不空
	 */
	public boolean isEmpty() {
		if (this.stackSize == 0) {

			return true;
		} // Empty
		else {

			return false;
		}
	}// of isEmpty

	public static void main(String[] args) {
		SqStack S = new SqStack();
		S.push(1);
		S.push(2);
		S.push(3);
		S.push(4);
		System.out.println("入栈,并打印栈:" + S.toString());
		int temp = S.pop();
		System.out.println("出栈,并打印栈顶元素:" + temp);
		S.isEmpty();
	}

} // SqStack
入栈,并打印栈:1 2 3 4 
出栈,并打印栈顶元素:4

4.2 输出样例

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

NoBug.己千之

鼓励,鼓励,更加努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值