使用c语言完成双向循环链表
时间: 2023-05-11 08:06:29 浏览: 119
好的,以下是使用 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 = NULL;
newNode->prev = NULL;
if (head == NULL) {
head = newNode;
head->next = head;
head->prev = head;
} else {
struct Node* temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
newNode->next = head;
head->prev = newNode;
}
}
void display() {
struct Node* temp = head;
if (head == NULL) {
printf("List is empty\n");
} else {
printf("List elements: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}
}
int main() {
insert(1);
insert(2);
insert(3);
insert(4);
insert(5);
display();
return 0;
}
```
希望这个代码能够帮到你!
阅读全文