數據結構 雙向鏈表

---------------start reading---------------

前言

雙向鏈表是鏈表的一種,都是以鏈式結構存儲。與單鏈表不同的是,雙向鏈表可以向前找前驅,而單鏈表只能自己定義前驅而且不能倒退。雙向鏈表有與單鏈表不同的結構,但實現的功能基本是一樣的。

雙向鏈表的結構
在這裏插入圖片描述
雙向鏈表的操作一定要注意,要改前驅地址。特別注意尾結點,當需要改後繼的前驅的時候,尾結點不能用p->prio->next=p->next->prio,p->next爲NULL解引用會導致程序崩潰

雙向鏈表的具體操作
頭文件

#pragma once
//帶頭節點雙向鏈表,非循環

typedef struct DNode
{
   int data;
   struct DNode *next;//後繼指針
    struct DNode *prio;//前驅指針
}DNode,*DList;

//鏈表初始化
void InitList(DList plist);//Node

//頭插
bool Insert_head(DList plist,int val);

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

//查找
DNode *Search(DList plist,int key);

//刪除
bool Delete(DList plist,int key);

int GetLength(DList plist);

bool IsEmpty(DList plist);

void Clear(DList plist);

void Destroy(DList plist);

void Show(DList plist);

具體實現

#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->prio = NULL;
}

//頭插
bool Insert_head(DList plist,int val)
{
	DNode *p = (DNode *)malloc(sizeof(DNode));
	p->data = val;

	p->next = plist->next;//1
	plist->next = p;
	p->prio = plist;
	if(p->next != NULL)
	{
		p->next->prio = p;
	}

	return true;
}
//尾插
bool Insert_tail(DList plist,int val)
{
	DNode *q = (DNode *)malloc(sizeof(DNode));

	q->data = val;

	DNode *p;
	for(p=plist;p->next!=NULL;p=p->next) ;

	//將q插入在p的後面
	q->next = p->next;//q->next = NULL;
	p->next = q;
	q->prio = p;

	return true;
}

//查找
DNode *Search(DList plist,int key)
{
	for(DNode *p=plist->next;p!=NULL;p=p->next)
	{
		if(p->data == key)
		{
			return p;
		}
	}

	return NULL;
}

//刪除
bool Delete(DList plist,int key)
{
	DNode *p = Search(plist,key);
	if(p == NULL)
	{
		return false;
	}

	p->prio->next = p->next;
	if(p->next != NULL)
	{
		p->next->prio = p->prio;
	}

	free(p);
	return true;
}

int GetLength(DList plist)
{
	int count = 0;

	for(DNode *p=plist->next;p!=NULL;p=p->next)
	{
		count++;
	}

	return count;
}

bool IsEmpty(DList plist)
{
	return plist->next == NULL;
}

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

void Show(DList plist)
{
	for(DNode *p=plist->next;p!=NULL;p=p->next)
	{
		printf("%d ",p->data);
	}
	printf("\n");
}