尘叶心繁

c 单链表示例

电脑版发表于:2019/8/21 22:28

main.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "link_list.h"

#define my_malloc malloc
#define my_free free
//定义一个形状
struct rect
{
	int x;
	int y;
	int m;
	int h;
};
struct circle
{
	int x;
	int y;
};
struct shape
{
	int type;
	union
	{
		struct rect r;
		struct circle c;
	}u;
	struct link_node link;
};
//创建一个空间
static struct shape* alloc_shape()
{
	struct shape* one = my_malloc(sizeof(struct shape));
	memset(one, 0, sizeof(struct shape));
	return one;
}
//释放
static void free_shape(struct shape* a)
{
	my_free(a);
}
//清理单链表中所有内存
static void All_shape(hhlinknode all)
{
		if (!all)
		{
			return;
		}
		struct shape* elem = LINK_TO_ELEM(all, struct shape, link);
		if (all->next)
		{
			All_shape(all->next);
		}
		free_shape(elem);
}
int main()
{
	hhlinknode Thead = NULL;
	struct shape* shapeone = alloc_shape();
	shapeone->type = 1;
	list_insert_head(&Thead, &shapeone->link);
	shapeone = alloc_shape();
	shapeone->type = 2;
	list_insert_head(&Thead, &shapeone->link);
	shapeone = alloc_shape();
	shapeone->type = 3;
	list_insert_tail(&Thead, &shapeone->link);
	hhlinknode walk = Thead;
	while (walk)
	{
		//通过偏移指针获取到类型
		struct shape* elem = LINK_TO_ELEM(walk, struct shape, link);
		//struct shape* elem = ((struct shape*)((unsigned char*)walk - (unsigned char*)(&((struct shape*)NULL)->link)));
		printf("%d \n", elem->type);
		walk = walk->next;
	}
	list_insert_remove(&Thead, &shapeone->link);
	All_shape(Thead);
	system("pause");
	return 0;
}


单链表:

link_list.h

#ifndef _LINK_LIST_H_
#define _LINK_LIST_H_


typedef struct link_node{
	struct link_node* next;
}hlinknode,*hhlinknode;
typedef struct link_node *list_head;
//单链表头部添加
void list_insert_head(hhlinknode* header, hhlinknode node);
//单链表尾部添加
void list_insert_tail(hhlinknode* header, hhlinknode node);
//单链表指定节点删除
void list_insert_remove(hhlinknode* header, hhlinknode node);

//#define LINK_TO_ELEM(link,elem_type,mem_name) (((elem_type)*)(((unsigned char*)link)-(unsigned char*)(&((elem_type*)NULL)->mem_name)))
//通过指针
#define LINK_TO_ELEM(link,elem_type,mem_name) \
	((elem_type*)((unsigned char*)link - (unsigned char*)(&((elem_type*)NULL)->mem_name)));

#endif


link_list.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "link_list.h"

void list_insert_head(hhlinknode* header, hhlinknode node)
{
	hhlinknode* walk = header;
	node->next = *walk;
	*walk = node;
}
void list_insert_tail(hhlinknode* header, hhlinknode node) 
{
	hhlinknode* walk = header;
	while (*walk)
	{
		walk = &((*walk)->next);
	}
	node->next = NULL;
	*walk = node;
}
void list_insert_remove(hhlinknode* header, hhlinknode node) {
	hhlinknode* walk = header;
	while (*walk)
	{
		if (*walk==node)
		{
			*walk = node->next;
			node = NULL;
			return;
		}
		//这里必须要改指针的指针,不是改指针的值
		walk = &(*walk)->next;
	}
}


关于TNBLOG
TNBLOG,技术分享。技术交流:群号677373950
ICP备案 :渝ICP备18016597号-1
App store Android
精彩评论
{{item.replyName}}
{{item.content}}
{{item.time}}
{{subpj.replyName}}
@{{subpj.beReplyName}}{{subpj.content}}
{{subpj.time}}
猜你喜欢