数据结构(C语言)双向链表

单链表之后的链表都是基于单链表的基础上修改的,所以必须要掌握单链表

在这里插入图片描述

该双向链表由dlist.h + dlist.cpp + main.cpp 组成(首尾不相连)

重点:头插 尾插 按值删除(多级指针,要判断)

dlist.h文件

#pragma once  //预防头文件重定义
//.h放定义和声明
//带头结点的双向链表:不循环,尾节点的后继为空,头结点的前驱为空
typedef struct DNode
{
	int data;//数据域
	struct DNode* next;//后继指针
	struct DNode* prio;//前驱指针
}DNode,*DList;

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

//头插,将新数据插入在第一个位置
bool Insert_head(DList plist, int val);

//尾插
bool Insert_tail(DList plist, int val);

//插入数据,在plist链表的pos位置插入val;
bool Insert(DList plist, int pos, int val);

//判空
bool IsEmpty(DList plist);

//获取节点个数
int GetLength(DList plist);

//在plist中查找第一个key值,找到返回地址,没有找到返回NULL;
DNode* Search(DList plist, int key);

//删除pos位置的值
bool DelPos(DList plist, int pos);

//删除第一个val的值
bool DelVal(DList plist, int val);

//返回key的前驱地址,如果不存在返回NULL;
DNode* GetPrio(DList plist, int key);

//返回key的后继地址,如果不存在返回NULL;
DNode* GetNext(DList plist, int key);


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

//销毁整个内存
void Destroy(DList plist);

//输出
void Show(const DList plist);

dlist.cpp文件

#include"dlist.h"//双向链表
#include<assert.h>
#include<malloc.h>
#include<stdio.h>

//初始化
void InitList(DList plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return;
	}
	plist->next = NULL;
	plist->prio = NULL;
}

//头插,将新数据插入在第一个位置
bool Insert_head(DList plist, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}

	DNode* p = (DNode*)malloc(sizeof(DNode));
	assert(p != NULL);
	p->data = val;

	p->next = plist->next;//要写在第一步
	plist->next = p;
	p->prio = plist;
	if (p->next != NULL)
	{
		p->next->prio = p;
	}

	return true;
}

//尾插
bool Insert_tail(DList plist, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}

	DNode* p = (DNode*)malloc(sizeof(DNode));
	assert(p != NULL);

	for (p = plist; p->next != NULL; p = p->next)
	{
		;
	}
	DNode* q = (DNode*)malloc(sizeof(DNode));
	assert(q != NULL);
	q->data = val;

	q->next = p->next;//写在第一句
	p->next = q;
	q->prio = p;

	return true;
}

//插入数据,在plist链表的pos位置插入val;
bool Insert(DList plist, int pos, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}
	if (pos<0 || pos>GetLength(plist))
	{
		return false;
	}
	DNode* p = (DNode*)malloc(sizeof(DNode));
	assert(p != NULL);
	p->data = val;

	DNode* q;
	int i;
	for (i = 0, q = plist; i < pos; i++, q = q->next)
	{
		;
	}
	//插入数据
	//将p插入到q的后面
	p->next = q->next;
	q->next = p;
	p->prio = q;
	if (p->next != NULL)//判断很重要
	{
		p->next->prio = p;
	}
	return true;
}

//判空
bool IsEmpty(DList plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}
	return plist->next == NULL;
}

//获取节点个数
int GetLength(DList plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return -1;
	}
	int count = 0;
	for (DNode* p = plist->next; p != NULL; p = p->next)
	{
		count++;
	}
	return count;
}

//在plist中查找第一个key值,找到返回地址,没有找到返回NULL;
DNode* Search(DList plist, int key)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return NULL;
	}
	DNode* p = (DNode*)malloc(sizeof(DNode));
	assert(p != NULL);
	for (p = plist->next; p != NULL; p = p->next)
	{
		if (p->data == key)
			return p;
	}
	return NULL;
}

//删除pos位置的值
bool DelPos(DList plist, int pos)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return false;
	}
	if (pos < 0 || pos >= GetLength(plist))
		return false;

	//找位置
	DNode* p=GetPrio(plist, pos);
	int i;
	for (p = plist, i = 0; i < pos; i++, p = p->next)
	{
		;
	}
	//删除
	DNode* q = p->next;
	p->next = q->next;
	if (p->next != NULL)
	{
		p->next->prio = p;
	}
	free(q);

	return true;
}

//删除第一个val的值
bool DelVal(DList plist, int val)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return NULL;
	}
	DNode* p = Search(plist, val);
	if (p == NULL)
		return false;

	//将p从双向链表中删除	
	p->prio->next = p->next;
	if (p->next != NULL)
	{
		p->next->prio = p->prio;
	}
	free(p);

	return true;

}

//返回key的前驱地址,如果不存在返回NULL;
DNode* GetPrio(DList plist, int key)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return NULL;
	}

	DNode* p = Search(plist, key);
	return p == NULL ? NULL : p->prio;
}

//返回key的后继地址,如果不存在返回NULL;
DNode* GetNext(DList plist, int key)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return NULL;
	}

	DNode* p = Search(plist, key);
	return p == NULL ? NULL : p->next;
}


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

//销毁整个内存
void Destroy(DList plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return;
	}
	//总是删除第一个节点
	DNode* p;
	while (plist->next != NULL)
	{
		p = plist->next;
		plist->next = p->next;
		free(p);
	}

}

//输出
void Show(const DList plist)
{
	assert(plist != NULL);
	if (plist == NULL)
	{
		return;
	}
	for (DNode* p = plist->next; p != NULL; p = p->next)
	{
		printf("%d  ", p->data);
	}
}

main文件,测试

#include<stdio.h>
#include"dlist.h"
int main()
{
	DNode head;
	InitList(&head);
	for (int i = 0; i < 10; i++)
	{
		Insert_tail(&head,i);
	}
	Show(&head);
	printf("len=%d\n", GetLength(&head));
	Insert(&head, 10,10);
	Show(&head);
	if (Search(&head, 11) == NULL)
		printf("没找到\n");
	else
		printf("找到了\n");
	DelVal(&head, 10);
	Show(&head);
	printf("\n");
	DelPos(&head, 5);
	Show(&head);


	Destroy(&head);
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值