【数据结构】 双循环链表代码演示

前端之家收集整理的这篇文章主要介绍了【数据结构】 双循环链表代码演示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include <stdio.h>
#include <stdlib.h>

typedef struct Data
{
	Data *prior;    // 前驱指针
	Data *next;     // 后继指针
	int elem;       // 数据元素
}DoubleLinked,*PDoubleLinked;

void DisDoubleLinked(PDoubleLinked L);
void InsertDoubleLinked(PDoubleLinked L,int index,int elem);
void DeleteDoubleLinked(PDoubleLinked L,int index);
void ModifyDoubleLinked(PDoubleLinked L,int elem);

void main()
{
	PDoubleLinked L,P,Q;
	int size = 0;

	/**先初始化在使用*/
	L = (PDoubleLinked)malloc(sizeof(DoubleLinked));

	// 两种策略 
	// 一  用到的时候再申请
	// 二  先申请 一大段 不够的时候再申请
	P = L;
	L->prior = NULL;

	printf("请输入双链表的大小:");
	scanf("%d",&size);

	while (size-- != 0)
	{
		P->next = (PDoubleLinked)malloc(sizeof(DoubleLinked));
		Q = P;
		P = P->next;
		P->prior = Q;
		P->next = NULL;
		P->elem = size*2+1;
	}
	P->next = L;
	L->prior = P;    // 循环表头

	// 显示双链表
	DisDoubleLinked(L);
	// 增
	InsertDoubleLinked(L,2,23);
	printf("\n");
	DisDoubleLinked(L);
	printf("\n");
	// 删
	DeleteDoubleLinked(L,3);
	DisDoubleLinked(L);
	printf("\n");
	// 改
	ModifyDoubleLinked(L,3,521);
	DisDoubleLinked(L);
	printf("\n");
}


void DisDoubleLinked(PDoubleLinked L)
{
	PDoubleLinked P = L->next;

	while (P != L)   // P != L 的时候循环进行
	{
		printf("%d\t",P->elem);
		P = P->next;
	}
}

void InsertDoubleLinked(PDoubleLinked L,int elem)
{
	PDoubleLinked P = L;
	PDoubleLinked T;
	PDoubleLinked Q = (PDoubleLinked)malloc(sizeof(DoubleLinked));
	Q->elem = elem;

	while (--index != 0)
	{
		P = P->next;
	}
	T = P->next;

	Q->prior = P;
	Q->next = T;

	P->next = Q;
	Q->next = T;
}

void DeleteDoubleLinked(PDoubleLinked L,int index)
{
	PDoubleLinked P = L;
	PDoubleLinked Q;

	while (--index != 0)
	{
		P = P->next;
	}

	Q = P->next;
	P->next = Q->next;
	Q->next->prior = P;
	free(Q);
}

void ModifyDoubleLinked(PDoubleLinked L,int elem)
{
	PDoubleLinked P = L;

	while (index-- != 0)
	{
		P = P->next;
	}

	P->elem = elem;
}

原文链接:https://www.f2er.com/datastructure/383110.html

猜你在找的数据结构相关文章