双向链表的插入算法C

时间: 2023-11-25 10:50:47 浏览: 29
以下是双向链表的插入算法C语言实现: ```c // 在pos位置之前插入数据 void list_insert(list_t *list, list_node_t *pos, list_node_t *node) { if (list == NULL || node == NULL) { return; } if (pos == NULL) { // 如果pos为空,则插入到链表头部 list_push_front(list, node); return; } if (pos == list->head) { // 如果pos为链表头部,则插入到链表头部 list_push_front(list, node); return; } node->prev = pos->prev; node->next = pos; pos->prev->next = node; pos->prev = node; list->size++; } ``` 该算法的实现思路是:首先判断链表和节点是否为空,然后判断要插入的位置是否为链表头部,如果是,则直接调用list_push_front()函数插入到链表头部;否则,将要插入的节点的prev指针指向pos的前一个节点,将要插入的节点的next指针指向pos,pos的前一个节点的next指针指向要插入的节点,pos的prev指针指向要插入的节点,最后链表的size加1。
相关问题

用c语言写一个循环双链表插入算法

循环双链表插入算法的C语言实现如下: ```c #include <stdio.h> #include <stdlib.h> // 定义双向链表节点结构体 typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; // 定义循环双向链表结构体 typedef struct List { Node* head; Node* tail; } List; // 初始化循环双向链表 void initList(List* list) { list->head = NULL; list->tail = NULL; } // 创建新节点 Node* createNode(int data) { Node* node = (Node*)malloc(sizeof(Node)); node->data = data; node->prev = NULL; node->next = NULL; return node; } // 在链表尾部插入节点 void insertAtTail(List* list, int data) { Node* node = createNode(data); if (list->head == NULL) { list->head = node; list->tail = node; node->prev = node; node->next = node; } else { node->prev = list->tail; node->next = list->head; list->tail->next = node; list->head->prev = node; list->tail = node; } } // 在链表中间插入节点 void insertAtMiddle(List* list, int data, int position) { Node* node = createNode(data); Node* current = list->head; int i = 1; while (i < position && current != NULL) { current = current->next; i++; } if (current == NULL) { printf("Invalid position\n"); return; } node->prev = current->prev; node->next = current; current->prev->next = node; current->prev = node; } // 打印链表 void printList(List* list) { Node* current = list->head; if (current == NULL) { printf("List is empty\n"); return; } do { printf("%d ", current->data); current = current->next; } while (current != list->head); printf("\n"); } int main() { List list; initList(&list); insertAtTail(&list, 1); insertAtTail(&list, 2); insertAtTail(&list, 3); insertAtTail(&list, 4); insertAtMiddle(&list, 5, 3); printList(&list); return ; } ``` 该算法实现了循环双向链表的初始化、在链表尾部插入节点、在链表中间插入节点和打印链表等功能。其中,insertAtMiddle函数实现了在链表中间插入节点的功能,需要传入链表、要插入的数据和插入的位置三个参数。如果插入位置不合法,会输出"Invalid position"。最后,通过调用printList函数打印链表。

请用c语言写两种双向链表排序算法

当涉及到双向链表的排序算法时,常见的方法有插入排序和归并排序。以下是使用C语言实现这两种算法的示例代码。 1. 插入排序(Insertion Sort): ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; void insert(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL && current->next->data < data) { current = current->next; } if (current->next != NULL) { current->next->prev = newNode; } newNode->next = current->next; newNode->prev = current; current->next = newNode; } } void printList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } void freeList(Node* head) { Node* current = head; while (current != NULL) { Node* temp = current->next; free(current); current = temp; } } int main() { Node* head = NULL; insert(&head, 5); insert(&head, 3); insert(&head, 8); insert(&head, 1); insert(&head, 6); printf("Before sorting: "); printList(head); // 插入排序 for (Node* i = head->next; i != NULL; i = i->next) { int key = i->data; Node* j = i->prev; while (j != NULL && j->data > key) { j->next->data = j->data; j = j->prev; } if (j == NULL) { head->data = key; } else { j->next->data = key; } } printf("After sorting: "); printList(head); freeList(head); return 0; } ``` 2. 归并排序(Merge Sort): ```c #include <stdio.h> #include <stdlib.h> typedef struct Node { int data; struct Node* prev; struct Node* next; } Node; Node* merge(Node* left, Node* right) { Node dummy; Node* tail = &dummy; while (left != NULL && right != NULL) { if (left->data <= right->data) { tail->next = left; left->prev = tail; left = left->next; } else { tail->next = right; right->prev = tail; right = right->next; } tail = tail->next; tail->prev = NULL; } if (left != NULL) { tail->next = left; left->prev = tail; } else { tail->next = right; right->prev = tail; } return dummy.next; } Node* mergeSort(Node* head) { if (head == NULL || head->next == NULL) { return head; } Node* slow = head; Node* fast = head->next; while (fast != NULL && fast->next != NULL) { slow = slow->next; fast = fast->next->next; } Node* right = slow->next; slow->next = NULL; Node* left = mergeSort(head); right = mergeSort(right); return merge(left, right); } void insert(Node** head, int data) { Node* newNode = (Node*)malloc(sizeof(Node)); newNode->data = data; newNode->prev = NULL; newNode->next = NULL; if (*head == NULL) { *head = newNode; } else { Node* current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; newNode->prev = current; } } void printList(Node* head) { Node* current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } void freeList(Node* head) { Node* current = head; while (current != NULL) { Node* temp = current->next; free(current); current = temp; } } int main() { Node* head = NULL; insert(&head, 5); insert(&head, 3); insert(&head, 8); insert(&head, 1); insert(&head, 6); printf("Before sorting: "); printList(head); // 归并排序 head = mergeSort(head); printf("After sorting: "); printList(head); freeList(head); return 0; } ``` 这两种算法都可以对双向链表进行排序,插入排序每次将一个元素插入到已排序的部分,而归并排序则将链表分割为较小的部分并进行合并。您可以根据需要选择适合您情况的算法。

相关推荐

最新推荐

recommend-type

数据机构C语言用双向循环链表实现通讯簿

利用双向循环链表作为储存结构设计并实现一个通讯录程序。可以实现信息的添加、插入、删除、查询和统计等功能 1.2 课程设计要求 (1) 每条信息至少包含:姓名(name)、街道(street)、城市(city)、邮编、(eip...
recommend-type

ansys maxwell

ansys maxwell
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

2. 通过python绘制y=e-xsin(2πx)图像

可以使用matplotlib库来绘制这个函数的图像。以下是一段示例代码: ```python import numpy as np import matplotlib.pyplot as plt def func(x): return np.exp(-x) * np.sin(2 * np.pi * x) x = np.linspace(0, 5, 500) y = func(x) plt.plot(x, y) plt.xlabel('x') plt.ylabel('y') plt.title('y = e^{-x} sin(2πx)') plt.show() ``` 运行这段
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

导入numpy库,创建两个包含9个随机数的3*3的矩阵,将两个矩阵分别打印出来,计算两个数组的点积并打印出来。(random.randn()、dot()函数)

可以的,以下是代码实现: ```python import numpy as np # 创建两个包含9个随机数的3*3的矩阵 matrix1 = np.random.randn(3, 3) matrix2 = np.random.randn(3, 3) # 打印两个矩阵 print("Matrix 1:\n", matrix1) print("Matrix 2:\n", matrix2) # 计算两个数组的点积并打印出来 dot_product = np.dot(matrix1, matrix2) print("Dot product:\n", dot_product) ``` 希望