当前位置:网站首页>栈和队列
栈和队列
2022-08-10 05:32:00 【cbys-1357】

代码实现:
public class Stack<T> implements Iterable<T>{
private Node head;
private int N;
// 定一个内部类
private static class Node<T>{
T item;
Node next;
public Node(T item,Node next) {
this.item=item;
this.next=next;
}
}
// 构造方法
public Stack() {
this.head=new Node(null,null);
this.N=0;
}
// 判断栈中元素是否为空
public boolean isEmpty() {
return N==0;
}
// 返回栈中元素个数
public int size() {
return N;
}
// 把t元素压入栈中
public void push(T t) {
Node<T> oldfirst=head.next;
Node<T> newNode=new Node(t,null);
head.next=newNode;
newNode.next=oldfirst;
N++;
}
// 弹出栈中的元素
public T pop() {
Node<T> oldfirst=head.next;
if(oldfirst==null) {
return null;
}
head.next=oldfirst.next;
N--;
return oldfirst.item;
}
@Override
public Iterator<T> iterator() {
// TODO 自动生成的方法存根
return new SIterator();
}
private class SIterator<T> implements Iterator{
private Node n;
public SIterator(){
this.n=head;
}
@Override
public boolean hasNext() {
// TODO 自动生成的方法存根
return n.next!=null;
}
@Override
public Object next() {
// TODO 自动生成的方法存根
n=n.next;
return n.item;
}
}
}
栈的应用:
案例:
1、逆波兰表达式求值问题
题目描述:

代码实现:
public static void main(String[] args) {
// 中缀表达式3*(17-15)+18/6的逆波兰表达式如下
String notation[]= {"3","17","15","-","*","18","6","/","+"};
int result=caculate(notation);
System.out.println("逆波兰表达式的结果为:"+result);
}
private static int caculate(String[] notation) {
// 创建一个栈对象stack存储操作数
Stack<Integer> stack=new Stack<Integer>();
// 从左往右遍历逆波兰表达式,得到每一个字符串
for(int i=0;i<notation.length;i++) {
String curr=notation[i];
// 判断该字符串是不是运算符
Integer o1;
Integer o2;
Integer result;
// 如果是运算符,则从stack栈中弹出两个操作数o1,o2
switch(curr) {
case "+":
// 如果是运算符,则从stack栈中弹出两个操作数o1,o2
o1=stack.pop();
o2=stack.pop();
// 使用该运算符计算o1和o2,得到结果result
result=o1+o2;
// 把该结果压入stack栈中
stack.push(result);
break;
case "-":
o1=stack.pop();
o2=stack.pop();
result=o2-o1;
stack.push(result);
break;
case "*":
o1=stack.pop();
o2=stack.pop();
result=o1*o2;
stack.push(result);
break;
case "/":
o1=stack.pop();
o2=stack.pop();
result=o2/o1;
stack.push(result);
break;
default:
// 如果不是,把该该操作数压入stack栈中
stack.push(Integer.parseInt(curr));
break;
}
}
return stack.pop();
}
}
2.队列

代码实现:
public class Queue<T> implements Iterable<T>{
// 首节点
private Node head;
// 尾结点
private Node last;
// 记录队列中元素个数
private int N;
// 定义内部类
private static class Node<T>{
T item;
Node next;
public Node(T item,Node next) {
this.item=item;
this.next=next;
}
}
// 构造方法
public Queue(){
head=new Node(null,null);
last=null;
N=0;
}
// 判断队列元素是否为空
public boolean isEmpty() {
return N==0;
}
// 返回队列中元素个数
public int size() {
return N;
}
//从队列中拿出一个元素
public T dequeue() {
if(isEmpty()) {
return null;
}
Node<T> oldfirst=head.next;
head.next=oldfirst.next;
// 元素个数-1
N--;
//
if(isEmpty()) {
last=null;
}
return oldfirst.item;
}
//向队列中插入元素t
public void enqueue(T t) {
if(last==null) {
last=new Node(t,null);
head.next=last;
}else {
Node oldlast=last;
last=new Node(t,null);
oldlast.next=last;
}
// 元素个数+1
N++;
}
@Override
public Iterator<T> iterator() {
return new QIterator();
}
private class QIterator<T> implements Iterator{
Node<T> n;
public QIterator() {
n= head;
}
@Override
public boolean hasNext() {
return n.next!=null;
}
@Override
public Object next() {
n=n.next;
return n.item;
}
}
}
边栏推荐
- IO stream【】【】【】
- impdp import data
- Mini Program Study Notes: Communication between Mini Program Components
- 常用类 BigDecimal
- 安装Robotics-toolbox-matlab, for 点云坐标系转换
- The submenu of the el-cascader cascade selector is double-clicked to display the selected content
- 图片批量添加水印批量加背景缩放批量合并工具picUnionV4.0
- Mockito基本使用指南
- 最新最全的数字藏品发售日历-07.27
- 毫米波雷达基础概念学习
猜你喜欢
Index Notes【】【】
深度学习中的学习率调整策略(1)
One step ahead, don't miss it again, the chain reading APP will be launched soon!
The latest and most complete digital collection sales calendar-07.26
最新最全的数字藏品发售日历-07.27
I use this recruit let the team to improve the development efficiency of 100%!
力扣——省份数量
view【】【】【】【】
21天挑战杯MySQL-Day05
Chain Reading Recommendation: From Tiles to Generative NFTs
随机推荐
第五次实验
cesium rotate image
win12 修改dns脚本
Count down the six weapons of the domestic interface collaboration platform!
集合 set接口
索引笔记【】【】
MySql 约束
链读 | 最新最全的数字藏品发售日历-07.28
三维点云分割
复杂的“元宇宙”,为您解读,链读APP即将上线!
图片批量添加水印批量缩放图片到指定大小
wiki confluence 安装
2021-06-22
链表API设计
Models corresponding to each architecture instruction set
matlab中的常用的类型转换
私有化搭建个人网盘 NextCloud
2021-07-09
【List练习】遍历集合并且按照价格从低到高排序,
el-dropdown drop-down menu style modification, remove the small triangle