C++实现双向链表

本文深入探讨了如何在C++中实现双向链表,从节点定义到插入、删除操作,详细阐述了双向链表的数据结构及其优势。通过实例代码,帮助读者理解其工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/*
 * DLList.h
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#ifndef DLLIST_H_
#define DLLIST_H_

typedef int ElemType;

typedef struct DLNode{
	ElemType data;
	DLNode* prior;
	DLNode* next;
}*DLList;

bool DLListCreate(DLList& DLL);
void DLListDestroy(DLList& DLL);

void DLListClear(DLList DLL);
int  DLListLength(DLList DLL);
bool DLListEmpty(DLList DLL);

bool DLListInsert(DLList DLL, int pos, ElemType e);
bool DLListDelete(DLList DLL, int pos, ElemType& e);
bool DLListAppend(DLList DLL, ElemType e);

int  DLListFindElem(DLList DLL, ElemType e);
bool DLListGetElem(DLList DLL, int pos, ElemType& e);

void DLListDisplay(DLList DLL);

void DLListSort(DLList DLL, bool desc);

#endif /* DLLIST_H_ */



/*
 * DLList.cpp
 *
 *  Created on: Oct 6, 2015
 *      Author: chris
 */

#include"DLList.h"
#include<iostream>
#include<cstdlib>

using namespace std;

bool DLListCreate(DLList& DLL)
{
	DLL = new DLNode;
	if(!DLL) return false;
	DLL->prior = DLL->next = NULL;
	return true;
}

void DLListDestroy(DLList& DLL)
{
	DLNode * pcur;
	while(DLL) {
		pcur = DLL;
		DLL = DLL->next;
		delete pcur;
	}
}

void DLListClear(DLList DLL)
{
	DLList sub = DLL->next;
	DLL->next = NULL;
	DLNode *pcur;
	while(sub) {
		pcur = sub;
		sub = sub->next;
		delete pcur;
	}
}

int DLListLength(DLList DLL)
{
	DLNode * pcur = DLL->next;
	int cnt = 0;
	while(pcur) {
		++cnt;
		pcur = pcur->next;
	}
	return cnt;
}

bool DLListEmpty(DLList DLL)
{
	return DLL->next == NULL;
}

bool DLListInsert(DLList DLL, int pos, E
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值