add some files

This commit is contained in:
fonkou 2025-12-02 15:23:22 +01:00
parent 2801e4b709
commit d264a7513e
5 changed files with 76 additions and 56 deletions

View File

@ -10,11 +10,7 @@
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
// 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) {
// allocate new node
TreeNode *node = malloc(sizeof(TreeNode));

View File

@ -0,0 +1 @@
kamte;2990

122
numbers.c
View File

@ -1,9 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include "numbers.h"
#include "bintree.h"
//TODO: getDuplicate und createNumbers implementieren
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
@ -14,77 +13,100 @@
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers.
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors.
unsigned int *createNumbers(unsigned int len)
{
if (len < 2) return NULL;
if (len < 2) return NULL;
unsigned int *numbers = malloc(len * sizeof(unsigned int));
if (!numbers) return NULL;
// Allocate memory for the array
unsigned int *numbers = malloc(len * sizeof(unsigned int));
if (!numbers) return NULL;
srand((unsigned int)time(NULL));
// Initialize random number generator
srand((unsigned int)time(NULL));
TreeNode *root = NULL;
unsigned int maxValue = 2 * len;
// We need to ensure len-1 unique numbers in range [1, 2*len]
unsigned int maxValue = 2 * len;
unsigned int uniqueCount = len - 1; // We'll generate len-1 unique values
unsigned int uniqueCount = len - 1; // we generate len-1 unique values
unsigned int i = 0;
// Generate unique numbers using a simple linear search approach
for (unsigned int i = 0; i < uniqueCount; i++) {
int isUnique;
unsigned int candidate;
// Fill len - 1 unique values using BST
while (i < uniqueCount) {
unsigned int r = (rand() % maxValue) + 1;
int isDup = 0;
// Keep generating until we find a unique number
do {
candidate = (rand() % maxValue) + 1; // Random number in [1, 2*len]
isUnique = 1;
root = addToTree(root, &r, sizeof(unsigned int), compareUnsigned, &isDup);
// Check if candidate already exists in our array so far
for (unsigned int j = 0; j < i; j++) {
if (numbers[j] == candidate) {
isUnique = 0;
break;
}
}
} while (!isUnique);
if (!isDup) {
numbers[i] = r;
i++;
numbers[i] = candidate;
}
}
// Pick one number already in the list → duplicate it
unsigned int duplicateIndex = rand() % uniqueCount;
unsigned int duplicateValue = numbers[duplicateIndex];
// Now we have len-1 unique numbers. Duplicate one of them.
// Choose a random index from the unique numbers
unsigned int duplicateIndex = rand() % uniqueCount;
unsigned int duplicateValue = numbers[duplicateIndex];
numbers[len - 1] = duplicateValue;
// Add the duplicate at the last position
numbers[len - 1] = duplicateValue;
// Cleanup tree
clearTree(root);
// Shuffle the entire array to randomize the position of the duplicate
for (unsigned int i = 0; i < len; i++) {
unsigned int swapIndex = rand() % len;
// Shuffle array for randomness
for (unsigned int j = 0; j < len; j++) {
unsigned int k = rand() % len;
unsigned int tmp = numbers[j];
numbers[j] = numbers[k];
numbers[k] = tmp;
}
// Swap numbers[i] and numbers[swapIndex]
unsigned int temp = numbers[i];
numbers[i] = numbers[swapIndex];
numbers[swapIndex] = temp;
}
return numbers;
return numbers;
}
// Returns only the only number in numbers which is present twice. Returns zero on errors.
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
{
if (!numbers || len < 2) return 0;
if (!numbers || len < 2) return 0;
// Make a copy because qsort modifies the array
unsigned int *copy = malloc(len * sizeof(unsigned int));
if (!copy) return 0;
// Create a copy of the array since we need to sort it
unsigned int *copy = malloc(len * sizeof(unsigned int));
if (!copy) return 0;
memcpy(copy, numbers, len * sizeof(unsigned int));
memcpy(copy, numbers, len * sizeof(unsigned int));
qsort(copy, len, sizeof(unsigned int), compareUnsigned);
// Check adjacent elements for equality
for (unsigned int i = 0; i < len - 1; i++) {
if (copy[i] == copy[i + 1]) {
unsigned int result = copy[i];
free(copy);
return result;
// Simple bubble sort implementation (no external function dependencies)
for (unsigned int i = 0; i < len - 1; i++) {
for (unsigned int j = 0; j < len - i - 1; j++) {
if (copy[j] > copy[j + 1]) {
// Swap if out of order
unsigned int temp = copy[j];
copy[j] = copy[j + 1];
copy[j + 1] = temp;
}
}
}
}
free(copy);
return 0; // no duplicate found (should not happen)
// Now find the duplicate by checking adjacent elements
unsigned int duplicate = 0;
for (unsigned int i = 0; i < len - 1; i++) {
if (copy[i] == copy[i + 1]) {
duplicate = copy[i];
break;
}
}
free(copy);
return duplicate;
}

View File

@ -4,6 +4,7 @@
// Returns len random numbers between 1 and 2x len in random order which are all different, except for two entries.
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
// creating random numbers.
unsigned int *createNumbers(unsigned int len);
// Returns only the only number in numbers which is present twice. Returns zero on errors.

View File

@ -6,7 +6,7 @@ This means that with each new element all other elements are pushed deeper into
The latest element is taken from the stack. */
#include <stdlib.h>
typedef struct Stack_node StackNode;
//TODO: passenden Datentyp als struct anlegen
// Pushes data as pointer onto the stack.