通用单向链表和双向链表

单链表

/***
module: singly linked list
*/
#ifndef __SLIST_H__
#define __SLIST_H__

typedef struct s_entry_s
{
	struct s_entry_s *flink;
}s_entry_t;

//单链表
typedef struct slist_entry_s
{
	s_entry_t* head;
	s_entry_t* tail;
}slist_entry_t;

void slist_init(slist_entry_t* slist);

void slist_add_tail(struct slist_entry_s* slist, struct s_entry_s* newnode);

void slist_add_head(slist_entry_t* slist, s_entry_t* newnode);

#define slist_for_each(pos, slist) \
	for (pos = (slist)->head;  pos != NULL; pos = pos->flink)

#endif 

单链表实现

#include "slist.h"
#include <stdlib.h>

void slist_init(slist_entry_t* slist)
{
	if (!slist)	return;
	slist->head = NULL;
	slist->tail = NULL;
}

void slist_add_tail(slist_entry_t* slist, s_entry_t* newnode)
{
	if (!slist || !newnode)
		return;
	newnode->flink = NULL;
	if (!slist->head)
	{
		slist->head = newnode;
		slist->tail = newnode;
	}
	else
	{
		slist->tail->flink = newnode;
		slist->tail = newnode;
	}
}

void slist_add_head(slist_entry_t* slist, s_entry_t* newnode)
{
	if (!slist || !newnode)
		return ;

	newnode->flink = slist->head;
	if (!slist->tail)
	{
		slist->tail = newnode;
	}

	slist->head = newnode;
}

双向链表节点定义

/****
module: double linked list
*****/
#ifndef __DLIST_H__
#define __DLIST_H__
typedef struct d_entry_s
{
	struct d_entry_s *flink;
	struct d_entry_s *blink;
}d_entry_t;

void dlist_init(d_entry_t* head);

void dlist_add_tail(d_entry_t* head, d_entry_t* newnode);

void dlist_add_head(d_entry_t* head, d_entry_t* newnode);

#define dlist_for_each(pos, head) \
	for (pos = (head)->flink; pos != head; pos = pos->flink)

#define dlist_reverse_for_each(pos, head)  \
	for (pos = (head)->blink; pos != head; pos = pos->blink)

#endif 

双向链表实现:

#include "dlist.h"

void dlist_init(d_entry_t* head)
{
	if (!head) return ;
	head->flink = head;
	head->blink = head;
}

void dlist_add(d_entry_t* prev, d_entry_t* next, d_entry_t* newnode)
{
	if (!newnode || !prev || !next) return;
	next->blink = newnode;
	newnode->flink = next;
	newnode->blink = prev;
	prev->flink = newnode;
}

void dlist_add_tail(d_entry_t* head, d_entry_t* newnode)
{
	if (!head || !newnode) return ;

	dlist_add(head->blink, head, newnode);
}

void dlist_add_head(d_entry_t* head, d_entry_t* newnode)
{
	if (!head || !newnode) return ;

	dlist_add(head, head->flink, newnode);
}

使用例子:

#include "slist.h"
#include "dlist.h"
#include "common.h"

#include <stdlib.h>

typedef struct Student
{
	s_entry_t node;
	int age;
}Student;

extern void testslink()
{
	slist_entry_t stu_head;
	slist_init(&stu_head);
	int i = 0;
	for (i = 0; i < 3; ++i)
	{
		struct Student* p = (struct Student*)malloc(sizeof(struct Student));
		p->age = i;
		slist_add_tail(&stu_head, &p->node);
	}
	printf("\n");
	s_entry_t *pos = NULL;
	slist_for_each(pos, &stu_head)
	{
		printf("age:%d ", ((Student*)pos)->age);
	}

	for (i = 0; i < 3; ++i)
	{
		struct Student* p = (struct Student*)malloc(sizeof(struct Student));
		p->age = i;
		slist_add_head(&stu_head, &p->node);
	}
	printf("\n");
	// show   2 1 0 0 1 2 
	
	slist_for_each(pos, &stu_head)
	{
		printf("age:%d ", ((Student*)pos)->age);
	}
}

typedef struct Teacher
{
	d_entry_t node;
	int salary;

}Teacher;

extern void testdlink()
{
	d_entry_t dlist_head;
	dlist_init(&dlist_head);
	int i = 0;
	for (i = 0; i < 3; ++i)
	{
		struct Teacher* p = (struct Teacher*)malloc(sizeof(struct Teacher));
		p->salary = 1000*i+1000;
		dlist_add_tail(&dlist_head, &p->node);
	}
	printf("\n");
	d_entry_t* pos = NULL;
	dlist_for_each(pos, &dlist_head)
	{
		printf("salary:%d ", ((Teacher*)pos)->salary);
	}
	printf("\n");
	struct Teacher* p = NULL;
	dlist_reverse_for_each(pos, &dlist_head)
	{
		p = container_of(pos, struct Teacher, node);
		//printf("salary:%d ", ((Teacher*)pos)->salary);
		printf("salary:%d ", p->salary);
	}

	printf("\n");
}

其中contain_of 宏如下:

#ifndef __COMMON_H__
#define __COMMON_H__

#define offsetof(TYPE, MEMBER) ((size_t)&((TYPE *)0)->MEMBER)

#define  container_of(ptr, type, member)  \
         (type *)( (char *)ptr - offsetof(type,member));

#endif

下载代码在 我的 github:

https://github.com/jxdeng3264/xBrain/tree/master/SCL

[赞赏]

发布了37 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/xiaod_szu/article/details/105006265