蒟蒻の第一次栈的尝试以失败告终//全RE零分求调
查看原帖
蒟蒻の第一次栈的尝试以失败告终//全RE零分求调
1131905
yoyoSGH楼主2024/9/25 16:54

PS:样例好像是对的

#include<bits/stdc++.h>
using namespace std;
const int MAX=100000;
//定义栈 
typedef struct Stack
{
	int data[MAX],top;
}Stact;
//初始化 
void doempty(Stack &S)
{
	S.top=-1;
}
//判空 
bool isitempty(Stack S)
{
	if(S.top==-1) return true;
	else return false;
}
//判高 
int size(Stack S)
{
	return (S.top)+1;
}
//进栈
void push(Stack &S,int thing)
{
	if(S.top==MAX-1)
	{
		cout<<"Sorry,it is full~"<<endl;
	}
	else
	{
		S.data[S.top+1]=thing;
		S.top++;
	}
}
//出栈
int pop(Stack &S,int thing)
{
	if(S.top==-1)
	{
		cout<<"Empty"<<endl;
	}
	else
	{
		thing=S.data[S.top];
		S.top--;
	}
}
//读取栈顶元素
void query(Stack S)
{
	if(S.top==-1) cout<<"Anguei!"<<endl;
	else cout<<S.data[S.top]<<endl;
} 
int main() {
	Stack S;
	doempty(S);
	int num;
	cin>>num;
	for(int i=0;i<num;i++)
	{
		int n;
		cin>>n;
		for(int j=0;j<n;j++)
		{
			string s;
			cin>>s;
			if(s=="push")
			{
				int x;
				cin>>x;
				push(S,x);
			}
			else if(s=="query")
			{
				query(S);
			}
			else if(s=="size")
			{
				cout<<size(S)<<endl;
			}
			else
			{
				int x;
				pop(S,x);
				cout<<x<<endl;
			}
		}
		doempty(S);
	}
	return 0;
}
2024/9/25 16:54
加载中...