数据结构学习之 带有头节点的双向循环链表

本文深入探讨了双向循环链表的基本操作实现,包括初始化、插入、查找、删除、清空等关键函数的代码解析,适合于理解链表数据结构及其在实际编程中的应用。
#pragma once
#include <stdbool.h>
//带头节点的双向循环链表
//链表尾节点的next为NULL,头节点的prio为NULL
//

typedef struct DNod
{
    int data; //数据
    struct DNode *next;  //下一个节点的地址
    struct DNode *prio;  //前趋节点的地址
}DNode,*DList;   //List = Node *

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

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

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

//在pos下标插入数据val
bool Insert_pos(DList plist, int pos, int val);

//查找,找到返回节点地址,没有找到返回NULL
DNode *Search(DList plist, int key);


//删除第一个key对应的节点
bool Delete(DList plist, int key);

//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist, int *rtval);

//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist, int *rtval);

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

//判空
bool IsEmpty(DList plist);

//清除所有数据
void Clear(DList plist);

//销毁所有节点
void Destory(DList plist);

//打印
void Show(DList plist);

 

#include <stdio.h>
#include <stdlib.h>
#include "dlist.h"
#include <assert.h>
//带头节点的双向循环链表
//链表尾节点的next为NULL,头节点的prio为NULL
//
/*
typedef struct DNod
{
    int data; //数据
    struct DNode *next;  //下一个节点的地址
    struct Dnode *prio;  //前趋节点的地址
}DNode,*DList;   //List = Node *
*/

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

static DNode *BuyNode(int val)
{
    DNode *p = (DNode *)malloc(sizeof(DNode));
    p->data = val;

    return p;
}

//头插法
bool Insert_head(DList plist, int val)
{
    DNode *p = BuyNode(val);

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

    return true;
}

//尾插法
bool Insert_tail(DList plist, int val)
{
    DNode *p = BuyNode(val);
    DNode *q;
    for(q = plist; q->next != NULL; q = q->next)
    {
	//将p插入在q的后面
	p->next = NULL;
	p->prio = q;
	q->next = p;
    }

    return true;
}

//在pos下标插入数据val
bool Insert_pos(DList plist, int pos, int val)
{
    
}

//查找,找到返回节点地址,没有找到返回NULL
Node *Search(DList plist, int key)
{
    for(DNode *p = plist->next; p != NULL; p = p->next)
    {
	if(p->data == key)
	{
	    return p;
	}
    }

    return NULL;
}

//删除第一个key对应的节点
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;
}

//删除第一个数据节点,并通过rtval获得删除的值
bool Delete_head(DList plist, int *rtval);

//删除最后一个数据节点,并通过rtval获得删除的值
bool Delete_tail(DList plist, int *rtval);

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

    return count;
}

//判空
bool IsEmpty(DList plist);

//清除所有数据
void Clear(DList plist);

//销毁所有节点
void Destory(DList plist);

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

    printf("\n");
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值