View Code of Problem 80

#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
typedef struct Node
{
	int data;
	struct Node *next;
}Linklist;
Linklist *create(int n)
{
	Linklist *phead=NULL,*pTail=NULL;//这里初始化很重要
	for(int i=1;i<=n;i++)
	{
		Linklist *p=(Linklist*)malloc(sizeof(Linklist));
		p->data=i;
		p->next=NULL;
		if(phead==NULL)
		{
			phead=p;
			pTail=p;
			p->next=phead;
		}
		else{
			pTail->next=p;
			pTail=p;
			p->next=phead;
		}
	}
	return phead;
}


int main()
{

	int n=0;
	while(scanf("%d",&n)!=EOF){
		if(n==0)
			break;
		Linklist *phead=create(n);
     	Linklist *p=phead;
	    int count=0;
    	while(p->next!=p)
	{
		p=p->next;
		count++;
		if(count==1)
		{
		   Linklist*q=p->next;
		   p->next=p->next->next;
		   free(q);
		   p=p->next;
		   count=0;
		}
	}
		printf("%d\n",p->data);}
}
     	




	




Double click to view unformatted code.


Back to problem 80