环形列表问题

for_grasp 2017-10-05 10:20:47
#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct circle
{
int people;
int count=0;
struct circle *next;
}one;
//创建链表环
one *create(int n)
{
one *head,*p1,*p2,t;
head=NULL;
printf("input the numbers of the people:\n");
for(int i=0;i<n;i++)
{
p1=(one *)malloc(sizeof(one)); if(!p1)exit(1); //开辟空间,如果分配失败退出
scanf("%d",&p1->people);
t.count++;
if(head==NULL)
head=p1;
else p2->next=p1;
p2=p1;
}
p2->next=head;
return head;
}
int main()
{
one *head,*p,*del,t;
int n,i=2,dis;
printf("please input the people numbers: \n");
scanf("%d",&n);
head=create(n);
p=head;
printf("please input the distance you want to selete:\n");
scanf("%d",&dis);
while(1) //无限循坏直到只剩一人break退出
{
if(t.count==2&&i==dis) //只剩两人时
{
printf("the no.%d is out \n",p->next->people);
del=p->next;
p->next=NULL;
free(del);
t.count--;
printf("the no.%d is the last person int the end:\n",p->people );
break;
} // if()
if(i==dis) //遇到等距数dis则删除结点,继续循环
{
printf("the no.%d is out \n",p->next->people);
del=p->next ;
p->next=del->next;
free(del); //删除结点
t.count--; //相应人数减一
i=1; //重新计数
} // if( )
p=p->next;
i++;
} // while()
return 0;
} ps:不知道段错误发生在无限循坏while里的哪部分?
...全文
216 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
这样改:

#include<stdio.h>
#include<malloc.h>
#include<stdlib.h>
typedef struct circle
{
	int people;
	int count = 0;
	struct circle *next;
}one;
//创建链表环
one *create(int n)
{
	one *head, *p1, *p2, t;
	head = NULL;
	printf("input the numbers of the people:\n");
	for (int i = 0;i < n;i++)
	{
		p1 = (one *)malloc(sizeof(one)); 
		if (!p1) {
			exit(1); //开辟空间,如果分配失败退出
		}

		scanf("%d", &p1->people);
		t.count++;		//这里计数没有用,除非将t声明成全局变量,或者作为引用参数传进来
		if (head == NULL) {
			head = p1;
		} else {
			p2->next = p1;
		}

		p2 = p1;
	}
	p2->next = head;
	return head;
}
int main()
{
	one *head, *p, *del, t;
	int n, i = 2, dis;
	printf("please input the people numbers: \n");
	scanf("%d", &n);
	head = create(n);
	p = head;
	t.count = n;	// 要计数的
	printf("please input the distance you want to selete:\n");
	scanf("%d", &dis);
	while (1)   //无限循坏直到只剩一人break退出
	{
		if (t.count == 2 && i == dis)   //只剩两人时 
		{
			printf("the no.%d is out \n", p->next->people);
			del = p->next;
			p->next = NULL;
			free(del);
			t.count--;
			printf("the no.%d is the last person int the end:\n", p->people);
			break;
		}    //  if()
		if (i == dis)   //遇到等距数dis则删除结点,继续循环 
		{
			printf("the no.%d is out \n", p->next->people);
			del = p->next;
			p->next = del->next;
			free(del);  //删除结点
			t.count--;   //相应人数减一 
			i = 1;       //重新计数 
		}  // if( )
		p = p->next;
		i++;
	}  // while() 
	return 0;
}
自信男孩 2017-10-09
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node
{
    int num;
    char name[20];
    struct node *next;
};

struct node *create_singly_link(int num)
{
    struct node*head, *prev, *cur;
    int i;

    head = NULL;
    for(i = 0; i < num; i++) {
        cur = (struct node *)malloc(sizeof(struct node));
        if (!cur) {
            printf("Malloc error!");
            return NULL;
        }
        cur->next = NULL;
        cur->num = i+1;
        snprintf(cur->name, 20-1, "%c", 'A'+i);
        if (!head) {
            head = cur;
            prev = cur;
        } else {
            prev->next = cur;
            prev = prev->next;
        }
    }
    prev->next = head;
    return head;
}

static void test_print(struct node *phead)
{
    struct node *pcur = phead->next;

    printf("num = %d\t name = %s\n", phead->num, phead->name);
    while (pcur != phead) {
        printf("num = %2d\t name = %s\n", pcur->num, pcur->name);
        pcur = pcur->next;
    }
}

int output( struct node *head, int start, int step, int total)
{
    struct node *p, *q;
    int count = 0;
    int num = 0, i;

    q = head;

    for(i = 0; i < start; i++)
        q = q->next;

    while(count < total-1) {
        p = q->next;
        num++;
        if(!(num % step)) {
            q->next = p->next;
            printf("%s\n", p->name);
            free(p);
            count++;
        } else {
            q = p;
        }
    }
    printf("num = %d\t, name = %s\n", q->num, q->name);
    free(q);

    return 0;
}
int main(int argc, const char *argv[])
{
    int total, step, start;
    struct node *head;

    printf("Please input total test number: ");
    scanf("%d", &total);
    head = create_singly_link(total);
    test_print(head);

    printf("Please input start position and steps: ");
    scanf("%d%d", &start, &step);

    output(head, start, step, total);

    return 0;
}
约瑟夫环,可以参考一下这个程序。
自信男孩 2017-10-09
  • 打赏
  • 举报
回复
#include<stdio.h>
//#include<malloc.h>
#include<stdlib.h>

typedef struct circle
{
    int people;
    struct circle *next;
}one;

int count;

//创建链表环
one *create(int n)
{
    one *head,*p1,*p2;
    int i;

    head = NULL;
    for(i = 0; i < n; i++)
    {
        p1 = (one *)malloc(sizeof(one));
        if(!p1)
            exit(1); //开辟空间,如果分配失败退出
        scanf("%d", &p1->people);
        count++;
        if(!head)
            head = p1;
        else
            p2->next = p1;
        p2 = p1;
    }
    p2->next = head;
    return head;
}

void print_cicle(one *head)
{
    one *p = head;
    int i = 0;

    while (i < 6) {
        printf("%d\t", p->people);
        p = p->next;
        i++;
    }

    putchar(10);
}

int main()
{
    one *head, *p, *pre;
    int n, i = 2, dis;
    printf("please input the people numbers: \n");
    scanf("%d",&n);
    head = create(n);
    printf("count = %d\n", count);

    print_cicle(head);
    p = head;
    pre = p;
    printf("please input the distance you want to selete:\n");
    scanf("%d", &dis);

    i = 0;
    while (1) {
        i++;
        //printf("+++++No.%d\n", p->people);
        if (count == 1)
            break;
        if (i % dis == 0) {
            printf("the no.%d is out \n", p->people);
            if (p == head) {
                head = head->next;
                free(p);
                pre = head;
                p = head;
            } else {
                pre->next = p->next;
                free(p);
                p = pre->next;
            }
            count--;
        } else {
            pre = p;
            p = p->next;
        }
    }
    printf("the no.%d is the last person int the end:\n", p->people);
    free(p);


    /*
    while(1)   //无限循坏直到只剩一人break退出
    {
        if(t.count==2&&i==dis)   //只剩两人时
        {
            printf("the no.%d is out \n",p->next->people);
            del=p->next;
            p->next=NULL;
            free(del);
            t.count--;
            printf("the no.%d is the last person int the end:\n",p->people );
            break;
        }    //  if()
        if(i==dis)   //遇到等距数dis则删除结点,继续循环
        {
            printf("the no.%d is out \n",p->next->people);
            del=p->next ;
            p->next=del->next;
            free(del);  //删除结点
            t.count--;   //相应人数减一
            i=1;       //重新计数
        }  // if( )
        p = p->next;
        i++;
    }  // while()
    */
    return 0;
}
参考一下吧
zarelaky 2017-10-06
  • 打赏
  • 举报
回复
第二个if中,p->next==p时,free后应该break
做一门精致,全面详细的 java数据结构与算法!!!让天下没有难学的数据结构,让天下没有难学的算法,不吹不黑,我们的讲师及其敬业,可以看到课程视频,课件,代码的录制撰写,都是在深夜,如此用心,其心可鉴,他不掉头发,谁掉头发???总之你知道的,不知道的,我们都讲,并且持续更新,走过路过,不要错过,不敢说是史上最全的课程,怕违反广告法,总而言之,言而总之,这门课你值得拥有,好吃不贵,对于你知识的渴求,我们管够管饱话不多说,牛不多吹,我们要讲的本门课程内容:稀疏数组、单向队列、环形队列、单向链表、双向链表、环形链表、约瑟夫问题、栈、前缀、中缀、后缀表达式、中缀表达式转换为后缀表达式、递归与回溯、迷宫问题、八皇后问题、算法的时间复杂度、冒泡排序、选择排序、插入排序、快速排序、归并排序、希尔排序、基数排序(桶排序)、堆排序、排序速度分析、二分查找、插值查找、斐波那契查找、散列、哈希表、二叉树、二叉树与数组转换、二叉排序树(BST)、AVL树、线索二叉树、赫夫曼树、赫夫曼编码、多路查找树(B树B+树和B*树)、图、图的DFS算法和BFS、程序员常用10大算法、二分查找算法(非递归)、分治算法、动态规划算法、KMP算法、贪心算法、普里姆算法、克鲁斯卡尔算法、迪杰斯特拉算法、弗洛伊德算法马踏棋盘算法。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧