# include <stdio.h>
# include <stdlib.h>
typedef struct node //定义链表中结点的结构
{
int code;
struct node *next;
}NODE,*LinkList;
/*错误信息输出函数*/
void Error(char *message)
{
fprintf(stderr,"Error:%s/n",message);
exit(1);
}
//创建循环链表
LinkList createList(int n)
{
LinkList head; //头结点
LinkList p; //当前创建的节点
LinkList tail; //尾节点
int i;
head=(NODE *)malloc(sizeof(NODE));//创建循环链表的头节点
if(!head)
{
Error("memory allocation error!/n");
}
head->code=1;
head->next=head;
tail=head;
for(i=2;i<n;i++)
{
//创建循环链表的节点
p=(NODE *)malloc(sizeof(NODE));
tail->next=p;
p->code=i;
p->next=head;
tail=p;
}
return head;
}
//创建循环链表方法2(软件设计师教程书上的方法)
LinkList createList2(int n)
{
LinkList head,p;
int i;
head=(NODE *)malloc(sizeof(NODE));
if(!head)
{
printf("memory allocation error/n");
exit(1);
}
head->code=1;
head->next=head;
for(i=n;i>1;--i)
{
p=(NODE *)malloc(sizeof(NODE));
if(!p)
{
printf("memory allocation error!/n");
exit(1);
}
p->code=i;
p->next=head->next;
head->next=p;
}
return head;
}
void output(LinkList head)
{
LinkList p;
p=head;
do
{
printf("%4d",p->code);
p=p->next;
}
while(p!=head);
printf("/n");
}
void main(void)
{
LinkList head;
int n;
printf("input a number:");
scanf("%d",&n);
head=createList(n);
output(head);
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有