mystat.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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. linlist_p anchor;
  12. linlist_p *anchor_adr = &anchor;
  13. void linlist_free_cell(linlist_p junk);
  14. void linlist_free(linlist_p *junk);
  15. linlist_p linlist_alloc_cell(char* payload);
  16. linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell);
  17. linlist_p linlist_extract_first(linlist_p *anchor);
  18. linlist_p linlistFind(char* payload, linlist_p anchor);
  19. linlist_p linlistExtract(linlist_p *anchor, linlist_p cell);
  20. void linlist_sort(linlist_p *anchor);
  21. int main() {
  22. linlist_p i;
  23. char buffer[255];
  24. char reverse_buffer[255][255];
  25. FILE *in = stdin;
  26. while (fgets(buffer, 255, in))
  27. {
  28. linlist_p newcell = linlist_alloc_cell(buffer);
  29. linlist_insert_first(anchor_adr,newcell);
  30. }
  31. linlist_sort(anchor);
  32. printf("\nSORTED OUTPUT\n");
  33. for (i = anchor; i != NULL; i = i->next)
  34. {
  35. fprintf(stdout, "%s", i->payload);
  36. }
  37. linlist_free(&anchor);
  38. }
  39. void linlist_sort(linlist_p *anchor)
  40. {
  41. bool swapped = true;
  42. linlist_p cell = anchor;
  43. linlist_p dump;
  44. while(swapped)
  45. {
  46. swapped = false;
  47. cell = anchor;
  48. while(cell->next)
  49. {
  50. if(strcmp(cell->payload,cell->next->payload) > 0)
  51. {
  52. // printf("swapping %s and %s",cell->payload,cell->next->payload);
  53. dump = cell->next->payload;
  54. cell->next->payload = cell->payload;
  55. cell->payload = dump;
  56. swapped = true;
  57. }else
  58. {
  59. }
  60. cell = cell->next;
  61. }
  62. }
  63. }
  64. linlist_p linlistExtract(linlist_p *anchor, linlist_p cell)
  65. {
  66. if(*anchor == cell)
  67. {
  68. anchor=cell->next;
  69. return cell;
  70. }else
  71. {
  72. }
  73. }
  74. linlist_p linlistFind(char* payload, linlist_p anchor)
  75. {
  76. printf("find\n");
  77. linlist_p cell = anchor;
  78. if(cell == NULL)
  79. {
  80. printf("anchor is null");
  81. return NULL;
  82. }
  83. while(cell)
  84. {
  85. printf("to search %s \n",payload);
  86. if(strcmp(payload,cell->payload))
  87. {
  88. printf("found same\n");
  89. return cell;
  90. }
  91. else
  92. {
  93. printf("found nothing\n");
  94. cell = cell->next;
  95. }
  96. }
  97. return NULL;
  98. }
  99. void linlist_free_cell(linlist_p junk)
  100. {
  101. free(junk);
  102. }
  103. void linlist_free(linlist_p *junk){
  104. if ((*junk)->next == NULL)
  105. {
  106. free(*junk);
  107. return;
  108. }
  109. else
  110. {
  111. linlist_free(&((*junk)->next));
  112. free(*junk);
  113. }
  114. }
  115. linlist_p linlist_alloc_cell(char* payload)
  116. {
  117. linlist_p newcell = malloc(sizeof(linlist_p));
  118. char *value;
  119. newcell->payload = strdup(payload);
  120. newcell->next = NULL;
  121. return (newcell);
  122. }
  123. linlist_p linlist_insert_first(linlist_p *anchor, linlist_p newcell)
  124. {
  125. newcell->next = *anchor;
  126. *anchor = newcell;
  127. return(*anchor);
  128. }
  129. linlist_p linlist_extract_first(linlist_p *anchor)
  130. {
  131. if (*anchor == NULL)
  132. {
  133. return NULL;
  134. }
  135. else
  136. {
  137. linlist_p *return_var;
  138. return_var = anchor;
  139. *anchor = (*anchor)->next;
  140. return (*return_var);
  141. }
  142. }