java自带队列和手写队列
查看原帖
java自带队列和手写队列
919284
wx_app楼主2024/3/17 11:23

java自带的队列实现

package com.suanfa;

import java.io.*;
import java.util.LinkedList;
import java.util.Queue;

public class QueueJAVAZiDai {
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    public static int n;
    public static void main(String[] args) throws IOException {
        Queue<Integer> queue = new LinkedList<>();
        n = nextInt();
        int chose=0;
        int x=0;
        for (int i = 1; i <= n; i++) {
            chose = nextInt();
            switch (chose) {
                case 1:
                    x = nextInt();
                    queue.add(x);
                    break;
                case 2:
                    if (queue.isEmpty()) {
                        out.println("ERR_CANNOT_POP");
                    }else{
                        queue.poll();
                    }
                    break;
                case 3:
                    if (queue.isEmpty()) {
                        out.println("ERR_CANNOT_QUERY");
                    }else{
                        out.println(queue.peek());
                    }
                    break;
                case 4:
                    out.println(queue.size());
                    break;
            }
        }
        out.flush();
        out.close();
    }

    public static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }
}

java自带队列

数组实现循环队列

package com.suanfa;

import java.io.*;
import java.util.Scanner;

public class Queue {
    public static int capacity = (int) 1e4 + 10;
    public static int front, rear,n;
    public static int arr[] = new int[capacity];
    public static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter out = new PrintWriter(System.out);
    public static void main(String[] args) throws IOException {
        n = nextInt();
        int chose = 0;
        int x=0;
        for (int i = 1; i <= n; i++) {
            chose = nextInt();
            switch (chose) {
                case 1:
                    x = nextInt();
                    push(x);
                    break;
                case 2:
                    pop();
                    break;
                case 3:
                    out.println(printFront()!=-1?printFront():"ERR_CANNOT_QUERY");
                    break;
                case 4:
                    out.println(size());
                    break;
            }
        }
        out.flush();
        out.close();
    }

    public static int nextInt() throws IOException {
        in.nextToken();
        return (int) in.nval;
    }
    public static boolean isEmpty() {
        return front == rear;
    }

    public static boolean isFull() {
        return (rear + 1) % capacity == front;
    }

    public static void push(int x) {
        arr[rear] = x;
        rear = (rear + 1) % capacity;
    }

    public static int printFront() {

        if (isEmpty()) {
            return -1;
        } else {
            return arr[front];
        }
    }
    public static void pop() {
        if (isEmpty()) {
            out.println("ERR_CANNOT_POP");
        } else {
            front = (front + 1) % capacity;
        }
    }

    public static int size() {
        return (rear - front + capacity) % capacity;
    }
}

循环数组队列

2024/3/17 11:23
加载中...