mystat.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define bool int
  5. #define false 0
  6. #define true 1
  7. typedef struct linlist{
  8. char *payload;
  9. struct linlist *next;
  10. }linlist_cell, *linlist_p ;
  11. typedef struct countlist{
  12. char *payload;
  13. int occurence;
  14. struct countlist *next;
  15. }countlist_cell, *countlist_p ;
  16. countlist_p count_anchor;
  17. linlist_p anchor;
  18. linlist_p *anchor_adr = &anchor;
  19. void linlist_free_cell(linlist_p junk);
  20. void linlist_free(linlist_p *junk);
  21. linlist_p linlist_alloc_cell(char* payload);
  22. linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell);
  23. linlist_p linlist_extract_first(linlist_p *anchor);
  24. linlist_p linlistFind(char* payload, linlist_p anchor);
  25. linlist_p linlistExtract(linlist_p *anchor, linlist_p cell);
  26. void linlist_sort(linlist_p *anchor);
  27. countlist_p countlist_alloc_cell(char* payload);
  28. countlist_p linlist_count(linlist_p *anchor);
  29. int main() {
  30. linlist_p i;
  31. char buffer[255];
  32. FILE *in = stdin;
  33. while (fgets(buffer, 255, in))
  34. {
  35. linlist_p newcell = linlist_alloc_cell(buffer);
  36. linlist_insert_first(anchor_adr,newcell);
  37. }
  38. linlist_count(anchor);
  39. printf("\nSORTED OUTPUT\n");
  40. for (i = anchor; i != NULL; i = i->next)
  41. {
  42. fprintf(stdout, "%s", i->payload);
  43. }
  44. linlist_free(&anchor);
  45. }
  46. countlist_p linlist_count(linlist_p *anchor)
  47. {
  48. linlist_p cell = *anchor;
  49. linlist_p del;
  50. char* payload;
  51. while(cell)
  52. {
  53. payload = cell->payload;
  54. del = linlist_find(payload, anchor);
  55. }
  56. }
  57. void linlist_sort(linlist_p *anchor)
  58. {
  59. bool swapped = true;
  60. linlist_p cell = anchor;
  61. linlist_p dump;
  62. while(swapped)
  63. {
  64. swapped = false;
  65. cell = anchor;
  66. while(cell->next)
  67. {
  68. if(strcmp(cell->payload,cell->next->payload) > 0)
  69. {
  70. // printf("swapping %s and %s",cell->payload,cell->next->payload);
  71. dump = cell->next->payload;
  72. cell->next->payload = cell->payload;
  73. cell->payload = dump;
  74. swapped = true;
  75. }else
  76. {
  77. }
  78. cell = cell->next;
  79. }
  80. }
  81. }
  82. linlist_p linlistExtract(linlist_p *anchor, linlist_p cell)
  83. {
  84. if(*anchor == cell)
  85. {
  86. anchor=cell->next;
  87. return cell;
  88. }else
  89. {
  90. }
  91. }
  92. linlist_p linlistFind(char* payload, linlist_p anchor)
  93. {
  94. printf("find\n");
  95. linlist_p cell = anchor;
  96. if(cell == NULL)
  97. {
  98. printf("anchor is null");
  99. return NULL;
  100. }
  101. while(cell)
  102. {
  103. printf("to search %s \n",payload);
  104. if(strcmp(payload,cell->payload))
  105. {
  106. printf("found same\n");
  107. return cell;
  108. }
  109. else
  110. {
  111. printf("found nothing\n");
  112. cell = cell->next;
  113. }
  114. }
  115. return NULL;
  116. }
  117. void linlist_free_cell(linlist_p junk)
  118. {
  119. free(junk);
  120. }
  121. void linlist_free(linlist_p *junk){
  122. if ((*junk)->next == NULL)
  123. {
  124. free(*junk);
  125. return;
  126. }
  127. else
  128. {
  129. linlist_free(&((*junk)->next));
  130. free(*junk);
  131. }
  132. }
  133. linlist_p linlist_alloc_cell(char* payload)
  134. {
  135. linlist_p newcell = malloc(sizeof(linlist_p));
  136. char *value;
  137. newcell->payload = strdup(payload);
  138. newcell->next = NULL;
  139. return (newcell);
  140. }
  141. countlist_p countlist_alloc_cell(char* payload)
  142. {
  143. countlist_p newcell = malloc(sizeof(countlist_p));
  144. char *value;
  145. newcell->payload = strdup(payload);
  146. newcell->next = NULL;
  147. return (newcell);
  148. }
  149. linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell)
  150. {
  151. newcell->next = *anchor;
  152. *anchor = newcell;
  153. return(*anchor);
  154. }
  155. countlist_p countlist_insert_first(countlist_p *anchor, countlist_p newcell)
  156. {
  157. newcell->next = *anchor;
  158. *anchor = newcell;
  159. return(*anchor);
  160. }
  161. LinList_p LinListExtract(LinList_p* anchor,LinList_p cell){
  162. if(*anchor == NULL){
  163. printf("liste ist leer");
  164. return NULL;
  165. }
  166. if((*anchor)->next == NULL){ //wenn liste nur ein element hat
  167. if(*anchor == cell){
  168. return NULL;
  169. }
  170. else{
  171. printf("element nicht in der liste enthalten");
  172. return *anchor;
  173. }
  174. }
  175. if(*anchor == cell){
  176. LinList_p ret = LinListExtractFirst(anchor);
  177. return ret;
  178. }
  179. while(*anchor != NULL && (*anchor)->next != cell){ //springe bis letztes element
  180. anchor = &(*anchor)->next;
  181. }
  182. LinList_p tmp = *anchor;
  183. if((*anchor)->next == cell){ //zelle gefunden
  184. tmp->next = cell->next;
  185. cell->next = NULL;
  186. return cell;
  187. }
  188. else{ //keine zelle gefunden
  189. return NULL;
  190. }
  191. }