#10782. 栈和队列【NOIP2012】新壳栈
栈和队列【NOIP2012】新壳栈
2. 【NOIP2012】新壳栈
小Z设计了一种新的数据结构"新壳栈"。首先,它和传统的栈一样支持压入、弹出操作。此外,其栈顶的前c个元素是它的壳,支持翻转操作。其中,c>2是一个固定的正整数,表示壳的厚度。每次操作(压入、弹出或翻转)都仅用与c无关的常数时间完成。

补全程序:
#include<iostream>
using namespace std;
const int NSIZE=100000,CSIZE=1000;
int n,c,r,tail,head,s[NSIZE],q[CSIZE];
int previous(int k){
if (direction)
return ((k+c-2)%c)+1;
else
return (k%c)+1;
}
int next (int k) {
if (direction)
① ;
else
return ((k+c-2)%c)+1;
}
void push() {
int element;
cin>>element;
if (next(head)==tail) {
n++;
② ;
tail=next(tail);
}
if (empty)
empty=false;
else
head=next(head);
③ =element;
}
void pop() {
if (empty) {
cout<<"Error:the stack is empty!"<<endl;
return;
}
cout<<④<<endl;
if (tail==head)
empty=true;
else {
head=previous(head);
if (n>0) {
tail=previous(tail);
⑤ =s[n];
n--;
}
}
}
void reverse() {
int temp;
if ( ⑥ == tail) {
direction=!direction;
temp=head;
head=tail;
tail=temp;
} else
cout<<"Error:lessthan"<<c<<"elements in the stack!"<<endl;
}
int main() {
cin>>c;
n=0;
tail=1;
head=1;
empty=true;
direction=true;
do{
cin>>r;
switch(r){
case1:push();break;
case2:pop();break;
case3:reverse();break;
}
} while (r!=0);
return 0;
}
选择题:
- ①处应填( )
{{ select(1) }}
- k%c+1
- k%c
- k
- (k+1)%c
- ②处应填( )
{{ select(2) }}
- s[n]=q[head]
- s[n]=tail
- s[n]=head
- s[n]=q[tail]
- ③处应填( )
{{ select(3) }}
- q[tail]
- tail
- head
- q[head]
- ④处应填( )
{{ select(4) }}
- q[tail+1]
- tail
- q[head]
- q[head+1]
- ⑤处应填( )
{{ select(5) }}
- tail
- q[tail+1]
- q[tail]
- q[head]
- ⑥处应填( )
{{ select(6) }}
- previous(head)
- next(head)
- previous(tail)
- next(tail)