数据结构C实现之线性表

研究生还是要读的,但是专业一定要是自己喜欢的 =。=

zju,wait me!

  1 #include <iostream>
  2 #include <cstdio>
  3 #include <cstdlib>
  4 #define ElementType int
  5 #define Maxsize 100
  6 #define Status bool
  7 #define not_exist 10000
  8 #define none -1
  9 using namespace std;
 10 typedef Status (*func)(ElementType,ElementType);  //a pointer to function
 11 
 12 //create a structure of list by using array
 13 typedef struct AList{
 14     ElementType arr[Maxsize];
 15     unsigned length;
 16 }*Alist;
 17 
 18 //create a structure of list by using linknode
 19 typedef struct LNode{
 20     ElementType value;
 21     struct LNode* next;
 22 }*Llist,*Node;
 23 
 24 //calculating's method of Alist
 25 Status InitAlist(Alist &l);
 26 Status DestroyAlit(Alist &l);
 27 Status ClearAlist(Alist &l);
 28 Status IsAlistEmpty(Alist l);
 29 unsigned AlistLength(Alist l);
 30 Status GetAlist(Alist l,unsigned i,ElementType &e);
 31 unsigned LocateAelem(Alist l,ElementType e,func compare);
 32 Status PriorAelem(Alist l,ElementType cur_e,ElementType &pre_e);
 33 Status NextAelem(Alist l,ElementType cur_e,ElementType &next_e);
 34 Status AlistInsert(Alist &l,unsigned i,ElementType e);
 35 Status AlistDelete(Alist &l,unsigned i,ElementType &e);
 36 Status AlistTraverse(Alist l,void (*visit)(ElementType e));
 37 
 38 //calculating's method of Llist
 39 Status InitLlist(Llist &l);
 40 Status DestroyLlit(Llist &l);
 41 Status ClearLlist(Llist &l);
 42 Status IsLlistEmpty(Llist l);
 43 unsigned LlistLength(Llist l);
 44 Status GetLlist(Llist l,unsigned i,ElementType &e);
 45 unsigned LocateLelem(Llist l,ElementType e,func compare);
 46 Status PriorLelem(Llist l,ElementType cur_e,ElementType &pre_e);
 47 Status NextLelem(Llist l,ElementType cur_e,ElementType &next_e);
 48 Status LlistInsert(Llist &l,unsigned i,ElementType e);
 49 Status LlistDelete(Llist &l,unsigned i,ElementType &e);
 50 Status LlistTraverse(Llist l,void (*visit)(ElementType e));
 51 
 52 //test func
 53 Status isequal(ElementType a, ElementType b){
 54     if(a==b){
 55         return true;
 56     }else{
 57         return false;
 58     }
 59 }
 60 void printit(ElementType e){
 61     printf("%d ",e);
 62 }
 63 
 64 int main(){
 65     // test these two struct of list
 66     //Alist l;
 67     //InitAlist(l);
 68     /*for(unsigned i=0;i<5;i++){
 69         AlistInsert(l,i,i*(i*3));
 70     }*/
 71     //printf("%d\n",LocateAelem(l,145,test1));
 72     //printf("%p",l);
 73     //AlistDelete(l,0,e);
 74     //printf("%d\n",e);
 75     //printf("%d\n",l->length);
 76     //AlistTraverse(l,printit);
 77     /*Llist l;
 78     InitLlist(l);
 79     for(int i=0;i<5;i++){
 80         LlistInsert(l,i,i*i);
 81     }
 82     LlistTraverse(l,printit);
 83     printf("\n");
 84     LlistInsert(l,3,7);
 85     LlistTraverse(l,printit);
 86     printf("\n");
 87     int e;
 88     LlistDelete(l,3,e);
 89     LlistTraverse(l,printit);
 90     printf("\n");
 91     GetLlist(l,3,e);
 92     printf("%d\n",e);*/
 93     return 0;
 94 }
 95 
 96 Status InitAlist(Alist &l){
 97     l=(Alist)malloc(sizeof(struct AList));
 98     if(l==NULL){
 99         return false;
100     }
101     l->length=0;
102     return true;
103 }
104 
105 Status DestroyAlit(Alist &l){
106     if(l!=NULL){
107         free(l);
108         l=NULL;
109         return true;
110     }else{
111         exit(0);
112     }
113 }
114 
115 Status ClearAlist(Alist &l){
116     if(l==NULL){
117         exit(0);
118     }
119     if(l->length!=0){
120         free(l);
121         InitAlist(l);
122         return true;
123     }else{
124         return false;
125     }
126 }
127 
128 Status IsAlistEmpty(Alist l){
129     if(l->length>0){
130         return false;
131     }else{
132         return true;
133     }
134 }
135 
136 unsigned AlistLength(Alist l){
137     if(l!=NULL){
138         return l->length;
139     }else{
140         exit(0);
141     }
142 }
143 
144 Status GetAlist(Alist l,unsigned i,ElementType &e){
145     if(l==NULL){
146         exit(0);
147     }else if(i<1||i>l->length){
148         return false;
149     }else{
150         e=l->arr[i];
151         return true;
152     }
153 }
154 
155 unsigned LocateAelem(Alist l,ElementType e,func compare){
156     if(l==NULL){
157         exit(0);
158     }else{
159         for(unsigned i=0;i<l->length;i++){
160             if(compare(e,l->arr[i])){
161                 return i;
162             }
163         }
164         return not_exist;
165     }
166 }
167 
168 Status PriorAelem(Alist l,ElementType cur_e,ElementType &pre_e){
169     if(l==NULL){
170         exit(0);
171     }else{
172         for(unsigned i=0;i<l->length;i++){
173             if(i!=0&&l->arr[i]==cur_e){
174                 pre_e=l->arr[i-1];
175                 return true;
176             }
177         }
178         return false;
179     }
180 }
181 
182 Status NextAelem(Alist l,ElementType cur_e,ElementType &next_e){
183     if(l==NULL){
184         exit(0);
185     }else{
186         for(unsigned i=0;i<l->length;i++){
187             if(i!=l->length-1&&l->arr[i]==cur_e){
188                 next_e=l->arr[i+1];
189                 return true;
190             }
191         }
192         return false;
193     }
194 }
195 
196 Status AlistInsert(Alist &l,unsigned i,ElementType e){
197     if(l==NULL){
198         exit(0);
199     }else if(l->length==0&&i==0){
200         l->arr[i]=e;
201         l->length++;
202         return true;
203     }else if(i<1||i>l->length){
204         return false;
205     }else{
206         for(unsigned k=l->length;k>i;k--){
207             l->arr[k]=l->arr[k-1];
208         }
209         l->arr[i]=e;
210         l->length++;
211         return true;
212     }
213 }
214 
215 Status AlistDelete(Alist &l,unsigned i,ElementType &e){
216     if(l==NULL||l->length==0){
217         exit(0);
218     }else if(i<0||i>l->length-1){
219         return false;
220     }else{
221         e=l->arr[i];
222         for(unsigned k=i;k<l->length-1;k++){
223             l->arr[k]=l->arr[k+1];
224         }
225         l->arr[l->length-1]=none;
226         l->length--;
227         return true;
228     }
229 }
230 
231 Status AlistTraverse(Alist l,void (*visit)(ElementType e)){
232     if(l==NULL){
233         exit(0);
234     }else if(l->length<1){
235         return false;
236     }else{
237         for(unsigned i=0;i<l->length;i++){
238             visit(l->arr[i]);
239         }
240         return true;
241     }
242 }
243 
244 Status InitLlist(Llist &l){
245     l=(Llist)malloc(sizeof(struct LNode));
246     if(l==NULL){
247         return false;
248     }
249     l->value=-1;
250     l->next=NULL;
251     return true;
252 }
253 
254 Status DestroyLlit(Llist &l){
255     if(l!=NULL){
256         Node p=l;
257         Node tmp;
258         while(p!=NULL){
259             tmp=p;
260             p=p->next;
261             free(tmp);
262         }
263         return true;
264     }else{
265         exit(0);
266     }
267 }
268 
269 Status ClearLlist(Llist &l){
270     if(l==NULL){
271         exit(0);
272     }else{
273         Llist p=l->next;
274         DestroyLlit(p);
275         InitLlist(l);
276         return true;
277     }
278 }
279 
280 Status IsLlistEmpty(Llist l){
281     if(l!=NULL){
282         return true;
283     }else{
284         return false;
285     }
286 }
287 
288 unsigned LlistLength(Llist l){
289     unsigned cnt=0;
290     Llist p=l;
291     while(p->next!=NULL){
292         p=p->next;
293         cnt++;
294     }
295     return cnt;
296 }
297 
298 Status GetLlist(Llist l,unsigned i,ElementType &e){
299     if(l==NULL){
300         exit(0);
301     }else if(i<1||i>LlistLength(l)){
302         return false;
303     }else{
304         unsigned cnt=0;
305         Llist p=l;
306         while(p->next!=NULL&&cnt<i){
307             p=p->next;
308             cnt++;
309         }
310         e=p->value;
311         return true;
312     }
313 }
314 
315 unsigned LocateLelem(Llist l,ElementType e,func compare){
316     if(l==NULL){
317         exit(0);
318     }else{
319         Llist p=l;
320         unsigned cnt=1;
321         while(p!=NULL){
322             if(compare(e,p->value)){
323                 return cnt;
324             }
325             p=p->next;
326             cnt++;
327         }
328     }
329     return not_exist;
330 }
331 
332 Status PriorLelem(Llist l,ElementType cur_e,ElementType &pre_e){
333     if(l==NULL){
334         exit(0);
335     }else{
336         Llist p=l;
337         Llist pre;
338         while(p!=NULL){
339             if(pre!=NULL&&p->value==cur_e){
340                 pre_e=pre->value;
341             }
342             pre=p;
343             p=p->next;
344         }
345         return true;
346     }
347 }
348 
349 Status NextLelem(Llist l,ElementType cur_e,ElementType &next_e){
350     if(l==NULL){
351         exit(0);
352     }else{
353         Llist p=l;
354         while(p!=NULL){
355             if(p->next!=NULL&&p->value==cur_e){
356                 next_e=p->next->value;
357             }
358             p=p->next;
359         }
360         return true;
361     }
362 }
363 
364 Status LlistInsert(Llist &l,unsigned i,ElementType e){
365     if(l==NULL){
366         exit(0);
367     }else if(i==0&&LlistLength(l)==0){
368         Node en;
369         InitLlist(en);
370         en->value=e;
371         l->next=en;
372         return true;
373     }else if(i<1||i>LlistLength(l)){
374         return false;
375     }
376     else{
377         Llist p=l;
378         unsigned cnt=0;
379         while(p!=NULL&&cnt!=i-1){
380             p=p->next;
381             cnt++;
382         }
383         Node en;
384         InitLlist(en);
385         en->value=e;
386         Llist tmp=p->next;
387         p->next=en;
388         en->next=tmp;
389         return true;
390     }
391 }
392 
393 Status LlistDelete(Llist &l,unsigned i,ElementType &e){
394     if(l==NULL){
395         exit(0);
396     }else if(i<1||i>LlistLength(l)){
397         return false;
398     }else{
399         Llist p=l;
400         unsigned cnt=0;
401         while(p!=NULL&&cnt!=i-1){
402             p=p->next;
403             cnt++;
404         }
405         Llist tmp=p->next;
406         e=tmp->value;
407         p->next=tmp->next;
408         free(tmp);
409         return true;
410     }
411 }
412 
413 Status LlistTraverse(Llist l,void (*visit)(ElementType e)){
414     if(l==NULL){
415         exit(0);
416     }else{
417         Llist p=l;
418         while(p!=NULL){
419             visit(p->value);
420             p=p->next;
421         }
422         return true;
423     }
424 }

 

posted on 2017-06-08 18:42  woaiwwc  阅读(106)  评论(0)    收藏  举报