【数据结构】双向循环链表的创建、初始化、插入、删除


一、部分实现

1.双向循环链式存储结构

在这里插入图片描述
代码如下:

/*链式存储结构*/
typedef struct DuLNode
{
	ElemType data;
	struct DuLNode* prior;
	struct DuLNode* next;
}DuLNode,*DuLinkList;

2.初始化双向循环链表

在这里插入图片描述
在这里插入图片描述


代码如下:
/*初始化双向循环链表*/
ElemType InitDuList(DuLinkList&L)
{
	L = new DuLNode;  //头指针指向头结点
	if (L == NULL)
	{
		return ERROR;
	}
	
	L->next = L;     //后指针域指向自己
	L->prior = L;    //前指针域指向自己

	return OK;
}

3.使用尾插法插入结点

代码如下:

/*使用尾插法插入结点*/
ElemType InitInsertDuList(DuLinkList&L,int number)
{
	DuLinkList p = L;
	while (number--)
	{
		DuLinkList q = new DuLNode;   //创建插入的结点,头指针P指向该结点
		if (p==NULL)
		{
			return ERROR;
		}
		cin >> q->data;              //输入插入结点的元素值

		/*尾插法*/
		p->next = q;
		q->prior = p;
		q->next = L;
		L->prior = q;
		p = q;
	}
}

4.插入第i个结点(在第i个结点之前插入)

在这里插入图片描述

代码如下:

/*插入第i结点(在第i个结点之前插入)后面插入的操作*/
ElemType InsertDuList(DuLinkList&L, int i, ElemType elem)
{
	DuLinkList pre = L;
	int j = 0;

	while (j<i)
	{
		pre = pre->next;   //移动到了第i个结点的位置,pre指向第i个结点
		j++;
		if ((pre == L)) return ERROR;
	}

	DuLinkList s = new DuLNode;
	s->data = elem;

	s->next = pre;          //插入操作
	s->prior = pre->prior;
	pre->prior->next = s;
	pre->prior = s;
}

5.删除第i个结点

在这里插入图片描述
代码如下:

/*删除第i个结点操作,并将删除的结点元素赋值给e*/
ElemType DeleDuList(DuLinkList&L, int i, ElemType&e)
{
	DuLinkList pre = L;
	int j = 0;
	while (j < i)
	{
		pre = pre->next;
		j++;
		if ((pre==L)) return ERROR;
	}
	e = pre->data;
	pre->prior->next = pre->next;  //删除操作
	pre->next->prior = pre->prior;
	delete pre;
	return OK;
}

6.遍历双向循环链表

代码如下:

void PrintDuList(DuLinkList L)
{
	DuLinkList p = L->next;
	while (p!=L)
	{
		cout << p->data << endl;
		p = p->next;
	}
	cout << endl;
}

7.主函数

代码如下:

int main()
{
	DuLinkList DuList;
	ElemType elem;

	/*初始化操作*/
	if (!(InitDuList(DuList))) cout << "初始化失败" << endl;
	
	/*初始化插入5个结点操作*/
	InitInsertDuList(DuList, 5);
	cout << "*****************" << endl;;

	/*删除操作*/
	if(DeleDuList(DuList, 3,elem)==ERROR) cout << "删除操作失败" << endl;
	/*遍历输出操作*/
	PrintDuList(DuList);

	/*插入操作*/
	if (InsertDuList(DuList, 3, 3) == ERROR) cout << "插入操作失败" << endl;
	/*遍历输出操作*/
	PrintDuList(DuList);

	return 0;
}

二、全部代码

代码如下:

#include<iostream>
#include<stdio.h>
using namespace std;

typedef int ElemType;

#define ERROR -1
#define OK    1

/*链式存储结构*/
typedef struct DuLNode
{
	ElemType data;
	struct DuLNode* prior;
	struct DuLNode* next;
}DuLNode,*DuLinkList;

/*初始化双向循环链表*/
ElemType InitDuList(DuLinkList&L)
{
	L = new DuLNode;  //头指针指向头结点
	if (L == NULL)
	{
		return ERROR;
	}
	
	L->next = L;     //后指针域指向自己
	L->prior = L;    //前指针域指向自己

	return OK;
}

/*使用尾插法插入结点*/
ElemType InitInsertDuList(DuLinkList&L,int number)
{
	DuLinkList p = L;
	while (number--)
	{
		DuLinkList q = new DuLNode;   //创建插入的结点,头指针P指向该结点
		if (p==NULL)
		{
			return ERROR;
		}
		cin >> q->data;              //输入插入结点的元素值

		/*尾插法*/
		p->next = q;
		q->prior = p;
		q->next = L;
		L->prior = q;
		p = q;
	}
}

/*插入第i结点(在第i个结点之前插入)后面插入的操作*/
ElemType InsertDuList(DuLinkList&L, int i, ElemType elem)
{
	DuLinkList pre = L;
	int j = 0;

	while (j<i)
	{
		pre = pre->next;   //移动到了第i个结点的位置,pre指向第i个结点
		j++;
		if ((pre == L)) return ERROR;
	}

	DuLinkList s = new DuLNode;
	s->data = elem;

	s->next = pre;          //插入操作
	s->prior = pre->prior;
	pre->prior->next = s;
	pre->prior = s;
}



/*删除第i个结点操作,并将删除的结点元素赋值给e*/
ElemType DeleDuList(DuLinkList&L, int i, ElemType&e)
{
	DuLinkList pre = L;
	int j = 0;
	while (j < i)
	{
		pre = pre->next;
		j++;

		if ((pre==L)) return ERROR;
	}

	e = pre->data;
	pre->prior->next = pre->next;    //删除操作
	pre->next->prior = pre->prior;
	delete pre;
	return OK;
}

/*遍历双向循环链表*/
void PrintDuList(DuLinkList L)
{
	DuLinkList p = L->next;
	while (p!=L)
	{
		cout << p->data << endl;
		p = p->next;
	}
	cout << endl;
}


int main()
{
	DuLinkList DuList;
	ElemType elem;

	/*初始化操作*/
	if (!(InitDuList(DuList))) cout << "初始化失败" << endl;
	
	/*初始化插入5个结点操作*/
	InitInsertDuList(DuList, 5);
	cout << "*****************" << endl;;

	/*删除操作*/
	if(DeleDuList(DuList, 3,elem)==ERROR) cout << "删除操作失败" << endl;
	/*遍历输出操作*/
	PrintDuList(DuList);

	/*插入操作*/
	if (InsertDuList(DuList, 3, 3) == ERROR) cout << "插入操作失败" << endl;
	/*遍历输出操作*/
	PrintDuList(DuList);

	return 0;
}

三、运行结果

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值