#include <stdio.h>
#include <stdlib.h>
struct node
{
int num;
struct node * next;
};
typedef struct node Node;
typedef Node * Link;
//释放节点
void Release(Link * head)
{
Link p;
while ((*head)->next != *head)
{
p = (*head)->next;
(*head)->next = p->next;
free(p);
}
p = *head;
*head = NULL;
free(p);
}
//函数功能:创建节点
int Node_creat(Link * new_node)
{
int n = 0;
do
{
*new_node = (Link)malloc(sizeof(Node));
n++;
if (n == 10)
{
return 0;
}
}while(!*new_node);
return -1;
}
//函数功能:创建链表
void Link_creat(Link * head)
{
if (!Node_creat(head))
{
printf("创建失败!\n");
exit(-1);
}
(*head)->next = *head;
}
//函数功能:插入节点:头插
void Link_insert_head(Link head,Link new_node)
{
new_node->next = head->next;
head->next = new_node;
}
//函数工能:插入节点:尾插
void Link_insert_tail(Link head,Link new_node)
{
Link p;
p = head;
while (p->next != head)
{
p = p->next;
}
new_node->next = p->next;
p->next = new_node;
}
//函数功能:删除指定数据的节点
void Link_delete(Link head,int date)
{
Link p,q;
p = head;
while(p->next != head)
{
if (p->next->num == date)
{
q = p->next;
p->next = q->next;
free(q);
return;
}
p = p->next;
}
}
//函数功能:释放节点
void Link_empty(Link head)
{
Link p;
while (head->next != head)
{
p = head->next;
head->next = p->next;
free(p);
}
}
//函数功能:打印链表
void Link_display(Link head)
{
if (head == NULL)
{
printf("链表已释放\n");
return;
}
if (head->next == head)
{
printf("链表为空\n");
return;
}
Link p;
p = head->next;
while ( p != head)
{
printf("%d\n",p->num);
p = p->next;
}
}
int main()
{
int i;
Link head,new_node;
Link_creat(&head);
for (i = 0; i < 10; i++ )
{
if (!Node_creat(&new_node))
{
Release(&head);
printf("创建失败!\n");
exit(-1);
}
new_node->num = i + 1;
//Link_insert_head(head,new_node);
Link_insert_tail(head,new_node);
}
Link_display(head);
Link_delete(head,10);
Link_display(head);
Link_empty(head);
Link_display(head);
Release(&head);
Link_display(head);
return 0;
}
09-22
2868
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-17
08-15