add some files
This commit is contained in:
parent
2801e4b709
commit
d264a7513e
@ -10,11 +10,7 @@
|
|||||||
|
|
||||||
// Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
// 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).
|
// 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,
|
TreeNode *addToTree(TreeNode *root,const void *data,size_t dataSize,CompareFctType compareFct,int *isDuplicate) {
|
||||||
const void *data,
|
|
||||||
size_t dataSize,
|
|
||||||
CompareFctType compareFct,
|
|
||||||
int *isDuplicate) {
|
|
||||||
if (!root) {
|
if (!root) {
|
||||||
// allocate new node
|
// allocate new node
|
||||||
TreeNode *node = malloc(sizeof(TreeNode));
|
TreeNode *node = malloc(sizeof(TreeNode));
|
||||||
|
|||||||
@ -0,0 +1 @@
|
|||||||
|
kamte;2990
|
||||||
122
numbers.c
122
numbers.c
@ -1,9 +1,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include "numbers.h"
|
#include "numbers.h"
|
||||||
#include "bintree.h"
|
|
||||||
|
|
||||||
//TODO: getDuplicate und createNumbers implementieren
|
//TODO: getDuplicate und createNumbers implementieren
|
||||||
/* * * Erzeugen eines Arrays mit der vom Nutzer eingegebenen Anzahl an Zufallszahlen.
|
/* * * 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 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
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// 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)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
if (len < 2) return NULL;
|
if (len < 2) return NULL;
|
||||||
|
|
||||||
unsigned int *numbers = malloc(len * sizeof(unsigned int));
|
// Allocate memory for the array
|
||||||
if (!numbers) return NULL;
|
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;
|
// We need to ensure len-1 unique numbers in range [1, 2*len]
|
||||||
unsigned int maxValue = 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
|
// Generate unique numbers using a simple linear search approach
|
||||||
unsigned int i = 0;
|
for (unsigned int i = 0; i < uniqueCount; i++) {
|
||||||
|
int isUnique;
|
||||||
|
unsigned int candidate;
|
||||||
|
|
||||||
// Fill len - 1 unique values using BST
|
// Keep generating until we find a unique number
|
||||||
while (i < uniqueCount) {
|
do {
|
||||||
unsigned int r = (rand() % maxValue) + 1;
|
candidate = (rand() % maxValue) + 1; // Random number in [1, 2*len]
|
||||||
int isDup = 0;
|
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] = candidate;
|
||||||
numbers[i] = r;
|
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Pick one number already in the list → duplicate it
|
// Now we have len-1 unique numbers. Duplicate one of them.
|
||||||
unsigned int duplicateIndex = rand() % uniqueCount;
|
// Choose a random index from the unique numbers
|
||||||
unsigned int duplicateValue = numbers[duplicateIndex];
|
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
|
// Shuffle the entire array to randomize the position of the duplicate
|
||||||
clearTree(root);
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
unsigned int swapIndex = rand() % len;
|
||||||
|
|
||||||
// Shuffle array for randomness
|
// Swap numbers[i] and numbers[swapIndex]
|
||||||
for (unsigned int j = 0; j < len; j++) {
|
unsigned int temp = numbers[i];
|
||||||
unsigned int k = rand() % len;
|
numbers[i] = numbers[swapIndex];
|
||||||
unsigned int tmp = numbers[j];
|
numbers[swapIndex] = temp;
|
||||||
numbers[j] = numbers[k];
|
}
|
||||||
numbers[k] = tmp;
|
|
||||||
}
|
|
||||||
|
|
||||||
return numbers;
|
return numbers;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 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.
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
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
|
// Create a copy of the array since we need to sort it
|
||||||
unsigned int *copy = malloc(len * sizeof(unsigned int));
|
unsigned int *copy = malloc(len * sizeof(unsigned int));
|
||||||
if (!copy) return 0;
|
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);
|
// Simple bubble sort implementation (no external function dependencies)
|
||||||
|
for (unsigned int i = 0; i < len - 1; i++) {
|
||||||
// Check adjacent elements for equality
|
for (unsigned int j = 0; j < len - i - 1; j++) {
|
||||||
for (unsigned int i = 0; i < len - 1; i++) {
|
if (copy[j] > copy[j + 1]) {
|
||||||
if (copy[i] == copy[i + 1]) {
|
// Swap if out of order
|
||||||
unsigned int result = copy[i];
|
unsigned int temp = copy[j];
|
||||||
free(copy);
|
copy[j] = copy[j + 1];
|
||||||
return result;
|
copy[j + 1] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
free(copy);
|
// Now find the duplicate by checking adjacent elements
|
||||||
return 0; // no duplicate found (should not happen)
|
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 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
|
// Returns NULL on errors. Use your implementation of the binary search tree to check for possible duplicates while
|
||||||
// creating random numbers.
|
// creating random numbers.
|
||||||
|
|
||||||
unsigned int *createNumbers(unsigned int len);
|
unsigned int *createNumbers(unsigned int len);
|
||||||
|
|
||||||
// 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.
|
||||||
|
|||||||
2
stack.h
2
stack.h
@ -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. */
|
The latest element is taken from the stack. */
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
typedef struct Stack_node StackNode;
|
||||||
//TODO: passenden Datentyp als struct anlegen
|
//TODO: passenden Datentyp als struct anlegen
|
||||||
|
|
||||||
// Pushes data as pointer onto the stack.
|
// Pushes data as pointer onto the stack.
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user