#include<stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node* prev;
struct node* next;
}node;
void print(node* first) {
node *p = NULL, *q = NULL;
if (first == NULL) {
return;
} //n输入0直接结束。
p = first->prev;
p->next = NULL;
p = first;
while (p != NULL) {
printf("%d\n", p->value);
q = p;
p = p->next;
free(q);
}
}
node* add(node* first, int value) {//建立双向循环链表
if (first == NULL) {
first = (node*)malloc(sizeof(node));
first->prev = first->next = first;
first->value = value;
return first;
}
node* p = first->prev;
node* q = first;
node* tmp = (node*)malloc(sizeof(node));
tmp->value = value;
tmp->next = q;
tmp->prev = p;
p->next = tmp;
q->prev = tmp;
first = tmp;
return first;
}
int main() {
int n;
int y, i;
scanf("%d", &n);
node* first = NULL;
for (i = 1; i <= n; i++) {
scanf("%d", &y);
first = add(first, y);
}
print(first);
return 0;
}
双向循环链表
最新推荐文章于 2024-01-30 09:00:00 发布