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
18
uebung5/aufgabe1.c
Normal file
18
uebung5/aufgabe1.c
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <stdio.h>
|
||||
|
||||
int main(){
|
||||
int i = 10;
|
||||
int *j = &i;
|
||||
char a = 'a';
|
||||
char *b = &a;
|
||||
double d = 9.23334;
|
||||
double *e = &d;
|
||||
|
||||
printf("%p\n", j);
|
||||
printf("%d\n", *j);
|
||||
printf("%p\n", b);
|
||||
printf("%c\n", *b);
|
||||
printf("%p\n", e);
|
||||
printf("%f\n", *e);
|
||||
return 0;
|
||||
}
|
||||
53
uebung5/aufgabe2.c
Normal file
53
uebung5/aufgabe2.c
Normal file
|
|
@ -0,0 +1,53 @@
|
|||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
#include <limits.h>
|
||||
|
||||
struct LargeDataStructure {
|
||||
int bigINT;
|
||||
long bigLONG;
|
||||
float bigFLOAT;
|
||||
long double bigLongDOUBLE;
|
||||
unsigned long long bigUnsLONG;
|
||||
};
|
||||
|
||||
void DatastructureProcessByValue(struct LargeDataStructure daten){
|
||||
long double oneStep = daten.bigLongDOUBLE;
|
||||
}
|
||||
|
||||
void DatastructureProcessByPointer(struct LargeDataStructure *pdaten){
|
||||
long double oneStep = pdaten->bigLongDOUBLE;
|
||||
}
|
||||
|
||||
int main(){
|
||||
struct LargeDataStructure BIG;
|
||||
clock_t start, end;
|
||||
|
||||
// Befüllung von BIG
|
||||
BIG.bigINT = INT_MAX;
|
||||
BIG.bigLONG = LONG_MAX;
|
||||
BIG.bigFLOAT = 213.2131313342534545;
|
||||
BIG.bigLongDOUBLE = LONG_MAX;
|
||||
BIG.bigUnsLONG = ULLONG_MAX;
|
||||
|
||||
|
||||
// 10000-Mal einer Prozedur eine große Datenstruktur übergeben als Wert
|
||||
start = clock();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
DatastructureProcessByValue(BIG);
|
||||
}
|
||||
end = clock();
|
||||
printf("Zeit für die Verarbeitung als regulärer Parameter: %f Sekunden\n", ((double)(end - start)) / CLOCKS_PER_SEC);
|
||||
|
||||
|
||||
|
||||
// 10000-Mal einer Prozedur eine große Datenstruktur übergeben als Pointer
|
||||
start = clock();
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
DatastructureProcessByPointer(&BIG);
|
||||
}
|
||||
end = clock();
|
||||
printf("Zeit für die Verarbeitung als Pointer: %f Sekunden\n", ((double)(end - start)) / CLOCKS_PER_SEC);
|
||||
|
||||
// Fazit: byValueTime=0.00095sec und byPointerTime=0.000056sec → Verarbeitung geht schneller mit Pointern!
|
||||
return 0;
|
||||
}
|
||||
76
uebung5/aufgabe3.c
Normal file
76
uebung5/aufgabe3.c
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
|
||||
struct Stack{
|
||||
int data;
|
||||
struct Stack *ptr;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
int main(){
|
||||
// Hinzufügen von Beispieldaten
|
||||
Push(1);
|
||||
Push(2);
|
||||
Push(3);
|
||||
|
||||
// Anzeige des obersten Elements und der Größe vom Stack
|
||||
printf("Top-Element: %d\n", Top());
|
||||
printf("Stack-Größe: %d\n", Size());
|
||||
|
||||
// Entfernung eines Elements
|
||||
int popped = Pop();
|
||||
if (popped != -1) {
|
||||
printf("Entferntes Element: %d\n", popped);
|
||||
}
|
||||
|
||||
// Anzeige des obersten Elements und der Größe vom Stack nach der Entfernung
|
||||
printf("Top-Element nach Pop: %d\n", Top());
|
||||
printf("Stack-Größe nach Pop: %d\n", Size());
|
||||
|
||||
return 0;
|
||||
}
|
||||
111
uebung5/aufgabe4.c
Normal file
111
uebung5/aufgabe4.c
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct list{
|
||||
int data;
|
||||
struct list *ptr;
|
||||
};
|
||||
|
||||
struct list *top = NULL;
|
||||
|
||||
int isEmpty(){
|
||||
return (top == NULL);
|
||||
}
|
||||
|
||||
void Add(int item){
|
||||
struct list *temp = (struct list *) malloc(sizeof(struct list));
|
||||
temp->ptr = top;
|
||||
temp->data = item;
|
||||
top = temp;
|
||||
|
||||
if(item >= top->data){
|
||||
temp->ptr = top;
|
||||
top = temp;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
int Get(int index){
|
||||
if (isEmpty())
|
||||
return 0;
|
||||
|
||||
struct list *temp = top;
|
||||
for (int i = 0; i < index; i++) {
|
||||
if (temp == NULL){
|
||||
printf("Element ist nicht enthalten!");
|
||||
return -1;
|
||||
} else if(i == index){
|
||||
return temp->data;
|
||||
} 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;
|
||||
}
|
||||
}
|
||||
|
||||
int Size(){
|
||||
if (isEmpty())
|
||||
return 0;
|
||||
|
||||
struct list *next = top->ptr;
|
||||
int size = 1;
|
||||
for (; next != NULL; next = next->ptr) {
|
||||
size++;
|
||||
}
|
||||
return size;
|
||||
}
|
||||
|
||||
void printList(){
|
||||
printf("Liste: [");
|
||||
for (int i = 0; i <= Size(); i++) {
|
||||
printf("%d", Get(i));
|
||||
printf(", ");
|
||||
}
|
||||
printf("]\n");
|
||||
}
|
||||
|
||||
int main(){
|
||||
// Hinzufügen von 1000 zufälligen Werten
|
||||
for (int i = 0; i < 1000; i++) {
|
||||
Add(rand() % 10000);
|
||||
}
|
||||
|
||||
// Größe der Liste
|
||||
printf("\nGröße von List: ");
|
||||
printf("%d\n", Size());
|
||||
|
||||
// Ausgeben der Liste
|
||||
printList();
|
||||
|
||||
// Ausgabe eines Elements
|
||||
printf("\nElement an der Stelle 100: ");
|
||||
printf("%d\n", Get(100));
|
||||
return 0;
|
||||
}
|
||||
140
uebung5/aufgabe5.c
Normal file
140
uebung5/aufgabe5.c
Normal file
|
|
@ -0,0 +1,140 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
struct list{
|
||||
void *data;
|
||||
struct list *ptr;
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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: ", Contains((void *) 255));
|
||||
|
||||
// Size von list
|
||||
printf("Anzahl der Elemente in der Liste: ");
|
||||
printf("%d\n", Size( ));
|
||||
|
||||
// Removetest
|
||||
Remove(0);
|
||||
Remove(4);
|
||||
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));
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue