fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct Student {
  6. char name[50];
  7. struct Student* next;
  8. } Student;
  9.  
  10. // Erstellt nur den Speicherplatz (malloc) und setzt Namen
  11. Student* createStudent(char* name) {
  12. Student* newS = malloc(sizeof(Student));
  13. if (newS == NULL) return NULL;
  14. strcpy(newS->name, name);
  15. newS->next = NULL;
  16. return newS;
  17. }
  18.  
  19. void printList(Student* head) {
  20. Student* temp = head;
  21. printf("Liste: \n");
  22. int count = 0;
  23. while (temp != NULL) {
  24. printf("%d: %s\n",count, temp->name);
  25. temp = temp->next;
  26. count++;
  27. }
  28. printf("\n");
  29. }
  30.  
  31. // --- DEINE AUFGABEN (Logik bleibt gleich!) ---
  32.  
  33. /*
  34.  * AUFGABE 1: Listen zusammenfügen
  35.  * Fall 1 (index 0): listB kommt VOR listA.
  36.  * Fall 2 (index > 0): listB kommt MITTEN in listA (nach index-1 Elementen).
  37.  */
  38. Student* mergeLists(Student* main_list, Student* second_list, int index) {
  39. Student* temp_main = main_list;
  40. Student* temp_sec = second_list;
  41. Student* prev = NULL;
  42. int id = 0;
  43. // TODO: Dein Code hier
  44. while(temp_main != NULL){
  45. if(index == 0){
  46. while(temp_sec->next != NULL){
  47. temp_sec = temp_sec->next;
  48. }
  49. temp_sec->next = temp_main;
  50. return second_list;
  51. }
  52. if(index == id){
  53. prev->next = temp_sec;
  54. while(temp_sec->next != NULL){
  55. temp_sec = temp_sec->next;
  56. }
  57. //printf("Prev: %s, Cur: %s, Next: %s\n", prev, temp_main, temp_main->next);
  58. temp_sec->next = temp_main;
  59. return main_list;
  60. }
  61. prev = temp_main;
  62. id++;
  63. temp_main = temp_main->next;
  64. }
  65.  
  66. return NULL;
  67. }
  68.  
  69. /* * AUFGABE 2: Speicher freigeben
  70.  * Achtung: Auch wenn wir in der Main Variablen wie a1, a2 haben...
  71.  * sobald wir freeList(head) aufrufen, sind die Speicherbereiche weg!
  72.  */
  73. void freeList(Student* head) {
  74. Student* temp = head;
  75. while(head != NULL){
  76. temp = head;
  77. free(head);
  78. head = temp->next;
  79. }
  80. }
  81.  
  82. // --- MAIN MIT MANUELLER VERKETTUNG ---
  83.  
  84. int main() {
  85. int scenario;
  86. printf("Waehle Szenario (1-3): ");
  87. if (scanf("%d", &scenario) != 1) return 1;
  88.  
  89. Student *listA = NULL, *listB = NULL, *result = NULL;
  90.  
  91. // Variablen für die einzelnen Schüler (wie in der Prüfung vermutet)
  92. Student *a1, *a2, *a3, *a4;
  93. Student *b1, *b2;
  94.  
  95. switch(scenario) {
  96. case 1:
  97. // Szenario 1: Einfaches Einfügen an Index 1
  98. // Wir bauen Liste A: Alpha -> Gamma
  99. a1 = createStudent("Tamim");
  100. a2 = createStudent("Tanja");
  101. a3 = createStudent("Benjamin");
  102. a4 = createStudent("Daniel");
  103. a1->next = a2; // Manuelle Verknüpfung!
  104.  
  105. a2->next = a3;
  106. a3->next = a4;
  107. listA = a1; // Der Head ist der erste Schüler
  108.  
  109. // Wir bauen Liste B: Beta
  110. b1 = createStudent("Peter");
  111. listB = b1;
  112.  
  113. printf("Liste A davor: "); printList(listA);
  114. printf("Liste B davor: "); printList(listB);
  115.  
  116. result = mergeLists(listA, listB, 1);
  117. break;
  118.  
  119. case 2:
  120. // Szenario 2: Mitten rein an Index 2
  121. // Liste A: Uwe -> Lube -> Keps
  122. a1 = createStudent("Uwe");
  123. a2 = createStudent("Lube");
  124. a3 = createStudent("Keps");
  125.  
  126. // Manuelle Verkettung:
  127. a1->next = a2;
  128. a2->next = a3;
  129. listA = a1;
  130.  
  131. // Liste B: Max -> Moritz
  132. b1 = createStudent("Max");
  133. b2 = createStudent("Moritz");
  134. b1->next = b2;
  135. listB = b1;
  136.  
  137. printf("Liste A davor: "); printList(listA);
  138. printf("Liste B davor: "); printList(listB);
  139.  
  140. // B soll zwischen Lube (Index 1) und Keps (Index 2)
  141. // Also an Position 2 eingefügt werden
  142. result = mergeLists(listA, listB, 2);
  143. break;
  144.  
  145. case 3:
  146. // Szenario 3: Neuer Head (Index 0)
  147. // Liste A: Chef -> Vize
  148. a1 = createStudent("Chef");
  149. a2 = createStudent("Vize");
  150. a1->next = a2;
  151. listA = a1;
  152.  
  153. // Liste B: Super-Chef -> Mega-Chef
  154. b1 = createStudent("Super-Chef");
  155. b2 = createStudent("Mega-Chef");
  156. b1->next = b2;
  157. listB = b1;
  158.  
  159. printf("Liste A davor: "); printList(listA);
  160. printf("Liste B davor: "); printList(listB);
  161.  
  162. result = mergeLists(listA, listB, 0);
  163. break;
  164. }
  165.  
  166. printf("Ergebnis: ");
  167. printList(result);
  168.  
  169. freeList(result);
  170. printf("Speicher freigegeben.\n");
  171.  
  172. return 0;
  173. }
Success #stdin #stdout 0.01s 5320KB
stdin
2
stdout
Waehle Szenario (1-3): Liste A davor: Liste: 
0: Uwe
1: Lube
2: Keps

Liste B davor: Liste: 
0: Max
1: Moritz

Ergebnis:      Liste: 
0: Uwe
1: Lube
2: Max
3: Moritz
4: Keps

Speicher freigegeben.