这一段双向链表,用c语言
时间: 2023-02-18 14:32:29 浏览: 50
以下是一段双向链表的C语言实现:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
struct Node *head = NULL;
void insert(int data) {
struct Node *newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = head;
newNode->prev = NULL;
if (head != NULL) {
head->prev = newNode;
}
head = newNode;
}
void printList() {
struct Node *temp = head;
while (temp != NULL) {
printf("%d ", temp->data);
temp = temp->next;
}
printf("\n");
}
int main() {
insert(3);
insert(2);
insert(1);
printList();
return 0;
}
输出结果为:1 2 3
相关问题
C语言写一段循环双向链表的代码
#include <stdio.h>
#include <stdlib.h>
// 双向链表结构体
typedef struct doubly_linked_list {
int data; // 数据域
struct doubly_linked_list *prev; // 前驱指针
struct doubly_linked_list *next; // 后继指针
} DoublyLinkedList;
// 创建双向链表
DoublyLinkedList *create_doubly_linked_list(int data) {
DoublyLinkedList *head = (DoublyLinkedList *) malloc(sizeof(DoublyLinkedList));
head->data = data;
head->prev = NULL;
head->next = NULL;
return head;
}
// 在双向链表尾部插入元素
DoublyLinkedList *insert_doubly_linked_list_tail(DoublyLinkedList *head, int data) {
DoublyLinkedList *new_node = (DoublyLinkedList *) malloc(sizeof(DoublyLinkedList));
new_node->data = data;
new_node->next = NULL;
DoublyLinkedList *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_node;
new_node->prev = p;
return head;
}
// 在双向链表头部插入元素
DoublyLinkedList *insert_doubly_linked_list_head(DoublyLinkedList *head, int data) {
DoublyLinkedList *new_node = (DoublyLinkedList *) malloc(sizeof(DoublyLinkedList));
new_node->data = data;
new_node->prev = NULL;
new_node->next = head;
head->prev = new_node;
head = new_node;
return head;
}
// 删除双向链表指定元素
DoublyLinkedList *delete_doubly_linked_list_node(DoublyLinkedList *head, int data) {
DoublyLinkedList *p = head;
while (p != NULL) {
if (p->data == data) {
if (p == head) {
head = p->next;
head->prev = NULL;
} else if (p->next == NULL) {
p->prev->next = NULL;
} else {
p->prev->next = p->next;
p->next->prev = p->prev;
}
free(p);
break;
}
p = p->next;
}
return head;
}
// 输出双向链表
void print_doubly_linked_list(DoublyLinkedList *head) {
DoublyLinkedList *p = head;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
int main() {
DoublyLinkedList *head = create_doubly_linked_list(1);
head = insert_doubly_linked_list_tail(head, 2);
head = insert_doubly_linked_list_tail(head, 3);
head = insert_doubly_linked_list_head(head, 0);
head = delete_doubly_linked_list_node(head, 2);
print_doubly_linked_list(head);
return 0;
}
请为我写一段计算双向链表长度的c语言代码
以下是一个简单的计算双向链表长度的C语言代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* prev;
struct Node* next;
};
// 计算双向链表长度
int countNodes(struct Node* head) {
int count = 0;
struct Node* current = head;
while (current != NULL) {
count++;
current = current->next;
}
return count;
}
int main() {
// 创建双向链表
struct Node* head = (struct Node*)malloc(sizeof(struct Node));
struct Node* second = (struct Node*)malloc(sizeof(struct Node));
struct Node* third = (struct Node*)malloc(sizeof(struct Node));
head->data = 1;
head->prev = NULL;
head->next = second;
second->data = 2;
second->prev = head;
second->next = third;
third->data = 3;
third->prev = second;
third->next = NULL;
// 计算双向链表长度并打印结果
int length = countNodes(head);
printf("双向链表的长度为: %d\n", length);
// 释放内存
free(head);
free(second);
free(third);
return 0;
}
```
上述代码中,我们创建了一个双向链表,并定义了一个计算链表长度的函数`countNodes()`。函数中使用一个计数器`count`遍历整个链表,并在遍历每个节点时将计数器加1。最终返回计数器的值即为链表的长度。在主函数中,我们调用了`countNodes()`函数来计算链表长度,并打印结果。最后,我们释放了双向链表中的所有节点的内存。