主题:利用c语言结构体编写数3游戏,提示程序出错,求解,谢谢
利用c语言的结构体编写数三游戏,编号1-10,报数1、2、3,当报到3的人退出,问最后剩下的一人的编号是什么?
我写的这段代码没有编译错误,但是一运行就提示程序错误,请问各位高手究竟问题在哪里呢?万分感谢啊!以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define n 10
#define New (struct node *)malloc(sizeof (struct node))
struct node
{
int number;
struct node *next;
};
struct node *build()
{
int i;
struct node *p,*q,*head;
head=NULL;
i=0;
while (i<10)
{
i=i+1;
p=New;
p->number=i;
if (head==NULL)
head=p;
else
q->next=p;
q=p;
if (i==10)
q->next=head;
}
return head;
}
struct node *work(struct node *head)
{
int k;
struct node *p,*q;
p=head;
k=0;
while ()
{
q=p;
k=k+1;
p=q->next;
if (k==2)
{
k=0;
q->next=p->next;
free(p);
p=q->next;
}
}
return p;
}
void main()
{
struct node *head,*p;
head=build();
printf("%d is head\n",*head);
p=work(head);
printf("%d is left\n",p->number);
}
谢谢
我写的这段代码没有编译错误,但是一运行就提示程序错误,请问各位高手究竟问题在哪里呢?万分感谢啊!以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define n 10
#define New (struct node *)malloc(sizeof (struct node))
struct node
{
int number;
struct node *next;
};
struct node *build()
{
int i;
struct node *p,*q,*head;
head=NULL;
i=0;
while (i<10)
{
i=i+1;
p=New;
p->number=i;
if (head==NULL)
head=p;
else
q->next=p;
q=p;
if (i==10)
q->next=head;
}
return head;
}
struct node *work(struct node *head)
{
int k;
struct node *p,*q;
p=head;
k=0;
while ()
{
q=p;
k=k+1;
p=q->next;
if (k==2)
{
k=0;
q->next=p->next;
free(p);
p=q->next;
}
}
return p;
}
void main()
{
struct node *head,*p;
head=build();
printf("%d is head\n",*head);
p=work(head);
printf("%d is left\n",p->number);
}
谢谢