vxworks6.9双向链表代码
时间: 2023-10-11 08:14:21 浏览: 47
以下是一个简单的双向链表的示例代码,实现了基本的插入、删除和遍历功能。请注意,这只是一个基本示例,你可以根据自己的需求进行修改和扩展。
```c
#include <stdio.h>
#include <stdlib.h>
// 双向链表节点
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
} Node;
// 创建一个新节点
Node* createNode(int data) {
Node* newNode = (Node*)malloc(sizeof(Node));
if (newNode == NULL) {
printf("内存分配失败\n");
exit(1);
}
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// 在链表的头部插入节点
void insertAtHead(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
(*head)->prev = newNode;
newNode->next = *head;
*head = newNode;
}
}
// 在链表的尾部插入节点
void insertAtTail(Node** head, int data) {
Node* newNode = createNode(data);
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// 删除链表中指定值的节点
void deleteNode(Node** head, int data) {
if (*head == NULL) {
printf("链表为空\n");
return;
}
Node* current = *head;
while (current != NULL) {
if (current->data == data) {
if (current == *head) {
*head = current->next;
if (*head != NULL) {
(*head)->prev = NULL;
}
} else {
current->prev->next = current->next;
if (current->next != NULL) {
current->next->prev = current->prev;
}
}
free(current);
return;
}
current = current->next;
}
printf("节点 %d 未找到\n", data);
}
// 遍历链表并打印节点值
void printList(Node* head) {
if (head == NULL) {
printf("链表为空\n");
} else {
Node* current = head;
printf("链表节点值: ");
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;
current = current->next;
free(temp);
}
*head = NULL;
}
int main() {
Node* head = NULL;
insertAtHead(&head, 3);
insertAtHead(&head, 2);
insertAtHead(&head, 1);
printList(head);
insertAtTail(&head, 4);
insertAtTail(&head, 5);
printList(head);
deleteNode(&head, 3);
deleteNode(&head, 6);
printList(head);
freeList(&head);
return 0;
}
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)