-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDoubleLink.c
53 lines (46 loc) · 1.05 KB
/
DoubleLink.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
//
// Created by qiguang.zhu on 2020/7/31.
//
/**
* 双向链表 {@link http://data.biancheng.net/view/167.html}
*/
#include <stdio.h>
#include <stdlib.h>
typedef struct DoubleLink {
struct DoubleLink *pre;
int data;
struct DoubleLink *next;
} DoubleLink;
DoubleLink *init(DoubleLink *head) {
head = (DoubleLink*)malloc(sizeof(DoubleLink));
head->pre = NULL;
head->next = NULL;
head->data = 1;
DoubleLink *list = head;
for (int i = 2; i < 6; ++i) {
DoubleLink *body = (DoubleLink*)malloc(sizeof(DoubleLink));
body->pre = NULL;
body->next = NULL;
body->data = i;
list->next = body;
body->pre = list;
list = list->next;
}
return head;
}
void printLink(DoubleLink *link) {
DoubleLink *tmp = link;
while (tmp) {
if (tmp->next == NULL) {
printf("%d", tmp->data);
} else {
printf("%d<->", tmp->data);
}
tmp = tmp->next;
}
}
int main() {
DoubleLink *head = NULL;
printLink(init(head));
return 0;
}