c++ 卡片游戏

#include
 using namespace std;
 
 struct chainNode
 {
 	int element;      //用来装牌的号码 
 	chainNode* next; 
 	chainNode(int element, chainNode *n)
 	{
 		this->element =element;
 		this->next = n;
	 }
 };
 
 class mylinkqueue
 {
 	public:
 		mylinkqueue();
 		~mylinkqueue();
 		int getsize();   //返回大小
		void pop();   //删除头元素
		int front();  //返回首元素 
		void push(int); //把元素加到队尾 
	
	private:
		int size;		
		chainNode* thefront;
		chainNode* theback;	  
 };

 //构造 
 mylinkqueue::mylinkqueue()
 {
 	size = 0;
 	thefront = NULL;
 	theback = NULL;
 }
 //析构 
 mylinkqueue::~mylinkqueue()
 {
 	chainNode *c=thefront;
 	while(c!=NULL)
 	{c=c->next;
 	delete thefront; 
 	thefront = c;} 
 }
 //求大小 
 int mylinkqueue::getsize()
 {
 	return size;
 }
 //删除 
 void mylinkqueue::pop()
 {
 	if(thefront==NULL)
 	{
 		return;
	 }	 
	 chainNode* nextNode = thefront->next;
	 delete thefront;
	 thefront = nextNode;
	 size--;
 }
 //添加
 void mylinkqueue::push(int a)
 {
 	chainNode* newNode = new chainNode(a,NULL);
 	if(size==0)
 		thefront = newNode;
 	else
 		{
 			theback->next=newNode;
		 }
		 theback= newNode;
				              //newNode成为了尾
	size++; 
 	
  } 
 //返回首元素
 int mylinkqueue::front()
 {
 	return thefront->element;
  } 
  
int main()
  {
  	int n;
  	cin>>n;
  	mylinkqueue q;
  	
	  
  	//初始化 
  	for(int i=1;i<n+1;i++)
  	{
  //	cout<<"i="<
  		q.push(i); 		
	  }
	
 //	cout<
  //	chainNode* a = q.getfront() ;
  
  	
  	int a=0;
  	while(q.getsize ()>1)
  	{	//cout<<"丢掉"<
  		q.pop();
  		a=q.front() ;
  	//	cout<<"丢掉"<
  		q.pop() ;
  	//	cout<<"把a插入 a="<
  		q.push(a); 
	  }
	  cout<<q.front() ;
  }

你可能感兴趣的:(数据结构,c++)