-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDLinkList.c
138 lines (122 loc) · 2.35 KB
/
DLinkList.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define true 1
#define FALSE 0
#define false 0
#define OK 1
#define ok 1
#define ERROR 0
#define error 0
#define null 0
#define INFEASIBLE -1
#define infeasible -1
#define OVERFLOW -2
#define overflow -2
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 10
#define ElemType int
typedef struct DNode {
ElemType data;
struct DNode *prior, *next;
} DNode, *DLinkList;
DLinkList CreatList1(DLinkList dl) {
int x = 0;
dl = (DLinkList) malloc(sizeof(DNode));
DNode *s;
dl->next = null;
scanf("%d", &x);
while (x != 9999) {
s = (DLinkList) malloc(sizeof(DNode));
s->data = x;
s->next = dl->next;
if (s->next != null)
s->next->prior = s;
s->prior = dl;
dl->next = s;
scanf("%d", &x);
}
return dl;
}
DNode *GetElem(DLinkList dl, int n) {
int i = 1;
DNode *p = dl->next;
if (n == 0) {
printf("0");
exit(0);
}
if (n < 1){
printf("no N+");
exit(0);
}
while (p && i < n) {
p = p->next;
i++;
}
return p;
}
DLinkList DLinkListPointInsert(DLinkList dl, int n, DNode *x) {
//查询是否有这个位置 ,有则返回位置
DNode *p;
p = (DNode *) malloc(sizeof(DNode));
p = GetElem(dl, n - 1);
if (!p) {
printf("null");
exit( false);
}
//插入
x->next = p->next;
x->prior = p;
x->next->prior = x;
p->next = x;
return dl;
}
//插入到某个元素的后面
DLinkList DLinkListElmeInsert(DLinkList dl, DNode w, DNode x) {
//查询是否有这个元素 有则返回位置
//插入
return dl;
}
DLinkList CreatList2(DLinkList dl) {
int x = 0;
dl = (DLinkList) malloc(sizeof(DNode));
DNode *s, *r = dl;
scanf("%d", &x);
while (x != 9999) {
s = (DLinkList) malloc(sizeof(DNode));
s->data = x;
s->prior = r;
r->next = s;
r = s;
scanf("%d", &x);
}
r->next = null;
return dl;
}
void outLinkList(DLinkList L) {
DNode *r = L->next;
while (r != null) { //因为链表赋值的时候第一个是没有date只有next的结点
printf("%d\t", r->data);
if (r->prior != null && r->prior != L)
printf("prior.data=%d\t", r->prior->data);
if (r->next != null)
printf("next.data=%d\t", r->next->data);
printf("\n");
r = r->next;
}
printf("\n");
}
int main(void) {
DLinkList list1;
DNode *a;
a = malloc(sizeof(DNode));
printf("begin\n");
a->data = 44;
list1 = malloc(sizeof(DNode));
list1 = CreatList2(list1);
outLinkList(list1);
list1 = DLinkListPointInsert(list1, 3, a);
outLinkList(list1);
printf("end \n");
return 0;
}