c.c


SUBMITTED BY: bitcoinsachen

DATE: Nov. 22, 2016, 11:24 a.m.

FORMAT: C

SIZE: 4.5 kB

HITS: 449

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "c.h"
  5. void readfromkeyboard(Element_t *item)
  6. {
  7. if(!item)
  8. {
  9. fprintf(stderr, "Nullpointer");
  10. exit(-1);
  11. }
  12. fprintf(stderr, "Waehle Song:");
  13. fgets(item->songtitle, 255, stdin);
  14. *strrchr(item->songtitle, '\n')=0;
  15. fprintf(stderr, "Waehle interpreter:");
  16. fgets(item->interpreter, 255, stdin);
  17. *strrchr(item->interpreter, '\n')=0;
  18. }
  19. Element_t *allocateElement()
  20. {
  21. Element_t *elem = malloc(sizeof(Element_t));
  22. if (elem == 0)
  23. {
  24. fprintf(stderr, "Error:␣no␣memory␣available!\n");
  25. return 0;
  26. }
  27. readfromkeyboard(elem);
  28. elem->next = 0;
  29. return elem;
  30. }
  31. void freeList(Element_t **list)
  32. {
  33. Element_t *next;
  34. Element_t *list2=*list;
  35. while (list2 != 0)
  36. {
  37. next = list2->next;
  38. free(list2);
  39. list2=next;
  40. }
  41. list2=0;
  42. }
  43. Element_t *insertLast(Element_t *list)
  44. {
  45. Element_t *elem = allocateElement();
  46. if (list != 0)
  47. /* es wurde eine Liste uebergeben */
  48. {
  49. Element_t *end = find_end(list);
  50. /* Ende finden */
  51. end->next = elem;
  52. /* neues Element an das Ende anfuegen
  53. */
  54. }
  55. else
  56. list = elem;
  57. /* elem ist neue Liste */
  58. return list;
  59. }
  60. Element_t *find_end(Element_t *list)
  61. {
  62. while (list->next != 0)
  63. list = list->next;
  64. return list;
  65. }
  66. void print_singleElement(Element_t *element)
  67. {
  68. printf("%s-%s\n", element->songtitle, element->interpreter);
  69. }
  70. void print_entireList(Element_t *list)
  71. {
  72. printf("-------Liste-------\n");
  73. long i=0;
  74. while(list)
  75. {
  76. i++;
  77. printf("%ld. ", i);
  78. print_singleElement(list);
  79. list = list->next;
  80. }
  81. printf("\nendd-------Liste-------");
  82. }
  83. /*void print_list(Element_t *list)
  84. {
  85. printf("----------␣Single␣Linked␣List␣---------\n");
  86. while (list != 0)
  87. {
  88. printf("%ld\n", list->value);
  89. list = list->next;
  90. }
  91. printf("---------------------------------------\n");
  92. }
  93. */
  94. void print_list_reverse(Element_t *list)
  95. {
  96. if(list->next!=0)
  97. print_list_reverse(list->next);
  98. print_singleElement(list);
  99. //list=list->next;
  100. }
  101. Element_t *elementAt(Element_t *list, long index)
  102. {
  103. Element_t *l =list;
  104. long i;
  105. for(i=0; i<index-1; i++)
  106. l=l->next;
  107. return l;
  108. }
  109. long elementsCount(Element_t *list)
  110. {
  111. if(list==0)
  112. return 0;
  113. long i=1;
  114. while(list->next)
  115. {
  116. list=list->next;
  117. i++;
  118. }
  119. return i;
  120. }
  121. Element_t *removeFirst(Element_t *list)
  122. {
  123. Element_t *begin = list;
  124. while (list != 0)
  125. {
  126. if (list->next != 0)
  127. /* Das gesuchte Element ist erreicht */
  128. {
  129. begin = list->next;
  130. /* Sichern des Rests der liste, falls */
  131. /* das erste Element geloescht wird */
  132. free(list);
  133. /* Element freigeben */
  134. return begin;
  135. }
  136. else
  137. /* nicht gefunden */
  138. {
  139. list = list->next;
  140. }
  141. }
  142. fprintf(stderr, "Error:␣element␣not␣found!\n");
  143. return begin;
  144. }
  145. void insertAt(Element_t *list, long insertAfter)
  146. {
  147. Element_t *elem;
  148. if (list != 0)
  149. {
  150. list = elementAt(list,insertAfter);
  151. if (list != 0)
  152. {
  153. elem = allocateElement(); /* neues Element anlegen */
  154. if (elem == 0)
  155. {
  156. fprintf(stderr, "Error:␣no␣memory␣allocated␣for␣push_front!\n");
  157. // return list;
  158. }
  159. elem->next = list->next;
  160. /* Nachfolger von list als Nachfolger */
  161. /*
  162. von elem setzen */
  163. list->next = elem;
  164. /* elem als Nachfolger von list festlegen */
  165. }
  166. else
  167. fprintf(stderr, "Element␣not␣found\n");
  168. }
  169. }
  170. void print_list_random(Element_t *list)
  171. {
  172. if( list->next)
  173. {
  174. if(rand()<266666)
  175. {
  176. print_list_random(list->next);
  177. fprintf(stdout, "%s\t%s\n", list->songtitle, list->interpreter);
  178. }
  179. else{
  180. fprintf(stdout, "%s\t%s\n", list->songtitle, list->interpreter);
  181. print_list_random(list->next);}
  182. }
  183. else{
  184. fprintf(stdout, "%s\t%s\n", list->songtitle, list->interpreter);
  185. }
  186. }

comments powered by Disqus