java学习第17天,链队列

队列在数据结构中非常基础,尤其是出队和入队的算法很有意思。可以好好看看,多多理解一下

package java11to20;

public class D17_LinkedQueue {
	class Node {
		int data;
		Node next;
		public Node(int par) {
			data = par;
			next = null;
		}
	}

	Node header;
	Node tail;

	public static void main(String[] args) {
		D17_LinkedQueue linkQueue = new D17_LinkedQueue();
		System.out.println("初始化,列表为:" + linkQueue.toString());

		for (int i = 0; i < 7; i++) {
			linkQueue.enqueue(i + 1);// 入队
		}
		System.out.println("入队完毕,队列为:" + linkQueue.toString());

		linkQueue.dequeue();// 出队
		System.out.println("出队完毕,队列为:" + linkQueue.toString());

		int value;
		for (int i = 0; i < 7; i++) {
			value = linkQueue.dequeue();
			System.out.println(String.format("出队值 %s,最新队列为:%s", value, linkQueue.toString()));
		}
	}

	public D17_LinkedQueue() {
		header = new Node(-1);
		header.next = null;
		tail = header;
	}

	public void enqueue(int par) {
		Node node = new Node(par);
		tail.next = node;
		tail = node;
	}

	public int dequeue() {
		if (header.next == null) {
			System.out.println("队空");
			return -1;
		}
		int resultValue = header.next.data;
		header.next = header.next.next;
		return resultValue;
	}

	public String toString() {
		String result = "";

		if (header.next == null) {
			return "空";
		}
		Node node = header.next;
		while (node != null) {
			result += node.data + ",";
			node = node.next;
		}
		return result;
	}
}

输出结果:

初始化,列表为:空
入队完毕,队列为:1,2,3,4,5,6,7,
出队完毕,队列为:2,3,4,5,6,7,
出队值 2,最新队列为:3,4,5,6,7,
出队值 3,最新队列为:4,5,6,7,
出队值 4,最新队列为:5,6,7,
出队值 5,最新队列为:6,7,
出队值 6,最新队列为:7,
出队值 7,最新队列为:空
队空
出队值 -1,最新队列为:空
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
队列是一种使用链表实现的队列数据结构。它具有队列的基本操作,如进队、出队、判断队空和求队中元素个数等。在求解迷宫问题时,可以使用链队列来实现路径的搜索和记录。 以下是使用Java实现链队列求解迷宫的示例代码: ```java import java.util.LinkedList; import java.util.Queue; public class MazeSolver { private static final int[][] DIRECTIONS = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}}; // 上下左右四个方向 public static void solveMaze(int[][] maze, int startX, int startY, int endX, int endY) { int rows = maze.length; int cols = maze[0].length; boolean[][] visited = new boolean[rows][cols]; // 记录每个位置是否已经访问过 int[][] prev = new int[rows][cols]; // 记录每个位置的前驱位置,用于最后回溯路径 Queue<int[]> queue = new LinkedList<>(); // 使用链队列存储待访问的位置 queue.offer(new int[]{startX, startY}); visited[startX][startY] = true; while (!queue.isEmpty()) { int[] curr = queue.poll(); int currX = curr[0]; int currY = curr[1]; if (currX == endX && currY == endY) { break; // 找到终点,结束搜索 } for (int[] direction : DIRECTIONS) { int nextX = currX + direction[0]; int nextY = currY + direction[1]; if (nextX >= 0 && nextX < rows && nextY >= 0 && nextY < cols && maze[nextX][nextY] == 0 && !visited[nextX][nextY]) { queue.offer(new int[]{nextX, nextY}); visited[nextX][nextY] = true; prev[nextX][nextY] = currX * cols + currY; // 记录前驱位置 } } } if (!visited[endX][endY]) { System.out.println("No path found."); // 没有找到路径 return; } // 回溯路径 int currX = endX; int currY = endY; while (currX != startX || currY != startY) { int prevX = prev[currX][currY] / cols; int prevY = prev[currX][currY] % cols; maze[currX][currY] = 2; // 标记路径 currX = prevX; currY = prevY; } maze[startX][startY] = 2; // 标记起点 maze[endX][endY] = 2; // 标记终点 // 打印迷宫和路径 for (int[] row : maze) { for (int cell : row) { if (cell == 0) { System.out.print("□ "); // 未访问的位置 } else if (cell == 1) { System.out.print("■ "); // 墙壁 } else { System.out.print("★ "); // 路径 } } System.out.println(); } } } ``` 使用上述代码,可以通过调用`solveMaze`方法来求解迷宫。其中,`maze`是一个二维数组表示迷宫,0表示可通行的位置,1表示墙壁,`startX`、`startY`表示起点的坐标,`endX`、`endY`表示终点的坐标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值