#ifndef _MY_HLIST_H
#define _MY_HLIST_H
typedef struct hlist_node {
struct hlist_node *next; /* next element */
struct hlist_node **pprev; /* address of previous next element */
} HNODE;
typedef struct hlist {
HNODE *first;
} HLIST;
void hlist_init_node(HNODE *node);
void hlist_init_list(HLIST *list);
int hlist_is_empty(HLIST *list);
void hlist_insert_after(HNODE *pos, HNODE *node);
void hlist_insert_before(HNODE *pos, HNODE *node);
void hlist_insert_head(HLIST *list, HNODE *node);
void hlist_delete(/*HLIST *list, */ HNODE *node);
#endif
#include <stdlib.h>
#include "hdll.h"
/*
参数:HNODE *node
返回值:void
作用:初始化node节点
*/
void hlist_init_node(HNODE *node)
{
node->next = NULL;
node->pprev = NULL;
}
/*
参数:HLIST *list
返回值:void
作用:初始化链表
*/
void hlist_init_list(HLIST *list)
{
list->first = NULL;
}
/*
参数:HLIST *list
返回值:int
作用:判断链表是否为空
*/
int hlist_is_empty(HLIST *list)
{
return (list->first == NULL);
}
/*
参数:HNODE *pos, HNODE *node
返回值:void
作用:在双链表pos后面插入node节点
*/
void hlist_insert_after(HNODE *pos, HNODE *node)
{
node->next = pos->next;
if (pos->next != NULL) {
pos->next->pprev = &(node->next);
}
pos->next = node;
node->pprev = &(pos->next);
}
/*
参数:HNODE *pos, HNODE *node
返回值:void
作用:在双链表pos前面插入node节点
*/
void hlist_insert_before(HNODE *pos, HNODE *node)
{
node->pprev = pos->pprev;
node->next = pos;
pos->pprev = &(node->next);
*(pos->pprev) = node;
}
/*
参数:HLIST *list, HNODE *node
返回值:void
作用:在双链表head插入node节点
*/
void hlist_insert_head(HLIST *list, HNODE *node)
{
node->next = list->first;
if (list->first != NULL) {
list->first->pprev = &(node->next);
}
list->first = node;
node->pprev = &(list->first);
}
/*
参数:HNODE *node
返回值:void
作用:删除指定节点node
*/
void hlist_delete(/*HLIST *list, */ HNODE *node)
{
if (node->next != NULL) {
node->next->pprev = node->pprev;
}
*(node->pprev) = node->next;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "hdll.h"
typedef struct life {
HNODE node;
int work;
int study;
int play;
} LIFT;
typedef struct all_life {
HLIST list;
} ALL_LIFT;
void hlist_print(ALL_LIFT *list)
{
HNODE *iter = list->list.first;
LIFT *plift = NULL;
int index = 0;
while (iter != NULL) {
plift = (LIFT *)iter;
printf("%2d: work: %d, study: %d, play: %d\n",
++index, plift->work, plift->study, plift->play);
iter = iter->next;
}
printf("\n");
}
LIFT *hlist_find(ALL_LIFT *list, int study)
{
HNODE *iter = list->list.first;
LIFT *plift = NULL;
while (iter != NULL) {
plift = (LIFT *)iter;
if (plift->study == study) {
break;
}
iter = iter->next;
}
return plift;
}
int main()
{
int i = 0;
int arr[] = { 123, 4, 3, 34, 35, 7, 8, 9, 5, 4, 2, 1 };
int len = sizeof(arr) / sizeof(arr[0]);
struct all_life real_life = { 0 };
struct life *my_life = NULL;
hlist_init_list(&(real_life.list));
for (i = 0; i < len; i++) {
my_life = (struct life *)malloc(sizeof(struct life));
if (my_life == NULL) {
printf("malloc error!\n");
return 1;
}
hlist_init_node(&(my_life->node));
my_life->work = arr[i];
my_life->study = arr[i] + 1;
my_life->play = arr[i] + 2;
hlist_insert_head(&(real_life.list), &(my_life->node));
}
hlist_print(&real_life);
for (i = 0; i < len; i++) {
my_life = hlist_find(&real_life, arr[i] + 1);
hlist_delete(&(my_life->node));
free(my_life);
my_life = NULL;
hlist_print(&real_life);
}
getchar();
return 0;
}