mystat.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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_adr);
  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. printf("inside first loop\n");
  54. payload = cell->payload;
  55. printf("searching for %s",payload);
  56. del = linlistFind(payload, *anchor);
  57. printf("del: %p\n",del);
  58. while(del)
  59. {
  60. printf("inside second loop\n");
  61. linlistExtract(*anchor,del);
  62. del = linlistFind(payload, anchor_adr);
  63. }
  64. break;
  65. }
  66. }
  67. void linlist_sort(linlist_p *anchor)
  68. {
  69. bool swapped = true;
  70. linlist_p cell = anchor;
  71. linlist_p dump;
  72. while(swapped)
  73. {
  74. swapped = false;
  75. cell = anchor;
  76. while(cell->next)
  77. {
  78. if(strcmp(cell->payload,cell->next->payload) > 0)
  79. {
  80. // printf("swapping %s and %s",cell->payload,cell->next->payload);
  81. dump = cell->next->payload;
  82. cell->next->payload = cell->payload;
  83. cell->payload = dump;
  84. swapped = true;
  85. }else
  86. {
  87. }
  88. cell = cell->next;
  89. }
  90. }
  91. }
  92. linlist_p linlistFind(char* payload,linlist_p anchor){
  93. while(anchor != NULL && strcmp(strdup((*anchor).payload), payload) != 0){ //pointer nicht null und das element
  94. anchor = anchor->next;
  95. }
  96. if(anchor != NULL){
  97. printf("Found: %s",(*anchor).payload);
  98. return anchor;
  99. }
  100. else{
  101. return NULL;
  102. }
  103. }
  104. void linlist_free_cell(linlist_p junk)
  105. {
  106. free(junk);
  107. }
  108. void linlist_free(linlist_p *junk)
  109. {
  110. if ((*junk)->next == NULL)
  111. {
  112. free(*junk);
  113. return;
  114. }
  115. else
  116. {
  117. linlist_free(&((*junk)->next));
  118. free(*junk);
  119. }
  120. }
  121. linlist_p linlist_alloc_cell(char* payload)
  122. {
  123. linlist_p newcell = malloc(sizeof(linlist_p));
  124. char *value;
  125. newcell->payload = strdup(payload);
  126. newcell->next = NULL;
  127. return (newcell);
  128. }
  129. countlist_p countlist_alloc_cell(char* payload)
  130. {
  131. countlist_p newcell = malloc(sizeof(countlist_p));
  132. char *value;
  133. newcell->payload = strdup(payload);
  134. newcell->next = NULL;
  135. return (newcell);
  136. }
  137. linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell)
  138. {
  139. newcell->next = *anchor;
  140. *anchor = newcell;
  141. return(*anchor);
  142. }
  143. countlist_p countlist_insert_first(countlist_p *anchor, countlist_p newcell)
  144. {
  145. newcell->next = *anchor;
  146. *anchor = newcell;
  147. return(*anchor);
  148. }
  149. linlist_p linlist_extract_first(linlist_p *anchor)
  150. {
  151. linlist_p res = *anchor;
  152. if(res)
  153. {
  154. *anchor = res->next;
  155. res->next = NULL;
  156. }
  157. return res;
  158. }
  159. linlist_p linlistExtract(linlist_p* anchor,linlist_p cell){
  160. if(*anchor == NULL){
  161. printf("liste ist leer");
  162. return NULL;
  163. }
  164. if((*anchor)->next == NULL){ //wenn liste nur ein element hat
  165. if(*anchor == cell){
  166. return NULL;
  167. }
  168. else{
  169. printf("element nicht in der liste enthalten");
  170. return *anchor;
  171. }
  172. }
  173. if(*anchor == cell){
  174. linlist_p ret = linlist_extract_first(anchor);
  175. return ret;
  176. }
  177. while(*anchor != NULL && (*anchor)->next != cell){ //springe bis letztes element
  178. anchor = &(*anchor)->next;
  179. }
  180. linlist_p tmp = *anchor;
  181. if((*anchor)->next == cell){ //zelle gefunden
  182. tmp->next = cell->next;
  183. cell->next = NULL;
  184. return cell;
  185. }
  186. else{ //keine zelle gefunden
  187. return NULL;
  188. }
  189. }