c语言之————有头循环单链表

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 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值