请用C语言帮我写一个双向链表
时间: 2023-03-14 16:24:09 浏览: 63
很高兴能够为您服务!C语言双向链表的实现是通过定义一个结构体来实现的,其中结构体中包含了一个指向前一个结点的指针,一个指向下一个结点的指针,以及存储数据的变量。实现双向链表的时候,除了需要注意的指针的指向外,还需要注意内存的分配和释放,以及链表的创建和销毁。
相关问题
请用C语言帮我写一个双向链表程序
您好,我可以为您提供一份双向链表的C语言程序示例,以供参考:
#include <stdio.h>
#include <stdlib.h> // 定义双向链表节点结构
struct Node
{
int data;
struct Node* prev;
struct Node* next;
}; // 函数声明
void insertAtBegining(struct Node** head_ref, int new_data);
void insertAtEnd(struct Node** head_ref, int new_data);
void deleteNode(struct Node** head_ref, int key);
void printList(struct Node* node); // 主函数
int main()
{
struct Node* head = NULL;
insertAtBegining(&head, 7);
insertAtEnd(&head, 8);
insertAtBegining(&head, 9);
insertAtEnd(&head, 1);
printList(head);
deleteNode(&head, 8);
printList(head);
return 0;
} // 插入节点到链表头部
void insertAtBegining(struct Node** head_ref, int new_data)
{
// 申请新节点
struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));
// 填充新节点的数据
new_node->data = new_data;
// 将新节点的下一个节点指针指向头节点
new_node->next = *head_ref;
// 如果头节点不为空,则将头节点的上一个节点指针指向新节点
if ((*head_ref) != NULL)
(*head_ref)->prev = new_node;
// 将头节点指针指向新节点
(*head_ref) = new_node;
}
// 插入节点到链表尾部
void insertAtEnd(struct Node** head_ref, int new_data)
{
// 申请新节点
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
struct Node *last = *head_ref; // 创建一个指向头节点的指针
// 填充新节点的数据
new_node->data = new_data;
// 新节点的下一个节点指针为空
new_node->next = NULL;
// 如果头节点为空,则将头节点和尾节点指针同时指向新节点
if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}
// 查找最后一个节点
while (last->next != NULL)
last = last->next;
// 将新节点的上一个节点指针指向最后一个节点
last->next = new_node;
// 将新节点的下一个节点指针指向空
new_node->prev = last;
return;
} // 删除指定键值的节点
void deleteNode(struct Node** head_ref, int key)
{
// 创建一个指向头节点的指针
struct Node* temp = *head_ref, *prev;
// 如果头节点的值等于要删除的节点,则将头节点指针指向头节点的下一个节点
if (temp != NULL && temp->data == key)
{
*head_ref = temp->next;
free(temp);
return;
}
// 查找要删除的节点
while (temp != NULL && temp->data != key)
{
prev = temp;
temp = temp->next;
}
// 如果没有找到要删除的节点,则退出函数
if (temp == NULL)
return;
// 将前一个节点的下一个节点指针指向要删除节点的下一个节点
prev->next = temp->next;
// 如果要删除节点的下一个节点不为空,则将下一个节点的上一个节点指针指向要删除节点的前一个节点
if (temp->next != NULL)
temp->next->prev = prev;
free(temp); // 释放内存
}
// 打印双向链表
void printList(struct Node* node)
{
struct Node* last;
printf("\n双向链表为:\n");
while (node != NULL)
{
printf(" %d ", node->data);
last = node;
node = node->next;
}
printf("\n逆序打印:\n");
while (last != NULL)
{
printf(" %d ", last->data);
last = last->prev;
}
}
帮我用C语言写一个双向链表
以下是一个基本的双向链表的C语言实现:
```
#include <stdio.h>
#include <stdlib.h>
// 链表节点结构体
struct Node {
int data; // 数据
struct Node* prev; // 前驱节点指针
struct Node* next; // 后继节点指针
};
// 创建新节点
struct Node* createNode(int data) {
struct Node* newNode = (struct Node*) malloc(sizeof(struct Node));
newNode->data = data;
newNode->prev = NULL;
newNode->next = NULL;
return newNode;
}
// 在链表尾部插入节点
void append(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
struct Node* current = *headRef;
if (current == NULL) {
*headRef = newNode;
} else {
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
newNode->prev = current;
}
}
// 在链表头部插入节点
void prepend(struct Node** headRef, int data) {
struct Node* newNode = createNode(data);
newNode->next = *headRef;
if (*headRef != NULL) {
(*headRef)->prev = newNode;
}
*headRef = newNode;
}
// 删除链表中指定数据的节点
void removeNode(struct Node** headRef, int data) {
struct Node* current = *headRef;
while (current != NULL && current->data != data) {
current = current->next;
}
if (current == NULL) {
return;
}
if (current->prev != NULL) {
current->prev->next = current->next;
} else {
*headRef = current->next;
}
if (current->next != NULL) {
current->next->prev = current->prev;
}
free(current);
}
// 打印链表
void printList(struct Node* head) {
struct Node* current = head;
while (current != NULL) {
printf("%d ", current->data);
current = current->next;
}
printf("\n");
}
int main() {
struct Node* head = NULL;
append(&head, 1);
append(&head, 2);
append(&head, 3);
prepend(&head, 0);
printList(head);
removeNode(&head, 2);
printList(head);
return 0;
}
```
该程序实现了以下操作:
- 创建一个节点结构体。
- 在链表尾部插入节点。
- 在链表头部插入节点。
- 删除链表中指定数据的节点。
- 打印链表。
请注意,该程序仅实现了基本的操作,您可以根据需要扩展该程序以实现其他操作。