双链表(二)



#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值