栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C语言数据结构之循环链表的简单实例

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C语言数据结构之循环链表的简单实例

 C语言数据结构之循环链表的简单实例

实例代码:

# include 
# include 
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;inext=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);
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/63500.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号