一、代码块
双链表结构
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;
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 = (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;
}
}
删除元素
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){
printf("The char '%c' does not exist.\r\n", paraChar);
return;
}
//删除元素
q = p->next;
r = q->next;
p->next = r;
//判断在尾部删除结点
if (r != NULL){
r->previous = q;
}
//释放内存
free(q);
}
查找元素
int locateElemt(DLNodePtr paraHeader, char paraChar){
int paraPosition = -1;
DLNodePtr p;
p = paraHeader;
//在链表中查找
while(p != NULL && p->data != paraChar){
p = p->next;
paraPosition++;
}
//判读是否找到
if(p == NULL){
paraPosition = -1;
}
return paraPosition;
}
插入、删除测试
void insertDeleteTest(){
//创建链表
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 insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition)
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
insertElement(tempList, '!', 5);
void deleteElement(DLNodePtr paraHeader, char paraChar)
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
insertElement(tempList, 'o', 1);
三、全部代码
//
// main.c
//
// Created on 2022/5/2.
//
#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;
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 (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 = (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;
}
}
//删除元素
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){
printf("The char '%c' does not exist.\r\n", paraChar);
return;
}
//删除元素
q = p->next;
r = q->next;
p->next = r;
//判断在尾部删除结点
if (r != NULL){
r->previous = q;
}
//释放内存
free(q);
}
//查找元素
int locateElemt(DLNodePtr paraHeader, char paraChar){
int paraPosition = -1;
DLNodePtr p;
p = paraHeader;
//在链表中查找
while(p != NULL && p->data != paraChar){
p = p->next;
paraPosition++;
}
//判读是否找到
if(p == NULL){
paraPosition = -1;
}
return paraPosition;
}
//插入、删除测试
void insertDeleteTest(){
//创建链表
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(){
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;
}
四、小结
与单链表相比,双链表能得到某结点的前驱,这意味着增加和删改操作会更高效,但所占空间会更多。