C语言编写双向循环链表
时间: 2023-05-27 22:06:40 浏览: 97
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *prev;
struct node *next;
} Node;
Node *head = NULL;
void insert(int data) {
Node *newNode = (Node *) malloc(sizeof(Node));
newNode->data = data;
if (head == NULL) {
head = newNode;
newNode->prev = head;
newNode->next = head;
} else {
Node *temp = head;
while (temp->next != head) {
temp = temp->next;
}
temp->next = newNode;
newNode->prev = temp;
newNode->next = head;
head->prev = newNode;
}
}
void display() {
if (head == NULL) {
printf("List is empty\n");
return;
}
Node *temp = head;
printf("List elements: ");
do {
printf("%d ", temp->data);
temp = temp->next;
} while (temp != head);
printf("\n");
}
int main() {
insert(10);
insert(20);
insert(30);
insert(40);
insert(50);
display();
return 0;
}
阅读全文