一道关于孩子分糖的问题!!循环链表实现
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
*/
相关文章推荐
- 一道关于孩子分糖的问题!!循环链表实现
- 关于网宿厦门研发中心笔试的一道PV操作题:利用java中的多线程实现生产者与消费者的同步问题
- 循环链表范例(约瑟夫问题) 数组实现
- 关于在SVG中如何实现gif动画的问题?
- 关于Session的机制,实现方式和安全、单点故障问题
- MySQL 实现树形的遍历(关于多级菜单栏以及多级上下部门的查询问题)
- 关于openstack部分计算节点无法实现热迁移问题描述
- 用循环链表实现约瑟夫问题
- 关于hibernate实现的set类问题
- 关于接口的多态性问题 接口的实现
- Spring关于使用注解@Configuration去配置FormattingConversionServiceFactoryBean来实现自定义格式字符串处理无效的问题(未找到是什么原因造成的)
- 关于位图的一道典型问题
- Josephus(约瑟夫)问题----分别用循环链表和数组实现
- 关于使用图片轮播插件无法实现效果的问题
- 关于CUDA实现最值问题
- 关于算法—— 一维字符串数组之间组合问题的C#实现
- 关于Simple.Data.PostgreSql的ExecuteReader没实现非常坑爹的问题
- 一道关于初始化的问题
- Struts2 关于i18n国际化的问题,可点击链接实现中英文切换
- Struts2 关于i18n国际化的问题,可点击链接实现中英文切换