大家都知道数据结构里面有一个结构叫做循环队列。顾名思义,这是一个队列,并且是循环的。但是现在,淘气的囧哥给这个循环队列加上了一些规矩,其中有5条指令:
(1) Push K, 让元素K进队列。
(2) Pop,对头元素出队列。
(3) Query K,查找队列中第K个元素,注意K的合法性。
(4) Isempty,判断队列是否为空。
(5) Isfull,判断队列是否已满。
现在有N行指令,并且告诉你队列大小是M。
第一行包含两个整数N和M。1<=N,M<=100000。
接下来有N行,表示指令,指令格式见题目描述。
其中元素均在int范围。
对于指令(1),若队列已满,输出failed,否则不做输出。
对于指令(2),若队列已空,输出failed,否则不做输出。
对于指令(3),输出队列中第K个元素,若不存在,输出failed。
对于指令(4)和(5),则用yes或者no回答。
详情见样例。
12 2
Push 1
Push 2
Push 3
Query 2
Query 3
Isempty
Isfull
Pop
Pop
Pop
Isempty
Isfull
failed
2
failed
no
yes
failed
yes
no
循环队列,底层实现是数组,给出Java和C++的实现。
其实可以不用结构体,直接数组就可以。
C++ AC
#include <stdio.h> #include <string> #include <string.h> using namespace std; #define INF 10000000; const int maxm = 100002; struct QueueLoop{ int array[maxm]; int front; int rear; }; QueueLoop queue; int n,m,i; int len; bool IsEmpty(){ int front = queue.front; int rear = queue.rear; if (rear == front) { return true; } return false; } bool Isfull(){ int front = queue.front; int rear = queue.rear; if ((rear + 1) % len == front) { return true; } return false; } bool pushElement(int k){ if (Isfull()) { return false; } int rear = queue.rear; queue.array[rear] = k; queue.rear = (rear+1) % len; return true; } bool popElement(){ if (IsEmpty()) { return false; } int front = queue.front; queue.front = (front+1) % len; return true; } void queryElement(int k){ int front = queue.front; int rear = queue.rear; if ( k <= 0 || rear - front < k) { printf("failed\n"); }else{ printf("%d\n",queue.array[k + front - 1]); } } int main(){ while(scanf("%d%d",&n,&m) != EOF){ queue.front = 0; queue.rear = 0; len = m+1; char operate[15]; for(i = 0 ; i < n; i++){ scanf("%s",operate); if (strcmp(operate,"Push") == 0) { int k; scanf("%d",&k); if (!pushElement(k)) { printf("failed\n"); } }else if (strcmp(operate,"Pop") == 0) { if (!popElement()) { printf("failed\n"); } }else if (strcmp(operate,"Query") == 0) { int k; scanf("%d",&k); queryElement(k); }else if (strcmp(operate,"Isempty") == 0) { printf("%s\n",IsEmpty() == true ? "yes" :"no"); }else { printf("%s\n",Isfull() == true ? "yes" :"no"); } } } return 0; } /************************************************************** Problem: 1415 User: wangzhenqing Language: C++ Result: Accepted Time:130 ms Memory:1412 kb ****************************************************************/
Java AC 做法1
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.StreamTokenizer; public class Main { /* * 1415 */ private static QueueLoop queue; private static int len; public static void main(String[] args) throws Exception{ StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in))); while (st.nextToken() != StreamTokenizer.TT_EOF) { int n = (int) st.nval; st.nextToken(); int m = (int) st.nval; queue = new QueueLoop(m); len = m + 1; while (n > 0) { n--; st.nextToken(); String operate = st.sval; if (operate.equals("Push")) { st.nextToken(); int k = (int) st.nval; if (!pushElement(k)) { System.out.println("failed"); } }else if (operate.equals("Pop")) { if (!popElement()) { System.out.println("failed"); } }else if (operate.equals("Query")) { st.nextToken(); int k = (int) st.nval; Integer value = queryElement(k); if (value == null) { System.out.println("failed"); }else { System.out.println(value); } }else if (operate.equals("Isempty")) { System.out.println(IsEmpty() == true ? "yes" :"no"); }else { System.out.println(Isfull() == true ? "yes" :"no"); } } } } private static boolean pushElement(int k){ if (Isfull()) { return false; } int rear = queue.rear; int array[] = queue.array; array[rear] = k; queue.array = array; queue.rear = (rear+1) % len; return true; } private static boolean popElement(){ if (IsEmpty()) { return false; } int front = queue.front; queue.front = (front+1) % len; return true; } private static Integer queryElement(int k){ int front = queue.front; int rear = queue.rear; int array[] = queue.array; if ( k <= 0 || rear - front < k) { return null; } return array[k + front - 1]; } private static boolean IsEmpty(){ int front = queue.front; int rear = queue.rear; if (rear == front) { return true; } return false; } private static boolean Isfull(){ int front = queue.front; int rear = queue.rear; if ((rear + 1) % len == front) { return true; } return false; } public static class QueueLoop{ int array[]; int front; int rear; public QueueLoop(int[] array, int front, int rear) { super(); this.array = array; this.front = front; this.rear = rear; } public QueueLoop(int size) { array = new int[size+1]; front = 0; rear = 0; } public QueueLoop() { super(); } } } /************************************************************** Problem: 1415 User: wzqwsrf Language: Java Result: Accepted Time:2280 ms Memory:43496 kb ****************************************************************/
Java AC 做法2
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
public class Main {
/*
* 1415
*/
private static int array[];
private static int front , rear;
public static void main(String[] args) throws Exception{
StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
while (st.nextToken() != StreamTokenizer.TT_EOF) {
int n = (int) st.nval;
st.nextToken();
int m = (int) st.nval;
array = new int[m+1];
front = rear = -1;
while (n > 0) {
n--;
st.nextToken();
String operate = st.sval;
if (operate.equals("Push")) {
st.nextToken();
int k = (int) st.nval;
if (rear - front == m) {
System.out.println("failed");
}else {
rear ++;
array[rear] = k;
}
}else if (operate.equals("Pop")) {
if (front == rear) {
System.out.println("failed");
}else {
front++;
}
}else if (operate.equals("Query")) {
st.nextToken();
int k = (int) st.nval;
if ( k <= 0 || rear - front < k) {
System.out.println("failed");
}else {
System.out.println(array[front + k]);
}
}else if (operate.equals("Isempty")) {
System.out.println(rear == front ? "yes" :"no");
}else {
System.out.println(rear - front == m ? "yes" :"no");
}
}
}
}
}
/**************************************************************
Problem: 1415
User: wzqwsrf
Language: Java
Result: Accepted
Time:2190 ms
Memory:42104 kb
****************************************************************/