数据结构-双向链表、双向循环链表
定义节点
typedef struct Node { int data; struct Node *head; struct Node *next; } DoubleLinkNode;
创建双向链表
//创建节点 DoubleLinkNode *rootNode = alloca(sizeof(DoubleLinkNode)); rootNode->data = 10; rootNode->head = NULL; rootNode->next = NULL; DoubleLinkNode *node1 = alloca(sizeof(DoubleLinkNode)); node1->data = 20; node1->head = NULL; node1->next = NULL; DoubleLinkNode *node2 = alloca(sizeof(DoubleLinkNode)); node2->data = 30; node2->head = NULL; node2->next = NULL; DoubleLinkNode *node3 = alloca(sizeof(DoubleLinkNode)); node3->data = 40; node3->head = NULL; node3->next = NULL; //连接节点 rootNode->next = node1; node1->head = rootNode; node1->next = node2; node2->head = node1; node2->next = node3; node3->head = node2; // 反转 DoubleLinkNode *node = reverseDoublelink(rootNode);
反转双向链表
DoubleLinkNode* reverseDoublelink (DoubleLinkNode *node) { if (node == NULL) {return NULL;} DoubleLinkNode *pre = NULL; while (node) { // 记录下一个节点 DoubleLinkNode *nextTemp = node->next; // 反转 node->head = nextTemp; node->next = pre; // 把前节点置为当前节点 pre = node; // 把当前节点置为下个节点 node = nextTemp; } return pre; }
双向循环链表节点
typedef struct Node { int data; struct Node *head; struct Node *next; } DoubleCirculationLinkNode;
双向循环链表创建
//创建节点 DoubleCirculationLinkNode *rootNode = alloca(sizeof(DoubleCirculationLinkNode)); rootNode->data = 10; rootNode->head = NULL; rootNode->next = NULL; DoubleCirculationLinkNode *node1 = alloca(sizeof(DoubleCirculationLinkNode)); node1->data = 20; node1->head = NULL; node1->next = NULL; DoubleCirculationLinkNode *node2 = alloca(sizeof(DoubleCirculationLinkNode)); node2->data = 30; node2->head = NULL; node2->next = NULL; DoubleCirculationLinkNode *node3 = alloca(sizeof(DoubleCirculationLinkNode)); node3->data = 40; node3->head = NULL; node3->next = NULL; //连接节点 rootNode->head = node3; rootNode->next = node1; node1->head = rootNode; node1->next = node2; node2->head = node1; node2->next = node3; node3->head = node2; node3->next = rootNode;
相关推荐
kka 2020-09-14
koushr 2020-11-12
范范 2020-10-28
qiangde 2020-09-13
范范 2020-07-30
mingyunxiaohai 2020-07-19
zhaochen00 2020-10-13
Mars的自语 2020-09-27
steeven 2020-09-18
聚沙成塔积水成渊 2020-08-16
earthhouge 2020-08-15
aanndd 2020-08-12
bluetears 2020-07-28
horizonheart 2020-07-19
liushall 2020-07-18
bluetears 2020-07-05
fengshantao 2020-07-02
liuweixiao0 2020-06-27