import java.util.Scanner;
/**
* @author xnl
* @Description:
* @date: 2022/5/26 21:31
*/
public class Solution {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// 接收控制台输入的数字
int num = scanner.nextInt();
MyQueue queue = new MyQueue(num);
scanner.nextLine();
for (int i = 0; i < num; i++){
String str = scanner.nextLine();
String[] split = str.split("\\s+");
// 根据输入的东西做对应的操作
if (split[0].equals("push")){
queue.push(Integer.parseInt(split[1]));
} else if (split[0].equals("pop")){
queue.pop();
} else if (split[0].equals("front")){
queue.front();
} else {
return;
}
}
}
}
/**
* 自已定义一个对列
*/
class MyQueue{
private int size; // 对列的大小
private int[] arr; // 对列存放元素真实的数组
private int index = -1; // 新增的下标
private int head = 0; // 对列头的下标
public MyQueue(){};
public MyQueue(int size){
this.size = size;
this.arr = new int[size];
}
private boolean isEmpty(){
return index == head;
}
public void push(int value){
arr[++index] = value;
}
public void pop(){
if (isEmpty()){
System.out.println("error");
return;
}
System.out.println(arr[head++]);
}
public void front(){
if (isEmpty() || index == -1){
System.out.println("error");
return;
}
System.out.println(arr[head]);
}
}
牛客网:AB7 【模板】队列
最新推荐文章于 2025-12-02 21:20:38 发布
本文介绍了一个自定义队列的数据结构实现方法,并通过Java代码示例展示了如何进行基本操作如push、pop及front等。该实现适用于需要定制化队列行为的应用场景。
836

被折叠的 条评论
为什么被折叠?



