stack Erklaerungen, numbers und bintree etwas angefangen

This commit is contained in:
Simon Dietrich 2025-12-04 12:07:28 +01:00
parent 41e01c3af3
commit 9437f26146
3 changed files with 36 additions and 4 deletions

View File

@ -12,8 +12,23 @@
// if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). // if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added).
TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate) TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFctType compareFct, int *isDuplicate)
{ {
if(root == NULL){
TreeNode *node;
node = (TreeNode*)malloc(sizeof(TreeNode));
if(node == NULL){
return NULL;
}
node->data = malloc(dataSize);
if(node->data == NULL){
free(node);
return NULL;
}
memcpy(node->data, data, dataSize);
node->left;
node->right;
} }
}
// Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction. // Iterates over the tree given by root. Follows the usage of strtok. If tree is NULL, the next entry of the last tree given is returned in ordering direction.
// Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element, // Use your implementation of a stack to organize the iterator. Push the root node and all left nodes first. On returning the next element,

View File

@ -16,7 +16,17 @@
// creating random numbers. // creating random numbers.
unsigned int *createNumbers(unsigned int len) unsigned int *createNumbers(unsigned int len)
{ {
if(len < 2)
return NULL;
srand(time(NULL));
int count = 0;
while(count < len - 1){
int value = (rand() % (2 * len)) + 1;
}
} }
// Returns only the only number in numbers which is present twice. Returns zero on errors. // Returns only the only number in numbers which is present twice. Returns zero on errors.

View File

@ -8,12 +8,16 @@
* `clearStack`: gibt den gesamten Speicher frei. */ * `clearStack`: gibt den gesamten Speicher frei. */
// Pushes data as pointer onto the stack. // Pushes data as pointer onto the stack.
StackNode *push(StackNode *stack, void *data) StackNode *push(StackNode *stack, void *data)
{ {
//Neue Node wird erstellt, Speicher wird zugewiesen
StackNode *newNode = (StackNode *)malloc(sizeof(StackNode)); StackNode *newNode = (StackNode *)malloc(sizeof(StackNode));
if (newNode == NULL) { if (newNode == NULL) {
return NULL; return NULL;
} }
//Data und der zeiger auf den nächsten Node werden zugewiesen
newNode->data = data; newNode->data = data;
newNode->next = stack; newNode->next = stack;
return newNode; return newNode;
@ -23,9 +27,11 @@ StackNode *push(StackNode *stack, void *data)
// freed by caller.) // freed by caller.)
StackNode *pop(StackNode *stack) StackNode *pop(StackNode *stack)
{ {
//Wenn Stack leer, wird NULL zurückgegeben
if(stack == NULL) { if(stack == NULL) {
return NULL; return NULL;
} }
//Nächster Node wird gespeichert, aktuellerr Node wird freigegeben
StackNode *next = stack->next; StackNode *next = stack->next;
free(stack); free(stack);
return next; return next;
@ -43,6 +49,7 @@ if(stack == NULL) {
// Clears stack and releases all memory. // Clears stack and releases all memory.
void clearStack(StackNode *stack) void clearStack(StackNode *stack)
{ {
//Speichert den aktuellen Node, freed ihn dann und geht zum nächsten
StackNode *temp; StackNode *temp;
while(stack != NULL) { while(stack != NULL) {
temp = stack; temp = stack;