2.3双向链表-作业

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

提示:这里可以添加本文要记录的大概内容:

例如:随着人工智能的不断发展,机器学习这门技术也越来越重要,很多人都开启了学习机器学习,本文就介绍了机器学习的基础内容。


提示:以下是本篇文章正文内容,下面案例可供参考

一、pandas是什么?

示例:pandas 是基于NumPy 的一种工具,该工具是为了解决数据分析任务而创建的。

二、使用步骤

1.代码

#include <stdio.h>
#include <malloc.h>

typedef struct DoubleLinkedNode {
	char data;//数据域
	struct DoubleLinkedNode* previous;//前向指针
	struct DoubleLinkedNode* next;//后向指针
}DLNode, * DLNodePtr;//一个结构体类型,一个指针

DLNodePtr initLinkList() {
	DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));//定义一个头指针,并为其开辟空间
	tempHeader->data = '\0';
	tempHeader->previous = NULL;
	tempHeader->next = NULL;
	return tempHeader;//返回头指针的地址
}

void printList(DLNodePtr paraHeader)
{
	DLNodePtr p = paraHeader->next;//p = H->next,方便直接输出第一个p->data.
	while (p != NULL) {
		printf("%c", p->data);
		p = p->next;//遍历输出表中的所有数据
	}
	printf("\r\n");
}

void insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition)
{
	DLNodePtr p, q, r;
	p = paraHeader;
	//用for循环来找paraPosition的位置
	for (int i = 0; i < paraPosition; i++) {
		p = p->next;
		if (p == NULL) {//如果paraPosition不合理,就退出循环.
			printf("The position %d is beyond  the scope of the list", paraPosition);
			return;
		}
	}
	//paraPosition合理,下一步给p开辟空间,存入data,开始插入步骤.
	q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
	q->data = paraChar;
	r = p->next;
	q->next = p->next;
	q->previous = p;
	p->next = q;//指前指后共四步骤,就写四行代码.
	if (r != NULL) {
		r->previous = q;//如果r不为空,r的前向要指向q.
	}
}

void deleteElement(DLNodePtr paraHeader, char paraChar)
{
	DLNodePtr p, q, r;
	p = paraHeader;
	//用while循环遍历寻找paraChar再做删除步骤,有可能遍历完所有元素,所以还得判断p->next!=NULL.
	while ((p->next != NULL) && (p->next->data != paraChar)) {
		p = p->next;
	}
	//退出循环有两种情况,这几行用来判断paraChar是不是有效数据
	if (p->next == NULL) {
		printf("The char %c is not exist of the list.\r\n", paraChar);
		return;
	}
	//上面说明paraChar是有效的,到这里就开始删除结点.
	q = p->next;
	r = q->next;
	p->next = r;//删除只需要把删除结点的前后两个结点连接起来,也就是只修改next和previous,共三行.
	if (r != NULL) {
		r->previous = p;//判断r是否为空,是就不管r的前向指针
	}

	free(q);
}

void insertdeleteTest()
{
	//test.
	DLNodePtr tempList = initLinkList();
	printList(tempList);

	insertElement(tempList, 'H', 0);
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'l', 2);
	insertElement(tempList, 'l', 3);
	insertElement(tempList, 'o', 4);
	insertElement(tempList, '!', 5);
	printList(tempList);

	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	insertElement(tempList, 'o', 1);
	printList(tempList);

}

void basicAddressTest()
{
	//printf address of the two node.
	DLNode tempNode1, tempNode2;
	tempNode1.data = 6;
	tempNode1.next = NULL;
	tempNode2.data = 4;
	tempNode2.next = NULL;

	printf("The first Node:%d, %d, %d\r\n",
		&tempNode1, &tempNode1.data, &tempNode1.next);
	printf("The second Node:%d, %d, %d\r\n",
		&tempNode2, &tempNode2.data, &tempNode2.next);

	tempNode1.next = &tempNode2;
}

void main()
{
	//invoking function.
	insertdeleteTest();
	basicAddressTest();
}

2.输出结果

Hello!
The char a is not exist of the list.
Hll!
Holl!
The first Node:-258345784, -258345784, -258345768
The second Node:-258345736, -258345736, -258345720
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值