c语言中如何写一循环链表队列,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;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);

}

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

时间: 2017-06-24

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值