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

#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
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值