写一个C语言双向循环链表
时间: 2023-03-13 19:09:28 浏览: 120
C语言双向循环链表的实现方法如下: 1. 定义一个双向循环链表节点结构:struct node{ int data; struct node *prev; struct node *next; }; 2. 定义一个头节点:struct node *head; 3. 初始化头节点:head->data = 0; head->prev = head; head->next = head; 4. 向双向循环链表中插入节点:struct node *addNode(struct node *head, int data) { struct node *newNode; newNode = (struct node *)malloc(sizeof(struct node)); newNode->data = data; newNode->next = head->next; newNode->prev = head; head->next->prev = newNode; head->next = newNode; return head; } 5. 从双向循环链表中删除节点:struct node *deleteNode(struct node *head, int data) { struct node *curNode; curNode = head->next; while (curNode != head) { if (curNode->data == data) { curNode->next->prev = curNode->prev; curNode->prev->next = curNode->next; free(curNode); break; } curNode = curNode->next; } return head; }
阅读全文