数据结构 双链表

双链表

头文件

#ifndef _DLINKED_LIST_H__
#define _DLINKED_LIST_H__
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
/*
typedef struct DNode{
	void *pelem;
	struct DNode *prev;
	struct DNode *next;
}DNode;

typedef struct DLinkedL{
	size_t elemSize;
	size_t size;
	struct DNode *head;
}*DLinkedList; 
*/

typedef int ETYPE;

typedef struct DNode{
	ETYPE elem;
	struct DNode *prev; //prior
	struct DNode *next; 
}DNode,*DLinkedList; 

#define DNODESIZE sizeof(struct DNode)


DLinkedList dlinked_list_create(void);

bool dlinked_list_empty(DLinkedList head);

size_t dlinked_list_size(DLinkedList head);

int dlinked_list_insert(DLinkedList head,size_t pos,ETYPE elem);

int dlinked_list_delete(DLinkedList head,size_t pos,ETYPE *pelem);

int dlinked_list_get(DLinkedList head,size_t pos,ETYPE *pelem);

ETYPE *dlinked_list_index(DLinkedList head,size_t pos);

ETYPE *dlinked_list_find(DLinkedList head,ETYPE *pelem);

void dlinked_list_clear(DLinkedList head);

void dlinked_list_destroy(DLinkedList head);


#endif //_DLINKED_LIST_H__

实现函数

#include "dlinkedlist.h"
/*
typedef int ETYPE;

typedef struct DNode{
	ETYPE elem;
	struct DNode *prev; //prior
	struct DNode *next; 
}DNode,*DLinkedList; 
*/

static struct DNode *create_dnode(ETYPE elem,struct DNode *prev,struct DNode *next){
	struct DNode *node = (struct DNode *)malloc(DNODESIZE);
	if(node != NULL){
		node->elem = elem;
		node->prev = prev;
		node->next = next;
	}
	return node;
}

DLinkedList dlinked_list_create(void){
//	struct DNode *head = (struct DNode*)malloc(DNODESIZE);
//	if(head == NULL){
//		return NULL;
//	}
//	head->next = NULL;
//	head->prev = NULL;
//	return head;
	return create_dnode(0,NULL,NULL);
}
bool dlinked_list_empty(DLinkedList head){
	return head->next == NULL;
}
size_t dlinked_list_size(DLinkedList head){
	size_t size = 0;
	struct DNode *node = head->next;
	for(;node!=NULL;node=node->next){
		++size;
	}
	return size;
}
static struct DNode *dlinked_list_get_prev_node(DLinkedList head,size_t pos){
	size_t i;
	struct DNode *node = head;
	for(i=0;i<pos&&node!=NULL;++i){//for(i=0;i<pos&&node!=NULL;++i,node=node->next);
		node = node->next;
	}
	return node;
}

//int dlinked_list_insert_back(DNode *p,ETYPE elem);
//int dlinked_list_insert_front(DNode *p,ETYPE elem);
int dlinked_list_insert(DLinkedList head,size_t pos,ETYPE elem){
	struct DNode *prev = dlinked_list_get_prev_node(head,pos);
	//dlinked_list_insert_back(prev,elem);
	if(prev == NULL){
		return -1;
	}
	/*
	struct DNode *curr = (struct DNode *)malloc(DNODESIZE);
	curr->next = prev->next;
	curr->prev = prev;
	curr->elem = elem; 
	*/
	struct DNode *curr = create_dnode(elem,prev,prev->next);//1,2接了两个指针 
	if(curr == NULL){
		return -1;
	}
	if(prev->next != NULL)
		prev->next->prev = curr;//3 
	
	prev->next = curr;//4 
	return 0;	
}
//int dlinked_list_delete(DNode *p);
int dlinked_list_delete(DLinkedList head,size_t pos,ETYPE *pelem){
	struct DNode *prev = dlinked_list_get_prev_node(head,pos);
	if(prev == NULL || prev->next == NULL){
		return -1;
	}
	struct DNode *curr = prev->next;
	*pelem = curr->elem;
	if(curr->next!=NULL)
		curr->next->prev = prev;
	prev->next = curr->next;
	free(curr);
	return 0;
}

int dlinked_list_get(DLinkedList head,size_t pos,ETYPE *pelem){
	struct DNode *curr = dlinked_list_get_prev_node(head,pos+1);
	if(curr == NULL){
		return -1;
	}
	*pelem = curr->elem;
	return 0;
}


ETYPE *dlinked_list_index(DLinkedList head,size_t pos){
	struct DNode *curr = dlinked_list_get_prev_node(head,pos+1);
	if(curr == NULL){
		return NULL;
	}
	return &curr->elem;
}
ETYPE *dlinked_list_find(DLinkedList head,ETYPE *pelem){
	struct DNode *node = head->next;
	for(;node!=NULL;node = node->next){
		if(node->elem == *pelem){
			return &node->elem;
		}
	}
	return NULL;
}

void dlinked_list_clear(DLinkedList head){
	struct DNode *node,*next;
	for(node=head->next;node!=NULL;node = next){
		next = node->next;
		free(node);
	}
	head->next = NULL;
}
void dlinked_list_destroy(DLinkedList head){
	dlinked_list_clear(head);
	free(head);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HOVL_C++

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值