问题 4121 --新壳栈(完善程序)

4121: 新壳栈(完善程序)★★★★

时间限制: 1 Sec  内存限制: 128 MB
提交: 15  解决: 7
[提交][状态][命题人:]

题目描述


#include <cstdio>
using namespace std;
const int
NSIZE = 100000,
CSIZE = 1000;
int n, c, r, tail, head, s[NSIZE], q[CSIZE];
//数组 s 模拟一个栈,n 为栈的元素个数
//数组 q 模拟一个循环队列,tail 为队尾的下标,head 为队头的下标
bool direction, empty;
int previous(int k)
{
	if (direction)
	return ((k + c - 2) % c) + 1;
	else
	return (k % c) + 1;
}
int next(int k)
{
	if (direction)
	 _____(1)_____;
	else
	return ((k + c - 2) % c) + 1;
}
void push()
{
	int element;
	cin>>element;
	if (next(head) == tail) 
	{
		n++;
		_____(2)_____;
		tail = next(tail);
	}
	if (empty)
		empty = false;
	else
		head = next(head);
	_____(3)____= element;
}
void pop()
{
	if (empty) 
	{
		cout<<"Error: the stack is empty!"<<endl;
		return;
	}
	cout<<____(4)_____<<endl;
	if (tail == head)
	empty = true;
	else 
	{
		head = previous(head);
		if (n > 0) 
		{
			tail = previous(tail);
			____(5)____= s[n];
			n--; 
		} 
	} 
}
void reverse()
{
	int temp;
	if (_____(6)_____== tail) 
	{
	direction = !direction;
	temp = head;
	head = tail;
	tail = temp;
	}
	else
		cout<<"Error: less than "<<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) 
		{
		case 1: push(); break;
		case 2: pop(); break;
		case 3: reverse(); break;
		}
	} while (r != 0);
	return 0;
}


输入

输出

提示

来源

[提交][状态]