数据结构-双向链表、双向循环链表

ding0 2019-11-16

数据结构-双向链表、双向循环链表

定义节点

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;

相关推荐