您的位置:首页 > 其它

一道关于孩子分糖的问题!!循环链表实现

2011-04-13 12:20 597 查看

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>

typedef struct CNODE
{
int data;
struct CNODE *next;
}CyList;

#define SIZE_OF_NODE sizeof(struct CNODE)

void Init_clist(CyList **List);
void Create_clist(CyList **List);
void Disp_clist(CyList *List);
int IS_success(CyList **List);
void process_clist(CyList **List);

int main()
{
CyList *clist = NULL;

Init_clist(&clist);
Create_clist(&clist);
printf("The cycle list you created is : /n");
Disp_clist(clist);

process_clist(&clist);
return 0;
}

void Init_clist(CyList **List)
{
(*List) = (CyList *)malloc(SIZE_OF_NODE);
if(!(*List))
{
printf("Memory assign failure!!/n");
exit(1);
}
(*List)->next = NULL;
}

void Create_clist(CyList **List)
{
int array[] = {10, 2, 8, 22, 16, 4, 10, 6, 14, 20};
CyList *head = *List, *curnode = NULL;
int len = sizeof(array) / sizeof(*array);
int i = 0;

for(i = 0; i < len; ++i)
{
curnode = (CyList *)malloc(SIZE_OF_NODE);
curnode->data = array[i];
head->next = curnode;
head = curnode;
}
(*List) = (*List)->next;
head->next = (*List);
}

void Disp_clist(CyList *List)
{
CyList *node = List;
if(List)
{
printf("%d-->", node->data);
node = node->next;
while(node->next != List)
{
printf("%d-->", node->data);
node = node->next;
}
printf("%d", node->data);
printf("/n");
}
}

int IS_success(CyList **List)
{
CyList *head = *List;
CyList *nextnode = NULL;

if((*List)->next != NULL)
{
nextnode = head->next;
}
while(nextnode->next!= *List)
{
if(head->data != nextnode->data )
{
return 0;
}
head = head->next;
nextnode = nextnode->next;
}
if(nextnode->next == *List)
{
if(head->data != nextnode->data)
{
return 0;
}
}
return 1;
}

void process_clist(CyList **List)
{
int count = 1, key;

while(1)
{
CyList *Curnode = *List;
CyList *nextnode = NULL;
key = (*List)->data;
if(!IS_success(List))
{
while(Curnode->next != *List)
{
nextnode = Curnode->next;

Curnode->data = ((Curnode->data % 2) == 1) ? Curnode->data + 1 : Curnode->data;
nextnode->data = ((nextnode->data % 2) == 1) ? nextnode->data + 1 : nextnode->data;

Curnode->data = ((Curnode->data + nextnode->data) % 2 ) == 1 ? (Curnode->data + nextnode->data) / 2 + 1 : (Curnode->data + nextnode->data) / 2;

Curnode = Curnode->next;
nextnode = Curnode->next;
}
(*List) = Curnode; //处理头结点
(*List)->data = ((Curnode->data + key) % 2 ) == 1 ? (Curnode->data + key) / 2 + 1 : (Curnode->data + key) / 2;

printf("After %dth adjust this cylist is : /n", count);
Disp_clist(*List);
++count;
}
else
{
return ;
}
}
}
/*
The cycle list you created is :
10-->2-->8-->22-->16-->4-->10-->6-->14-->20
After 1th adjust this cylist is :
15-->6-->5-->15-->19-->10-->7-->8-->10-->17
After 2th adjust this cylist is :
16-->11-->6-->11-->18-->15-->9-->8-->9-->14
After 3th adjust this cylist is :
15-->14-->9-->9-->15-->17-->13-->9-->9-->12
After 4th adjust this cylist is :
14-->15-->12-->10-->13-->17-->16-->12-->10-->11
After 5th adjust this cylist is :
13-->15-->14-->11-->12-->16-->17-->14-->11-->11
After 6th adjust this cylist is :
12-->15-->15-->13-->12-->14-->17-->16-->13-->12
After 7th adjust this cylist is :
12-->14-->16-->15-->13-->13-->16-->17-->15-->13
After 8th adjust this cylist is :
13-->13-->15-->16-->15-->14-->15-->17-->17-->15
After 9th adjust this cylist is :
14-->14-->15-->16-->16-->15-->15-->17-->18-->17
After 10th adjust this cylist is :
16-->14-->15-->16-->16-->16-->16-->17-->18-->18
After 11th adjust this cylist is :
17-->15-->15-->16-->16-->16-->16-->17-->18-->18
After 12th adjust this cylist is :
18-->17-->16-->16-->16-->16-->16-->17-->18-->18
After 13th adjust this cylist is :
18-->18-->17-->16-->16-->16-->16-->17-->18-->18
After 14th adjust this cylist is :
18-->18-->18-->17-->16-->16-->16-->17-->18-->18
After 15th adjust this cylist is :
18-->18-->18-->18-->17-->16-->16-->17-->18-->18
After 16th adjust this cylist is :
18-->18-->18-->18-->18-->17-->16-->17-->18-->18
After 17th adjust this cylist is :
18-->18-->18-->18-->18-->18-->17-->17-->18-->18
After 18th adjust this cylist is :
18-->18-->18-->18-->18-->18-->18-->18-->18-->18
Press any key to continue
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: