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
|
||||
Loading…
Add table
Add a link
Reference in a new issue