发布时间:2023-06-06 15:00
双向链表中各节点包含以下 3 部分信息:
指针域:用于指向当前节点的直接前驱节点;
数据域:用于存储数据元素。
指针域:用于指向当前节点的直接后继节点;
指针域 (prior) |
数据域 (data) |
指针域 (next) |
双链表结构
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);
}
运行结果
Hello!
The char \'a\' does not exist.
Hll!
Holl!
The first node: 6487504, 6487504, 6487520
The second node: 6487472, 6487472, 6487488
1、locateElement()函数
int locateElement(DLNodePtr paraHeader, char paraValue){
DLNodePtr p=paraHeader;
for(int i=0; ;i++){
if(p->data==paraValue){
return i;
}
p=p->next;
}
return -1;
}
2、insertElement()函数更多测试
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, \'\\t\', 5);
insertElement(tempList, \'S\', 6);
insertElement(tempList, \'w\', 7);
insertElement(tempList, \'p\', 8);
insertElement(tempList, \'u\', 9);
insertElement(tempList, \'!\', 10);
printList(tempList);
// 删除链表
deleteElement(tempList, \'e\');
deleteElement(tempList, \'a\');
deleteElement(tempList, \'o\');
deleteElement(tempList, \'!\');
printList(tempList);
// 插入链表
insertElement(tempList, \'p\', 3);
printList(tempList);
}
运行结果
Hello Swpu!
The char \'a\' does not exist.
Hll Swpu
Hllp Swpu
The first node: 6487504, 6487504, 6487520
The second node: 6487472, 6487472, 6487488