在这里插入图片描述
我自己手写了一个简易的栈和队列,嫌麻烦的同学可以直接用LinkedList哈。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
for (int i = 0; i < n; i++) {
int m = sc.nextInt();
String str = sc.next();
stack s = new stack();
queue q = new queue();
for (int j = 0; j < m; j++) {
if(str.equals("FIFO")){ //队列操作
String ss = sc.next();
if(ss.equals("IN")){
Node node = new Node(sc.nextInt());
q.add(node);
}else{
if(q.isEmpty()) System.out.println("None");
else System.out.println(q.remove());
}
}else{ //栈操作
String ss = sc.next();
if(ss.equals("IN")){
Node node = new Node(sc.nextInt());
s.add(node);
}else{
if(s.isEmpty()) System.out.println("None");
else System.out.println(s.pop());
}
}
}
}
}
}
}
//栈
class stack{
Node head = new Node(0);
public void add(Node newNode){
newNode.next = head.next;
head.next = newNode;
}
public int pop () {
int num = head.next.data;
head.next = head.next.next;
return num;
}
//判空
public boolean isEmpty(){
if(head.next == null) return true;
else return false;
}
}
class queue{
Node head = new Node(0);
public void add (Node newNode){
Node temp = head;
while(true){
if(temp.next == null) break;
temp = temp.next;
}
temp.next = newNode;
}
public int remove(){
int num = head.next.data;
head.next = head.next.next;
return num;
}
//判空
public boolean isEmpty(){
if(head.next == null) return true;
else return false;
}
}
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
}
}