再次温习——双循环链表

1.DuLinkList.h头文件

#pragma once

typedef int ElemType;

typedef struct DuL_node {
	ElemType data;
	struct DuL_node* prew;
	struct DuL_node* next;
}DuNode,* PDuNode;

typedef struct {
	DuNode* head;
	int cursize;
}DuLinkList;


void  InitList(DuLinkList* pList);

void PrintDuList(const DuLinkList* pList);

DuNode* FindValue(const DuLinkList* pList, ElemType val);

bool Insert_Pre(DuLinkList* pList, DuNode* ptr, ElemType val);

void Push_Front(DuLinkList* pList, ElemType val);

void  Push_Back(DuLinkList* pList, ElemType val);

void EraseNode(DuLinkList* pList, DuNode* ptr);

void Pop_Front(DuLinkList* pList);

void Pop_Back(DuLinkList* pList);

bool is_Empty(DuLinkList* pList);

void  DestoryDuList(DuLinkList* pList);

void ClearDuList(DuLinkList* pList);

void Remove(DuLinkList* pList,ElemType val);

void Remove_All(DuLinkList* pList, ElemType val);

2.DuLinkList.cpp源文件

#include"DuLinkList.h"
#include<assert.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
using namespace std;

//初始化链表
void  InitList(DuLinkList*pList) {
	assert(pList != NULL);
	//pList = (DuLinkList*)malloc(sizeof(DuLinkList));
	if (pList == NULL)  exit(1);
	pList->head = (DuNode*)malloc(sizeof(DuNode));
	if(NULL== pList ->head )  exit(1);
	pList->head->next = pList->head;
	pList->head->prew = pList->head;
	pList->cursize = 0;
}

void PrintDuList(const DuLinkList* pList) {
	assert(pList != NULL);
	DuNode* ptr = pList->head->next ;
	while (ptr!= pList->head) {
		cout << ptr->data << "	";
		ptr = ptr->next;
	}
	cout << endl;
}

DuNode* FindValue(const DuLinkList* pList, ElemType val) {
	assert(pList != NULL);
	DuNode* ptr = pList->head->next ,*ret=NULL;
	while (ptr != pList->head) {
		if (ptr->data == val) {
			ret = ptr;
			break;
		}
		ptr = ptr->next;
	}
	return ptr;
}

bool Insert_Pre(DuLinkList* pList, DuNode* ptr, ElemType val) {
	assert(pList != NULL && ptr != NULL);


	DuNode* s = (DuNode*)malloc(sizeof(DuNode));
	if (s == NULL)  return false;
	s->data = val;
	
	s->prew = ptr->prew;
	s->next = ptr;
	ptr->prew= s;
	s->prew->next = s;

	pList->cursize++;
	return true; 
}

bool Insert_Next(DuLinkList* pList, DuNode* ptr, ElemType val) {
	assert(pList != NULL && ptr != NULL);


	DuNode* s = (DuNode*)malloc(sizeof(DuNode));
	if (s == NULL)  return false;
	s->data = val;

	s->prew = ptr;
	s->next = ptr->next;
	s->next->prew = s;
	ptr->next = s;

	pList->cursize++;
	return true;
}

void  Push_Front(DuLinkList* pList, ElemType val) {
	assert(pList != NULL);

	Insert_Pre(pList, pList->head->next, val);
}

void  Push_Back(DuLinkList* pList, ElemType val) {
	assert(pList != NULL);
	Insert_Next(pList, pList->head->prew , val);
}

void EraseNode(DuLinkList* pList, DuNode* ptr) {
	assert(pList != NULL && ptr->next != NULL);
	ptr->next->prew = ptr->prew;
	ptr->prew->next = ptr->next;
	free(ptr);
	pList->cursize--;
}

void Pop_Front(DuLinkList* pList) {
	assert(pList != NULL);
	EraseNode(pList, pList->head->next);
}

void Pop_Back(DuLinkList* pList) {
	assert(pList != NULL);
	EraseNode(pList, pList->head->prew);

}

bool is_Empty(DuLinkList* pList) {
	assert(pList != NULL);
	if (pList->head->next == pList->head)   return true;
	else return false;
}

void  DestoryDuList(DuLinkList* pList) {
	assert(pList != NULL);
	ClearDuList(pList);
	free(pList->head);
}

void  ClearDuList(DuLinkList* pList) {
	assert(pList != NULL);
	while (!is_Empty(pList)) {
		Pop_Back(pList);
	}
}

void Remove(DuLinkList* pList, ElemType val) {
	assert(pList != NULL);
	DuNode* ptr = pList->head->next ;
	while (ptr != pList->head) {
		if (ptr->data == val) {
			EraseNode(pList, ptr);
			return;
		}
		ptr = ptr->next;
	}

}

void Remove_All(DuLinkList* pList, ElemType val) {
	assert(pList != NULL);
	DuNode* ptr = pList->head->next;
	DuNode* p = ptr;
	while (ptr != pList->head) {
		if (ptr->data == val)
		{
			p = ptr;
			ptr = ptr->next;
			EraseNode(pList, p);
		}
		else ptr = ptr->next;
	}

}

3.主程序

#include"DuLinkList.h"
#include<iostream>
using namespace std;
#include<Windows.h>
#include<stdlib.h>


int main() {
	DuLinkList head;
	InitList(&head);
	for (int i = 0; i < 5; i++) {
		Push_Back(&head, i+1);
		PrintDuList(&head);
	}

	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值