纯手撸の双链表,希望对你有所帮助

前言:就编者而言,双联表无非就是在单链表的基础上多了一个指向前驱的指针域罢了,在处理上基本和单链表类似,但其中又有一些新的东西,例如已知一个中间的节点,就可以知道它的前一个节点,这意味着双链表既可以正向遍历也可以倒过来。接下来我们就来看看代码吧! 

目录

一、老师の代码(未加以改动)

1.1

1.2定义双链表

 1.3初始化双链表

1.4打印链表

1.5插入节点の函数

1.6删除一个节点

1.7测试函数以及地址の打印

1.8运行结果

二、作者小哥哥の代码

2.1定义、初始化和打印链表

2.2 插入一个节点の函数

2.3查找一个节点の函数

2.4删除一个节点の函数

2.5测试和打印地址

2.6完整代码呈现及运行结果


前言:就编者而言,双联表无非就是在单链表的基础上多了一个指向前驱的指针域罢了,在处理上基本和单链表类似,但其中又有一些新的东西,例如已知一个中间的节点,就可以知道它的前一个节点,这意味着双链表既可以正向遍历也可以倒过来。接下来我们就来看看代码吧!

一、老师の代码(未加以改动)

1.1

老样子,我们先来看看老师写の代码:

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

/**
 * Double linked list of integers. The key is char.
 */
typedef struct DoubleLinkedNode{
	char data;
	struct DoubleLinkedNode *previous;
	struct DoubleLinkedNode *next;
} DLNode, *DLNodePtr;

/**
 * Initialize the list with a header.
 * @return The pointer to the header.
 */
DLNodePtr initLinkList(){
	DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
	tempHeader->data = '\0';
	tempHeader->previous = NULL;
	tempHeader->next = NULL;
	return tempHeader;
}// Of initLinkList

/**
 * Print the list.
 * @param paraHeader The header of the list.
 */
void printList(DLNodePtr paraHeader){
	DLNodePtr p = paraHeader->next;
	while (p != NULL) {
		printf("%c", p->data);
		p = p->next;
	}// Of while
	printf("\r\n");
}// Of printList

/**
 * Insert an element to the given position.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 * @param paraPosition The given position.
 */
void insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition){
	DLNodePtr p, q, r;

	// Step 1. Search to the position.
	p = paraHeader;
	for (int i = 0; i < paraPosition; i ++) {
		p = p->next;
		if (p == NULL) {
			printf("The position %d is beyond the scope of the list.", paraPosition);
			return;
		}// Of if
	} // Of for i

	// Step 2. Construct a new node.
	q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
	q->data = paraChar;

	// Step 3. Now link.
	r = p->next;
	q->next = p->next;
	q->previous = p;
	p->next = q;
	if (r != NULL) {
		r->previous = q;
	}// Of if
}// Of insertElement

/**
 * Delete an element from the list.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 */
void deleteElement(DLNodePtr paraHeader, char paraChar){
	DLNodePtr p, q, r;
	p = paraHeader;

	// Step 1. Locate.
	while ((p->next != NULL) && (p->next->data != paraChar)){
		p = p->next;
	}// Of while

	// Step 2. Error check.
	if (p->next == NULL) {
		printf("The char '%c' does not exist.\r\n", paraChar);
		return;
	}// Of if

	// Step 3. Change links.
	q = p->next;
	r = q->next;
	p->next = r;
	if (r != NULL) {
		r->previous = p;
	}// Of if

	// Step 4. Free the space.
	free(q);
}// Of deleteElement

/**
 * Unit test.
 */
void insertDeleteTest(){
	// Step 1. Initialize an empty list.
	DLNodePtr tempList = initLinkList();
	printList(tempList);

	// Step 2. Add some characters.
	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);

	// Step 3. Delete some characters (the first occurrence).
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	// Step 4. Insert to a given position.
	insertElement(tempList, 'o', 1);
	printList(tempList);
}// Of appendInsertDeleteTest

/**
 * Address test: beyond the book.
 */
void basicAddressTest(){
	DLNode tempNode1, tempNode2;

	tempNode1.data = 4;
	tempNode1.next = NULL;

	tempNode2.data = 6;
	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;
}// Of basicAddressTest

/**
 * The entrance.
 */
void main(){
	insertDeleteTest();
	basicAddressTest();
}// Of main

 这样看似乎太冗杂,我们不妨才分开了来看。

1.2定义双链表

typedef struct DoubleLinkedNode{
	char data;
	struct DoubleLinkedNode *previous;
	struct DoubleLinkedNode *next;
} DLNode, *DLNodePtr;

 1.3初始化双链表

DLNodePtr initLinkList(){
	DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
	tempHeader->data = '\0';
	tempHeader->previous = NULL;
	tempHeader->next = NULL;
	return tempHeader;
}// Of initLinkList

 先用malloc函数动态分布一个空间给tempHeader,因为是初始化,所以数据域和两个指针域里都为空。

1.4打印链表

void printList(DLNodePtr paraHeader){
	DLNodePtr p = paraHeader->next;
	while (p != NULL) {
		printf("%c", p->data);
		p = p->next;
	}// Of while
	printf("\r\n");
}// Of printList

1.5插入节点の函数

void insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition){
	DLNodePtr p, q, r;

	// Step 1. Search to the position.
	p = paraHeader;
	for (int i = 0; i < paraPosition; i ++) {
		p = p->next;
		if (p == NULL) {
			printf("The position %d is beyond the scope of the list.", paraPosition);
			return;
		}// Of if
	} // Of for i

	// Step 2. Construct a new node.
	q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
	q->data = paraChar;

	// Step 3. Now link.
	r = p->next;
	q->next = p->next;
	q->previous = p;
	p->next = q;
	if (r != NULL) {
		r->previous = q;
	}// Of if
}// Of insertElement

1.6删除一个节点

void deleteElement(DLNodePtr paraHeader, char paraChar){
	DLNodePtr p, q, r;
	p = paraHeader;

	// Step 1. Locate.
	while ((p->next != NULL) && (p->next->data != paraChar)){
		p = p->next;
	}// Of while

	// Step 2. Error check.
	if (p->next == NULL) {
		printf("The char '%c' does not exist.\r\n", paraChar);
		return;
	}// Of if

	// Step 3. Change links.
	q = p->next;
	r = q->next;
	p->next = r;
	if (r != NULL) {
		r->previous = p;
	}// Of if

	// Step 4. Free the space.
	free(q);
}// Of deleteElement

1.7测试函数以及地址の打印

void insertDeleteTest(){
	// Step 1. Initialize an empty list.
	DLNodePtr tempList = initLinkList();
	printList(tempList);

	// Step 2. Add some characters.
	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);

	// Step 3. Delete some characters (the first occurrence).
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	// Step 4. Insert to a given position.
	insertElement(tempList, 'o', 1);
	printList(tempList);
}// Of appendInsertDeleteTest

/**
 * Address test: beyond the book.
 */
void basicAddressTest(){
	DLNode tempNode1, tempNode2;

	tempNode1.data = 4;
	tempNode1.next = NULL;

	tempNode2.data = 6;
	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;
}// Of basicAddressTest

/**
 * The entrance.
 */
void main(){
	insertDeleteTest();
	basicAddressTest();
}// Of main

1.8运行结果

Hello!
The char 'a' does not exist.
Hll!
Holl!
The first node: 1703632, 1703632, 1703640
The second node: 1703620, 1703620, 1703628
Press any key to continue

看了老师の代码,第一遍可能不是那么容易理解。接下来看看编者个人的理解吧!上代码!

二、作者小哥哥の代码

2.1定义、初始化和打印链表

//定义一个双链表 
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;
	while(p != NULL) {
		printf("%c ",p->data);
		p = p->next;
	}
	printf("\r\n");
}

基本上与老师の代码无异。

2.2 插入一个节点の函数

//插入节点 
void insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition) {
	DLNodePtr p, q, r;
	
	p = paraHeader;//让p指向头节点 
	for(int i = 0; i < paraPosition; i ++) {
		p = p->next;//遍历链表 
		if(p == NULL) {
			printf("The Position %d is beyond the scope of the list!", paraPosition);
			return;
		}
	}
	
	//动态分配一块空间给q,即q为要插入的节点 
	q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode)); 
	q->data = paraChar;
	r = p->next;
	q->next = p->next;//让q的next指向p的下一个节点 
	q->previous = p;//让q的previous指向p 
	p->next = q;//让p的next指向q 
	if(r != NULL) {//检验p是不是最后一个节点 
		r->previous = q;
	}
}

 以下为插入一个节点の图示:

2.3查找一个节点の函数

此为作者自己添加的。

//查找节点 
void locateElement(DLNodePtr paraHeader, char paraChar) {
	DLNodePtr p;
	p = paraHeader;//让p指向头节点 
	int i = 0;//i表示第几个节点 
	while((p->next->data != paraChar) && (p->next != NULL)) {
		i ++;//位置往后移 
		p = p->next;//让p指向p的下一个节点 
	}
	if (p->next == NULL) {//检验p是不是最后一个节点 
		printf("The char %c is not in this list!\r\n", paraChar);
		return;
	}
	printf("The paraChar %c is on the %dth location!\r\n", paraChar, i);
}

2.4删除一个节点の函数

//删除节点 
void deleteElement(DLNodePtr paraHeader, char paraChar) {
	DLNodePtr p, q, r;
	
	p = paraHeader;
	while((p->next != NULL) && (p->next->data != paraChar)) {
		p = p->next;//从头遍历链表,查找需要删除的位置 
	}
	
	if(p->next == NULL) {//遍历完成,未找到指定的paraChar 
		printf("The char '%c' does not exist!\r\n",paraChar);
		return;
	}
	
	//q作为删除的节点 
	q = p->next;
	r = q->next;
	p->next = r;//把删除的节点的前后链接起来
	if(r != NULL) {//判断p是否为最后一个节点 
		r->previous = p;
	}
	free(q);//释放掉q的空间 
}

以下为删除一个节点の图示: 

2.5测试和打印地址

void 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);
	
	//测试locateElement!
	locateElement(tempList, 'l');
	
	//测试deleteElement!
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	//测试insertElement!
	insertElement(tempList, 'o', 1);
	printList(tempList);
}

//打印地址 
void basicAddressTest(){
	DLNode tempNode1, tempNode2;

	tempNode1.data = '4';
	tempNode1.next = NULL;

	tempNode2.data = '6';
	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;
}

int main() {
	Test();
	basicAddressTest();
	return 0;
}

2.6完整代码呈现及运行结果

#include<stdio.h>
#include<stdlib.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;
	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;//让p指向头节点 
	for(int i = 0; i < paraPosition; i ++) {
		p = p->next;//遍历链表 
		if(p == NULL) {
			printf("The Position %d is beyond the scope of the list!", paraPosition);
			return;
		}
	}
	
	//动态分配一块空间给q,即q为要插入的节点 
	q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode)); 
	q->data = paraChar;
	r = p->next;
	q->next = p->next;//让q的next指向p的下一个节点 
	q->previous = p;//让q的previous指向p 
	p->next = q;//让p的next指向q 
	if(r != NULL) {//检验p是不是最后一个节点 
		r->previous = q;
	}
}

//查找节点 
void locateElement(DLNodePtr paraHeader, char paraChar) {
	DLNodePtr p;
	p = paraHeader;//让p指向头节点 
	int i = 0;//i表示第几个节点 
	while((p->next->data != paraChar) && (p->next != NULL)) {
		i ++;//位置往后移 
		p = p->next;//让p指向p的下一个节点 
	}
	if (p->next == NULL) {//检验p是不是最后一个节点 
		printf("The char %c is not in this list!\r\n", paraChar);
		return;
	}
	printf("The paraChar %c is on the %dth location!\r\n", paraChar, i);
}

//删除节点 
void deleteElement(DLNodePtr paraHeader, char paraChar) {
	DLNodePtr p, q, r;
	
	p = paraHeader;
	while((p->next != NULL) && (p->next->data != paraChar)) {
		p = p->next;//从头遍历链表,查找需要删除的位置 
	}
	
	if(p->next == NULL) {//遍历完成,未找到指定的paraChar 
		printf("The char '%c' does not exist!\r\n",paraChar);
		return;
	}
	
	//q作为删除的节点 
	q = p->next;
	r = q->next;
	p->next = r;//把删除的节点的前后链接起来
	if(r != NULL) {//判断p是否为最后一个节点 
		r->previous = p;
	}
	free(q);//释放掉q的空间 
}

void 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);
	
	//测试locateElement!
	locateElement(tempList, 'l');
	
	//测试deleteElement!
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	//测试insertElement!
	insertElement(tempList, 'o', 1);
	printList(tempList);
}

//打印地址 
void basicAddressTest(){
	DLNode tempNode1, tempNode2;

	tempNode1.data = '4';
	tempNode1.next = NULL;

	tempNode2.data = '6';
	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;
}

int main() {
	Test();
	basicAddressTest();
	return 0;
}
H e l l o !
The paraChar l is on the 2th location!
The char 'a' does not exist!
H l l !
H o l l !
The first node: 6487504, 6487504, 6487520
The second node: 6487472, 6487472, 6487488

以上仅为作者自身观点,如有不妥之处欢迎留言指出。

老师代码出处:数据结构 C 代码 2.3: 双向链表_闵帆的博客-CSDN博客

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值