数组实现
public class ArrayStackDemo {
public static void main(String[] args) {
ArrayStack stack = new ArrayStack(4);
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
while (loop) {
System.out.println("list 展示栈全部数据");
System.out.println("pop 出栈");
System.out.println("push 入栈");
System.out.println("exit 退出程序");
key = scanner.next();
switch (key) {
case "list":
stack.list();
break;
case "pop":
int data = stack.pop();
System.out.printf("%d出栈",data);
System.out.println();
break;
case "push":
int d = 0;
System.out.println("请输入要入栈的值:");
d = scanner.nextInt();
stack.push(d);
System.out.printf("%d入栈",d);
System.out.println();
break;
case "exit":
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序结束");
}
}
class ArrayStack{
private int maxSize;
private int top = -1;
private int[] stack;
public ArrayStack(int maxSize) {
this.maxSize = maxSize;
this.stack = new int[this.maxSize];
}
public boolean isFull() {
return top == maxSize-1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int n){
if (isFull()) {
System.out.println("栈已满,入不了栈了兄弟!");
return;
}
top++;
stack[top] = n;
}
public int pop(){
if (isEmpty()) {
throw new RuntimeException("栈已空,没人了兄弟别移了!");
}
int data = stack[top];
top--;
return data;
}
public void list() {
if (isEmpty()) {
System.out.println("栈空的,没人了兄弟!");
return;
}else {
for (int i = top;i >= 0;i--) {
System.out.printf("arr[%d]=%d\t",i,stack[i]);
}
System.out.println();
}
}
}
链表实现
public class SingleLinkedListStackDemo {
public static void main(String[] args) {
SingleLinkedListStack stack = new SingleLinkedListStack();
boolean loop = true;
Scanner scanner = new Scanner(System.in);
String key = "";
while (loop) {
System.out.println("list 展示栈全部数据");
System.out.println("pop 出栈");
System.out.println("push 入栈");
System.out.println("exit 退出程序");
key = scanner.next();
switch (key) {
case "list":
stack.reverse();
stack.list();
break;
case "pop":
StackNode data = stack.pop();
System.out.printf("%s出栈",data);
System.out.println();
break;
case "push":
int d = 0;
System.out.println("请输入要入栈的值:");
d = scanner.nextInt();
StackNode dataNode = new StackNode(d);
stack.push(dataNode);
System.out.printf("%s入栈",dataNode);
System.out.println();
break;
case "exit":
scanner.close();
loop = false;
break;
default:
break;
}
}
System.out.println("程序结束");
}
}
class SingleLinkedListStack{
StackNode head = new StackNode(-1);
public void push(StackNode sNode) {
StackNode temp = head;
while(true) {
if (temp.getNext() == null) {
break;
}
temp = temp.getNext();
}
temp.setNext(sNode);
}
public StackNode pop() {
reverse();
StackNode temp = head;
if (temp.getNext() == null) {
throw new RuntimeException("栈空,无数据");
}
while(true) {
if (temp.getNext().getNext() == null) {
break;
}
temp = temp.getNext();
}
StackNode data = temp.getNext();
temp.setNext(null);
return data;
}
public void reverse() {
if (head == null || head.getNext()== null) {
System.out.println("当前链表为空");
return;
}
StackNode newHead = new StackNode(-1);
StackNode cur = head.getNext();
StackNode next = null;
while(cur != null) {
next = cur.getNext();
cur.setNext(newHead.getNext());
newHead.setNext(cur);;
cur = next;
}
head.setNext(newHead.getNext());
}
public void list() {
StackNode temp = head.getNext();
while (true) {
if(temp == null) {
break;
}
System.out.println(temp.toString());
temp = temp.getNext();
}
}
}
class StackNode{
private int no;
private StackNode next;
public StackNode(int no) {
this.no = no;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public StackNode getNext() {
return next;
}
public void setNext(StackNode next) {
this.next = next;
}
@Override
public String toString() {
return "StackNode [no=" + no + "]";
}
}
栈应用 —— 计算器
public class Calculator {
public static void main(String[] args) {
String expression = "70*2*2-5+1-150+3-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()) {
operStack.push(ch);
}else {
if (operStack.getPriority(ch) <= operStack.getPriority((char) 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 {
keepNum += ch;
if (index == expression.length() - 1) {
numStack.push(Integer.parseInt(keepNum));
}else {
if (operStack.isOper(expression.substring(index+1,index+2).charAt(0))) {
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);
System.out.println(res);
numStack.push(res);
}
System.out.printf("表达式%s的结果为=%d",expression,numStack.pop());
System.out.println();
System.out.println("程序结束");
}
}
class ArrayStack2{
private int maxSize;
private int top = -1;
private int[] stack;
public ArrayStack2(int maxSize) {
this.maxSize = maxSize;
this.stack = new int[this.maxSize];
}
public boolean isFull() {
return top == maxSize-1;
}
public boolean isEmpty() {
return top == -1;
}
public void push(int n){
if (isFull()) {
System.out.println("栈已满,入不了栈了兄弟!");
return;
}
top++;
stack[top] = n;
}
public int pop(){
if (isEmpty()) {
throw new RuntimeException("栈已空,没人了兄弟别移了!");
}
int data = stack[top];
top--;
return data;
}
public void list() {
if (isEmpty()) {
System.out.println("栈空的,没人了兄弟!");
return;
}else {
for (int i = top;i >= 0;i--) {
System.out.printf("arr[%d]=%d\t",i,stack[i]);
}
System.out.println();
}
}
public int getPriority(char 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;
default:
res = -1;
break;
}
return res;
}
public int peek() {
return stack[top];
}
}