鱼C论坛

 找回密码
 立即注册
查看: 3630|回复: 6

关于约瑟夫环问题

[复制链接]
发表于 2018-2-7 18:39:38 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
//n个人围圈报数,报m出列,最后剩下的是几号?
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
        int data;
        struct node *next;
}node;

node *create(int n)
{
        node *p = NULL, *head;
        head = (node*)malloc(sizeof (node ));
        p = head;
        node *s;
        int i = 1;

        if( 0 != n )
        {
                while( i <= n )
                {
                        s = (node *)malloc(sizeof (node));
                        s->data = i++;    // 为循环链表初始化,第一个结点为1,第二个结点为2。
                        p->next = s;
                        p = s;
                }
                s->next = head->next;
        }

        free(head);

        return s->next ;
}

int main()
{
        int n = 41;
        int m = 3;
        int i;
        node *p = create(n);
        node *temp;

        m %= n;   // m在这里是等于2

        while (p != p->next )
        {
                for (i = 1; i < m-1; i++)
                {
                        p = p->next ;
                }

                printf("%d->", p->next->data );

                temp = p->next ;                                //删除第m个节点
                p->next = temp->next ;
                free(temp);

                p = p->next ;
        }

        printf("%d\n", p->data );

        return 0;
}


p->next = temp->next 这里为什不可以写成p=temp?问题在哪里?望大神解答谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-7 18:51:22 | 显示全部楼层
要问 为什不可以写成p=temp,先得明白 p->next 与 p 有什么区别?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-7 19:21:54 | 显示全部楼层
人造人 发表于 2018-2-7 18:51
要问 为什不可以写成p=temp,先得明白 p->next 与 p 有什么区别?

那您说说?我的理解是p->next是p指向的下一个地址
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-2-7 21:05:14 | 显示全部楼层
土沙拉 发表于 2018-2-7 19:21
那您说说?我的理解是p->next是p指向的下一个地址
  1. #include <stdio.h>
  2. #include <stdlib.h>

  3. typedef struct Test_tag
  4. {
  5.         int num;
  6.         struct Test_tag *next;
  7. } Test;

  8. int main(void)
  9. {
  10.         Test *head = malloc(sizeof(Test));
  11.         Test *n1 = malloc(sizeof(Test));
  12.         Test *n2 = malloc(sizeof(Test));
  13.         Test *n3 = malloc(sizeof(Test));

  14.         head->num = 0;
  15.         head->next = n1;

  16.         n1->num = 1;
  17.         n1->next = n2;

  18.         n2->num = 2;
  19.         n2->next = n3;

  20.         n3->num = 3;
  21.         n3->next = NULL;

  22.         printf("head:      : 0x%.8X\n", (unsigned int)head);
  23.         printf("head->num  : 0x%.8X\n", (unsigned int)head->num);
  24.         printf("head->next : 0x%.8X\n", (unsigned int)head->next);

  25.         printf("n1         : 0x%.8X\n", (unsigned int)n1);
  26.         printf("n1->num    : 0x%.8X\n", (unsigned int)n1->num);
  27.         printf("n1->next   : 0x%.8X\n", (unsigned int)n1->next);

  28.         printf("n2         : 0x%.8X\n", (unsigned int)n2);
  29.         printf("n2->num    : 0x%.8X\n", (unsigned int)n2->num);
  30.         printf("n2->next   : 0x%.8X\n", (unsigned int)n2->next);

  31.         printf("n3         : 0x%.8X\n", (unsigned int)n3);
  32.         printf("n3->num    : 0x%.8X\n", (unsigned int)n3->num);
  33.         printf("n3->next   : 0x%.8X\n", (unsigned int)n3->next);


  34.        
  35.         Test *p;
  36.         Test *temp;

  37.         p = head;
  38.         while(p != NULL)
  39.         {
  40.                 printf("%d ", p->num);
  41.                 p = p->next;
  42.         }
  43.         printf("\n");

  44. #if 1
  45.         // 删除 n2
  46.         p = n1;
  47.         temp = p->next;
  48.         p->next = temp->next;
  49.         free(temp);
  50. #else
  51.         // 删除 n2
  52.         p = n1;
  53.         temp = p->next;
  54.         p = temp->next;
  55.         free(temp);
  56. #endif

  57.         printf("head:      : 0x%.8X\n", (unsigned int)head);
  58.         printf("head->num  : 0x%.8X\n", (unsigned int)head->num);
  59.         printf("head->next : 0x%.8X\n", (unsigned int)head->next);

  60.         printf("n1         : 0x%.8X\n", (unsigned int)n1);
  61.         printf("n1->num    : 0x%.8X\n", (unsigned int)n1->num);
  62.         printf("n1->next   : 0x%.8X\n", (unsigned int)n1->next);

  63.         printf("n2         : 0x%.8X\n", (unsigned int)n2);
  64.         printf("n2->num    : 0x%.8X\n", (unsigned int)n2->num);
  65.         printf("n2->next   : 0x%.8X\n", (unsigned int)n2->next);

  66.         printf("n3         : 0x%.8X\n", (unsigned int)n3);
  67.         printf("n3->num    : 0x%.8X\n", (unsigned int)n3->num);
  68.         printf("n3->next   : 0x%.8X\n", (unsigned int)n3->next);


  69.         p = head;
  70.         while(p != NULL)
  71.         {
  72.                 printf("%d ", p->num);
  73.                 p = p->next;
  74.         }
  75.         printf("\n");

  76.         return 0;
  77. }
复制代码



#if 1
  1. head:      : 0x004861E0
  2. head->num  : 0x00000000
  3. head->next : 0x00486218
  4. n1         : 0x00486218
  5. n1->num    : 0x00000001
  6. n1->next   : 0x00486080
  7. n2         : 0x00486080
  8. n2->num    : 0x00000002
  9. n2->next   : 0x004860B8
  10. n3         : 0x004860B8
  11. n3->num    : 0x00000003
  12. n3->next   : 0x00000000
  13. 0 1 2 3
  14. head:      : 0x004861E0
  15. head->num  : 0x00000000
  16. head->next : 0x00486218
  17. n1         : 0x00486218
  18. n1->num    : 0x00000001
  19. n1->next   : 0x004860B8
  20. n2         : 0x00486080
  21. n2->num    : 0xDDDDDDDD
  22. n2->next   : 0xDDDDDDDD
  23. n3         : 0x004860B8
  24. n3->num    : 0x00000003
  25. n3->next   : 0x00000000
  26. 0 1 3
  27. 请按任意键继续. . .
复制代码


#if 0
  1. head:      : 0x010061E0
  2. head->num  : 0x00000000
  3. head->next : 0x01006218
  4. n1         : 0x01006218
  5. n1->num    : 0x00000001
  6. n1->next   : 0x01006080
  7. n2         : 0x01006080
  8. n2->num    : 0x00000002
  9. n2->next   : 0x010060B8
  10. n3         : 0x010060B8
  11. n3->num    : 0x00000003
  12. n3->next   : 0x00000000
  13. 0 1 2 3
  14. head:      : 0x010061E0
  15. head->num  : 0x00000000
  16. head->next : 0x01006218
  17. n1         : 0x01006218
  18. n1->num    : 0x00000001
  19. n1->next   : 0x01006080
  20. n2         : 0x01006080
  21. n2->num    : 0xDDDDDDDD
  22. n2->next   : 0xDDDDDDDD
  23. n3         : 0x010060B8
  24. n3->num    : 0x00000003
  25. n3->next   : 0x00000000
  26. 0 1 -572662307 请按任意键继续. . .
复制代码


无标题.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-2-7 21:13:21 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-2-9 11:49:03 | 显示全部楼层
  1.                 for (i = 1; i < m-1; i++)
  2.                 {
  3.                         p = p->next ;
  4.                 }                  //  执行完for语句后走了m-1个节点

  5.                 printf("%d->", p->next->data );      //把第M个节点的值输出 也就是当前结点的下一个节点的值

  6.                 temp = p->next ;                                //把第m个节点给temp
  7.                 p->next = temp->next ;                    //把第M个节点的位置改变成第M+1一个节点
  8.                 free(temp);                                       //删除第M个节点                        
复制代码
      
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

头像被屏蔽
发表于 2018-2-28 14:37:21 | 显示全部楼层
提示: 作者被禁止或删除 内容自动屏蔽
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 19:37

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表