|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct book
{
int date;
struct book *next;
}A ,*AA, **AAA;
/*
void creation_ring(AAA x)
{
AA p, team,fp=NULL;int n,j,k;
time_t t; srand(time(&t));
printf("请输入链表的长度:\n");
scanf("%d",&n);getchar();k=j=n;
if(*x!=NULL)
{*x=NULL;}
while(n--)
{
if(( fp=(AA)malloc(sizeof(A)))==NULL)
{printf("申请内存失败");exit(1); }
else
{
if(*x==NULL)
{*x=fp;fp->next=*x;}
else
{p=team->next;team->next=fp;fp->next=p;}
team=fp;
}
}
while(j--)
{team=team->next;team->date=rand()%100;}
printf("创建成功,链表自动分配数据为:\n");
for(int i=0;i<k;i++)
{team=team->next;printf("%d ",team->date);}
}
*/
void creation_bing(AAA x)
{
AA p, team,fp=NULL;int n=8,j=8;
time_t t; srand(time(&t));
if(*x!=NULL)
{*x=NULL;}
while(n--)
{
if(( fp=(AA)malloc(sizeof(A)))==NULL)
{printf("申请内存失败");exit(1); }
else
{
if(*x==NULL)
{*x=fp;fp->next=*x;}
else
{p=team->next;team->next=fp;fp->next=p;}
team=fp;
}
}
while(j--)
{team=team->next;team->date=rand()%100;}
team->next=team->next->next->next;
printf("创建成功,链表自动分配数据为:\n");
p=*x;
for(int i=0;i<8;i++)
{printf("%d ",p->date);p=p->next;}
}
void creation_line(AAA x)
{
AA p, team,fp=NULL;int n,j,k;
time_t t; srand(time(&t));
printf("请输入链表的长度:\n");
scanf("%d",&n);getchar();k=j=n;
if(*x!=NULL)
{*x=NULL;}
while(n--)
{
if(( fp=(AA)malloc(sizeof(A)))==NULL)
{printf("申请内存失败");exit(1); }
else
{
if(*x==NULL)
{*x=fp;fp->next=NULL;}
else
{fp->next=*x;*x=fp;}
}
}
while(j--)
{fp->date=rand()%100;fp=fp->next;} fp=*x;
printf("创建成功,链表自动分配数据为:\n");
for(int i=0;i<k;i++)
{printf("%d ",fp->date);fp=fp->next;}
}
void one_way (AAA x)
{
AA fp1=*x,fp2=*x;
int k=0,f=0,j;
printf("方法一:\n\n");
while(1)
{
fp1=fp1->next;k++;j=k;
while(j--)
{
fp2=fp2->next;f++;
if(fp1==fp2) {break;}
}
if(f!=k) {printf("该链表是有环,结点位置为%d\n",f+1);break;}
fp2=*x;f=0;
if(fp1==NULL){printf("该链表是无环的\n");break;}
}
}
void two_way(AAA x)
{
AA fp1=*x,fp2=*x;
printf("方法二:\n\n");
while(1)
{
printf("fp1=%d fp2=%d\n",fp1->date,fp2->date);
if(fp1!=NULL)
{ fp1=fp1->next;
if(fp2->next->next!=NULL)
{fp2=fp2->next->next;}
else{printf("该表无环\n");break;}
}
if(fp1==fp2){printf("该表有环\n");break;}
}
}
void out(AAA x)
{
printf("程序已退出\n"); exit(1);
}
void(*option(char*c))(AAA x)
{
switch(*c)
{
case'1':return creation_bing;
case'2':return creation_line;
case'3':return one_way;
case'4':return two_way;
case'5':return out;
}
}
void command(void)
{
printf("\n #####指令集#######\n");
printf(" 1=创建环表");
printf(" 2=创建线表\n");
printf(" 3=判断是否有环法一\n");
printf(" 4=判断是否有环法二\n");
printf(" 5=退出程序");
printf(" #################\n");
}
int main()
{
AA fp; char c;
command();
while(1)
{
printf("请输入指令:\n");
scanf("%c",&c);getchar();
(*option(&c))(&fp);
putchar('\n');
}
return 0;
}
|
|