双向循环链表的前插 后插 前删 后删

心累 好复杂 我真的学得会么😭😭😭
我能坚持几天 嘤嘤嘤😭😭😭
lay了
双向链表插来删去真的好可怕。。我哭了 我没装😭😭😭

//双向循环链表
struct DNode
{
	int data;
	DNode *next;
	DNode *prev;
};
struct DCircleList
{
	DNode* Head;
};
void InitDCircleList(DCircleList *L)
{
	//DNode *n;
	DNode *n = new DNode();
	n->next = n->prev = n;
	L->Head = n;
}
int GetDlistLength(DCircleList L)
{
	DNode* CurrNode;
	CurrNode = L.Head;
	int count = 1;
	while (CurrNode->next != L.Head)
	{
		CurrNode = CurrNode->next;
		count++;
	}
	return count;
}
void PrintDlist(DCircleList L)
{
	DNode* CurrNode;
	CurrNode = L.Head;
	int count = 0;
	while (count < GetDlistLength(L))
	{
		cout << CurrNode->data << " ";
		CurrNode = CurrNode->next; count++;
	}
	cout << endl;

}
void InsertDList_front(DCircleList *L,int i,int elem)
{
	int count = 0;
	DNode *n = new DNode();
	n->data = elem;
	n->next = n->prev = n;
	//异常抛出
	if (i<0||i>GetDlistLength(*L))
	{
		return;
	}
	if (i==0)//判定为头节点
	{
		L->Head=n  ;
		return;
	}
	DNode *CurrNode = L->Head;
	while (count<i-1)
	{
		CurrNode = CurrNode->next;
		count++;
	}
	n->next = CurrNode->next;
	CurrNode->next->prev=n ;
	n->prev = CurrNode;
	CurrNode->next = n;
}
void InsertDList_back(DCircleList *L, int i, int elem)
{
	int count = 0;
	DNode *n = new DNode();
	n->data = elem;
	n->next = n->prev = n;
	DNode *CurrNode = L->Head->prev;
	if (i==0)//判定其为头节点
	{
		L->Head=n ;
		return;
	}
	while (count<i-1)
	{
		CurrNode = CurrNode->prev;
		count++;
	}
	n->next = CurrNode;
	n->prev = CurrNode->prev;
	CurrNode->prev->next = n;
    CurrNode->prev=n;


}
void DeleteDList_front(DCircleList *L, int i)
{
	int count = 0;
	DNode *n = new DNode();
	DNode *CurrNode = L->Head;
	if (i<0||i>GetDlistLength(*L))
	{
		return;
	}
	while (count<i-1)
	{
		CurrNode = CurrNode->next;
		count++;
	}
	n = CurrNode->next;
	CurrNode->next = CurrNode->next->next;
	CurrNode = CurrNode->next->prev;
	delete n;
}
void DeleteDList_back(DCircleList *L, int i)
{
	int count = 0;
	DNode *n = new DNode();
	DNode *CurrNode = L->Head->prev;
	if (i<0 || i>GetDlistLength(*L))
	{
		return;
	}
	while (count < i - 1)
	{
		CurrNode = CurrNode->next;
		count++;
	}
	n = CurrNode->prev;
	//先把前驱的前驱给到前驱
	//再把前驱的后继给到自己
	CurrNode->prev = CurrNode->prev->prev;
    CurrNode->prev->next= CurrNode;
	delete n;


}
void PrintDlist(DCircleList L,int startPosition,int type)
//重载打印链表的版本 就能想怎么打印 就怎么打印了 
//唉 做循环双向链表好难😑
{
	int count = 0;
	int move = 0;
	if (startPosition<0|| startPosition>GetDlistLength(L))
	{
		return;
	}

	switch (type)
	{
	case 0://往后稍稍
	{
		DNode* CurrNode = L.Head;
		while (move < startPosition)
		{
			CurrNode = CurrNode->next;
			move++;

		}
		while (count < GetDlistLength(L))
		{
			cout << CurrNode->data << " ";
			CurrNode = CurrNode->next;
			count++;
		}
		cout << endl;
		break;
	}
	case 1://往前稍稍
	{
		DNode* CurrNode = L.Head;
		while (move < startPosition)
		{
			CurrNode = CurrNode->prev;
			move++;

		}
		while (count < GetDlistLength(L))
		{
			cout << CurrNode->data << " ";
			CurrNode = CurrNode->next;
			count++;
		}
		cout << endl;
		break;
	}

	default:
		break;
	}
}
void test08()
{

	DCircleList(L);
	InitDCircleList(&L);
	for (int i = 0; i < 10; i++)
	{
		InsertDList_back(&L, i, i+1);
	}

	PrintDlist(L);
	DeleteDList_front(&L,1);
	PrintDlist(L);
	DeleteDList_back(&L, 1);
	PrintDlist(L);
	PrintDlist(L, 2, 0);
	PrintDlist(L, 2, 1);


}

int main()
{
	cout << "正在测试我的代码。。" << endl;
	srand((unsigned int)time(NULL));
	test08();
	system("pause");
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值