标签:
1 #include<stdio.h>
2 #include<stdlib.h>
3
4 #define OK 1
5 #define ERROR 0
6
7 typedef int Elemtype;
8 typedef int Status;
9
10 typedef struct Node{
11 Elemtype data;
12 struct Node* prior;
13 struct Node* next;
14 }Node;
15 typedef struct Node* DolLinkList;
16
17 Status InitDolLinkList(DolLinkList *DL){
18 *DL = (DolLinkList)malloc(sizeof(struct Node));
19 if(!(*DL))
20 return ERROR;
21 (*DL)->next = *DL;
22 (*DL)->prior = *DL;
23 return OK;
24 }
25 Status ClearDolLinkList(DolLinkList DL){
26 //将双向循环链表清为空表
27 DolLinkList s,f = DL->next;
28 while(f != DL){
29 s = f;
30 f = f->next;
31 free(s);
32 }
33 DL->next = DL;
34 DL->prior = DL;
35 return OK;
36 }
37 Status DestoryDolLinkList(DolLinkList DL){
38 ClearDolLinkList(DL);
39 free(DL);
40 DL = NULL;
41 }
42 int Length_DolLinkList(DolLinkList DL){
43 int length = 0;
44 DolLinkList t = DL->next;
45 while(t != DL){
46 t = t->next;
47 length++;
48 }
49 return length;
50 }
51 Status GetElement_DolLinkList(DolLinkList DL,int position,int *value){
52 int count = 0;
53 int length = Length_DolLinkList(DL);
54 if(position < 1 || position > length){
55 return ERROR;
56 }
57 else{
58 while(count < position){
59 DL = DL->next;
60 count++;
61 }
62 *value = DL->data;
63 return OK;
64 }
65 }
66 Status LocateElement_DolLinkList(DolLinkList DL,Elemtype value,int *position){
67 *position = 1;
68 DolLinkList t = DL->next;
69 while(t != DL){
70 if(t->data == value){
71 return OK;
72 }
73 t = t->next;
74 (*position)++;
75 }
76 return ERROR;
77 }
78 Status GetPriorElement_DolLinkList(DolLinkList DL,int currentElement,int *priorElement){
79 int r1;
80 int count = 0;
81 if(LocateElement_DolLinkList(DL,currentElement,&r1)){
82 while(count < r1){
83 count++;
84 DL = DL->next;
85 }
86 if(count == 1)
87 *priorElement = DL->prior->prior->data;
88 else
89 *priorElement = DL->prior->data;
90 return OK;
91 }
92 else
93 return ERROR;
94 }
95 Status GetNextElement_DolLinkList(DolLinkList DL,int currentElement,int *nextElement){
96 int r1;
97 if(LocateElement_DolLinkList(DL,currentElement,&r1)){
98 int count = 0;
99 while(count < r1){
100 count++;
101 DL = DL->next;
102 }
103 if(count == Length_DolLinkList(DL))
104 *nextElement = DL->next->next->data;
105 else
106 *nextElement = DL->next->data;
107 return OK;
108 }
109 else
110 return ERROR;
111 }
112 Status Insert_DolLinkList(DolLinkList DL,int position,int value){
113 if(position < 1 || position > Length_DolLinkList(DL) + 1)
114 return ERROR;
115 else{
116 int count = 0;
117 while(count < position){
118 count++;
119 DL = DL->next;
120 }
121 DolLinkList new = (DolLinkList)malloc(sizeof(struct Node));
122 new->data = value;
123 new->prior = DL->prior;
124 DL->prior->next = new;
125 new->next = DL;
126 DL->prior = new;
127 return OK;
128 }
129 }
130 Status Delete_DolLinkList(DolLinkList DL,int position,int *value){
131 if(position < 1 || position > Length_DolLinkList(DL))
132 return ERROR;
133 else{
134 int count = 0;
135 while(count < position){
136 count++;
137 DL = DL->next;
138 }
139 *value = DL->data;
140 DL->next->prior = DL->prior;
141 DL->prior->next = DL->next;
142 free(DL);
143 }
144 }
145 Status CreateDolLinkList_TailInsert(DolLinkList DL,int number){
146 DolLinkList new;
147 int i;
148 printf("PLEASE ENTER %d ELEMNET!\n",number);
149 for(i = 1; i <= number; i++){
150 new = (DolLinkList)malloc(sizeof(struct Node));
151 if(!new)
152 return ERROR;
153 printf("please enter element%d: ",i);
154 scanf("%d",&(new->data));
155 new->prior = DL->prior;
156 new->next = DL;
157 DL->prior->next = new;
158 DL->prior = new;
159 }
160 return OK;
161 }
162 void DisplayDolLinkList(DolLinkList DL){
163 DolLinkList temp = DL;
164 while(temp->next != DL){
165 temp = temp->next;
166 printf("%d ",temp->data);
167 }
168 printf("\nDisplay Executed!\n\n");
169 }
170 int main(){
171 DolLinkList DL;
172 InitDolLinkList(&DL);
173 CreateDolLinkList_TailInsert(DL,5);
174 printf("the length of list is %d\n",Length_DolLinkList(DL));
175 DisplayDolLinkList(DL);
176
177 int r1,r2;
178 if(GetElement_DolLinkList(DL,3,&r1))
179 printf("the element of postion 3 is %d\n",r1);
180 else
181 printf("Error:getElement:position\n");
182 if(GetElement_DolLinkList(DL,6,&r2))
183 printf("the element of postion 6 is %d\n",r2);
184 else
185 printf("Error:getElement:position\n");
186
187 if(LocateElement_DolLinkList(DL,4,&r1))
188 printf("the position of element 4 is %d\n",r1);
189 else
190 printf("Error:LocateElement:value\n");
191 if(LocateElement_DolLinkList(DL,8,&r1))
192 printf("the position of element 8 is %d\n",r1);
193 else
194 printf("Error:LocateElement:value\n");
195
196 if(GetPriorElement_DolLinkList(DL,6,&r1))
197 printf("before the 6 is %d\n",r1);
198 else
199 printf("Error:GetPriorElement:currentElement\n");
200 if(GetPriorElement_DolLinkList(DL,1,&r2))
201 printf("before the 1 is %d\n",r2);
202 else
203 printf("Error:GetPriorElement:currentElement\n");
204
205 if(GetNextElement_DolLinkList(DL,2,&r1))
206 printf("next the 2 is %d\n",r1);
207 else
208 printf("Error:nextElement:currentElement\n");
209 if(GetNextElement_DolLinkList(DL,5,&r1))
210 printf("next the 5 is %d\n",r1);
211 else
212 printf("Error:nextElement:currentElement\n");
213
214 Insert_DolLinkList(DL,3,666);
215 Insert_DolLinkList(DL,7,999);
216 Insert_DolLinkList(DL,1,111);
217 Insert_DolLinkList(DL,9,2222);
218 DisplayDolLinkList(DL);
219
220 Delete_DolLinkList(DL,2,&r1);
221 Delete_DolLinkList(DL,8,&r1);
222 DisplayDolLinkList(DL);
223 return 0;
224 }
标签:
原文地址:http://www.cnblogs.com/robin-xu/p/5203083.html