单链表与双链表 模拟

单向无头不循环链表 模拟

#include 
#include 
using namespace std;

/*
我采用C/C++混合编写,但对于指针部分,仍用纯C,不使用C++当中的引用
*/

typedef int SLTDataType;
typedef struct SLTNode
{
	SLTDataType data;
	struct SLTNode* next;
}SLTNode;

//新增一个节点
SLTNode* BuySLTNode(SLTDataType x)
{
	SLTNode* newnode = new SLTNode;
	newnode->data = x;
	newnode->next = nullptr;
	return newnode;
}

//尾插
void SLTPushBack(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = BuySLTNode(x);
	if (*pphead == nullptr)
	{
		*pphead = newnode;
	}
	else
	{
		SLTNode* tail = *pphead;
		while (tail->next != nullptr)
		{
			tail = tail->next;
		}
		tail->next = newnode;
	}
}

//头插
void SLTPushFront(SLTNode** pphead, SLTDataType x)
{
	assert(pphead);
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = *pphead;
	*pphead = newnode;
}

//尾删
void SLTPopBack(SLTNode** pphead)
{
	assert(pphead);
	assert(*pphead);
	if ((*pphead)->next == nullptr)
	{
		delete (*pphead);
		*pphead = nullptr;
	}
	else
	{
		SLTNode* prev = *pphead;
		SLTNode* tail = *pphead;
		while (tail->next != nullptr)
		{
			prev = tail;
			tail = tail->next;
		}
		prev->next = nullptr;
		delete tail;
	}
}

//头删
void SLTPopFront(SLTNode** pphead)
{
	assert(pphead);
	assert(*pphead);
	SLTNode* second = (*pphead)->next;
	delete (*pphead);
	*pphead = second;
}

//查找
SLTNode* SLTFind(SLTNode* phead, SLTDataType x)
{
	SLTNode* cur = phead;
	while (cur != nullptr)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return nullptr;
}

//任意插入(pos位置前)
void SLTInsert(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pos);
	assert(pphead);
	assert(*pphead);
	SLTNode* newnode = BuySLTNode(x);
	if (*pphead == pos)
	{
		newnode->next = *pphead;
		*pphead = newnode;
	}
	else
	{
		SLTNode* cur = *pphead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = newnode;
		newnode->next = pos;
	}
}

//任意插入(pos位置后)
void SLTInsertAfter(SLTNode** pphead, SLTNode* pos, SLTDataType x)
{
	assert(pos);
	assert(pphead);
	assert(*pphead);
	SLTNode* newnode = BuySLTNode(x);
	newnode->next = pos->next;
	pos->next = newnode;
}

//任意删除(pos位置前)
void SLTErase(SLTNode** pphead, SLTNode* pos)
{
	assert(pos);
	assert(pphead);
	assert(*pphead);
	if (pos == (*pphead))
	{
		SLTNode* second = (*pphead)->next;
		delete (*pphead);
		*pphead = second;
	}
	else
	{
		SLTNode* cur = *pphead;
		while (cur->next != pos)
		{
			cur = cur->next;
		}
		cur->next = pos->next;
		delete pos;
	}
}

//任意删除(pos位置后)
void SLTEraseAfter(SLTNode** pphead, SLTNode* pos)
{
	assert(pos);
	assert(pos->next);
	assert(pphead);
	assert(*pphead);
	SLTNode* del = pos->next;
	pos->next = pos->next->next;
	delete del;
}

//打印
void SLTPrint(SLTNode* phead)
{
	SLTNode* cur = phead;
	while (cur != nullptr)
	{
		cout << cur->data << "->";
		cur = cur->next;
	}
	cout << "nullptr" << endl;
}

//销毁
void SLTDestroy(SLTNode** pphead)
{
	SLTNode* prev = *pphead;
	SLTNode* cur = *pphead;
	while (cur!=nullptr)
	{
		prev = cur;
		cur = cur->next;
		delete prev;
	}
	*pphead = nullptr;
}

双向循环带头链表 模拟

#include 
#include 
using namespace std;

/*
我采用C/C++混合编写
*/

typedef int LTDataType;
typedef struct LTNode
{
	LTDataType data;
	struct LTNode* prev;
	struct LTNode* next;
}LTNode;

//新增一个节点
LTNode* BuyLTNode(LTDataType x)
{
	LTNode* newnode = new LTNode;
	newnode->data = x;
	newnode->prev = nullptr;
	newnode->next = nullptr;
	return newnode;
}

//初始化头节点
LTNode* LTInit()
{
	LTNode* phead = BuyLTNode(-1);
	phead->prev = phead;
	phead->next = phead;
	return phead;
}

//尾插
void LTPushBack(LTNode* phead, LTDataType x)
{
	assert(phead);
	LTNode* newnode = BuyLTNode(x);
	LTNode* tail = phead->prev;
	tail->next = newnode;
	newnode->prev = tail;
	newnode->next = phead;
	phead->prev = newnode;
}

//头插
void LTPushFront(LTNode* phead, LTDataType x)
{
	assert(phead);
	LTNode* newnode = BuyLTNode(x);
	LTNode* first = phead->next;
	newnode->next = first;
	first->prev = newnode;
	phead->next = newnode;
	newnode->prev = phead;
}//尾删
void LTPopBack(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	LTNode* tail = phead->prev;
	tail->prev->next = phead;
	phead->prev = tail->prev;
	delete tail;
}

//头删
void LTPopFront(LTNode* phead)
{
	assert(phead);
	assert(phead->next != phead);
	LTNode* first = phead->next;
	first->next->prev = phead;
	phead->next = first->next;
	delete first;
}

//任意插入
void LTInsert(LTNode* pos, LTDataType x)
{
	assert(pos);
	LTNode* newnode = BuyLTNode(x);
	pos->prev->next = newnode;
	newnode->prev = pos->prev;
	newnode->next = pos;
	pos->prev = newnode;
}

//任意删除
void LTErase(LTNode* pos)
{
	assert(pos);
	pos->prev->next = pos->next;
	pos->next->prev = pos->prev;
	delete pos;
}

//查找
LTNode* LTFind(LTNode* phead, LTDataType x)
{
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		if (cur->data == x)
		{
			return cur;
		}
		cur = cur->next;
	}
	return nullptr;
}

//销毁
void LTDestroy(LTNode* phead)
{
	assert(phead);
	LTNode* cur = phead->next;
	while (cur != phead)
	{
		LTNode* tmp = cur->next;
		delete cur;
		cur = tmp;
	}
	delete phead;
}

//打印​
void LTPrint(LTNode* phead)
{
	assert(phead);
	LTNode* cur = phead->next;
	cout << "<=head=>";
	while (cur != phead)
	{
		cout << "%d=>" << cur->data;
		cur = cur->next;
	}
	cout << endl;
}

你可能感兴趣的:(DS,数据结构,c++)