双向循环列表

双向循环列表的实现。

根据定义实现。不解释,具体细节看代码。

list.h

cpp 复制代码
#pragma once




#pragma pack(1)

typedef struct _MyListEntry {
	_MyListEntry* next;
	_MyListEntry* prev;
}MyListEntry;

#pragma pack()


class MyListClass {
public:
	MyListEntry* m_list=0;

	int m_keyOffset;
	int m_keySize;

	MyListClass();

	~MyListClass();

	int InsertEnd(MyListEntry* list);

	int InsertHead(MyListEntry* list);

	MyListEntry* Search(int offset,char * data,int size);

	int Remove(MyListEntry* list);


};

list.cpp

cpp 复制代码
#include "list.h"
#include <Windows.h>







MyListClass::MyListClass() {
	m_list = new MyListEntry;
	m_list->next = 0;
	m_list->prev = 0;
	m_keyOffset = 0;
	m_keySize = 0;
}


MyListClass::~MyListClass() {

	MyListEntry* n = m_list->next;
	MyListEntry* b = n;

	do
	{
		if (n == 0) {
			break;
		}

		MyListEntry* t = n;
		n = n->next;
		delete t;
	} while (n != b);

	if (m_list) {
		delete m_list;
	}
}


MyListEntry* MyListClass::Search(int offset,char * data,int size) {
	if (data == 0 || size == 0) {
		return 0;
	}

	MyListEntry* n = m_list->next;
	MyListEntry* b = n;

	do
	{
		if (n == 0) {
			break;
		}
		char* obj = (char*)((char*)n + offset);
		if (memcmp(data, obj,size) == 0) {
			return n;
		}
		n = n->next;

	} while (n != b);

	return 0;
}





int MyListClass::InsertHead(MyListEntry* list) {
	if (list == 0) {
		return 0;
	}
	MyListEntry* n = m_list->next;
	MyListEntry* p = m_list->prev;

	if (n == 0 || p == 0) {
		m_list->next = list;
		m_list->prev = list;

		list->prev = list;
		list->next = list;
		return 0;
	}

	p->next = list;
	n->prev = list;

	list->prev = p;
	list->next = n;

	m_list->next = list;

	return TRUE;
}

int MyListClass::InsertEnd(MyListEntry* list) {
	if (list == 0) {
		return 0;
	}

	MyListEntry* n = m_list->next;
	MyListEntry* p = m_list->prev;

	if (n == 0 || p == 0) {
		m_list->next = list;
		m_list->prev = list;
		list->next = list;
		list->prev = list;
		return TRUE;
	}

	p->next = list;
	n->prev = list;

	list->prev = p;
	list->next = n;

	m_list->prev = list;

	return TRUE;
}


int MyListClass::Remove(MyListEntry* list) {
	if (list == 0 || list == m_list) {
		return 0;
	}

	if (list == m_list->next) {
		
		MyListEntry* p = m_list->prev;
		if (p == list) {
			m_list->next = 0;
			m_list->prev = 0;
		}
		else {
			MyListEntry* n = m_list->next->next;
			n->prev = p;
			p->next = n;
			m_list->next = n;
		}
		delete list;		
		return TRUE;
	}
	else if (list == m_list->prev) {
		MyListEntry* n = m_list->next;	
		if (list == n) {
			m_list->next = 0;
			m_list->prev = 0;
		}
		else {
			MyListEntry* p = m_list->prev->prev;		
			n->prev = p;
			p->next = n;
			m_list->prev = p;
		}
		delete list;
		return TRUE;
	}

	MyListEntry* n = m_list->next;
	MyListEntry* b = n;
	do
	{
		if (list == n) {
			MyListEntry* prev = n->prev;
			MyListEntry* next = n->next;
			prev->next = next;
			next->prev = prev;
			delete list;
			return TRUE;
		}
		n = n->next;
	} while (n != b);

	return FALSE;
}
相关推荐
xxwxx__3 小时前
C++ STL set 与 map 全套详解:关联式容器、红黑树底层、代码坑点、刷题实战
数据结构·c++
程曦曦3 小时前
MySQL 生产库误删 98 张表后的时间点恢复实战:从 binlog 解析到资金对账
linux·数据结构·其他·算法·ubuntu·运维开发
郝学胜-神的一滴4 小时前
Effective Python 条款 13:善用星号解包,告别下标切片拆分的坑
服务器·开发语言·数据结构·python·程序人生·pycharm
Logic1014 小时前
C语言/数据结构位运算题解:异或XOR找出流水线上的“独特零件编号“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
张小姐的猫4 小时前
【AI大模型接入SDK】 —— 数据管理 & 与Session模块进行联动
数据结构·数据库·c++·人工智能·python·chatgpt
careathers5 小时前
【数据结构】栈
java·数据结构
mmmmath_35 小时前
LeetCode.018.四数之和
数据结构·算法·leetcode
淡海水6 小时前
11-02-Unity-Mono-vs-IL2CPP-两种脚本后端的数据结构行为差异
数据结构·unity·游戏引擎·il2cpp·mono
梦帮科技7 小时前
Next.js 16 模块化单体实战:App Router、领域模块与 Route Handler 分层
css·数据结构·链表·正则表达式·node.js·json·html5
雨落在了我的手上7 小时前
Java数据结构(十三):排序
数据结构