恋战

//链栈
#include<iostream.h>
template<class DataType>
struct Node
{
	DataType data;
	Node<DataType> *next;
};
template<class DataType>
class LinkStack
{
	public:
		LinkStack(){top=NULL;};
		~LinkStack(){};
		void Push(DataType x);
		DataType Pop();
		DataType GetTop(){if(top!=NULL)return top->data;}
		int Empty(){if(top==NULL)return 1;else return 0;}
	private:
		Node<DataType> *top;
};
template<class DataType>
void LinkStack<DataType>::Push(DataType x)
{
	Node<DataType> *s=NULL;
	s=new Node<DataType>;s->data=x;
	s->next=top;top=s;
}
template<class DataType>
DataType LinkStack<DataType>::Pop()
{
	int x;
	Node<DataType> *p=NULL;
	if(top==NULL)throw"下溢";
	x=top->data;p=top;
	top=top->next;
	delete p;
	return x;
}
void hehe()
{
	cout<<"1.入栈操作"<<endl;
	cout<<"2.出栈操作"<<endl;
	cout<<"3.查看栈头"<<endl;
	cout<<"4.推出程序"<<endl;
}
int main()
{
	int i;
	LinkStack<int> L;
	if(L.Empty())
		cout<<"队列为空"<<endl;
	else
		cout<<"队列非空"<<endl;
	do{
    hehe();
	cin>>i;
switch(i)
{
case 1:int a;cout<<"请输入元素:";cin>>a; L.Push(a);break;
case 2:cout<<"弹出第一个元素"<<endl; L.Pop();break;
case 3:cout<<"队头元素:"<<endl;cout<<L.GetTop()<<endl;break;
case 4:i=0;break;
default:cout<<endl;break;
	}}
while(i!=0);
	return 0;
	}

猜你喜欢

转载自blog.csdn.net/weixin_41936498/article/details/80086179