时间:2022-11-09 09:12:06 | 栏目:JAVA代码 | 点击:次
代码实现
//双向链表的结点,包含一个数据域,两个指针域 typedef struct DoublyNode { ElementType date; //数据域 struct DoublyNode* prev; //指向前缀结点 struct DoublyNode* next; //指向后缀结点 }DoublyNode;
//双向链表 typedef struct DoublyLinkList { int length; DoublyNode* next; };
//双向链表的结点,包含一个数据域,两个指针域 typedef struct DoublyNode { ElementType date; //数据域 struct DoublyNode* prev; //指向前缀结点 struct DoublyNode* next; //指向后缀结点 }DoublyNode; //双向链表 typedef struct DoublyLinkList { int length; DoublyNode* next; };
代码实现
代码实现
void InsertDoublyLinkList(DoublyLinkList* dlist, int pos, ElementType element) { //创建空节点 DoublyNode* node = (DoublyLinkList*)malloc(sizeof(DoublyLinkList)); node->date = element; node->prev = NULL; node->next = NULL; //在第一个位置插入结点 if (pos == 1) { node->next = dlist->next; dlist->next = node; node->next->prev = node; dlist->length++; return; } DoublyLinkList* currNode = dlist->next; for (int i = 1; currNode && i < pos - 1; i++) { currNode = currNode->next; } if (currNode) { node->prev = currNode; if (currNode->next) { //如果前置结点非空->因为空就表示没有后继结点了 //将插入位置的前置结点改为指向新结点 currNode->next->prev = node; } node->next = currNode->next; currNode->next = node; dlist->length++; } }
代码实现
代码实现
void DeleteDoublyLinkList(DoublyLinkList* dlist, int pos) { if (pos == 1) { DoublyLinkList* node = dlist->next; if (node) { dlist->next; if (node->next) { //如果哟有第二个结点,那么设置第二个结点的前置结点为NULL node->next->prev = NULL; } free(node); dlist->length--; } return; } DoublyLinkList* node = dlist->next; for (int i = 1; i < pos; i++) { node = node->next; } if (node) { if (node->next) { node->next->prev = node->prve; } node->prev->next = node->next; free(node); dlist->length--; } return; }