Untitled


SUBMITTED BY: Guest

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

FORMAT: C

SIZE: 2.8 kB

HITS: 487

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "h.h"
  4. #include <string.h>
  5. void readFromKeyboard(Element_t *item)
  6. {
  7. if (!item)
  8. {
  9. fprintf(stderr, "Nullpointer");
  10. }
  11. else
  12. {
  13. printf("Songtitle:\n");
  14. fgets(item->songtitle,255,stdin);
  15. *strrchr(item->songtitle, '\n')=0;
  16. printf("Interpreter:\n");
  17. fgets(item->interpreter,255,stdin);
  18. *strrchr(item->interpreter, '\n')=0;
  19. }
  20. }
  21. Element_t *allocateElement()
  22. {
  23. Element_t *elem = malloc(sizeof(Element_t));
  24. if (elem == 0)
  25. {
  26. fprintf(stderr, "Error:␣no␣memory␣available!\n");
  27. return 0;
  28. }
  29. readFromKeyboard(elem);
  30. elem->next = 0;
  31. return elem;
  32. }
  33. Element_t *find_end(Element_t *list)
  34. {
  35. while (list->next != 0)
  36. list = list->next;
  37. return list;
  38. }
  39. Element_t *insertLast(Element_t *list)
  40. {
  41. Element_t *elem = allocateElement();
  42. if (list != 0)
  43. {
  44. Element_t *end = find_end(list);
  45. end->next = elem;
  46. }
  47. else
  48. list = elem;
  49. return list;
  50. }
  51. void freeList(Element_t **list)
  52. {
  53. Element_t *list2=*list;
  54. Element_t *next;
  55. while (list2 != 0)
  56. {
  57. next = list2->next;
  58. free(list2);
  59. list2 = next;
  60. }
  61. *list=0;
  62. }
  63. void print_singleElement(Element_t *element)
  64. {
  65. printf("Ausgabe:");
  66. printf("%s, %s\n", element->songtitle, element->interpreter);
  67. }
  68. void print_entireList(Element_t *list)
  69. {
  70. printf("Ausgabe alle:");
  71. long i=1;
  72. while (list)
  73. {
  74. printf("%ld: %s, %s\n", i, list->songtitle, list->interpreter);
  75. list = list->next;
  76. i++;
  77. }
  78. printf("\n");
  79. }
  80. void print_list_reverse(Element_t *list)
  81. {
  82. if (list->next)
  83. print_list_reverse(list->next);
  84. print_singleElement(list);
  85. }
  86. int main()
  87. {
  88. Element_t *list=0;
  89. long Eingabe=1;
  90. long Element;
  91. while (Eingabe!=0)
  92. {
  93. printf("1.Hinzufügen \n3.Ausgabe \n4.Ausgabe Element\n\n");
  94. printf("Auswahl:");
  95. scanf("%ld", &Eingabe);
  96. getchar();
  97. switch(Eingabe)
  98. {
  99. case 1:
  100. list=insertLast(list);
  101. break;
  102. case 3:
  103. print_entireList(list);
  104. break;
  105. case 4:
  106. {
  107. printf("Welches Element?\n");
  108. scanf("%ld", &Element);
  109. getchar();
  110. long i;
  111. for (i=0; i<Element-1; i++)
  112. {
  113. list = list->next;
  114. }
  115. print_singleElement(list);
  116. }
  117. case 5:
  118. {
  119. printf("Umgekehrt:");
  120. print_list_reverse(list);
  121. break;
  122. }
  123. }
  124. }
  125. printf("Hello world!\n");
  126. return 0;
  127. }

comments powered by Disqus