list.h
#pragma once
typedef size_t elem_t;
struct node_info {
elem_t data;
struct node_info *next;
};
list.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
/*有头循环单链表*/
/*链表插入节点
1. list_add_head
2. list_add_tail
*/
void list_add_head(struct node_info *head, elem_t data)
{
struct node_info *new = (struct node_info *)
malloc(sizeof(struct node_info));
if (new == NULL) {
fprintf(stderr,"Error: malloc failed!\n");
return ; // exit abort return
}
new->next = head->next;
head->next = new;
new->data = data;
}
void list_add_tail(struct node_info *head, elem_t data)
{
struct node_info *new = (struct node_info *)
malloc(sizeof(struct node_info));
if (new == NULL) {
fprintf(stderr,"Error: malloc failed!\n");
return ;
}
struct node_info *cur = NULL;
//遍历到最后有效节点
for (cur = head->next;
cur != NULL && cur->next != head;
cur = cur->next)
;
if (cur != NULL) {
new->next = head;
cur->next = new;
new->data = data;
}
}
void list_for_each(const struct node_info *head,
void (*todo)(struct node_info *head,
struct node_info *info))
{
struct node_info *cur = NULL;
for (cur = head->next;
cur != NULL && cur != head;
cur = cur->next)
{
todo((struct node_info *)head,cur);
}
}
/*
删除指定节点
@ node:待删除节点的地址
*/
void node_del(struct node_info *head, struct node_info *node)
{
struct node_info *cur = NULL;
for (cur = head; cur != NULL && cur->next != head;
cur = cur->next){
if (cur->next == node){
cur->next = node->next;
node->next = NULL;
free(node);
break;
}
}
}
/*链表初始化*/
void list_init(struct node_info *head)
{
head->data = 0;
head->next = head;
}
/*链表销毁*/
void list_destroy(struct node_info *head)
{
while(head->next != head) {
node_del(head, head->next);
}
free(head);
}
int list_is_empty(struct node_info *head)
{
return head->next == head;
}
测试代码:
test.c
#include <stdio.h>
#include <stdlib.h>
#include "list.h"
void print_node(struct node_info *head,
<span style="white-space:pre"> </span>struct node_info *info)
{
<span style="white-space:pre"> </span>printf("%lu ",info->data);<span style="white-space:pre"> </span>
}
int main(void)
{
// 0...9
struct node_info *head = (struct node_info *)
<span style="white-space:pre"> </span>malloc(sizeof(struct node_info));
list_init(head);
elem_t i;
for (i = 0; i < 10; ++i) {
<span style="white-space:pre"> </span>list_add_tail(head, i);<span style="white-space:pre"> </span>
}
printf("插入数据0-9后打印:\n");
list_for_each(head, print_node);
<span style="white-space:pre"> </span>printf("\n");
struct node_info *cur = NULL;
struct node_info *tmp = NULL;
for (cur = head->next;
<span style="white-space:pre"> </span>(cur != head) && (tmp = cur->next);
<span style="white-space:pre"> </span>cur = tmp) {
<span style="white-space:pre"> </span>if (cur->data > 5 && cur->data < 8) {
<span style="white-space:pre"> </span>node_del(head, cur);
<span style="white-space:pre"> </span>}<span style="white-space:pre"> </span>
}
printf("删除5-8之间的数据后打印:\n");
list_for_each(head, print_node);
<span style="white-space:pre"> </span>printf("\n");
list_destroy(head);<span style="white-space:pre"> </span>
}
编译的Makefiel
test : test.c list.c
gcc -o $@ $^
.PHONY: clean
clean:
$(RM) *.o test
测试结果:
01slist/01head_loop# ./test
插入数据0-9后打印:
0 1 2 3 4 5 6 7 8 9
删除5-8之间的数据后打印:
0 1 2 3 4 5 8 9