数据结构约瑟夫环问题(解决了给加50分)

网上找的,但是那个老师要求只生成N个结点,不能有空的结点,怎么修改?#include"stdio.h"#include"stdlib.h"typedefstructLNo... 网上找的,但是那个老师要求只生成N个结点,不能有空的结点,怎么修改?
#include "stdio.h"
#include "stdlib.h"
typedef struct LNode
{
int pwd;//每个人所持有的密码
int num;//每个人的编号
struct LNode *next;
}LNode,*cirLinkList;

void CreateLinkList(cirLinkList &L, int n)//构造链表并初始化
{
L=(cirLinkList)malloc(sizeof(LNode));
if (!L)
{
printf("\nError in initing the linklist.\n");
exit(1);
}
L->next=L;//构造循环
L->num=0;
L->pwd=0;
cirLinkList p,q;
q=L;
for (int i=1;i<=n;i++)
{
p=(LNode *)malloc(sizeof(LNode));
if (!p)
{
printf("\nError in creating the linklist.\n");
exit(1);
}
scanf("%d",&p->pwd);
p->num=i;
L->next=p;
L=p;
}
L->next=q->next;
free(q);
}

void main()
{
cirLinkList L,p,q;
int n,x;
L=NULL;
printf("Please input the total number of people:");
scanf("%d",&n);//总共的人数n
printf("The origin passward is:");
scanf("%d",&x);//初始密码为x
printf("Please input every people's passward:\n");
CreateLinkList(L,n);//建立约瑟夫环
p=L;
printf("The order of people going out of the ring is:\n");
for (int i=1;i<=n;i++)
{
for (int j=1;j<x;j++)
{
p=p->next;//指针移动到第x个结点处
}
q=p->next;
x=q->pwd;//把要删除的人的密码作为新密码
printf("%d ",q->num);
p->next=q->next;//删除结点
free(q);//释放结点q
}
}
展开
 我来答
k_studio
2009-12-17
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
就把你有问题的地方改了一下
void CreateLinkList(cirLinkList &L, int n)//构造链表并初始化
{
L=(cirLinkList)malloc(sizeof(LNode));
if (!L)
{
printf("\nError in initing the linklist.\n");
exit(1);
}
L->next=L;//构造循环
L->num=1;
scanf("%d",&L->pwd);
cirLinkList p,q;
q=L;
for (int i=2;i<=n;i++)
{
p=(LNode *)malloc(sizeof(LNode));
if (!p)
{
printf("\nError in creating the linklist.\n");
exit(1);
}
scanf("%d",&p->pwd);
p->num=i;
L->next=p;
L=p;
}
L->next=q;
}
光点科技
2023-08-15 广告
通常情况下,我们会按照结构模型把系统产生的数据分为三种类型:结构化数据、半结构化数据和非结构化数据。结构化数据,即行数据,是存储在数据库里,可以用二维表结构来逻辑表达实现的数据。最常见的就是数字数据和文本数据,它们可以某种标准格式存在于文件... 点击进入详情页
本回答由光点科技提供
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式