Created
June 28, 2020 15:18
-
-
Save NicolaiSchmid/bba5b6ce4ab68283478341e98ef0fcdb to your computer and use it in GitHub Desktop.
Praxis der Programmierung 2020 Hausaufgabe
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdlib.h> | |
#include <stdio.h> | |
#define cr printf("\n") | |
struct le | |
{ | |
int value; | |
struct le *next; | |
}; | |
typedef struct le listelement; | |
typedef listelement *list; | |
void insert(int v, list *l) | |
{ | |
listelement *new; | |
new = malloc(sizeof(listelement)); | |
new->value = v; | |
new->next = *l; | |
*l = new; | |
} | |
void print_list(list l) | |
{ | |
if (l == NULL) | |
{ | |
printf("leer"); | |
return; | |
} | |
while (l != NULL) | |
{ | |
printf("%d ", l->value); | |
l = l->next; | |
} | |
} | |
void delete_all(list *l) | |
{ | |
list next; | |
while (*l != NULL) | |
{ | |
next = (*l)->next; | |
free(*l); | |
*l = next; | |
} | |
} | |
int length(list l) | |
{ | |
int count = 0; | |
while (l != NULL) | |
{ | |
count++; | |
l = l->next; | |
} | |
return count; | |
} | |
int delete_head(list *l) | |
{ | |
if (*l == NULL) | |
return -1; | |
list old = *l; | |
*l = old->next; | |
free(old); | |
return 0; | |
} | |
int contains(int v, list l) | |
{ | |
listelement *current = l; | |
int counter = 0; | |
while (current != NULL) | |
{ | |
if (current->value == v) | |
return counter; | |
current = current->next; | |
counter++; | |
} | |
return -1; | |
} | |
int insert_pos(int v, int pos, list *l) | |
{ | |
listelement *previous = NULL; | |
listelement *current = *l; | |
int counter = 0; | |
// Wrong index | |
if (pos < 0) | |
return -1; | |
while (current != NULL) | |
{ | |
if (counter == pos) | |
{ | |
listelement *new; | |
new = malloc(sizeof(listelement)); | |
new->value = v; | |
new->next = current; | |
if (counter == 0) | |
*l = new; | |
else | |
previous->next = new; | |
return 0; | |
} | |
previous = current; | |
current = current->next; | |
counter++; | |
} | |
return -1; | |
} | |
list reverse(list l) | |
{ | |
listelement *previous = NULL; | |
listelement *current = l; | |
listelement *next = NULL; | |
while (current != NULL) | |
{ | |
next = current->next; | |
current->next = previous; | |
previous = current; | |
current = next; | |
} | |
return previous; | |
} | |
int main() | |
{ | |
list l = NULL; | |
insert(3, &l); | |
insert(0, &l); | |
insert(1, &l); | |
insert(2, &l); | |
insert(3, &l); | |
print_list(l); | |
delete_head(&l); | |
print_list(l); | |
cr; | |
printf("43 inserted: %d", insert_pos(43, 1, &l)); | |
cr; | |
print_list(l); | |
cr; | |
printf("77 inserted: %d", insert_pos(77, 500, &l)); | |
cr; | |
printf("Length: %d", length(l)); | |
cr; | |
printf("Contains 44: %d", contains(44, l)); | |
cr; | |
printf("Contains 43: %d", contains(43, l)); | |
cr; | |
printf("Contains 3: %d", contains(3, l)); | |
cr; | |
printf("Reverse:\n"); | |
print_list(l); | |
cr; | |
print_list(reverse(l)); | |
cr; | |
printf("Delete all: "); | |
delete_all(&l); | |
print_list(l); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment