C 循环的、单链接的节点被一个接一个地删除的位置
晚上好,我的以下任务的程序没有真正执行它应该执行的操作,我找不到错误 “一组n人被安排成一个圆圈,这些人由1到n编号。此外,一个固定的 给出了自然数m。现在,从第1个人开始计数,第m个人从列表中移除 圈出一个圆圈,然后从下一个人开始重新计数。重复计数,直到只剩下一个人为止。 任务是找到数字L(n,m)∈ 最后一个人的{1,…,n}。 对于固定的n和m,这个问题可以通过一个程序来解决,其中人圈由a表示 循环单链接列表,即“最后一个”条目指向“第一个”条目的单链接列表 例如:L(n=7,m=4)=2,L(21,3)=2,L(100,10)=26“ 例如,当我首先输入n=7&m=4,然后给出列表,然后开始删除过程时,我得到以下输出:C 循环的、单链接的节点被一个接一个地删除的位置,c,singly-linked-list,circular-list,C,Singly Linked List,Circular List,晚上好,我的以下任务的程序没有真正执行它应该执行的操作,我找不到错误 “一组n人被安排成一个圆圈,这些人由1到n编号。此外,一个固定的 给出了自然数m。现在,从第1个人开始计数,第m个人从列表中移除 圈出一个圆圈,然后从下一个人开始重新计数。重复计数,直到只剩下一个人为止。 任务是找到数字L(n,m)∈ 最后一个人的{1,…,n}。 对于固定的n和m,这个问题可以通过一个程序来解决,其中人圈由a表示 循环单链接列表,即“最后一个”条目指向“第一个”条目的单链接列表 例如:L(n=7,m=4)=2
1) input n&m
2) give out list
3) start deleting process
4) END
1
n: 7
m: 4
1) input n&m
2) give out list
3) start deleting process
4) END
2
1->2->3->4->5->6->7
1) input n&m
2) give out list
3) start deleting process
4) END
3
The last person is: 7
1) input n&m
2) give out list
3) start deleting process
4) END
1
n: 6
m: 2
它说最后一个留下来的人是7,这是不真实的。我想它总是说n是最后一个人,不管m的输入是什么。它也只能“工作”一个周期。在我第一次选择选项3)后,我仍然可以输入新的数字n&m,但是程序似乎完全停止工作
谢谢你的时间和帮助
#include <stdio.h>
#include <stdlib.h>
int n,m;
struct s_ring
{
unsigned int position;
struct s_ring *next;
};
typedef struct s_ring *t_ring;
t_ring allocate(void)
{
t_ring pointer;
pointer=(t_ring)malloc(sizeof(*pointer));
if(pointer==NULL)
{
printf("Error malloc");
exit(1);
}
pointer->next=NULL;
return pointer;
}
void circle(t_ring start, t_ring q, int i, int n)
{
t_ring p=start;
while(p->next!=NULL && p->next->position<q->position)
p=p->next;
if(i<n)
{
q->next=p->next;
p->next=q;
}
if(i==n)
{
q->next=start->next;
p->next=q;
start=q;
}
return;
}
void print_list(t_ring p)
{
unsigned int i=p->position;
while(i!=p->next->position)
{
printf("%u->",p->position);
p=p->next;
}
printf("%u\n",p->position);
return;
}
void delete_m(t_ring pointer,t_ring start,int m)
{
t_ring p=start,q;
int i=1;
while(p->position != p->next->position)
{
p=p->next;
i++;
if(i==m)
{
q=p->next;
p->next=p->next->next;
free(q);
i=1;
}
}
if(p->position==p->next->position)
{
pointer=p->position;
}
return pointer;
}
int main()
{
int i;
unsigned int op;
t_ring start,
pointer;
pointer=start;
start=allocate();
do
{
printf("\n 1) input n&m " );
printf("\n 2) give out list " );
printf("\n 3) start deleting process " );
printf("\n 4) END \n" );
scanf("%u",&op);
switch(op)
{
case 1:
printf("n: ");
scanf("%d",&n);
printf("m: ");
scanf("%d",&m);
for(i=1; i<=n; i++)
{
pointer=allocate();
pointer->position=i;
circle(start,pointer,i,n);
}
break;
case 2:
print_list(start->next);
break;
case 3:
delete_m(pointer,start,m);
printf("The last person is: %d",*pointer);
}
}
while(op!=4);
return 0;
}
#包括
#包括
int n,m;
结构s_环
{
无符号整数位置;
结构s_环*下一步;
};
类型定义结构s_环*t_环;
t_环分配(无效)
{
t_环指针;
指针=(t_环)malloc(sizeof(*指针));
if(指针==NULL)
{
printf(“错误malloc”);
出口(1);
}
指针->下一步=空;
返回指针;
}
空心圆(t_环起点,t_环q,int i,int n)
{
t_环p=启动;
while(p->next!=NULL&&p->next->position)
p=p->next;
如果(inext=p->next;
p->next=q;
}
如果(i==n)
{
q->next=开始->下一步;
p->next=q;
开始=q;
}
返回;
}
无效打印列表(t\U环p)
{
无符号整数i=p->位置;
同时(i!=p->next->position)
{
printf(“%u->”,p->位置);
p=p->next;
}
printf(“%u\n”,p->position);
返回;
}
void delete_m(t_环指针,t_环开始,int m)
{
t_环p=启动,q;
int i=1;
同时(p->position!=p->next->position)
{
p=p->next;
i++;
如果(i==m)
{
q=p->next;
p->next=p->next->next;
免费(q);
i=1;
}
}
如果(p->position==p->next->position)
{
指针=p->位置;
}
返回指针;
}
int main()
{
int i;
无符号整数运算;
铃声响起,
指针;
指针=开始;
开始=分配();
做
{
printf(“\n 1)输入n&m”);
printf(“\n 2)分发列表”);
printf(“\n 3)开始删除进程”);
printf(“\n 4)END\n”);
scanf(“%u”,和op);
开关(op)
{
案例1:
printf(“n:”);
scanf(“%d”和“&n”);
printf(“m:”);
scanf(“%d”、&m);
对于(i=1;i位置=i;
圆圈(起点、指针、i、n);
}
打破
案例2:
打印列表(开始->下一步);
打破
案例3:
删除m(指针、开始、m);
printf(“最后一个人是:%d”,*指针);
}
}
而(op!=4);
返回0;
}
您的代码不正确,至少是因为函数delete\m
具有返回类型void
,但返回指针
:)
我可以建议以下方法,如下面的演示程序所示
#include <stdio.h>
#include <stdlib.h>
struct ring_list
{
unsigned int n;
struct ring_list *next;
};
void display( struct ring_list *head )
{
if ( head )
{
struct ring_list *tail = head;
do
{
printf( "%u -> ", head->n );
} while ( ( head = head->next ) != tail );
}
puts( "null" );
}
void clear( struct ring_list **head )
{
while ( *head )
{
struct ring_list *current = *head;
*head = current == ( *head )->next ? NULL : ( *head )->next;
free( current );
}
}
void init( struct ring_list **head, unsigned int n )
{
if ( *head ) clear( head );
struct ring_list *tail = *head;
for ( unsigned int i = 0; i < n; i++ )
{
*head = malloc( sizeof( struct ring_list ) );
( *head )->n = i + 1;
if ( i == 0 ) tail = *head;
head = &( *head )->next;
}
*head = tail;
}
unsigned int remove_each_n( struct ring_list **head, unsigned int n )
{
struct ring_list **initial_head = head;
unsigned int result = 0;
if ( *head != NULL && n != 0 )
{
--n;
while ( *head != ( *head )->next )
{
for ( unsigned int i = 0; i < n; i++ )
{
head = &( *head )->next;
}
struct ring_list *current = *head;
*head = current->next;
if ( ( *head )->next == current ) ( *head )->next = *head;
free( current );
}
result = ( *head )->n;
}
*initial_head = *head;
return result;
}
int main(void)
{
struct ring_list *head = NULL;
unsigned int n = 7;
unsigned int m = 4;
init( &head, n );
display( head );
unsigned int result = remove_each_n( &head, m );
printf( "L( %u, %u ) = %u\n\n", n, m, result );
n = 21;
m = 3;
init( &head, n);
display( head );
result = remove_each_n( &head, m );
printf( "L( %u, %u ) = %u\n\n", n, m, result );
n = 100;
m = 10;
init( &head, n);
result = remove_each_n( &head, m );
printf( "L( %u, %u ) = %u\n\n", n, m, result );
return 0;
}
你试过调试这个程序吗?最好的方法是在调试器中运行程序。和/或甚至添加更多调试打印语句。最好学会自己进行有效调试,而不是过早地转向堆栈溢出。
I input n=7&m=4 It
-请在程序开始时发布完整的输入和程序给出的精确输出。typedef struct s_ring*t_ring代码>-隐藏指针不是一个好主意。我尝试过使用调试器,但不知怎的它无法工作。已多次重新安装代码块,并确保使用了正确的文件。但不知何故,调试器总是丢失。我知道最好是自学,但很遗憾我没有时间做这个家庭作业了。你会想复习:。
#include <stdio.h>
#include <stdlib.h>
struct ring_list
{
unsigned int n;
struct ring_list *next;
};
void display( struct ring_list *head )
{
if ( head )
{
struct ring_list *tail = head;
do
{
printf( "%u -> ", head->n );
} while ( ( head = head->next ) != tail );
}
puts( "null" );
}
void clear( struct ring_list **head )
{
while ( *head )
{
struct ring_list *current = *head;
*head = current == ( *head )->next ? NULL : ( *head )->next;
free( current );
}
}
void init( struct ring_list **head, unsigned int n )
{
if ( *head ) clear( head );
struct ring_list *tail = *head;
for ( unsigned int i = 0; i < n; i++ )
{
*head = malloc( sizeof( struct ring_list ) );
( *head )->n = i + 1;
if ( i == 0 ) tail = *head;
head = &( *head )->next;
}
*head = tail;
}
unsigned int remove_each_n( struct ring_list **head, unsigned int n )
{
struct ring_list **initial_head = head;
unsigned int result = 0;
if ( *head != NULL && n != 0 )
{
--n;
while ( *head != ( *head )->next )
{
for ( unsigned int i = 0; i < n; i++ )
{
head = &( *head )->next;
}
struct ring_list *current = *head;
*head = current->next;
if ( ( *head )->next == current ) ( *head )->next = *head;
free( current );
}
result = ( *head )->n;
}
*initial_head = *head;
return result;
}
int main(void)
{
struct ring_list *head = NULL;
unsigned int n = 7;
unsigned int m = 4;
init( &head, n );
display( head );
unsigned int result = remove_each_n( &head, m );
printf( "L( %u, %u ) = %u\n\n", n, m, result );
n = 21;
m = 3;
init( &head, n);
display( head );
result = remove_each_n( &head, m );
printf( "L( %u, %u ) = %u\n\n", n, m, result );
n = 100;
m = 10;
init( &head, n);
result = remove_each_n( &head, m );
printf( "L( %u, %u ) = %u\n\n", n, m, result );
return 0;
}
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> null
L( 7, 4 ) = 2
1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10 -> 11 -> 12 -> 13 -> 14 -> 15 -> 16 -> 17 -> 18 -> 19 -> 20 -> 21 -> null
L( 21, 3 ) = 2
L( 100, 10 ) = 26