关闭

循环链表

标签: 链表
159人阅读 评论(0) 收藏 举报
分类:

以下是循环单向链表的接口定义:

#ifndef _CLIST_H
#define _CLIST_H

#include <stdlib.h>

typedef struct CListElmt_
{
	void *data;
	struct CListElmt_ *next;
}CListElmt;

typedef struct CList_
{
	int size;
	void (*destroy)(void *data);
	void (*match)(const void *key1,const void *key2);
	CListElmt *head;
}CList;

void clist_init(CList *list,void (*destroy)(void *data));

void clist_destroy(CList *list);

int clist_ins_next(CList *list,CListElmt *element,const void *data);

int clist_rem_next(CList *list,CListElmt *element,void **data);

#define clist_size(list) ((list)->size)
#define clist_head(list) ((list)->head)
#define clist_next(element) ((element)->next)
#define clist_data(element) ((element)->data)

#endif  /* _CLIST_H */

下面是对接口内容的实现:

#include "list.h"
#include  <stdio.h>
#include <string.h>


void clist_init(CList *list,void (*destroy)(void *data))
{
	list->size = 0;
	list->destroy = destroy;
	list->head = NULL;

	return;
}

void clist_destroy(CList *list)
{
	void *data;
	while(clist_size(list) > 0)
	{
		if(clist_rem_next(list,list->head,(void **)&data) == 0 && list->destroy != NULL)
			list->destroy(data);
	}
	memset(list,0,sizeof(CList));
	return;
}

int clist_ins_next(CList *list,CListElmt *element,const void *data)
{
	CListElmt *new_element;
	new_element = (CListElmt *)malloc(sizeof(CListElmt ));
	if(new_element == NULL)
		return -1;
	new_element->data = (void *)data;

	if(clist_size(list) == 0)
	{
		new_element->next = new_element;
		list->head = new_element;
	}
	else  //不存在最后一个元素,所有不需要考虑最后的元素
	{
		new_element->next = element->next;
		element->next = new_element;
	}
	list->size++;
	return 0;
}

int clist_rem_next(CList *list,CListElmt *element,void **data)
{
	CListElmt *old_element;
	if(clist_size(list) == 0)
		return -1;

	*data = element->next->data;
	if(element->next == element)
	{
		old_element = element->next;
		list->head = NULL;
	}
	else
	{
		old_element = element->next;
		element->next = element->next->next;
		if(old_element == clist_head(list))
			list->head = old_element->next;
	}
	free(old_element);	
	list->size--;

	return 0;
}
最后内容是利用这些API来实现一些小应用:

#include "list.h"
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define SIZE 30
CList list;

typedef struct hello_
{
	int a;
	int b;
	int c;
}hello;

void destroy(void *data)
{
	printf("in destroy.\n");
	//free(data); 
	return;
}

hello *init(const int a,const int b,const int c)
{
	hello *ptr;
	ptr = (hello *)malloc(sizeof(hello));
	if(ptr == NULL)
		return NULL;
	ptr->a = a;
	ptr->b = b;
	ptr->c = c;

	return ptr;
}

//查找前驱指针
CListElmt *find_prev(int n)
{
	int i;
	CListElmt *elem;
	elem = clist_head(&list);
	for(i = 1;i < n;i++)
		elem = clist_next(elem);
	return elem;
}

//查找要删除的数据域指针
hello *find_data(int n)
{
	int i;
	hello *hel;
	CListElmt *elem;
	elem = clist_head(&list);
	for(i = 0;i < n;i++)
	{
		elem = clist_next(elem);
		hel = clist_data(elem);
	}
	return hel;
}

/* 遍历函数 */
void loop_print(void )
{
	int i;
	CListElmt *elem;
	hello *p;
	elem = clist_head(&list);
	for(i = 0;i < clist_size(&list);i++)
	{
		p = clist_data(elem);
		printf("a=%d b=%d c=%d\n",p->a,p->b,p->c);
		elem = clist_next(elem);
	}
}

/* 准备插入的元素 */
static const hello hello_zhm = {7,8,9};

int main(void)
{
	hello *hel;
	CListElmt *elem;
	clist_init(&list,destroy);
	printf("Init OK!\n");
	
	hello *ptr_1,*ptr_2,*ptr_3,*ptr_4,*ptr_5;
	ptr_1 = init(1,1,1);
	ptr_2 = init(2,2,2);
	ptr_3 = init(3,3,3);
	ptr_4 = init(4,4,4);
	ptr_5 = init(5,5,5);

	clist_ins_next(&list,NULL,(void *)ptr_1);
	elem = clist_head(&list);
	clist_ins_next(&list,elem,(void *)ptr_2);
	elem = clist_next(elem);
	clist_ins_next(&list,elem,(void *)ptr_3);
	elem = clist_next(elem);
	clist_ins_next(&list,elem,(void *)ptr_4);
	elem = clist_next(elem);
	clist_ins_next(&list,elem,(void *)ptr_5);
	loop_print();

	
	printf("remove the third .now it is ...\n");
	elem = find_prev(2);
	hel = find_data(2);
	clist_rem_next(&list,elem,(void **)&hel);
	loop_print();

	printf("after insert into a fouth node .now ..\n");
	elem = find_prev(3);
	clist_ins_next(&list,elem,(void *)&hello_zhm);
	loop_print();
	
	printf("free the list !\n");
	clist_destroy(&list);
	loop_print();
	printf("free success.\n");
	return 0;
}

下面是运行结果:


    (完毕)

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:916次
    • 积分:67
    • 等级:
    • 排名:千里之外
    • 原创:6篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类