Chiriri's blog Chiriri's blog
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)

Iekr

苦逼后端开发
首页
  • Java

    • JavaSE
    • JavaEE
    • 设计模式
  • Python

    • Python
    • Python模块
    • 机器学习
  • Golang

    • Golang
    • gRPC
  • 服务器

    • Linux
    • MySQL
    • NoSQL
    • Kubernetes
  • 项目

    • 传智健康
    • 畅购商城
  • Hadoop生态

    • Hadoop
    • Zookeeper
    • Hive
    • Flume
    • Kafka
    • Azkaban
    • Hbase
    • Scala
    • Spark
    • Flink
  • 大数据项目

    • 离线数仓
  • 青训营

    • 第四届青训营
  • HTML

    • HTML
    • JavaScript
  • Vue

    • Vue2
    • TypeScript
    • Vue3
    • Uni-APP
  • 数据结构与算法
  • C语言
  • 考研数据结构
  • 计算机组成原理
  • 计算机操作系统
  • Java基础

    • Java基础
    • Java集合
    • JUC
    • JVM
  • 框架

    • Spring
    • Dubbo
    • Spring Cloud
  • 数据库

    • MySQL
    • Redis
    • Elasticesearch
  • 消息队列

    • RabbitMQ
    • RocketMQ
  • 408

    • 计算机网络
    • 操作系统
    • 算法
  • 分类
  • 标签
  • 归档
  • 导航站
GitHub (opens new window)
  • 数据结构

    • 位运算
    • 最基本的数据结构
    • 前缀和数组
    • random的随机行为
    • 对数器
    • 二分法查找
    • 方法参数传递是值还是引用
    • 链表
    • 位图
    • 位运算实现四则运算
    • 二叉树
    • 排序
    • 时间复杂度
    • 队列和栈
      • 数组实现队列
      • 栈实现队列
      • 队列实现栈
    • 递归
    • 堆(优先级队列)
    • 贪心
    • 并查集
    • 图
    • 从暴力递归到动态规划
    • 滑动窗口
    • 单调栈
    • 线段树
  • 数据结构与算法

  • 计算机组成原理

  • 操作系统

  • 408
  • 数据结构
Iekr
2022-03-08
目录

队列和栈

# 队列和栈

# 数组实现队列

public class MyQueue {
	private int[] arr;
	private int pushi;
	private int polli;
	private int size;
	private final int limit;

	public MyQueue(int limit) {
		this.limit = limit;
		pushi = 0;// 头指针
		polli = 0;// 尾指针
		size = 0;
		arr = new int[limit];
	}

	public void push(int value) {
		if (size == limit) {
			throw new RuntimeException("队列已满 无法继续添加");
		}
		size++;
		arr[pushi] = value;
		pushi = nextIndex(pushi);
	}

	public int pop(int value) {
		if (size == 0) {
			throw new RuntimeException("队列为空");
		}
		size--;
		int ans = arr[polli];
		polli = nextIndex(polli);
		return ans;

	}

	public boolean isEmpty() {
		return size == 0;
	}

	private int nextIndex(int index) {
		// index = (index+1) % limit;
		return index < limit - 1 ? index + 1 : 0;
	}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

# 栈实现队列

// 使用两个栈实现
	public static class Myqueue {
		private Stack<Integer> StackPull;
		private Stack<Integer> StackPop;
       
	public Myqueue() {
		StackPull = new Stack<Integer>();
		StackPop = new Stack<Integer>();
	}

	public void push(int value) {
		StackPull.push(value);
		pushToPop();
	}

	private void pushToPop() {
		if (StackPop.isEmpty()) {
			while (!StackPull.isEmpty()) {
				StackPop.push(StackPull.pop()); // 将栈顶 压到 pop栈中
			}
		}

	}

	public int pop() {
		if (StackPull.isEmpty() && StackPop.isEmpty()) {
			throw new RuntimeException("队列中无元素");
		}
		pushToPop();
		return StackPop.pop();
	}

	public int peek() {
		if (StackPull.isEmpty() && StackPop.isEmpty()) {
			throw new RuntimeException("队列中无元素");
		}
		pushToPop();
		return StackPop.peek();
	}

}

public static void main(String[] args) {
	Myqueue test = new Myqueue();
	test.push(1);
	test.push(2);
	test.push(5);
	System.out.println(test.peek());
	System.out.println(test.pop());
	System.out.println(test.peek());
	System.out.println(test.pop());
	System.out.println(test.peek());
	System.out.println(test.pop());
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

# 队列实现栈

// 两个队列实现
	public static class TwoQueueStack<T> {
		public Queue<T> queue;
		public Queue<T> help;

		public TwoQueueStack() {
			queue = new LinkedList<>();
			help = new LinkedList<>();
		}

		public void push(T value) {
			queue.offer(value); // 加入到队尾
		}

		public T pop() {
			while (queue.size() > 1) {
				help.offer(queue.poll()); // 除了队尾的元素 全部转移到另外一个队列中
			}
			T ans = queue.poll(); // 当前队尾元素为栈顶
			Queue<T> tep = queue; // 缓存当前空队列
			queue = help; // 重新转移到queue中
			help = tep;
			return ans;
		}

		public T peek() {
			while (queue.size() > 1) {
				help.offer(queue.poll());
			}
			T ans = queue.poll();
			help.offer(ans); // 由于是查看栈顶 所以获取到值后 重新添加到另外队列中
			Queue<T> tep = queue;
			queue = help;
			help = tep;
			return ans;
		}

		public boolean isEmpty() {
			return queue.isEmpty();
		}

	}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
编辑 (opens new window)
上次更新: 2023/12/06, 01:31:48
时间复杂度
递归

← 时间复杂度 递归→

最近更新
01
k8s
06-06
02
进程与线程
03-04
03
计算机操作系统概述
02-26
更多文章>
Theme by Vdoing | Copyright © 2022-2025 Iekr | Blog
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式