132 lines
3.2 KiB
C
132 lines
3.2 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
#define LEN 100
|
|
|
|
typedef struct listElement {
|
|
char name[LEN];
|
|
struct listElement *next;
|
|
} listElement;
|
|
|
|
typedef struct {
|
|
listElement *head;
|
|
} liste;
|
|
|
|
listElement *eingabeName() {
|
|
listElement *name = malloc(sizeof(listElement));
|
|
if (name == NULL) {
|
|
printf("Speicher konnte nicht zugewiesen werden.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
|
|
printf("Name: ");
|
|
fgets(name->name, LEN, stdin);
|
|
name->next = NULL;
|
|
|
|
if (strcmp(name->name, "\n") == 0) {
|
|
free(name);
|
|
return NULL;
|
|
}
|
|
|
|
if (name->name[strlen(name->name) - 1] == '\n') {
|
|
name->name[strlen(name->name) - 1] = '\0';
|
|
}
|
|
|
|
return name;
|
|
}
|
|
|
|
void einfuegenName(listElement *newElement, liste *liste1, liste *liste2) {
|
|
int index;
|
|
printf("\tWelche Liste (1 oder 2): ");
|
|
if (scanf("%d", &index) != 1 || index < 1 || index > 2) {
|
|
printf("Falsche Eingabe.\n");
|
|
exit(EXIT_FAILURE);
|
|
}
|
|
getchar();
|
|
|
|
listElement **targetList = (index == 1) ? &liste1->head : &liste2->head;
|
|
|
|
listElement *temp = *targetList;
|
|
if (temp == NULL) {
|
|
*targetList = newElement;
|
|
} else {
|
|
while (temp->next != NULL) {
|
|
temp = temp->next;
|
|
}
|
|
temp->next = newElement;
|
|
}
|
|
}
|
|
|
|
void sortiereListe(liste *lst) {
|
|
if (!lst->head) return;
|
|
listElement *current, *index;
|
|
char temp[LEN];
|
|
|
|
for (current = lst->head; current->next != NULL; current = current->next) {
|
|
for (index = current->next; index != NULL; index = index->next) {
|
|
if (strcmp(current->name, index->name) > 0) {
|
|
strcpy(temp, current->name);
|
|
strcpy(current->name, index->name);
|
|
strcpy(index->name, temp);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void mergeLists(liste *liste1, liste *liste2, liste *mergedList) {
|
|
listElement *temp;
|
|
|
|
for (temp = liste1->head; temp != NULL; temp = temp->next) {
|
|
listElement *newElement = malloc(sizeof(listElement));
|
|
strcpy(newElement->name, temp->name);
|
|
newElement->next = mergedList->head;
|
|
mergedList->head = newElement;
|
|
}
|
|
|
|
for (temp = liste2->head; temp != NULL; temp = temp->next) {
|
|
listElement *newElement = malloc(sizeof(listElement));
|
|
strcpy(newElement->name, temp->name);
|
|
newElement->next = mergedList->head;
|
|
mergedList->head = newElement;
|
|
}
|
|
}
|
|
|
|
void printliste(liste *lst) {
|
|
listElement *temp = lst->head;
|
|
while (temp != NULL) {
|
|
printf("%s\n", temp->name);
|
|
temp = temp->next;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
liste liste1 = {NULL};
|
|
liste liste2 = {NULL};
|
|
liste gemischteListe = {NULL};
|
|
|
|
listElement *name;
|
|
while ((name = eingabeName()) != NULL) {
|
|
einfuegenName(name, &liste1, &liste2);
|
|
}
|
|
|
|
sortiereListe(&liste1);
|
|
sortiereListe(&liste2);
|
|
|
|
mergeLists(&liste1, &liste2, &gemischteListe);
|
|
sortiereListe(&gemischteListe);
|
|
|
|
printf("\n......1. Liste......\n");
|
|
printliste(&liste1);
|
|
printf("....Listenende....\n");
|
|
|
|
printf("\n......2. Liste......\n");
|
|
printliste(&liste2);
|
|
printf("....Listenende....\n");
|
|
|
|
printf("\n....Gemischte Liste....\n");
|
|
printliste(&gemischteListe);
|
|
printf("....Listenende....\n");
|
|
|
|
return 0;
|
|
}} |