与普通链表相比,注意建立链表、后插、显示里的不同
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct hnode
{
int num;
struct hnode * next;
};
typedef struct hnode Hnode;
typedef struct hnode * Hlink;
enum result_val{RET_NO = 1000,RET_OK};
int is_malloc_ok(Hlink new_node)
{
if(NULL == new_node)
{
printf("malloc error!\n");
return RET_NO;
}
return RET_OK;
}
int create_node(Hlink* new_node)
{
*new_node = (Hlink)malloc(sizeof(Hnode));
if(RET_NO == is_malloc_ok(*new_node))
{
return RET_NO;
}
return RET_OK;
}
void create_link(Hlink * head)
{
create_node(head);
(*head)->next = *head;
}
void insert_node_tail(Hlink head,Hlink new_node)
{
Hlink p = NULL;
p = head;//
while(p->next != head)
{
p = p->next;
}
p->next = new_node;
new_node->next = head;
}
void display_link(Hlink head)
{
Hlink p = NULL;
p = head;//
if(head->next == head)
{
printf("no such link!\n");
}
else
{
p = head->next;
while(p != head)
{
printf("%d\n",p->num);
//printf("%5d\n",p->num);
p = p->next;
}
}
}
int main()
{
int a,i;
Hlink head;
Hlink new_node;
printf("请输入n的值:");
scanf("%d",&a);
create_link(&head);
for(i=1;i<a+1;i++)
{
create_node(&new_node);
new_node->num = i;
insert_node_tail(head,new_node);
}
display_link(head);
return 0;
}
输出结果:
请输入n的值:5
1
2
3
4
5