https://github.com/liutianshx2012/Algorithms-Data_structures/tree/master/Data_structures/src2
//
// List.h
// Algorithms&Data_structures
//
// Created by TTc on 15-2-2.
// Copyright (c) 2015年 TTc. All rights reserved.
//
/* defined(__Algorithms_Data_structures__List__) */
#ifndef __Algorithms_Data_structures__List__
#define __Algorithms_Data_structures__List__
#include <stdlib.h>
/* define a structure for linked List element */
typedef struct ListElmt_{
void *data;
struct ListElmt_ *next;
}ListElmt;
/*define a structure for linked lists */
typedef struct List_{
int size;
int(*match)(const void * key1,const void *key2);
void(*destroy)(void *data);
ListElmt *head;
ListElmt *tail;
}List;
int test_list();
void print_list(const List *list);
void print_listNode(ListElmt *element);
/* Public Interfaces */
void list_init(List *list ,void (*destroy)(void *data));
void list_destroy(List *list);
int list_ins_next(List *list, ListElmt *element, const void *data);
int list_rem_next(List *list, ListElmt *element, void **data);
#define list_size(list) ((list) -> size)
#define list_head(list) ((list) -> head)
#define list_tail(list) ((list) -> tail)
#define list_is_head(list, element) ((element) == (list) -> head ? 1:0)
#define list_is_tail(list, element) ((element) == (list) -> tail ? 1:0)
#define list_data(element) ((element) -> data)
#define list_next(element) ((element) -> next)
#endif
//
// List.c
// Algorithms&Data_structures
//
// Created by TTc on 15-2-2.
// Copyright (c) 2015年 TTc. All rights reserved.
//
#include <stdio.h>
#include <string.h>
#include "list.h"
/*list init */
void list_init(List *list, void (*destroy)(void *data)) {
/* initialize the list */
list->size = 0;
list->destroy = destroy;
list->head = NULL;
list->tail = NULL;
}
/* 复杂度O(n) 因为 list_rem_next 删除操作的复杂度 O(1)*/
void list_destroy(List *list) {
void *data; //keep the data in the element of the list
/*remove the element */
while( list_size(list) > 0 )
{
if(list_rem_next(list, NULL, (void **)&data) == 0 &&
list->destroy != NULL)
{
list->destroy(data);
}
}
memset(list, 0, sizeof(list));
return;
}
/*
list_ins_next 将一个元素插入有element指定的元素之后。
新元素的数据只想由用户传入的数据。
想链表中插入数据要有2中情况要考虑:
插入头或其他位置,具体参照函数的实现。当传入参数element为NULL,表示从插入到表头;
*/
int list_ins_next(List *list, ListElmt *element, const void *data) {
ListElmt *new_element;
if ((new_element = (ListElmt *)malloc(sizeof(ListElmt))) == NULL) {
return -1;
}
/* insert the element into the list */
new_element->data = (void *)data;
if (element == NULL) { //从链表头部插入时,新元素之前没有别的节点了
/* handle insertion at the head of the list */
if (list_size(list) == 0) {
//无论何时,当插入的元素位于链表的末尾时,度必须更新链表的尾指针
// tail成员使其指向新的节点
list->tail = new_element;
}
//第一步:把新元素的next指针指向当前链表的头部
new_element->next = list->head;
//第二步:重置头节点指针
list->head = new_element;
} else {
/* handle insertion somewhere other than at the head */
if (element->next == NULL) {
list->tail = new_element;
}
//第一步:将新元素的next指针指向它之后的那个元素
new_element->next = element->next;
//第二步:将新元素位置之前的节点next指针指向新插入的元素
element->next = new_element;
}
//最后更新统计链表中节点个数的size成员
list->size++;
return 0;
}
/*
1:从链表中移除element所指定的元素之后的那个节点。
2:移除element之后的那个节点而不是移除element本身。
3:与插入操作相似,需要考虑亮点 A:移除的是List的头节点
;B:移除其余位置上的节点
*/
/* 原理
1:移除操作很简单:一般情况下链表要移除一个元素,只需要将 要移除的目标节点的
前一个元素
的next指针 指向 目标节点的 一下元素。
2:但是,当移除的 目标节点是头节点时,
目标节点之前并没有其他元素了,因此,在这种情况下,只需要将链表的head成员指针
指向目标节点的下一个元素。
3:同insert操作类似,当传入的element为NULL时 就代表链表的 head头节点需要移除
4:无论何时,当移除的目标节点 时链表 的尾部节点时,都必须更新链表数据成员
姐噢谷中的 tail成员,使其指向 新的尾节点,或者当移除操作 使得整个链表成为
空链表时,需要把 tail设置为NULL。
5:最后更新链表的 size成员 使其-1.
6:当这个函数调用返回时,data将指向已经 移除节点的 数据域。
*/
/* 复杂度O(1) */
int list_rem_next(List *list, ListElmt *element, void **data) {
/* Don‘t allow remove from an empty list */
if (list_size(list) == 0) {
return -1;
}
ListElmt *old_element;
/* remove the head element from the list */
//处理特殊情况, 要删除链表的 头节点 (也就是 list的head 节点)
if (element == NULL) {
*data = list->head->data;
//获取 要删除的节点(以方便后面free内存操作)
old_element = list->head;
//将链表的head成员指针 指向目标节点的下一个元素
list->head = list->head->next;
if (list_size(list) == 1) {
list->tail = NULL;
}
} else {
// element->next 指向要删除的
// 节点元素,若为NULL,说明element已经使最后的节点元素了
if (element->next == NULL) {
return -1;
}
*data = element->next->data;
//获取要删除节点指针(方面以后free内存)
old_element = element->next;
//将目标节点的元素的前一个元素的 next指针 指向 目标节点的next指向的元素
element->next = element->next->next;
if (element->next == NULL) {
list->tail = NULL;
}
}
/* free */
free(old_element);
//更新 链表长度
list->size--;
return 0;
}
void print_listNode(ListElmt *element) {
int *data, i;
i = 0;
while (1) {
data = list_data(element);
printf("print_listNode====> list[%d]=%d\n", i, *data);
i++;
element = list_next(element);
if (element == NULL) {
break;
}
}
}
void print_list(const List *list) {
ListElmt *element;
int *data, i;
fprintf(stdout, "List size is %d\n", list_size(list));
i = 0;
element = list_head(list);
while (1) {
data = list_data(element);
// fprintf(stdout, "list[%03d]=%03d\n", i, *data);
printf("print_list======>list[%d]=%d\n", i, *data);
i++;
if (list_is_tail(list, element))
break;
else
element = list_next(element);
}
return;
}
int test_list() {
List list, list2;
ListElmt *element;
int *data, *data2, i;
//初始化list
list_init(&list, free);
list_init(&list2, free);
element = list_head(&list);
int array1[20] = {1, 1, 0, 4, 7, 2, 7, 3, 0, 1};
int array2[20] = {2, 5, 5, 6, 8, 0, 3, 0, 5, 1};
for (i = 9; i >= 0; i--) {
if ((data = (int *)malloc(sizeof(int))) == NULL)
return 1;
*data = array1[i];
if (list_ins_next(&list, NULL, data) != 0) //逐个插入元素
return 1;
if ((data2 = (int *)malloc(sizeof(int))) == NULL)
return 1;
*data2 = array2[i];
if (list_ins_next(&list2, NULL, data2) != 0) //逐个插入元素
return 1;
}
print_list(&list); //打印初始list
printf("开始打印list2......");
// element = list_head(&list); //指向头结点
// for (i = 0; i < 7; i++)
// element = list_next(element);
//
// data = list_data(element);
// fprintf(stdout, "Removing an element after the one containing %03d\n",
// *data);
//
// if (list_rem_next(&list, element, (void **)&data) != 0) //删除指定结点
// return 1;
// print_list(&list);
// fprintf(stdout, "Inserting 011 at the tail of the list\n");
// *data = 11;
// if (list_ins_next(&list, list_tail(&list), data) != 0) //插入指定结点
// return 1;
// print_list(&list);
// fprintf(stdout, "Removing an element after the first element\n");
//
// element = list_head(&list);
// if (list_rem_next(&list, element, (void **)&data) != 0)
// return 1;
//
// print_list(&list);
//
// fprintf(stdout, "Inserting 012 at the head of the list\n");
//
// *data = 12;
// if (list_ins_next(&list, NULL, data) != 0)
// return 1;
//
// print_list(&list);
//
// fprintf(stdout, "Iterating and removing the fourth element\n");
//
// element = list_head(&list);
// element = list_next(element);
// element = list_next(element);
//
// if (list_rem_next(&list, element, (void **)&data) != 0)
// return 1;
//
// print_list(&list);
//
// fprintf(stdout, "Inserting 013 after the first element\n");
//
// *data = 13;
// if (list_ins_next(&list, list_head(&list), data) != 0)
// return 1;
//
// print_list(&list);
//
// i = list_is_head(&list, list_head(&list));
// fprintf(stdout, "Testing list_is_head...Value=%d (1=OK)\n", i);
// i = list_is_head(&list, list_tail(&list));
// fprintf(stdout, "Testing list_is_head...Value=%d (0=OK)\n", i);
// i = list_is_tail(&list,list_tail(&list));
// fprintf(stdout, "Testing list_is_tail...Value=%d (1=OK)\n", i);
// i = list_is_tail(&list,list_head(&list));
// fprintf(stdout, "Testing list_is_tail...Value=%d (0=OK)\n", i);
fprintf(stdout, "Destroying the list\n");
list_destroy(&list);
return 0;
}
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "list.h"
/* destroy */
void destroy(void *data)
{
printf("in destroy\n");
free(data);
return;
}
/* main */
int main(int argc, char **argv)
{
ListElmt *elem_ptr = NULL;
int i;
int ret;
int *data = NULL;
/* Create a linked list */
List list_exp;
/* init the list */
printf("\ninit the list:\n");
list_init(&list_exp, destroy);
/* insert the element */
printf("\ncreate a list:\n");
for(i = 0; i < 10; i++ )
{
data = (int *)malloc(sizeof(int));
if( data == NULL )
return -1;
*data = i;
list_ins_next(&list_exp, NULL, (void *)data);
}
/* now the list has 10 element, then traverse and print every element */
elem_ptr = list_exp.head;
for( i = 0; i < 10; i++ )
{
printf("%d ", *(int *)list_data(elem_ptr) );
elem_ptr = list_next(elem_ptr);
}
printf("\n");
/* Remove the element, its value of the data equal 4,then traverse and print again */
elem_ptr = list_exp.head;
for( i = 0; i < list_size(&list_exp); i++ )
{
if( *(int *)list_data(elem_ptr) == 5 )
{
ret = list_rem_next(&list_exp, elem_ptr, (void **)&data);
if( ret == 0 )
{
destroy(data);
}
}
elem_ptr = list_next(elem_ptr);
}
printf("after remove the element: value = 4\n");
//traverse and print again
elem_ptr = list_exp.head;
for( i = 0; i <list_size(&list_exp); i++ )
{
printf("%d ", *(int *)list_data(elem_ptr) );
elem_ptr = list_next(elem_ptr);
}
printf("\n\n");
printf("here begin to destroy the list :\n");
//destroy the linked list
list_destroy(&list_exp);
return 0;
}
时间: 2024-09-21 10:50:12