初学数据结构四
1 #include<stdio.h> 2 #include<stdlib.h> 3 struct _Node 4 { 5 int data; 6 struct _Node *next; 7 }; 8 typedef struct _Node node_t; 9 typedef struct _Linklist 10 { 11 node_t *phead; 12 node_t *ptail; 13 int len; 14 }Linklist; 15 static node_t *GetNode(int i) 16 { 17 node_t *pNode; 18 pNode=(node_t*)malloc(sizeof(node_t)); 19 if(!pNode) 20 { 21 prinf("Error, the memory is not enough!\n"); 22 exit(-1); 23 } 24 pNode->data=i; 25 pNode->next=NULL; 26 return pNode; 27 } 28 void init_list(Linklist *plist) 29 { 30 node_t *p; 31 p=GetNode(1); 32 plist->phead=p; 33 plist->ptail=p; 34 p->next=plist->phead; 35 plist->len=1; 36 } 37 static void Create_List(Linklist*plist,int n) 38 { 39 int i=0; 40 node_t *pNew; 41 for(i=2;i<=n;i++) 42 { 43 pNew=GetNode(i); 44 plist->tail->next=pNew; 45 plist->tail=pNew; 46 pNew->next=plist->phead; 47 plist->len++; 48 } 49 printf("Completethe e-way circulation chain table the"); 50 } 51 void Print_List(Linklist* plist) 52 { 53 node_t *pCur=plist->head; 54 do 55 { 56 printf("THe %d person.\n",plist->data); 57 pCur=pCur->next; 58 }while(pCur!=plist->phead); 59 printf("The length of the list:%d\n",plist->len); 60 } 61 void joseph(Linklist *plist,int m) 62 { 63 node_t *pPre=plist->ptail; 64 node_t *pCur=plist->phead; 65 int i; 66 while(plist->len!=1) 67 { 68 i=0; 69 while(plist->len!=1) 70 { 71 i=0; 72 while(i<m-1) 73 { 74 pPre=pPre->next; 75 i++; 76 } 77 pCur=pPre->next; 78 pPre->next=pCur->next; 79 free(pCur); 80 plist->len--; 81 } 82 printf("The last one is:",pPre->data); 83 } 84 int main() 85 { 86 int n=0; 87 printf("Please input the length of the Circle list:"); 88 scanf_s(%d,&m); 89 Linklist pList; 90 init_list(&pList); 91 Create_List(&pList,n); 92 Print_List(&pList); 93 joseph(&pList,m); 94 system("pause"); 95 return 0; 96 }