#include<iostream>
using namespace std;
typedef struct DoubleLinkNode{
struct DoubleLinkNode* previous;//指向前驱
char data;
struct DoubleLinkNode* next;//指向后继
}DLNode,*DLNodePtr;//定义数据结构
DLNodePtr initLinkList(){
DLNodePtr tempHeader=(DLNodePtr)malloc(sizeof(DLNode));
tempHeader->data='\0';
tempHeader->previous=nullptr;
tempHeader->next=nullptr;
return tempHeader;
}
//初始化
void printList(DLNodePtr paraHeader){
DLNodePtr p=paraHeader->next;
while(p!=nullptr){
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==nullptr){
printf("The position %d is beyond the scope of the list.", paraPosition);
return;
}
}
q=(DLNodePtr)malloc(sizeof(DLNode));
q->data=paraChar;
r=p->next;
q->next=p->next;
p->next=q;
if(r!=nullptr){
r->previous=q;
}
}
//插入元素
void deleteElement(DLNodePtr paraHeader, char paraChar){
DLNodePtr p,q,r;
p=paraHeader;
while((p->next!=nullptr)&&(p->next->data!=paraChar))
p=p->next;
if(p->next==nullptr){
printf("The char '%c' does not exist.\r\n", paraChar);
return;
}
q=p->next;
r=q->next;
p->next=r;
if(r!=nullptr){
r->previous=p;
}
free(q);
}
//按值删除元素
void LocateElement(DLNodePtr paraHeader,int paraPosition){
DLNodePtr p=paraHeader;
for(int i=0;i<paraPosition;i++){
p=p->next;
if(p==nullptr){
printf("error\r\n");
return ;
}
}
printf("%c\r\n",p->data);
}
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);
LocateElement(tempList,2);
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
LocateElement(tempList,9);
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;
}
int main(){
insertDeleteTest();
basicAddressTest();
return 0;
}
从这段代码中学到了双向链表的基本操作以及 C++ 中结构体的使用。
首先,通过定义 struct DoubleLinkNode
结构体,创建了双向链表的节点,其中包含指向前驱和后继节点的指针。通过 typedef
定义了 DLNode
和 DLNodePtr
,分别表示双向链表节点和节点指针,方便后续代码中的使用。
接着,定义了几个操作函数:
-
initLinkList()
:用于初始化双向链表,创建一个带头结点的空链表。 -
printList()
:用于打印双向链表中的数据,从头结点的下一个节点开始遍历链表。 -
insertElement()
:向双向链表中插入元素,在指定位置后插入一个新节点。 -
deleteElement()
:按值删除双向链表中的元素。 -
LocateElement()
:根据位置查找双向链表中的元素。
接着,在 insertDeleteTest()
函数中进行了这些操作的测试,包括初始化双向链表、插入元素、按值删除元素以及根据位置查找元素等操作,并通过 printList()
函数打印出每一步操作后的链表情况。
最后,在 basicAddressTest()
函数中展示了结构体在内存中的存储情况,以及如何通过指针建立节点之间的连接。
通过这段代码的学习,深入理解了双向链表的结构和基本操作,以及如何在 C++ 中使用结构体和指针来实现链表的相关功能。同时,对于内存管理、指针操作等方面也有了更深入的认识。