mirror of
https://github.com/theoleuthardt/learningC.git
synced 2026-06-13 09:37:53 +00:00
Initial commit
This commit is contained in:
commit
826d4c8c9d
81 changed files with 7268 additions and 0 deletions
97
examTemplate/examTemp_modules/list.c
Normal file
97
examTemplate/examTemp_modules/list.c
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "list.h"
|
||||
|
||||
|
||||
struct list *top = NULL;
|
||||
|
||||
int isEmpty(){
|
||||
return (top == NULL);
|
||||
}
|
||||
|
||||
void Add(void *data){
|
||||
struct list *temp = (struct list *) malloc(sizeof(struct list));
|
||||
temp->data = data;
|
||||
if(top == NULL){
|
||||
temp->ptr = NULL;
|
||||
top = temp;
|
||||
return;
|
||||
}
|
||||
temp->ptr = top;
|
||||
top = temp;
|
||||
}
|
||||
|
||||
void *Get(int index){
|
||||
if (top == NULL)
|
||||
return NULL;
|
||||
|
||||
struct list *temp = top;
|
||||
for (int i = 0; i <= index; i++) {
|
||||
if (temp == NULL){
|
||||
printf("Element ist nicht enthalten!");
|
||||
return NULL;
|
||||
} else if(i == index){
|
||||
return temp->data;
|
||||
} else{
|
||||
temp = temp->ptr;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int Size(){
|
||||
if (isEmpty())
|
||||
return 0;
|
||||
|
||||
struct list *next = top->ptr;
|
||||
int size = 1;
|
||||
for (; next != NULL; next = next->ptr) {
|
||||
size++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
int Contains(void *item){
|
||||
if (top == NULL) {
|
||||
return -1;
|
||||
}
|
||||
struct list *temp = top;
|
||||
for (int i = 0; i < Size(); i++) {
|
||||
if (temp == NULL){
|
||||
printf("Element ist nicht enthalten!");
|
||||
return -1;
|
||||
} else if(item == temp->data){
|
||||
return i;
|
||||
} else{
|
||||
temp = temp->ptr;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void Remove(int index){
|
||||
if(isEmpty())
|
||||
return;
|
||||
|
||||
struct list *temp = top->ptr;
|
||||
struct list *next = top;
|
||||
|
||||
if(index == 0){
|
||||
free(top);
|
||||
top = temp;
|
||||
return;
|
||||
}
|
||||
|
||||
for(int i = 1; i <= index; i++){
|
||||
if (temp == NULL)
|
||||
return;
|
||||
else if (i == index){
|
||||
next->ptr = temp->ptr;
|
||||
free(temp);
|
||||
return;
|
||||
} else
|
||||
next = temp;
|
||||
temp = temp->ptr;
|
||||
}
|
||||
}
|
||||
16
examTemplate/examTemp_modules/list.h
Normal file
16
examTemplate/examTemp_modules/list.h
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
#ifndef EXAMTEMPLATE_LIST_H
|
||||
#define EXAMTEMPLATE_LIST_H
|
||||
|
||||
struct list{
|
||||
void *data;
|
||||
struct list *ptr;
|
||||
};
|
||||
|
||||
void Add(void *data);
|
||||
void *Get(int index);
|
||||
int Size();
|
||||
int Contains(void *item);
|
||||
void Remove(int index);
|
||||
|
||||
|
||||
#endif //EXAMTEMPLATE_LIST_H
|
||||
49
examTemplate/examTemp_modules/stack.c
Normal file
49
examTemplate/examTemp_modules/stack.c
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "stack.h"
|
||||
|
||||
struct Stack *top = NULL;
|
||||
|
||||
int isEmpty(){
|
||||
return (top == NULL);
|
||||
}
|
||||
|
||||
void Push(int item){
|
||||
struct Stack *temp = (struct Stack *) malloc(sizeof(struct Stack));
|
||||
temp->ptr = top;
|
||||
temp->data = item;
|
||||
top = temp;
|
||||
}
|
||||
|
||||
int Pop(){
|
||||
if (top == NULL){
|
||||
printf("Stack ist leer!\n");
|
||||
return -1;
|
||||
}
|
||||
struct Stack *temp = top->ptr;
|
||||
int popped = top->data;
|
||||
free(top);
|
||||
top = temp;
|
||||
return popped;
|
||||
}
|
||||
|
||||
int Top(){
|
||||
if(isEmpty()) {
|
||||
printf("Stack ist leer!\n");
|
||||
return -1;
|
||||
}
|
||||
return top->data;
|
||||
}
|
||||
|
||||
int Size(){
|
||||
if(isEmpty())
|
||||
return 0;
|
||||
|
||||
struct Stack *next = top->ptr;
|
||||
int size = 1;
|
||||
for (; next != NULL; next = next->ptr) {
|
||||
size++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
15
examTemplate/examTemp_modules/stack.h
Normal file
15
examTemplate/examTemp_modules/stack.h
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
#ifndef EXAMTEMPLATE_STACK_H
|
||||
#define EXAMTEMPLATE_STACK_H
|
||||
|
||||
struct Stack{
|
||||
int data;
|
||||
struct Stack *ptr;
|
||||
};
|
||||
|
||||
int isEmpty();
|
||||
void Push(int item);
|
||||
int Pop();
|
||||
int Top();
|
||||
int Size();
|
||||
|
||||
#endif //EXAMTEMPLATE_STACK_H
|
||||
130
examTemplate/main.c
Normal file
130
examTemplate/main.c
Normal file
|
|
@ -0,0 +1,130 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "modules/list.h"
|
||||
|
||||
void menu() {
|
||||
int iteration = 1;
|
||||
int option;
|
||||
|
||||
printf("Willkommen im X! Wähle eine Option 1 bis X zum Ausführen.\n");
|
||||
printf("(1): Option 1\n");
|
||||
printf("(2): Option 2\n");
|
||||
printf("(3): Option 3\n");
|
||||
printf("(4): Option 4\n");
|
||||
printf("(5): Option 5\n");
|
||||
printf("(6): Beenden\n");
|
||||
printf("Auswahl: ");
|
||||
scanf("%d", &option);
|
||||
printf("\n");
|
||||
|
||||
switch (option) {
|
||||
case 1:
|
||||
// Case 1
|
||||
printf("\n");
|
||||
iteration++;
|
||||
break;
|
||||
case 2:
|
||||
// Case 2
|
||||
iteration++;
|
||||
break;
|
||||
case 3:
|
||||
// Case 3
|
||||
iteration++;
|
||||
break;
|
||||
case 4:
|
||||
// Case 4
|
||||
iteration++;
|
||||
break;
|
||||
case 5:
|
||||
// Case 5
|
||||
iteration++;
|
||||
break;
|
||||
case 6:
|
||||
break;
|
||||
default:
|
||||
printf("Bitte gib eine Zahl zwischen 1 und X ein!");
|
||||
break;
|
||||
}
|
||||
|
||||
while(iteration > 1) {
|
||||
printf("Weitere Option ausführen? \n");
|
||||
printf("(1): Option 1\n");
|
||||
printf("(2): Option 2\n");
|
||||
printf("(3): Option 3\n");
|
||||
printf("(4): Option 4\n");
|
||||
printf("(5): Option 5\n");
|
||||
printf("(6): Beenden\n");
|
||||
printf("Auswahl: ");
|
||||
scanf("%d", &option);
|
||||
printf("\n");
|
||||
|
||||
switch (option) {
|
||||
case 1:
|
||||
// Case 1
|
||||
printf("\n");
|
||||
iteration++;
|
||||
break;
|
||||
case 2:
|
||||
// Case 2
|
||||
iteration++;
|
||||
break;
|
||||
case 3:
|
||||
// Case 3
|
||||
iteration++;
|
||||
break;
|
||||
case 4:
|
||||
// Case 4
|
||||
iteration++;
|
||||
break;
|
||||
case 5:
|
||||
// Case 5
|
||||
iteration++;
|
||||
break;
|
||||
case 6:
|
||||
return;
|
||||
default:
|
||||
iteration++;
|
||||
printf("Bitte gib eine Zahl zwischen 1 und X ein!");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
// Befüllung mit verschiedenen Daten
|
||||
int number1 = 255;
|
||||
int number2 = rand() % 500;
|
||||
int number3 = rand() % 500;
|
||||
int number4 = rand() % 500;
|
||||
int number5 = rand() % 500;
|
||||
|
||||
Add(&number1);
|
||||
Add(&number2);
|
||||
Add(&number3);
|
||||
Add(&number4);
|
||||
Add(&number5);
|
||||
|
||||
// Ausgabe der Liste
|
||||
printf("%s%d\n", "Item 0: ", *(int *) Get(0));
|
||||
printf("%s%d\n", "Item 1: ", *(int *) Get(1));
|
||||
printf("%s%d\n", "Item 2: ", *(int *) Get(2));
|
||||
printf("%s%d\n", "Item 3: ", *(int *) Get(3));
|
||||
printf("%s%d\n", "Item 4: ", *(int *) Get(4));
|
||||
|
||||
// Contains Überprüfung
|
||||
if(Contains((void *) 255) != -1)
|
||||
printf("%d", "Die Zahl 255 ist am Index: ", *(int *) Contains((void *) 255));
|
||||
|
||||
// Size von list
|
||||
printf("Anzahl der Elemente in der Liste: ");
|
||||
printf("%d\n", Size());
|
||||
|
||||
// Removetest
|
||||
Remove(0);
|
||||
Remove(3);
|
||||
printf("%s%d\n", "Item 0: ", *(int *) Get(0));
|
||||
printf("%s%d\n", "Item 1: ", *(int *) Get(1));
|
||||
printf("%s%d\n", "Item 2: ", *(int *) Get(2));
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue