栈
栈的介绍
- 栈的英文名称为stack
- 栈是一个先入后出的有序列表
- 栈 是 限制 线性表中元素的插入和删除 ,只能在线性表的同一端进行的一种特殊线性表。允许插入和删除的一端,为变化的一端,称之为栈顶,另一端为固定的一端,称之为栈底
- 根据栈的定义可知,最先放入栈中的元素在栈底,最后放入的元素在栈顶,而删除元素刚好相反,最后放入的元素先删除,最先放入的元素最后删除
栈的应用场景举例
- 子程序的调用:在跳往子程序前,会先将下个指令的地址存到堆栈中,知道子程序执行完后再将地址取出,以回到原来的程序中
- 处理递归调用:和子程序的调用类似,只是除了存储下一个指令的地址外,也将参数、区域变量等数据存入堆栈中
- 表达式的转换(中缀表达式转后缀表达式)与求值
- 二叉树的遍历
- 图形的深度优先(depth-first)搜索法
栈的代码实现及其分析
代码实现
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(5);
Scanner scanner = new Scanner(System.in);
boolean flag = true;
while (flag){
System.out.println("1、添加数据进栈,2、删除栈顶数据,3、显示栈中数据,4、退出");
System.out.println("请选择功能:");
int i = scanner.nextInt();
switch (i) {
case 1:
System.out.println("请输入要入栈的数据:");
int value = scanner.nextInt();
arrayStack.push(value);
break;
case 2:
arrayStack.pop();
break;
case 3:
arrayStack.list();
break;
case 4:
flag = false;
break;
}
}
}
}
//定义一个类表示栈结构
class ArrayStack{
private int maxSize;//栈的大小
private int[] stack;//数组模拟栈,数据存放到数组当中
private int top = -1;//top表示栈顶,初始化为-1
//栈的构造器
public ArrayStack(int maxSize){
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//判断栈满
public boolean isFull(){
return top == maxSize-1;
}
//判断栈空
public boolean isEmpty(){
return top == -1;
}
//入栈
public void push(int value){
if (isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//出栈,将栈顶的数据返回
public int pop(){
if (isEmpty()){
//抛出异常
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
//遍历栈,,,遍历时需要从栈顶开始显示数据
public void list(){
if (isEmpty()){
System.out.println("栈空,没有数据");
}
for (int i = top; i >= 0; i--) {
System.out.println("stack"+i+":"+stack[i]);
}
}
}
栈的练习题目:
思路分析
使用中缀表达式进行实现
代码示范:
public class Calculator {
public static void main(String[] args) {
String expression = "70+2*6-4";
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
int index = 0;//扫描索引
int num1 = 0;
int num2 = 0;
int oper = 0;
int res = 0;
char ch = ' ';
String keepNum = "";
while (true){
ch = expression.substring(index,index+1).charAt(0);
if (operStack.isOper(ch)){
if (!operStack.isEmpty()){
if (operStack.priority(ch) <= operStack.priority(operStack.peek())){
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1,num2,oper);
numStack.push(res);
operStack.push(ch);
}else {
operStack.push(ch);
}
}else {
operStack.push(ch);
}
}else {
keepNum += ch;
if (index == expression.length() -1){
numStack.push(Integer.parseInt(keepNum));
}else {
if (operStack.isOper(expression.substring(index+1, index+2).charAt(0))){
//如果后一位时运算符,则入栈keepNum
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
index++;
if (index >= expression.length()){
break;
}
}
while (true){
if (operStack.isEmpty()){
break;
}
num1 = numStack.pop();
num2 = numStack.pop();
oper = operStack.pop();
res = numStack.cal(num1,num2,oper);
numStack.push(res);
}
System.out.println(expression+"="+numStack.pop());
}
}
//
//定义一个类表示栈结构,需要扩展功能
class ArrayStack2{
private int maxSize;//栈的大小
private int[] stack;//数组模拟栈,数据存放到数组当中
private int top = -1;//top表示栈顶,初始化为-1
//栈的构造器
public ArrayStack2(int maxSize){
this.maxSize = maxSize;
stack = new int[this.maxSize];
}
//查看当前栈顶的值,注意不是pop
public int peek(){
return stack[top];
}
//判断栈满
public boolean isFull(){
return top == maxSize-1;
}
//判断栈空
public boolean isEmpty(){
return top == -1;
}
//入栈
public void push(int value){
if (isFull()){
System.out.println("栈满");
return;
}
top++;
stack[top] = value;
}
//出栈,将栈顶的数据返回
public int pop(){
if (isEmpty()){
//抛出异常
throw new RuntimeException("栈空");
}
int value = stack[top];
top--;
return value;
}
//遍历栈,,,遍历时需要从栈顶开始显示数据
public void list(){
if (isEmpty()){
System.out.println("栈空,没有数据");
}
for (int i = top; i >= 0; i--) {
System.out.println("stack"+i+":"+stack[i]);
}
}
//定义一个方法用于进行运算符的优先级,优先级用数字表示
//数字越大,则优先级越高
public int priority(int oper){
if (oper == '*' || oper == '/'){
return 1;
}else if (oper == '+' || oper == '-'){
return 0;
}else {
return -1;//目前规定表达式中只包含加减乘除
}
}
//判断是否是一个运算符
public boolean isOper(char val){
return val == '+' || val == '-' || val == '*' || val == '/';
}
//计算方法
public int cal(int num1, int num2, int oper){
int res = 0;//用于存放计算的结果
switch (oper){
case '+':
res = num1 +num2;
break;
case '-':
res = num2 - num1;//注意顺序
break;
case '*' :
res = num1 * num2;
break;
case '/':
res = num2/num1;
break;
}
return res;
}
}