#include<stdio.h>
#include<malloc.h>
#include<conio.h>
typedef struct node{
char name; //链表中数据项为成员名字
struct node *next;
}LinkList;
//数据结点
typedef struct{
int length;
struct node *next;
}Tnode;
//头结点
Tnode *head=NULL;
Tnode *CreatList()
{//头插法建立链表
Tnode *head;
LinkList *p;
unsigned char fg;
head=(Tnode *)malloc(sizeof(Tnode));
head->next=NULL;
head->length=0;
printf("请输入参加的成员的名单:并以@结束!\n");
scanf("%c",&fg);
while(fg!='@')
{
p=(LinkList *)malloc(sizeof(LinkList));
head->length++; //头结点中存储链表中的数据结点数目
p->name=fg;
p->next=head->next;
head->next=p;
scanf("%c",&fg);
}
if(fg=='@') printf("成员输入结束!\n");
return head;
}
int EmptyList(Tnode *head)
{//判断链表是否为空表是就返回1,不是则返回0
if(head->length==0)
return 1;
else return 0;
}
LinkList *LocateList(Tnode *head,int number)
{//返回number-1号结点的地址,number大于1
LinkList *p=head->next;
int i=1;
while(i<(number-1))
{
p=p->next;
i++;
}
return p;
}
void DeleteList(Tnode *head,int number)
{//删除序号为number号的结点
LinkList *p,*q;
if(number<0||number>head->length)
{
printf("number输入有误!");
return;
}
if(number==1)
{
q=head->next;
head->next=head->next->next;
head->length=head->length-1;
printf("%c ",q->name); //打印出被删除的结点中的数据项
free(q);
}
else
{
p=LocateList(head,number);
q=p->next;
p->next=q->next;
head->length=head->length-1;
printf("%c ",q->name);
free(q);
}
}
void FunList(Tnode *head,int n)
{//报到n的人出列 //约瑟夫
int number=0;
int j=0;
int count=0;
do{
count++;
j++;
if(count==n)
{
count=0;
number=j%(head->length);
j=j-1;
DeleteList(head,number);
}
}while(!EmptyList(head));
}
void main()
{
int tag;
CreatList();
printf("请输入几号出列:\n");
scanf("%d",&tag);
FunList(head,tag);
getch();
}
First-chance exception in Cpp1.exe: 0xC0000005: Access Violation.
这是错误代码,并且还打了个箭头指向 if(head->length==0) 这句!
不知道出现了什么问题
[此贴子已经被作者于2007-9-30 22:31:57编辑过]