#include <stdio.h>//循环链表的创建与释放
#include <stdlib.h>
#define ERR 0
#define OK 1
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef struct node * Link;
void create_link(Link * head)
{
*head = (Link)malloc(sizeof(Node));
(*head)->next = *head;
// *head = NULL;
}
void insert_node_circle(Link* head,Link new_node)
{
Link p = NULL;
p = *head;
while(p->next != *head)
{
p = p->next;
}
p ->new = new_node;
new_node ->next = *head;
}
void display_link(Link head)
{
Link p;
p = head;
if(p == NULL)
{
printf("Link is already clear\n");
}
else
{
p = p->next;
while(p != NULL)
{
printf("num = %d\n",p->num);
p = p->next;
}
}
}
void is_malloc_ok(Link new_node)
{
if(new_node == NULL)
{
printf("malloc error!\n");
exit(-1);
}
}
void create_node(Link * new_node,int i)
{
*new_node =(Link)malloc(sizeof(Node));
while(is_malloc_ok(*new_node) == ERR)
{
*new_node = (Link)malloc(sizeof(Node));
}
is_malloc_ok(*new_node);
(*new_node)->num = i;
}
void release(Link * head)
{
Link p;
if(*head == NULL)
{
printf("Link has been already cleared\n");
}
else
{
p = (*head)->next;
while(p != *head)
{
(*head)->next = p->next;
free(p);
p = (*head)->next;
}
free(*head);
*head = NULL
}
}
int main()
{
Link head = NULL;
Link new_node = NULL;
int i;
int num_val,num_loc;
create_link(&head);
for(i = 0;i < 10;i++)
{
create_node(&new_node,i);
insert_node_head(&head,new_node);
}
display_link(head);
printf("\n");
release(&head);
display_link(head);
return 0;
}