#include <stdio.h>
#include <malloc.h>
//双向链表
typedef struct node { //结点
int num;
node* prior; /*前驱指针*/
node* next; /*后继指针*/
} NODE;
NODE* last = NULL; //记录上一个结点指针
/**
创建一个结点
param: int num
return: NODE*
*/
NODE* create_node(int num) {
NODE* node = (NODE*) malloc(sizeof(NODE)); //申请内存
node->num = num;
node->prior = last;
node->next = NULL;
last = node;//保存结点指针
return node;
}
void l_print(NODE* head) {
NODE* p = head;
while (p) {
printf("%d ", p->num);
p = p->next;
}
printf("\n");
}
void r_print(NODE* end) {
NODE* p = end;
while (p) {
printf("%d ", p->num);
p = p->prior;
}
printf("\n");
}
int main() {
NODE* head = create_node(0);
NODE* p = head;
int i;
for (i = 1; i <= 10; i++) {
p->next = create_node(i);
p = p->next;
}
printf("正向遍历链表:");
l_print(head);
printf("反向遍历链表:");
r_print(last);
return 0;
}