标签:
Description
A queue is a data structure based on the principle of ‘First In First Out‘ (FIFO). There are two ends; one end can be used only to insert an item and the other end to remove an item. A Double Ended Queue is a queue where you can insert an item in both sides as well as you can delete an item from either side. There are mainly four operations available to a double ended queue. They are:
- pushLeft(): inserts an item to the left end of the queue with the exception that the queue is not full.
- pushRight(): inserts an item to the right end of the queue with the exception that the queue is not full.
- popLeft(): removes an item from the left end of the queue with the exception that the queue is not empty.
- popRight(): removes an item from the right end of the queue with the exception that the queue is not empty.
Now you are given a queue and a list of commands, you have to report the behavior of the queue.
Input
Input starts with an integer T (≤ 20), denoting the number of test cases.
Each case starts with a line containing two integers n, m (1 ≤ n ≤ 10, 1 ≤ m ≤ 100), where n denotes the size of the queue and m denotes the number of commands. Each of the next m lines contains a command which is one of:
pushLeft x pushes x (-100 ≤ x ≤ 100) in the left end of the queue
pushRight x pushes x (-100 ≤ x ≤ 100) in the right end of the queue
popLeft pops an item from the left end of the queue
popRight pops an item from the right end of the queue
Output
For each case, print the case number in a line. Then for each operation, show its corresponding output as shown in the sample. Be careful about spelling.
Sample Input
1
3 8
pushLeft 1
pushLeft 2
pushRight -1
pushRight 1
popLeft
popRight
popLeft
popRight
Sample Output
Case 1:
Pushed in left: 1
Pushed in left: 2
Pushed in right: -1
The queue is full
Popped from left: 2
Popped from right: -1
Popped from left: 1
The queue is empty
#include<stdio.h> #include<stdlib.h> int main() { char s[20]; int num[600]; int a,b,n,m,T,k,c,judge; while(scanf("%d",&T)!=EOF&&T!=0) { for(int j=1;j<=T;j++) { printf("Case %d:\n",j); scanf("%d%d",&n,&m); judge=0; a=300;b=300;k=0; for(int i=1;i<=m;i++) { getchar(); scanf("%s",s); if(s[1]==‘o‘) { if(k==0)printf("The queue is empty\n"); else if(k==1&&s[3]==‘L‘){k--;printf("Popped from left: %d\n",num[a]);} else if(k==1&&s[3]==‘R‘){k--;printf("Popped from right: %d\n",num[a]);} else if(k>1&&s[3]==‘L‘){k--;printf("Popped from left: %d\n",num[a]);a++;} else if(k>1&&s[3]==‘R‘){k--;printf("Popped from right: %d\n",num[b]);b--;} } if(s[1]==‘u‘) { scanf("%d",&c); if(k==n)printf("The queue is full\n"); if(k<n) { if(k==0&&s[4]==‘L‘){k++;num[a]=c;printf("Pushed in left: %d\n",num[a]);} else if(k==0&&s[4]==‘R‘){k++;num[a]=c;printf("Pushed in right: %d\n",num[a]);} else if(k>0&&s[4]==‘L‘){k++;a--;num[a]=c;printf("Pushed in left: %d\n",num[a]);} else if(k>0&&s[4]==‘R‘){k++;b++;num[b]=c;printf("Pushed in right: %d\n",num[b]);} } } } } } }
LightOJ 1212 <Double Ended Queue >
标签:
原文地址:http://www.cnblogs.com/914295860-jry/p/5822327.html