05_堆栈
01_概念
02_应用场景
03_Java代码实现
04_数组模拟栈的使用
package DataStructureReview; import java.util.Arrays; import java.util.Stack; public class ArrayStackDemo { public static void main(String[] args) { Stack<String> stack = new Stack(); stack.push("x"); stack.push("y"); stack.push("z"); System.out.println("栈顶元素为:" + stack.peek()); System.out.println("获取栈顶元素:" + stack.pop()); System.out.println("测试堆栈是否为空:" + stack.empty()); System.out.println("查看对象:x,在堆栈中的位置为:" + stack.search("z")); System.out.println("==================数组模拟堆栈======================================"); ArrayStack stackArr = new ArrayStack(3); stackArr.push("x"); stackArr.push("y"); stackArr.push("z"); System.out.println("栈顶元素为:" + stackArr.peek()); System.out.println("获取栈顶元素:" + stackArr.pop()); System.out.println("测试堆栈是否为空:" + stackArr.empty()); System.out.println("查看对象:x,在堆栈中的位置为:" + stackArr.search("z")); } } class ArrayStack { //初始化数组 private String[] arr; //top指针 private int top = -1; public ArrayStack(int size) { this.arr = new String[size]; } //empty方法 public boolean empty() { return top == -1; } //push方法 public void push(String e) { //满堆栈判断 if (top == arr.length) { throw new RuntimeException("此堆栈已满,无法添加元素"); } arr[++top] = e; } //pop方法 public String pop() { //空栈判断 if (empty()) { throw new RuntimeException("此堆栈为空,无法添加弹出元素"); } return arr[top--]; } //peek方法 public String peek() { //空栈判断 if (empty()) { throw new RuntimeException("此堆栈为空,无法添加查看元素"); } return arr[top]; } //search方法,返回值为-1表示此对象不在此堆栈 public int search(String e) { System.out.println(Arrays.toString(arr)); //空栈判断 if (empty()) { return -1; } int i = top; int cur = 1; boolean flag = false; for (; i >= 0; i--) { if (arr[i] == e) { flag = true; break; } cur++; } if(flag != true){ cur = -1; } return cur; } }
05_链表模拟堆栈的使用
package DataStructureReview; import java.util.Stack; public class SingleLinkedListStackDemo { public static void main(String[] args) { Stack<String> stack = new Stack(); stack.push("x"); stack.push("y"); stack.push("z"); stack.push("m"); stack.push("k"); System.out.println("栈顶元素为:" + stack.peek()); System.out.println("获取栈顶元素:" + stack.pop()); System.out.println("测试堆栈是否为空:" + stack.empty()); System.out.printf("查看对象:%s,在堆栈中的位置为:%d \n","m" , stack.search("m")); System.out.println("==================链表模拟堆栈======================================"); SingleLinkedListStack liststack = new SingleLinkedListStack(new SingleLinkedList1()); liststack.push(new Node("x")); liststack.push(new Node("y")); liststack.push(new Node("z")); liststack.push(new Node("m")); liststack.push(new Node("k")); System.out.println("栈顶元素为:" + liststack.peek()); System.out.println("获取栈顶元素:" + liststack.pop()); System.out.println("测试堆栈是否为空:" + liststack.empty()); System.out.printf("查看对象:%s,在堆栈中的位置为:%d \n","m" , liststack.search("m")); } } class SingleLinkedListStack { //初始化链表对象 private SingleLinkedList1 list; public SingleLinkedListStack(SingleLinkedList1 list) { this.list = list; } //empty方法 public boolean empty() { return list.head.next == null; } //peek方法 public Node peek() { if (empty()) { throw new RuntimeException("当前堆栈为空,无法查看数据!"); } return list.getLastOne(); } //pop方法 public Node pop() { if (empty()) { throw new RuntimeException("当前堆栈为空,无法弹出数据!"); } return list.delete(); } //push方法 public void push(Node e) { list.add(e); } //search方法 public int search(String e) { //判断是否为空链表 if (empty()) { return -1; } Node temp = list.head.next; boolean flag = false; int sum = 1; while (true) { if (temp.value == e) { flag = true; break; } if (temp.next == null) { break; } sum++; temp = temp.next; } if (flag) { sum = list.size() + 1 - sum; } else { sum = -1; } return sum; } } class SingleLinkedList1 { //初始化head节点 public Node head = new Node(""); //add方法,在链表尾部添加 public void add(Node node) { //判断是否为空链表 if (head.next == null) { head.next = node; return; } //遍历链表 Node temp = head.next;//辅助指针 while (true) { if (temp.next == null) { temp.next = node; break; } temp = temp.next; } } //delete方法 public Node delete() { //判断是否为空链表 if (head.next == null) { System.out.println("当前链表为空!"); return null; } //遍历链表 Node temp = head;//辅助指针 while (true) { if (temp.next.next == null) { break; } temp = temp.next; } Node cur = temp.next; temp.next = null; return cur; } //getLastOne方法,返回倒数第一个节点 public Node getLastOne() { //判断是否为空链表 if (head.next == null) { System.out.println("当前链表为空!"); return null; } //遍历链表 Node temp = head.next;//辅助指针 while (true) { if (temp.next == null) { break; } temp = temp.next; } return temp; } //printList方法 public int size() { //判断是否为空链表 if (head.next == null) { return 0; } //遍历链表 Node temp = head.next;//辅助指针 int sum = 0; while (true) { sum++; if (temp.next == null) { break; } temp = temp.next; } return sum; } } class Node { public String value; public Node next; public Node(String value) { this.value = value; } @Override public String toString() { return value; } }
06_表达式的分类
07_算数表达式(前缀、中缀、后缀)
07_1_前缀表达式
07_2_中缀表达式
07_3_后缀表达式
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· SpringCloud带你走进微服务的世界