栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

链表的基本操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

链表的基本操作

哈哈哈,第一次写链表,用的是尾插法;写的链表也是双向循环链表的基本操作,不过熟练之后他和单链表都是一回事。

对了,本人写的是耿国华的《数据结构——用C语言描述》(第2版)里面的内容,希望能帮到有学到的朋友/

代码如下:

#define _CRT_SECURE_NO_WARNINGS 1
#include
#include
#include
#define TRUE 1
#define FALSE 0

typedef struct DNode {
	int data;
	struct DNode* prior, * next;
}DNode,* DoubleList;

//声明函数
DoubleList InitDlinkList(DoubleList* CL);//初始化循环双向链表
void CreateDlinklist(DoubleList CL);//建表
DoubleList merge(DoubleList LA, DoubleList LB);//合并
int DlinkIns(DoubleList L, int i, int e);//插入
int DlinkDel(DoubleList L, int o, int* t);//删除

	int main() {
	DoubleList LA, LB, LC;
	DNode* p;
	int i, o;
	int e,*t;
	int flag = 0;

	//初始化表
	InitDlinkList(&LA);
	InitDlinkList(&LB);

	//建立表LA
	printf("请输入LA表的值:n");
	LA = (DNode*)malloc(sizeof(struct DNode));
	CreateDlinklist(LA);
	p = LA->next;
	while (p != LA)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("n");

	//建表LB
	printf("请输入LB表的值:n");
	LB = (DNode*)malloc(sizeof(struct DNode));
	CreateDlinklist(LB);
	p = LB->next;
	while (p != LB)
	{
		printf("%d ", p->data);
		p = p->next;
	}
	printf("n");

	//合并表LA,LB到LC
	LC = merge(LA, LB);

	//输出表LC
	printf("输出表LC:n");
	p = LC->next;
	while (p != LC) {
		printf("%d", p->data);
		p = p->next;
	}
	printf("n");

	//向插入一个新结点e
	printf("请输入要增加结点的位置和元素值: ");
	scanf("%d %d", &i, &e);
	DlinkIns(LC, i, e);
	printf("输出插入后的LC表:n");
	p = LC->next;
	while (p != LC) {
		printf("%d", p->data);
		p = p->next;
	}
	printf("n");

	//向LC表删除一个结点t
	printf("请输入要删除的位置:");
	scanf("%d", &o);
	t = (int*)malloc(sizeof(int));
	flag = DlinkDel(LC, o, t);
	if (flag == 1)
		printf("删除第%d结点%c", o, t);
	else
		printf("删除位置不合法");
	printf("输出删除后的LC表:n");
	p = LC->next;
	while (p != LC) {
		printf("%d", p->data);
		p = p->next;
	}

	return 0;
}

//初始化
DoubleList InitDlinkList(DoubleList* CL) {
	(*CL) = (DNode*)malloc(sizeof(DNode));
	(*CL)->next = (*CL);
	(*CL)->prior = (*CL);
	return (*CL);
}

//建表
void CreateDlinklist(DoubleList CL) {
	DNode* ptrl, * s;
	int c;
	CL->next = CL;
	CL->prior = CL;
	ptrl = CL;
	scanf("%d", &c);
	while (c != -1) {
		s = (DNode*)malloc(sizeof(DNode));
		s->data = c;
		ptrl->prior->next == ptrl;
		ptrl == ptrl->next->prior;
		ptrl->next = s;
		s->prior = ptrl;
		ptrl = s;
		scanf("%d", &c);
	}
	ptrl->next = CL;
	CL->prior = ptrl;
}

//合并为表LC
DoubleList merge(DoubleList LA, DoubleList LB) {
	DNode* p, * q;
	p = LA;
	q = LB;
	while (p->next != LA)
		p = p->next;
	while (q->next != LB)
		q = q->next;
	p->next = LB;
	q->next = LA->next;
	LA->next->prior = q;
	LB->prior = p;
	free(LA);
	return (LB);
}

//向表LC中第i个位置前插入新节点e
int DlinkIns(DoubleList L, int i, int e) {
	DNode* s, * p;
	int k;
	if (i <= 0)
		return FALSE;
	p = L;
	k = 0;
	while(p->next!=L&&knext;
		k = k + 1;
	}
	if (p->next == L) {//表查完还没找到位置i
		printf("插入位置不合理;");
		return FALSE;
	}
	s = (DNode*)malloc(sizeof(DNode));
	if (s) {
		s->data = e;
		s->prior = p->prior;
		p->prior->next = s;
		s->next = p;
		p->prior = s;
		return TRUE;
	}
	else
		return FALSE;
}

//删除t
int DlinkDel(DoubleList L, int o, int* t) {
	DNode* p;
	int k;
	p = L;
	k = 0;
	while (p->next != L && k < o) {	//寻找被删除节点o的前驱o-1,并且使p指向它
		p = p->next;
		k = k + 1;
	}		//查找第o-1个结点
	if (p->next == L) {
		printf("删除位置不合法;");
		return FALSE;
	}
	*t = p->data;
	p->prior->next = p->next;
	p->next->prior = p->prior;
	free (p);
	return TRUE;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1027201.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号