Java Study!! - 队列
type: channel
link: https://www.kaijavademo.top/tag/%E9%98%9F%E5%88%97/
items: 算法题：数据结构篇(持续更新)

算法题：数据结构篇(持续更新)
type: item
date: 2023-09-09T16:55:00+00:00
description: 这是我用于分享和复习算法笔记的地方，此篇主要是围绕关于数据结构的一些算法题。我在AcWing上刷题，可以和我一起:)AcWing 826. 单链表原址解题过程import java.io.*; import java.util.*; public class Main { private static final int N = 100010; private static final int[] e = new int[N]; private static final int[] ne = new int[N]; private static int idx; private static int head; public static void main(String[] args) throws IOException{ init(); //单链表 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] arrStr = br.readLine().split(" "); int n = Integer.parseInt(arrStr[0]); while(n -- > 0) { arrStr = br.readLine().split(" "); String let = arrStr[0]; if("H".equals(let)) { int x = Integer.parseInt(arrStr[1]); addToHead(x); } if("D".equals(let)) { int k = Integer.parseInt(arrStr[1]); if(k == 0) head = ne[head]; else remove(k - 1); } if("I".equals(let)) { int k = Integer.parseInt(arrStr[1]); int x = Integer.parseInt(arrStr[2]); add(k - 1, x); } } for(int i = head ;i != -1;i= ne[i]) System.out.print(e[i] + " "); br.close(); } private static void init() { //初始化的方法 idx = 0; head = -1; } //将x插入到头结点 private static void addToHead(int x ) { e[idx] = x; ne[idx] = head; head = idx ++; } //将x插入到下标为k的结点后,尾插法 private static void add(int k,int x) { e[idx] = x; ne[idx] = ne[k]; ne[k] = idx ++; } //删除下标为k后面的结点删除 private static void remove(int k) { ne[k] = ne[ne[k]]; } } 附上我的视频，(●'◡'●)[video title="单链表 " url="https://img.kaijavademo.top/typecho/uploads/2023/09/videos/%E5%8D%95%E9%93%BE%E8%A1%A8.mkv " container="b8fo4ln3enn" subtitle=" " poster=" "] [/video]AcWing 829. 模拟队列原址解题过程import java.io.*; import java.util.*; public class Main { private static final int N = 100010; private static final int[] q = new int[N]; private static int hh = 0;//队头 private static int tt = -1;//队尾 //往队尾插入元素，队头弹出元素 public static void main(String[] args) throws IOException{ //829. 模拟队列 BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String[] arrStr = br.readLine().split(" "); int m = Integer.parseInt(arrStr[0]); while(m-- > 0)…