Java实现链队列

一、代码如下

public class LinkQueue<T>{
    private Node<T> front,rear;
    private int length;

    //构造一个空的队列
    public LinkQueue() {
        length = 0;
        front = rear = new Node<>(null);
    }

    //在队列的队尾插入一个新元素
    public void EnQueue(T obj)  {
        rear = rear.next = new Node<T>(obj,null);
        length++;
    }

    //删除队列队头元素
    public T DeQueue( ) {
        if (isEmpty()){
            System.out.println("链队列为空,无法执行删除操作");
            return null;
        }
        Node<T> p = front.next;
        T x = p.data;
        front.next = p.next;
        length--;
        if (front.next == null){
            rear = front;
        }
        return x;
    }

    //取队列队头元素
    public T getHead( ){
        if (isEmpty()){
            System.out.println("链队列为空,无法取对头元素");
            return null;
        }
        return front.next.data;
    }

    //求出队列中数据元素的个数
    public int size( ){
        return length;
    }

    //判断当前队列是否为空
    public boolean isEmpty(){
        return length == 0;
    }

    //依次访问队列中每个元素并输出
    public void nextOrder(){
        Node<T> p = front.next;
        while (p != null){
            System.out.println(p.data);
            p = p.next;
        }
    }

    //销毁一个已存在的队列
    public void clear(){
        front.next = rear.next = null;
    }
}

二、测试代码如下

public class testLinkQueue {
    public static void main(String[] args) {
        LinkQueue<Integer> queue = new LinkQueue<>();
        int a[] = {23,45,2,6,9,4,87,20};
        for(int i = 0;i<a.length;i++){
            queue.EnQueue(a[i]);
        }
        System.out.println("链队列为:");
        System.out.println("长度为"+queue.size());
        queue.nextOrder();
        queue.DeQueue();
        System.out.println("链队列为:");
        System.out.println("长度为"+queue.size());
        queue.nextOrder();
        System.out.println("链队列的头元素为:" + queue.getHead());
    }
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
链队列是一种使用链表实现的队列数据结构。它具有队列的基本操作,如进队、出队、判断队空和求队中元素个数等。在求解迷宫问题时,可以使用链队列实现路径的搜索和记录。 以下是使用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`表示终点的坐标。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值