概念及应用
基本操作(数组模拟栈)
package com.Eeeeye;
import java.util.Scanner;
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack arrayStack = new ArrayStack(4);
String key = "";
boolean loop = true;//控制是否退出菜单
Scanner scanner = new Scanner(System.in);
while (loop) {
System.out.println("show:显示栈");
System.out.println("exit: 退出程序");
System.out.println("push");
System.out.println("pop");
System.out.println("请输入");
key = scanner.next();
switch (key) {
case "show":
arrayStack.list();
break;
case "push":
System.out.println("请输入一个数");
int value = scanner.nextInt();
arrayStack.push(value);
break;
case "pop":
try {
int res = arrayStack.pop();
System.out.println("出栈的数据是" + res);
} catch (Exception e) {
System.out.println(e.getMessage());
}
break;
case "exit":
loop = false;
scanner.close();
default:
break;
}
}
System.out.println("程序退出了");
}
}
class ArrayStack {
private int maxSize;
private int[] stack;
private int top = -1;//数组从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("栈空无法遍历");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("stack[%d] = %d\n", i, stack[i]);
}
}
}
链表模拟栈
class LinkedListStack {
private int maxSize;
private int size;
private Node top;
public LinkedListStack(int maxSize) {
this.maxSize = maxSize;
//根据输入创建一个特定的链表
}
public LinkedListStack() {
}
public boolean isFull() {
return size == maxSize;
}
public boolean isEmpty() {
return top == null;
}
public void push(int no) {
Node node = new Node(no);
if (isFull()) {
System.out.println("栈已满");
}
if (isEmpty()) {
top = node;
} else {
node.next = top;
top = node;
}
size++;
}
public int pop() {
if (isEmpty()){
System.out.println("栈为空");
}
int no = top.no;
top = top.next;
size--;
return no;
}
public void list() {
if (isEmpty()) {
System.out.println("栈空无法遍历");
return;
}
Node temp = top;
while (true){
if (temp == null) {
break;
}
System.out.println(temp);
temp = temp.next;
}
}
/**
* 获取
*
* @return maxSize
*/
public int getMaxSize() {
return maxSize;
}
/**
* 设置
*
* @param maxSize
*/
public void setMaxSize(int maxSize) {
this.maxSize = maxSize;
}
/**
* 获取
*
* @return size
*/
public int getSize() {
return size;
}
/**
* 设置
*
* @param size
*/
public void setSize(int size) {
this.size = size;
}
public String toString() {
return "LinkedListStack{maxSize = " + maxSize + ", size = " + size + "}";
}
}
class Node {
public int no;
public Node next;
public Node() {
}
public Node(int no) {
this.no = no;
}
public String toString() {
return "Node{no = " + no + "}";
}
}
用栈完成一个简易的计算器,计算简单的表达式
package com.Eeeeye.stack;
public class Calculator {
public static void main(String[] args) {
String expression = "70+2*6-2";
ArrayStack2 numStack = new ArrayStack2(10);
ArrayStack2 operStack = new ArrayStack2(10);
//定义需要的相关变量
int index = 0;//用于扫描
int num1 = 0;
int num2 = 0;
int oper = 0;//也可以写为char
int res = 0;
char ch = ' ';//将每次扫描到的char保存到ch
String keepNum = "";
//死循环扫描expression
while (true) {
//将字符以字符串的形式取出后再转为字符
ch = expression.substring(index, index + 1).charAt(0);
//判断ch是什么,然后做相应的处理
if (operStack.isOper(ch)) {//如果是运算符
if (!operStack.isEmpty()) {
//比较优先级,如果当前操作优先级小于栈中的运算符
//就需要从数栈中pop出两个数,从符号栈中Pop出一个符号
//进行运算得到结果,此结果入数栈,然后将当前运算符入符号栈
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 {//如果是数,则直接入数栈
//1.当处理多位数时,不能发现是一个数就立即入栈,因为可能是多位数
//2.往expression后多看一位,当是运算符时,才进行入栈操作
//3.因此需要定义一个字符串变量用于拼接
//处理多位数
keepNum += ch;//在循环中储存上一个ch的值并与当前循环的ch值相加
//如果ch已经是expression的最后一位,直接入栈,否则index+2会出现下标越界异常
if (index == expression.length() - 1) {
numStack.push(Integer.parseInt(keepNum));
break;
} else {
if (operStack.isOper(expression.substring(index + 1, index + 2).charAt(0))) {
//如果后一位是运算符,则入栈
numStack.push(Integer.parseInt(keepNum));
keepNum = "";
}
}
}
index++;
//index从0开始
if (index >= expression.length()) {//也可以是等于
break;
}
}
//扫描完毕,按顺序,从数栈和符号栈中pop出相应的数和符号,并运算
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;//数组从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("栈空无法遍历");
return;
}
for (int i = top; i >= 0; i--) {
System.out.printf("stack[%d] = %d\n", i, stack[i]);
}
}
//返回运算符的优先级,优先级是程序员来确定的,优先级使用数字表示
//数字越大,优先级越高
public int priority(int oper) {
//Java中int和char可以混用
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;
default:
break;
}
return res;
}
}