#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Student {
char name[50];
struct Student* next;
} Student;
// Erstellt nur den Speicherplatz (malloc) und setzt Namen
Student* createStudent(char* name) {
Student
* newS
= malloc(sizeof(Student
)); if (newS == NULL) return NULL;
newS->next = NULL;
return newS;
}
void printList(Student* head) {
Student* temp = head;
int count = 0;
while (temp != NULL) {
printf("%d: %s\n",count
, temp
->name
); temp = temp->next;
count++;
}
}
// --- DEINE AUFGABEN (Logik bleibt gleich!) ---
/*
* AUFGABE 1: Listen zusammenfügen
* Fall 1 (index 0): listB kommt VOR listA.
* Fall 2 (index > 0): listB kommt MITTEN in listA (nach index-1 Elementen).
*/
Student* mergeLists(Student* main_list, Student* second_list, int index) {
Student* temp_main = main_list;
Student* temp_sec = second_list;
Student* prev = NULL;
int id = 0;
// TODO: Dein Code hier
while(temp_main != NULL){
if(index == 0){
while(temp_sec->next != NULL){
temp_sec = temp_sec->next;
}
temp_sec->next = temp_main;
return second_list;
}
if(index == id){
prev->next = temp_sec;
while(temp_sec->next != NULL){
temp_sec = temp_sec->next;
}
//printf("Prev: %s, Cur: %s, Next: %s\n", prev, temp_main, temp_main->next);
temp_sec->next = temp_main;
return main_list;
}
prev = temp_main;
id++;
temp_main = temp_main->next;
}
return NULL;
}
/* * AUFGABE 2: Speicher freigeben
* Achtung: Auch wenn wir in der Main Variablen wie a1, a2 haben...
* sobald wir freeList(head) aufrufen, sind die Speicherbereiche weg!
*/
void freeList(Student* head) {
Student* temp = head;
while(head != NULL){
temp = head;
head = temp->next;
}
}
// --- MAIN MIT MANUELLER VERKETTUNG ---
int main() {
int scenario;
printf("Waehle Szenario (1-3): "); if (scanf("%d", &scenario
) != 1) return 1;
Student *listA = NULL, *listB = NULL, *result = NULL;
// Variablen für die einzelnen Schüler (wie in der Prüfung vermutet)
Student *a1, *a2, *a3, *a4;
Student *b1, *b2;
switch(scenario) {
case 1:
// Szenario 1: Einfaches Einfügen an Index 1
// Wir bauen Liste A: Alpha -> Gamma
a1 = createStudent("Tamim");
a2 = createStudent("Tanja");
a3 = createStudent("Benjamin");
a4 = createStudent("Daniel");
a1->next = a2; // Manuelle Verknüpfung!
a2->next = a3;
a3->next = a4;
listA = a1; // Der Head ist der erste Schüler
// Wir bauen Liste B: Beta
b1 = createStudent("Peter");
listB = b1;
printf("Liste A davor: "); printList
(listA
); printf("Liste B davor: "); printList
(listB
);
result = mergeLists(listA, listB, 1);
break;
case 2:
// Szenario 2: Mitten rein an Index 2
// Liste A: Uwe -> Lube -> Keps
a1 = createStudent("Uwe");
a2 = createStudent("Lube");
a3 = createStudent("Keps");
// Manuelle Verkettung:
a1->next = a2;
a2->next = a3;
listA = a1;
// Liste B: Max -> Moritz
b1 = createStudent("Max");
b2 = createStudent("Moritz");
b1->next = b2;
listB = b1;
printf("Liste A davor: "); printList
(listA
); printf("Liste B davor: "); printList
(listB
);
// B soll zwischen Lube (Index 1) und Keps (Index 2)
// Also an Position 2 eingefügt werden
result = mergeLists(listA, listB, 2);
break;
case 3:
// Szenario 3: Neuer Head (Index 0)
// Liste A: Chef -> Vize
a1 = createStudent("Chef");
a2 = createStudent("Vize");
a1->next = a2;
listA = a1;
// Liste B: Super-Chef -> Mega-Chef
b1 = createStudent("Super-Chef");
b2 = createStudent("Mega-Chef");
b1->next = b2;
listB = b1;
printf("Liste A davor: "); printList
(listA
); printf("Liste B davor: "); printList
(listB
);
result = mergeLists(listA, listB, 0);
break;
}
printList(result);
freeList(result);
printf("Speicher freigegeben.\n");
return 0;
}