数据结构:双向链表

1.头文件(dlist.h)

#pragma once


typedef int ElemType;

typedef struct DNode
{
	ElemType data;
	struct DNode* next;
	struct  DNode* prior;
}DNode,*DList;


//初始化plist
void InitList(DList plist);

//往plist中头部插入数字val
bool Insert_head(DList plist, ElemType val);

//往plist中的尾部插入数字val
bool Insert_tail(DList plist, ElemType val);

//在plist中查找val值,找到返回该节点地址,失败返回NULL
DNode* Search(DList plist, ElemType val);

//删除plist中的第一个val
bool DeleteVal(DList plist, ElemType val);

//判断plist是否为空链表(没有数据节点)
bool IsEmpty(DList plist);

//获取plist长度,数据节点的个数
int GetLength(DList plist);

//获取plist链表的pos位置的值
bool GetElem(DList plist, int pos, int* rtval);//rtval:输出参数

//获取val的前驱
DNode* Prior(DList plist, ElemType val);

//获取val的后继
DNode* Next(DList plist, ElemType val);

//输出plist的所有数据
void Show(DList plist);

//清空数据
void Clear(DList plist);

//销毁
void Destroy(DList plist);

2.函数实现文件(dlist.cpp)

#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include"dlist.h"


void InitList(DList plist)
{
	assert(plist != NULL);
	if (plist == NULL)
		return;
	plist->next = NULL;
	plist->prior = NULL;
}


bool Insert_head(DList plist, ElemType val)
{
	DNode* p = (DNode*)malloc(sizeof(DNode));
	assert(p != NULL);
	if (p == NULL)
		return false;
	p->data = val;
	p->next = plist->next;
	plist->next = p;
	p->prior = plist;
	if (p->next != NULL)
		p->next->prior = p;
	return true;
}

bool Insert_tail(DList plist, ElemType val)
{
	DNode* p = (DNode*)malloc(sizeof(DNode));
	assert(p != NULL);
	if (p == NULL)
	{
		return false;
	}
	p->data = val;
	DNode* q;
	for (q = plist; q->next != NULL; q = q->next)
		;
	p->next = q->next;
	q->next = p;
	p->prior = q;
	return true;	
}



//在plist中查找val值,找到返回该节点地址,失败返回NULL
DNode* Search(DList plist, ElemType val)
{
	for (DNode* p = plist->next; p != NULL; p = p->next)
	{
		if (p->data == val)
			return p;
	}
	return NULL;
}

//删除plist中的第一个val
bool DeleteVal(DList plist, ElemType val)
{
	DNode* p = Search(plist, val);
	if (p == NULL)
		return NULL;
	p->prior->next = p->next;
	if (p->next != NULL)
	{
		p->next->prior = p->prior;
	}
	
	free(p);
}

//判断plist是否为空链表(没有数据节点)
bool IsEmpty(DList plist)
{
	return plist->next == NULL;
}


//获取plist长度,数据节点的个数
int GetLength(DList plist)
{
	int count=0;
	for (DNode* p = plist->next; p != NULL; p = p->next)
	{
		count++;
	}
	return count;
}

//获取plist链表的pos位置的值
bool GetElem(DList plist, int pos, int* rtval)//rtval:输出参数
{
	if (pos < 0 || pos >= GetLength(plist))
	{
		return false;
	}
	DNode* p = plist->next;
	for (int i = 0; i < pos; i++, p = p->next)
	{
		;
	}
	*rtval = p->data;
	return true;
}

//获取val的前驱
DNode* Prior(DList plist, ElemType val)
{
	DNode* p = Search(plist, val);
	if (p == NULL)
		return NULL;
	return p->prior;
}

//获取val的后继
DNode* Next(DList plist, ElemType val)
{
	DNode* p = Search(plist, val);
	if (p == NULL)
		return NULL;
	return p->next;
}

//输出plist的所有数据
void Show(DList plist)
{
	for (DNode* p = plist->next; p != NULL; p = p->next)
	{
		printf("%d", p->data);	
	}
	printf("\n");
}

//清空数据
void Clear(DList plist)
{
	Destroy(plist);
}

//销毁
void Destroy(DList plist)
{
	DNode* p;
	while (plist->next!=NULL)
	{
		p = plist->next;
		plist->next = p->next;
		free(p);
	}
}

3.测试文件(test,cpp)

#include<stdio.h>
#include"dlist.h"

int main()
{
	DNode head;
	InitList(&head);
	for (int i = 0; i < 10; i++)
	{
		//Insert_head(&head, i);
		Insert_tail(&head, i);
	}

	DeleteVal(&head, 0);
	DeleteVal(&head, 1);
	DeleteVal(&head, 9);
	DeleteVal(&head, 20);
	Show(&head);

	Destroy(&head);
	Destroy(&head);
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值