TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
list.c
Go to the documentation of this file.
1 #include "turtle.h"
2 #include "t_datamanipulation.h"
3 #include "t_random.h"
4 
5 #define MAX_NAME 256
6 
7 /*--------------------------------------------------------------------------*/
8 
9 /* Translating a buffer of longs into a list */
10 
12 
13 
14 {
15 LONGPAIR* head;
16 if(s[0]==0 || s[1]==0)
17  return nxt;
18 else {
19  head=(LONGPAIR* )malloc(sizeof(LONGPAIR));
20  if(!head) t_error("I cannot allocate further memory");
21  head->i=s[0];
22  head->j=s[1];
23  head->next = buffer2list(s+2,nxt);
24  return head;
25  }
26 }
27 
28 
29 /*--------------------------------------------------------------------------*/
30 
32 
33 
34 {
35 
36 REALPAIR* head;
37 if(s[0]==0 || s[1]==0)
38  return nxt;
39 else {
40  head=(REALPAIR* )malloc(sizeof(REALPAIR));
41  if(!head) t_error("I cannot allocate further memory");
42  head->x=s[0];
43  head->y=s[1];
44  head->next = realbuffer2list(s+2,nxt);
45  return head;
46  }
47 }
48 
49 /*--------------------------------------------------------------------------*/
50 
52 {
53 
54 LONGPAIR *head;
55 
56  head=(LONGPAIR* )malloc(sizeof(LONGPAIR));
57  if(!head) t_error("I cannot allocate further memory");
58  head->next = NULL;
59  head->i=0;
60  head->j=0;
61 
62  return head;
63 
64 
65 }
66 
67 
68 /*--------------------------------------------------------------------------*/
69 
71 {
72 
73 LONGPOKER *head;
74 
75  head=(LONGPOKER* )malloc(sizeof(LONGPAIR));
76  if(!head) t_error("I cannot allocate further memory");
77  head->next = NULL;
78  head->i=0;
79  head->j=0;
80  head->k=0;
81  head->l=0;
82 
83  return head;
84 
85 
86 }
87 
88 /*--------------------------------------------------------------------------*/
89 
91 {
92 
93 REALPAIR *head;
94 
95  head=(REALPAIR* )malloc(sizeof(REALPAIR));
96  if(!head) t_error("I cannot allocate further memory");
97  head->next = NULL;
98  head->x=0;
99  head->y=0;
100 
101 
102  return head;
103 
104 
105 }
106 /*--------------------------------------------------------------------------*/
107 
109 {
110 
111 IX *head;
112 
113  head=(IX* )malloc(sizeof(IX));
114  if(!head) t_error("I cannot allocate further memory");
115  head->next = NULL;
116  head->i=0;
117  head->x=0;
118 
119 
120  return head;
121 
122 
123 }
124 
125 /*--------------------------------------------------------------------------*/
126 
128 {
129 
130 IJX *head;
131 
132  head=(IJX* )malloc(sizeof(IJX));
133  if(!head) t_error("I cannot allocate further memory");
134  head->next = NULL;
135  head->i=0;
136  head->j=0;
137  head->x=0;
138 
139 
140  return head;
141 
142 
143 }
144 /*--------------------------------------------------------------------------*/
145 
147 {
148 
149 XYZ *head;
150 
151  head=(XYZ* )malloc(sizeof(XYZ));
152  if(!head) t_error("I cannot allocate further memory");
153  head->next = NULL;
154  head->x=0;
155  head->y=0;
156  head->z=0;
157 
158 
159  return head;
160 
161 
162 }
163 
164 
165 
166 PHRASE *new_word(long n,char *text)
167 
168 {
169 
170 long m;
171 PHRASE *head;
172  if(n > MAX_NAME) {
173  m= MAX_NAME;
174  }else if(n <= 0){
175  printf("\nWarning::returning a NULL PHRASE pointer\n");
176  return NULL;
177  } else {
178  m=n;
179  }
180  head=(PHRASE*)malloc(sizeof(PHRASE));
181  if(!head) t_error("I cannot allocate further memory");
182  head->next = NULL;
183  head->nh=m;
184  head->word=(char *)malloc((m+1)*sizeof(char));
185  strcpy(head->word, text);
186 
187  return head;
188 
189 
190 }
191 
192 /*--------------------------------------------------------------------------*/
193 
194 
195 void print_longlist_elements(LONGPAIR * list,short columns)
196 {
197 
198 long count=1;
199 LONGPAIR *tmp;
200 
201 if(list==NULL){
202  printf(" NULL LIST\n");
203 }
204 else{
205  tmp=list;
206  putchar('\n');
207  printf("%ld %ld\t",tmp->i,tmp->j);
208  while(tmp->next!=NULL){
209  tmp=tmp->next;
210  count++;
211  if(count%columns==0) putchar('\n');
212  printf("%ld %ld\t",tmp->i,tmp->j);
213  }
214  putchar('\n');
215 }
216 }
217 
218 
219 /*--------------------------------------------------------------------------*/
220 
221 
222 void print_pokerlist_elements(LONGPOKER * list,short columns)
223 {
224 
225 long count=1;
226 LONGPOKER *tmp;
227 
228 if(list==NULL){
229  printf(" NULL LIST\n");
230 }
231 else{
232  tmp=list;
233  putchar('\n');
234  printf("%ld %ld %ld %ld\t",tmp->i,tmp->j,tmp->k,tmp->l);
235  while(tmp->next!=NULL){
236  tmp=tmp->next;
237  count++;
238  if(count%columns==0) putchar('\n');
239  printf("%ld %ld %ld %ld\t",tmp->i,tmp->j,tmp->k,tmp->l);
240  }
241  putchar('\n');
242 }
243 }
244 
245 /*--------------------------------------------------------------------------*/
246 
247 
248 void write_indexed_pokerlist_elements(FILE *ostream,LONGPOKER * list,short columns)
249 {
250 
251 long count=1;
252 LONGPOKER *tmp;
253 
254 if(list==NULL){
255  printf(" NULL LIST\n");
256 }
257 else{
258  tmp=list;
259  putchar('\n');
260  fprintf(ostream,"%ld %ld %ld %ld\t",tmp->i,tmp->j,tmp->k,tmp->l);
261  while(tmp->next!=NULL){
262  tmp=tmp->next;
263  count++;
264  if(count%columns==0) {putchar('\n');}
265  fprintf(ostream,"%ld %ld %ld %ld\t",tmp->i,tmp->j,tmp->k,tmp->l);
266  }
267  putchar('\n');
268 }
269 }
270 
271 /*--------------------------------------------------------------------------*/
272 
273 void print_reallist_elements(REALPAIR * list,short columns)
274 {
275 
276 long count=1;
277 
278 REALPAIR *tmp;
279 
280 if(list==NULL){
281  printf(" NULL LIST\n");
282 }
283 else{
284  tmp=list;
285  putchar('\n');
286  printf("%f %f\t",tmp->x,tmp->y);
287  while(tmp->next!=NULL){
288  tmp=tmp->next;
289  count++;
290  if(count%columns==0) putchar('\n');
291  printf("%f %f\t",tmp->x,tmp->y);
292  }
293  putchar('\n');
294 }
295 }
296 /*--------------------------------------------------------------------------*/
297 
298 void print_ix_elements(IX * list,short columns)
299 {
300 
301 long count=1;
302 IX *tmp;
303 
304 if(list==NULL){
305  printf(" NULL LIST\n");
306 }
307 else{
308  tmp=list;
309  putchar('\n');
310  printf("%ld %f\t",tmp->i,tmp->x);
311  while(tmp->next!=NULL){
312  tmp=tmp->next;
313  count++;
314  if(count%columns==0) putchar('\n');
315  printf("%ld %f\t",tmp->i,tmp->x);
316  }
317  putchar('\n');
318 }
319 }
320 
321 /*--------------------------------------------------------------------------*/
322 
323 void print_ijx_elements(IJX * list,short columns)
324 {
325 
326 long count=1;
327 IJX *tmp;
328 
329 if(list==NULL){
330  printf(" NULL LIST\n");
331 }
332 else{
333  putchar('\n');
334  tmp=list;
335  printf("%ld %ld %f\n", tmp->i, tmp->j, tmp->x);
336  while( tmp->next!=NULL){
337  tmp= tmp->next;
338  count++;
339  if(count%columns==0) putchar('\n');
340  printf("%ld %ld %f\n", tmp->i, tmp->j, tmp->x);
341  }
342  putchar('\n');
343 }
344 }
345 
346 /*--------------------------------------------------------------------------*/
347 
348 void print_xyz_elements(XYZ * list,short columns)
349 {
350 
351 long count=1;
352 XYZ *tmp;
353 
354 if(list==NULL){
355  printf(" NULL LIST\n");
356 }
357 else{
358  putchar('\n');
359  tmp=list;
360  printf("%f %f %f\n", tmp->x, tmp->y, tmp->z);
361  while( tmp->next!=NULL){
362  tmp= tmp->next;
363  count++;
364  if(count%columns==0) putchar('\n');
365  printf("%f %f %f\n", tmp->x, tmp->y,tmp->z);
366  }
367  putchar('\n');
368 }
369 }
370 
371 /*--------------------------------------------------------------------------*/
372 
373 void print_phrase_elements(PHRASE * list,short columns)
374 
375 {
376 
377 long count=1;
378 PHRASE *tmp;
379 
380 if(list==NULL){
381  printf(" NULL LIST\n");
382 }
383 else{
384  tmp=list;
385  if(columns==1){
386  printf("%s\n", tmp->word);
387  while( tmp->next!=NULL){
388  tmp= tmp->next;
389  count++;
390  printf("%s\n", tmp->word);
391  }
392  }else{
393  printf("%s ", tmp->word);
394  while( tmp->next!=NULL){
395  tmp= tmp->next;
396  count++;
397  if(count%columns==0) putchar('\n');
398  printf("%s ", tmp->word);
399 
400  }
401  }
402  putchar('\n');
403 }
404 }
405 
406 /*--------------------------------------------------------------------------*/
407 
409 
410 {
411  if(head==NULL)
412  return 0;
413  else
414  return(1+count_longpair_elements(head->next));
415 }
416 
417 /*--------------------------------------------------------------------------*/
418 
420 
421 {
422  if(head==NULL)
423  return 0;
424  else
425  return(1+count_longpoker_elements(head->next));
426 }
427 
428 /*--------------------------------------------------------------------------*/
429 
430 long count_ix_elements(IX * head)
431 
432 {
433  if(head==NULL)
434  return 0;
435  else
436  return(1+count_ix_elements(head->next));
437 }
438 
439 /*--------------------------------------------------------------------------*/
440 
442 
443 {
444  if(head==NULL)
445  return 0;
446  else
447  return(1+count_realpair_elements(head->next));
448 }
449 
450 /*--------------------------------------------------------------------------*/
451 
453 
454 {
455  if(head==NULL)
456  return 0;
457  else
458  return(1+count_ijx_elements(head->next));
459 }
460 
461 
462 /*--------------------------------------------------------------------------*/
463 
465 
466 {
467  if(head==NULL)
468  return 0;
469  else
470  return(1+count_xyz_elements(head->next));
471 }
472 
473 /*--------------------------------------------------------------------------*/
474 
476 
477 {
478  if(head==NULL)
479  return 0;
480  else
481  return(1+count_phrase_elements(head->next));
482 }
483 
484 
485 
486 /*--------------------------------------------------------------------------*/
487 /* Pointing to the antecedent to a numbered element of the list.
488 If NULL the first is being pointed. */
489 LONGPAIR * point2longpair(LONGPAIR * head,long point)
490 
491 {
492  /*
493  printf("#%d#\n",point);
494  print_longlist_elements(head,10);
495  */
496  if(head==NULL) {
497  t_error("This element does not exist in the list");
498  }else if (point<=0){
499  return NULL;
500  }else if (point==1) {
501  return head;
502  }else {
503  point2longpair(head->next,point-1);
504  }
505  return NULL;
506 
507 }
508 
509 
510 /*--------------------------------------------------------------------------*/
511 LONGPOKER * point2longpoker(LONGPOKER * head,long point)
512 
513 {
514  if(head==NULL) {
515  t_error("This element does not exist in the list");
516  }else if (point<=0){
517  return NULL;
518  }else if (point==1) {
519  return head;
520  }else {
521  point2longpoker(head->next,point-1);
522  }
523  return NULL;
524 }
525 
526 /*--------------------------------------------------------------------------*/
527 REALPAIR * point2realpair(REALPAIR * head,long point)
528 
529 {
530 
531  if(head==NULL) {
532  t_error("This element does not exist in the list");
533  }else if (point<=0){
534  return NULL;
535  }else if (point==1) {
536  return head;
537  }else {
538  point2realpair(head->next,point-1);
539  }
540  return NULL;
541 
542 }
543 
544 /*--------------------------------------------------------------------------*/
545 IX * point2ix(IX * head,long point)
546 
547 {
548 
549  if(head==NULL) {
550  t_error("This element does not exist in the list");
551  }else if (point<=0){
552  return NULL;
553  }else if (point==1){
554  return head;
555  }else {
556  point2ix(head->next,point-1);
557  }
558  return NULL;
559 
560 }
561 
562 /*--------------------------------------------------------------------------*/
563 IJX * point2ijx(IJX * head,long point)
564 
565 {
566 
567  if(head==NULL){
568  t_error("This element does not exist in the list");
569  }else if (point<=0){
570  return NULL;
571  }else if (point==1) {
572  return head;
573  }else {
574  point2ijx(head->next,point-1);
575  }
576  return NULL;
577 
578 }
579 /*--------------------------------------------------------------------------*/
580 XYZ * point2measure(XYZ * head,long point)
581 
582 {
583 
584  if(head==NULL){
585  t_error("This element does not exist in the list");
586  }else if (point<=0){
587  return NULL;
588  }else if (point==1) {
589  return head;
590  }else {
591  point2measure(head->next,point-1);
592  }
593 
594  return NULL;
595 
596 }
597 
598 /*--------------------------------------------------------------------------*/
599 
600 /* Deleting one element in a list and returning the pointer
601 to the head */
602 
603 
605 {
606 LONGPAIR * q;
607 if(head==NULL) t_error("NULL cannot be deleted");
608 if(ante==NULL) {
609  q=head;
610  head=head->next;
611  }
612  else{
613  q=ante->next;
614  ante->next=q->next;
615  }
616 free(q);
617 return head;
618 }
619 
620 
621 
623 {
624 LONGPOKER * q;
625 if(head==NULL) t_error("NULL cannot be deleted");
626 if(ante==NULL) {
627  q=head;
628  head=head->next;
629  }
630  else{
631  q=ante->next;
632  ante->next=q->next;
633  }
634 free(q);
635 return head;
636 }
637 
638 /* Deleting one element in a list appending the first part of the list
639 preceding the element to the rest */
640 
641 
643 {
644 LONGPAIR * q;
645 if(head==NULL) t_error("NULL cannot be deleted");
646 if(ante==NULL) {
647  q=head;
648  head=head->next;
649  }
650  else{
651  q=ante->next;
652  ante->next=NULL;
653  head=appendto(q->next,head);
654 
655  }
656 free(q);
657 return head;
658 }
659 
660 /*--------------------------------------------------------------------------*/
661 
663 {
664 
665 REALPAIR * q;
666 if(head==NULL) t_error("NULL cannot be deleted");
667 if(ante==NULL) {
668  q=head;
669  head=head->next;
670  }
671  else{
672  q=ante->next;
673  ante->next=q->next;
674  }
675 free(q);
676 return head;
677 }
678 /*--------------------------------------------------------------------------*/
679 
680 IX* delete_ix(IX * head,IX * ante)
681 {
682 
683 IX * q;
684 if(head==NULL) t_error("NULL cannot be deleted");
685 if(ante==NULL) {
686  q=head;
687  head=head->next;
688  }
689  else{
690  q=ante->next;
691  ante->next=q->next;
692  }
693 free(q);
694 return head;
695 }
696 
697 /*--------------------------------------------------------------------------*/
698 
699 IJX * delete_ijx(IJX * head,IJX * ante)
700 {
701 
702 IJX * q;
703 if(head==NULL) t_error("NULL cannot be deleted");
704 if(ante==NULL) {
705  q=head;
706  head=head->next;
707  }
708  else{
709  q=ante->next;
710  ante->next=q->next;
711  }
712 free(q);
713 return head;
714 }
715 
716 /*--------------------------------------------------------------------------*/
717 
718 XYZ * delete_xyz(XYZ * head,XYZ * ante)
719 {
720 
721 XYZ * q;
722 if(head==NULL) t_error("NULL cannot be deleted");
723 if(ante==NULL) {
724  q=head;
725  head=head->next;
726  }
727  else{
728  q=ante->next;
729  ante->next=q->next;
730  }
731 free(q);
732 return head;
733 }
734 
735 
736 /*--------------------------------------------------------------------------*/
737 
738 /*Selecting one point randomly in a list. It returns: NULL if the
739 point chosen is the first of the list. The antecedent to the point in any different case.
740 The reason is that you need to know the antecedent for deleting the point from the list.*/
741 
742 /* commentata per warning: integer overflow in expression
743 
744 LONGPAIR * select_longpair_randomly(LONGPAIR * pointer)
745 
746 {
747 
748 long length,utmp;
749 LONGPAIR * linktmp;
750 
751 if(pointer==NULL) t_error("NULL cannot be selected");
752 length=count_longpair_elements(pointer);
753 utmp=rrand(length);
754 if(utmp>=length) t_error("wrong random number");
755 linktmp=point2longpair(pointer,utmp);
756 return linktmp;
757 }*/
758 /*--------------------------------------------------------------------------*/
759 /* commentata per warning: integer overflow in expression
760 REALPAIR * select_realpair_randomly(REALPAIR * pointer)
761 
762 {
763 
764 unsigned long length,utmp;
765 REALPAIR * linktmp;
766 
767 if(pointer==NULL) t_error("NULL cannot be selected");
768 length=count_realpair_elements(pointer);
769 utmp=rrand(length);
770 if(utmp>=length) t_error("wrong random number");
771 linktmp=point2realpair(pointer,utmp);
772 return linktmp;
773 }
774 */
775 /*--------------------------------------------------------------------------*/
776 
777 /* commentata per warning: integer overflow in expression
778 IX * select_ix_randomly(IX * pointer)
779 
780 {
781 
782 unsigned long length,utmp;
783 IX * linktmp;
784 
785 if(pointer==NULL) t_error("NULL cannot be selected");
786 length=count_ix_elements(pointer);
787 utmp=rrand(length);
788 if(utmp>=length) t_error("wrong random number");
789 linktmp=point2ix(pointer,utmp);
790 return linktmp;
791 }
792 
793 */
794 /*--------------------------------------------------------------------------*/
795 
796 /* commentata per warning: integer overflow in expression
797 IJX * select_ijx_randomly(IJX * pointer)
798 
799 {
800 
801 unsigned long length,utmp;
802 IJX * linktmp;
803 
804 if(pointer==NULL) t_error("NULL cannot be selected");
805 length=count_ijx_elements(pointer);
806 utmp=rrand(length);
807 if(utmp>=length) t_error("wrong random number");
808 linktmp=point2ijx(pointer,utmp);
809 return linktmp;
810 }*/
811 
812 /*--------------------------------------------------------------------------*/
813 /* commentata per warning: integer overflow in expression
814 XYZ * select_xyz_randomly(XYZ * pointer)
815 
816 {
817 
818 unsigned long length,utmp;
819 XYZ * linktmp;
820 
821 if(pointer==NULL) t_error("NULL cannot be selected");
822 length=count_xyz_elements(pointer);
823 utmp=rrand(length);
824 if(utmp>=length) t_error("wrong random number");
825 linktmp=point2measure(pointer,utmp);
826 return linktmp;
827 
828 }
829 
830 */
831 /* Recursive deletion of a list */
832 /*--------------------------------------------------------------------------*/
833 
835 {
836 if(head!=NULL){
837  delete_longpair_list(head->next);
838  free(head);
839 }
840 }
841 
842 /*--------------------------------------------------------------------------*/
843 
845 {
846 if(head!=NULL){
848  free(head);
849 }
850 }
851 
852 /*--------------------------------------------------------------------------*/
853 
855 {
856 if(head!=NULL){
857  delete_realpair_list(head->next);
858  free(head);
859 }
860 }
861 /*--------------------------------------------------------------------------*/
862 
863 void delete_ix_list(IX * head)
864 {
865 if(head!=NULL){
866  delete_ix_list(head->next);
867  free(head);
868 }
869 }
870 
871 /*--------------------------------------------------------------------------*/
872 
873 void delete_ijx_list(IJX * head)
874 
875 {
876 
877 if(head!=NULL){
878  delete_ijx_list(head->next);
879  free(head);
880 }
881 }
882 
883 /*--------------------------------------------------------------------------*/
884 
885 void delete_xyz_list(XYZ * head)
886 {
887 if(head!=NULL){
888  delete_xyz_list(head->next);
889  free(head);
890 }
891 }
892 
893 /*--------------------------------------------------------------------------*/
894 
896 {
897 /* char ch; */
898 if(head!=NULL){
899  delete_phrase(head->next);
900  free(head->word);
901  free(head);
902 }
903 }
904 
905 /*--------------------------------------------------------------------------*/
906 
907 LONGPAIR * appendto(LONGPAIR * head, LONGPAIR * element)
908 {
909 
910 LONGPAIR * tmp=head;
911 
912 if(head==NULL) return element;
913 else{
914  while(tmp->next!=NULL) tmp=tmp->next;
915  tmp->next=element;
916  return head;
917 }
918 }
919 
920 
921 /*--------------------------------------------------------------------------*/
922 
923 PHRASE * join_words(PHRASE * head, PHRASE* element)
924 
925 {
926 
927 PHRASE * tmp=head;
928 
929 if(head==NULL) {
930  return element;
931 }
932 else{
933  while(tmp->next!=NULL) tmp=tmp->next;
934  tmp->next=element;
935  return head;
936 }
937 }
938 
939 /*--------------------------------------------------------------------------*/
940 
941 LONGPAIR * prependto(LONGPAIR * head, LONGPAIR * element)
942 {
943 
944 LONGPAIR* home;
945 
946 home=element;
947 if(head==NULL){
948  return element;
949 }else if(element==NULL){
950  return head;
951 }else {
952 while(home->next!=NULL){
953  printf("k\n");
954  home=home->next;
955 }
956 home->next=head;
957 return home;
958 }
959 
960 }
961 /*--------------------------------------------------------------------------*/
963 
964 {
965 
966 LONGPAIR * tmp=head;
967 
968 if(head==NULL) return NULL;
969 else{
970 while(tmp->next!=NULL) tmp=tmp->next;
971 
972 tmp->next=head;
973 tmp=head->next;
974 head->next=NULL;
975 return tmp;
976 }
977 }
978 
979 /*--------------------------------------------------------------------------
980 This rotate the list n times
981 ---------------------------------------------------------------------------*/
982 LONGPAIR * rotate(LONGPAIR * head,int n)
983 {
984 
985 LONGPAIR * tmp=head,*back;
986 int m=0;
987 
988 if(n==0) {
989  return tmp;
990 }else if(head==NULL){
991  return NULL;
992 }else{
993  while(tmp->next!=NULL && m<n) {back=tmp;tmp=tmp->next; m++;}
994  if(m==n) {
995  back->next=NULL;
996  back=tmp;
997  while(back->next!=NULL) back=back->next;
998  back->next=head;
999  return tmp;
1000  }
1001  else if(tmp->next==NULL)
1002  return rotate(head,n-m);
1003 }
1004 
1005 return NULL;
1006 
1007 }
1008