Compare commits
No commits in common. "master" and "main" have entirely different histories.
BIN
Spiel_Aufgabenstellung.pdf
Normal file
BIN
Spiel_Aufgabenstellung.pdf
Normal file
Binary file not shown.
57
bintree.c
57
bintree.c
@ -3,18 +3,21 @@
|
|||||||
#include "bintree.h"
|
#include "bintree.h"
|
||||||
#include "stack.h"
|
#include "stack.h"
|
||||||
|
|
||||||
/* Adds a copy of data's pointer destination to the tree using compareFct for ordering. Accepts duplicates
|
/* Fügt eine Kopie der Daten in den Baum ein, geordnet nach compareFct. Akzeptiert Duplikate,
|
||||||
if isDuplicate is NULL, otherwise ignores duplicates and sets isDuplicate to 1 (or to 0 if a new entry is added). */
|
wenn isDuplicate NULL ist, andernfalls ignoriert Duplikate und setzt isDuplicate auf 1 (oder auf 0 bei neuem Eintrag). */
|
||||||
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)
|
||||||
{
|
{
|
||||||
|
// Überprüfe ungültige Eingabeparameter
|
||||||
if (compareFct == NULL || data == NULL || dataSize == 0)
|
if (compareFct == NULL || data == NULL || dataSize == 0)
|
||||||
return root; // invalid input: do nothing
|
return root; // ungültige Eingabe: nichts tun
|
||||||
|
|
||||||
|
// Wenn der Baum leer ist, erstelle einen neuen Wurzelknoten
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
|
|
||||||
{
|
{
|
||||||
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
|
TreeNode *node = (TreeNode *)malloc(sizeof(TreeNode));
|
||||||
if (node == NULL)
|
if (node == NULL)
|
||||||
return NULL; // allocation failed
|
return NULL; // Speicherallokation fehlgeschlagen
|
||||||
|
|
||||||
node->data = malloc(dataSize);
|
node->data = malloc(dataSize);
|
||||||
if (node->data == NULL)
|
if (node->data == NULL)
|
||||||
@ -31,26 +34,29 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
// Vergleiche neue Daten mit aktueller Wurzel
|
||||||
int cmp = compareFct(data, root->data);
|
int cmp = compareFct(data, root->data);
|
||||||
|
// Wenn neue Daten kleiner sind, füge in linken Unterbaum ein
|
||||||
if (cmp < 0)
|
if (cmp < 0)
|
||||||
{
|
{
|
||||||
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
root->left = addToTree(root->left, data, dataSize, compareFct, isDuplicate);
|
||||||
}
|
}
|
||||||
|
// Wenn neue Daten größer sind, füge in rechten Unterbaum ein
|
||||||
else if (cmp > 0)
|
else if (cmp > 0)
|
||||||
{
|
{
|
||||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
}
|
}
|
||||||
else // cmp == 0 -> duplicate
|
// Wenn gleich (Duplikat)
|
||||||
|
else
|
||||||
{
|
{
|
||||||
|
// Wenn Duplikate erkannt werden sollen, setze Flag und ignoriere
|
||||||
if (isDuplicate != NULL)
|
if (isDuplicate != NULL)
|
||||||
{
|
{
|
||||||
*isDuplicate = 1;
|
*isDuplicate = 1;
|
||||||
// ignore duplicate insertion
|
|
||||||
}
|
}
|
||||||
|
// Andernfalls erlaube Duplikate durch Einfügen in rechten Unterbaum
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// duplicates allowed: insert to right subtree for stability
|
|
||||||
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
root->right = addToTree(root->right, data, dataSize, compareFct, isDuplicate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -58,25 +64,23 @@ TreeNode *addToTree(TreeNode *root, const void *data, size_t dataSize, CompareFc
|
|||||||
return root;
|
return root;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Iterates over the tree given by root in-order (ascending order).
|
/* Iteriert über den Baum in aufsteigender Reihenfolge (in-order).
|
||||||
Follows the usage of strtok: If root != NULL then create/reset iterator for that tree.
|
Verwendet die Logik von strtok: Wenn root != NULL, initialisiere/reset Iterator für diesen Baum.
|
||||||
If root == NULL, continue iteration from last position.
|
Wenn root == NULL, setze Iteration von letzter Position fort.
|
||||||
Uses stack to manage traversal state. */
|
Verwendet Stack zur Verwaltung des Traversierungs-Zustands. */
|
||||||
void *nextTreeData(TreeNode *root)
|
void *nextTreeData(TreeNode *root)
|
||||||
{
|
{
|
||||||
// static iterator state
|
// Statischer Stack zur Aufrechterhaltung des Iterator-Zustands zwischen Aufrufen
|
||||||
static StackNode *iterStack = NULL;
|
static StackNode *iterStack = NULL;
|
||||||
static TreeNode *currentRoot = NULL;
|
|
||||||
|
|
||||||
// initialize iterator for a new tree
|
// Wenn ein neuer Baum bereitgestellt wird, initialisiere den Iterator
|
||||||
if (root != NULL)
|
if (root != NULL)
|
||||||
{
|
{
|
||||||
// clear any previous iterator state
|
// Lösche vorherigen Iterator-Zustand
|
||||||
clearStack(iterStack);
|
clearStack(iterStack);
|
||||||
iterStack = NULL;
|
iterStack = NULL;
|
||||||
currentRoot = root;
|
|
||||||
|
|
||||||
// push root and all its left descendants
|
// Pushe die Wurzel und alle linken Nachfahren auf den Stack
|
||||||
TreeNode *cur = root;
|
TreeNode *cur = root;
|
||||||
while (cur != NULL)
|
while (cur != NULL)
|
||||||
{
|
{
|
||||||
@ -86,20 +90,20 @@ void *nextTreeData(TreeNode *root)
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// if user asks to continue but iterator not initialized, nothing to return
|
// Wenn Iteration fortgesetzt wird, aber kein Stack initialisiert, gib NULL zurück
|
||||||
if (iterStack == NULL)
|
if (iterStack == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// get next node
|
// Wenn Stack leer ist, keine weiteren Elemente
|
||||||
if (iterStack == NULL)
|
if (iterStack == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
// pop the top node
|
// Poppe den nächsten Knoten vom Stack (in-order-Traversierung)
|
||||||
TreeNode *node = (TreeNode *)top(iterStack);
|
TreeNode *node = (TreeNode *)top(iterStack);
|
||||||
iterStack = pop(iterStack);
|
iterStack = pop(iterStack);
|
||||||
|
|
||||||
// after popping node, push its right child and all left descendants of that right child
|
// Pushe den rechten Unterbaum des aktuellen Knotens und seine linken Nachfahren
|
||||||
TreeNode *r = node->right;
|
TreeNode *r = node->right;
|
||||||
while (r != NULL)
|
while (r != NULL)
|
||||||
{
|
{
|
||||||
@ -110,26 +114,31 @@ void *nextTreeData(TreeNode *root)
|
|||||||
return node->data;
|
return node->data;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Releases all memory resources (including data copies). */
|
/* Gibt alle Speicherressourcen frei (einschließlich Datenkopien). */
|
||||||
void clearTree(TreeNode *root)
|
void clearTree(TreeNode *root)
|
||||||
{
|
{
|
||||||
|
// Basisfall: wenn Baum leer, nichts tun
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
// Rekursiv linken und rechten Unterbaum löschen
|
||||||
if (root->left != NULL)
|
if (root->left != NULL)
|
||||||
clearTree(root->left);
|
clearTree(root->left);
|
||||||
if (root->right != NULL)
|
if (root->right != NULL)
|
||||||
clearTree(root->right);
|
clearTree(root->right);
|
||||||
|
|
||||||
|
// Daten und Knoten selbst freigeben
|
||||||
free(root->data);
|
free(root->data);
|
||||||
root->data = NULL;
|
root->data = NULL;
|
||||||
free(root);
|
free(root);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Returns the number of entries in the tree given by root. */
|
/* Gibt die Anzahl der Einträge im Baum zurück. */
|
||||||
unsigned int treeSize(const TreeNode *root)
|
unsigned int treeSize(const TreeNode *root)
|
||||||
{
|
{
|
||||||
|
// Basisfall: leerer Baum hat Größe 0
|
||||||
if (root == NULL)
|
if (root == NULL)
|
||||||
return 0;
|
return 0;
|
||||||
|
// Größe ist 1 (aktueller Knoten) plus Größen der Unterbäume
|
||||||
return 1 + treeSize(root->left) + treeSize(root->right);
|
return 1 + treeSize(root->left) + treeSize(root->right);
|
||||||
}
|
}
|
||||||
|
|||||||
BIN
highscore.o
Normal file
BIN
highscore.o
Normal file
Binary file not shown.
@ -1,5 +0,0 @@
|
|||||||
player_name;4993
|
|
||||||
player_name;4992
|
|
||||||
player_name;4990
|
|
||||||
player_name;4953
|
|
||||||
player1;3999
|
|
||||||
39
numbers.c
39
numbers.c
@ -16,27 +16,44 @@ static int compareUInt(const void *a, const void *b)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns len random numbers between 1 and 2x len in random order which are all different,
|
// --- Hilfsfunktion: Shuffle des Arrays (Fisher-Yates) ------------------
|
||||||
// except for two entries. Uses the binary search tree to avoid duplicates.
|
static void shuffle(unsigned int *array, unsigned int len)
|
||||||
|
{
|
||||||
|
for (unsigned int i = len - 1; i > 0; i--)
|
||||||
|
{
|
||||||
|
unsigned int j = rand() % (i + 1);
|
||||||
|
unsigned int temp = array[i];
|
||||||
|
array[i] = array[j];
|
||||||
|
array[j] = temp;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gibt ein Array mit len zufälligen Zahlen zwischen 1 und 2*len zurück, die alle unterschiedlich sind,
|
||||||
|
// außer zwei Einträgen (ein Duplikat). Verwendet den Binärbaum, um Duplikate zu vermeiden.
|
||||||
unsigned int *createNumbers(unsigned int len)
|
unsigned int *createNumbers(unsigned int len)
|
||||||
{
|
{
|
||||||
|
// Überprüfe ungültige Länge
|
||||||
if (len < 2)
|
if (len < 2)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
// Allokiere Speicher für das Array
|
||||||
unsigned int *arr = malloc(sizeof(unsigned int) * len);
|
unsigned int *arr = malloc(sizeof(unsigned int) * len);
|
||||||
if (!arr)
|
if (!arr)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
// Initialisiere Zufallszahlengenerator
|
||||||
srand((unsigned int)time(NULL));
|
srand((unsigned int)time(NULL));
|
||||||
|
|
||||||
TreeNode *root = NULL;
|
TreeNode *root = NULL;
|
||||||
|
|
||||||
unsigned int count = 0;
|
unsigned int count = 0;
|
||||||
while (count < len - 1) // generate len-1 UNIQUE numbers
|
// Generiere len-1 eindeutige Zahlen
|
||||||
|
while (count < len - 1)
|
||||||
{
|
{
|
||||||
unsigned int val = (rand() % (2 * len)) + 1;
|
unsigned int val = (rand() % (2 * len)) + 1;
|
||||||
|
|
||||||
int isDup = 0;
|
int isDup = 0;
|
||||||
|
// Füge in Baum ein und prüfe auf Duplikat
|
||||||
root = addToTree(root, &val, sizeof(unsigned int), compareUInt, &isDup);
|
root = addToTree(root, &val, sizeof(unsigned int), compareUInt, &isDup);
|
||||||
|
|
||||||
if (!isDup)
|
if (!isDup)
|
||||||
@ -45,28 +62,33 @@ unsigned int *createNumbers(unsigned int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// pick a random existing value to duplicate
|
// Wähle einen zufälligen bestehenden Wert als Duplikat
|
||||||
unsigned int duplicateIndex = rand() % (len - 1);
|
unsigned int duplicateIndex = rand() % (len - 1);
|
||||||
arr[len - 1] = arr[duplicateIndex];
|
arr[len - 1] = arr[duplicateIndex];
|
||||||
|
|
||||||
|
// Shuffle das Array, damit das Duplikat nicht immer am Ende steht
|
||||||
|
shuffle(arr, len);
|
||||||
|
|
||||||
|
// Baum freigeben
|
||||||
clearTree(root);
|
clearTree(root);
|
||||||
return arr;
|
return arr;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns the only number in the array that occurs twice.
|
// Gibt die einzige Zahl im Array zurück, die zweimal vorkommt.
|
||||||
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
||||||
{
|
{
|
||||||
|
// Überprüfe ungültige Eingaben
|
||||||
if (!numbers || len < 2)
|
if (!numbers || len < 2)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
// copy array
|
// Kopiere Array
|
||||||
unsigned int *copy = malloc(sizeof(unsigned int) * len);
|
unsigned int *copy = malloc(sizeof(unsigned int) * len);
|
||||||
if (!copy)
|
if (!copy)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
memcpy(copy, numbers, sizeof(unsigned int) * len);
|
memcpy(copy, numbers, sizeof(unsigned int) * len);
|
||||||
|
|
||||||
// sort
|
// Sortiere das Array (einfache Bubble-Sort)
|
||||||
for (unsigned int i = 0; i < len - 1; i++)
|
for (unsigned int i = 0; i < len - 1; i++)
|
||||||
{
|
{
|
||||||
for (unsigned int j = i + 1; j < len; j++)
|
for (unsigned int j = i + 1; j < len; j++)
|
||||||
@ -80,7 +102,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// find adjacent duplicate
|
// Finde angrenzendes Duplikat
|
||||||
unsigned int duplicate = 0;
|
unsigned int duplicate = 0;
|
||||||
for (unsigned int i = 0; i < len - 1; i++)
|
for (unsigned int i = 0; i < len - 1; i++)
|
||||||
{
|
{
|
||||||
@ -91,6 +113,7 @@ unsigned int getDuplicate(const unsigned int numbers[], unsigned int len)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Speicher freigeben
|
||||||
free(copy);
|
free(copy);
|
||||||
return duplicate;
|
return duplicate;
|
||||||
}
|
}
|
||||||
|
|||||||
68
test_bintree.c
Normal file
68
test_bintree.c
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include "bintree.h"
|
||||||
|
|
||||||
|
// Hilfsfunktion: Vergleich von int
|
||||||
|
static int compareInt(const void *a, const void *b) {
|
||||||
|
int ia = *(const int *)a;
|
||||||
|
int ib = *(const int *)b;
|
||||||
|
if (ia < ib) return -1;
|
||||||
|
if (ia > ib) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("===== TEST BINTREE =====\n");
|
||||||
|
|
||||||
|
TreeNode *root = NULL;
|
||||||
|
int isDup;
|
||||||
|
|
||||||
|
// Test 1: addToTree - Einfügen eindeutiger Werte
|
||||||
|
int val1 = 10, val2 = 5, val3 = 15;
|
||||||
|
root = addToTree(root, &val1, sizeof(int), compareInt, &isDup);
|
||||||
|
if (isDup != 0) { printf("FAIL: Erstes Einfügen sollte kein Duplikat sein\n"); return 1; }
|
||||||
|
root = addToTree(root, &val2, sizeof(int), compareInt, &isDup);
|
||||||
|
if (isDup != 0) { printf("FAIL: Zweites Einfügen sollte kein Duplikat sein\n"); return 1; }
|
||||||
|
root = addToTree(root, &val3, sizeof(int), compareInt, &isDup);
|
||||||
|
if (isDup != 0) { printf("FAIL: Drittes Einfügen sollte kein Duplikat sein\n"); return 1; }
|
||||||
|
printf("PASS: addToTree - eindeutige Werte\n");
|
||||||
|
|
||||||
|
// Test 2: addToTree - Duplikat hinzufügen
|
||||||
|
int dup = 10;
|
||||||
|
root = addToTree(root, &dup, sizeof(int), compareInt, &isDup);
|
||||||
|
if (isDup != 1) { printf("FAIL: Duplikat sollte erkannt werden\n"); return 1; }
|
||||||
|
printf("PASS: addToTree - Duplikat erkannt\n");
|
||||||
|
|
||||||
|
// Test 3: treeSize
|
||||||
|
unsigned int size = treeSize(root);
|
||||||
|
if (size != 3) { printf("FAIL: treeSize sollte 3 sein, ist %u\n", size); return 1; }
|
||||||
|
printf("PASS: treeSize\n");
|
||||||
|
|
||||||
|
// Test 4: nextTreeData - Iteration (in-order: 5, 10, 15)
|
||||||
|
void *data = nextTreeData(root);
|
||||||
|
if (data == NULL || *(int*)data != 5) { printf("FAIL: Erstes Element sollte 5 sein\n"); return 1; }
|
||||||
|
data = nextTreeData(NULL);
|
||||||
|
if (data == NULL || *(int*)data != 10) { printf("FAIL: Zweites Element sollte 10 sein\n"); return 1; }
|
||||||
|
data = nextTreeData(NULL);
|
||||||
|
if (data == NULL || *(int*)data != 15) { printf("FAIL: Drittes Element sollte 15 sein\n"); return 1; }
|
||||||
|
data = nextTreeData(NULL);
|
||||||
|
if (data != NULL) { printf("FAIL: Nach dem letzten Element sollte NULL kommen\n"); return 1; }
|
||||||
|
printf("PASS: nextTreeData - Iteration\n");
|
||||||
|
|
||||||
|
// Test 5: clearTree
|
||||||
|
clearTree(root);
|
||||||
|
root = NULL;
|
||||||
|
// Nach clearTree sollte treeSize 0 sein (aber da root NULL, testen wir indirekt)
|
||||||
|
printf("PASS: clearTree (no crash)\n");
|
||||||
|
|
||||||
|
// Test 6: Leerer Baum
|
||||||
|
size = treeSize(NULL);
|
||||||
|
if (size != 0) { printf("FAIL: Leerer Baum sollte Größe 0 haben\n"); return 1; }
|
||||||
|
data = nextTreeData(NULL);
|
||||||
|
if (data != NULL) { printf("FAIL: Leerer Baum sollte NULL zurückgeben\n"); return 1; }
|
||||||
|
printf("PASS: Leerer Baum\n");
|
||||||
|
|
||||||
|
printf("ALL BINTREE TESTS PASSED\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
test_bintree.exe
Normal file
BIN
test_bintree.exe
Normal file
Binary file not shown.
@ -0,0 +1,60 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "numbers.h"
|
||||||
|
|
||||||
|
// Einfache Funktion, um zu zählen, wie oft eine Zahl im Array vorkommt
|
||||||
|
int countOccurrences(const unsigned int *arr, unsigned int len, unsigned int value) {
|
||||||
|
int count = 0;
|
||||||
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
if (arr[i] == value) count++;
|
||||||
|
}
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Testfunktion für createNumbers und getDuplicate
|
||||||
|
void testNumbers(unsigned int len) {
|
||||||
|
printf("Teste mit Laenge %u:\n", len);
|
||||||
|
|
||||||
|
// Erstelle Zahlenarray
|
||||||
|
unsigned int *numbers = createNumbers(len);
|
||||||
|
if (numbers == NULL) {
|
||||||
|
printf("Fehler: Konnte Array nicht erstellen.\n");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Gib Array aus
|
||||||
|
printf("Generierte Zahlen: ");
|
||||||
|
for (unsigned int i = 0; i < len; i++) {
|
||||||
|
printf("%u ", numbers[i]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
|
||||||
|
// Finde Duplikat
|
||||||
|
unsigned int duplicate = getDuplicate(numbers, len);
|
||||||
|
printf("Gefundenes Duplikat: %u\n", duplicate);
|
||||||
|
|
||||||
|
// Überprüfe, ob es genau zweimal vorkommt
|
||||||
|
int occ = countOccurrences(numbers, len, duplicate);
|
||||||
|
if (occ == 2) {
|
||||||
|
printf("Korrekte Überprüfung: %u kommt genau zweimal vor.\n", duplicate);
|
||||||
|
} else {
|
||||||
|
printf("Fehler: %u kommt %d mal vor (sollte 2 sein).\n", duplicate, occ);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Speicher freigeben
|
||||||
|
free(numbers);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("Testprogramm für numbers.c\n");
|
||||||
|
printf("=========================\n\n");
|
||||||
|
|
||||||
|
// Teste mit verschiedenen Längen
|
||||||
|
testNumbers(5);
|
||||||
|
testNumbers(10);
|
||||||
|
testNumbers(20);
|
||||||
|
|
||||||
|
printf("Tests abgeschlossen.\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
test_numbers.exe
Normal file
BIN
test_numbers.exe
Normal file
Binary file not shown.
36
test_stack.c
36
test_stack.c
@ -0,0 +1,36 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include "stack.h"
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
printf("===== TEST STACK =====\n");
|
||||||
|
|
||||||
|
StackNode *s = NULL;
|
||||||
|
|
||||||
|
// Test push
|
||||||
|
int a = 10, b = 20, c = 30;
|
||||||
|
s = push(s, &a);
|
||||||
|
s = push(s, &b);
|
||||||
|
s = push(s, &c);
|
||||||
|
|
||||||
|
if (*(int*)top(s) != 30) {
|
||||||
|
printf("FAIL: top() should return 30\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("PASS: push + top\n");
|
||||||
|
|
||||||
|
// Test pop
|
||||||
|
s = pop(s);
|
||||||
|
if (*(int*)top(s) != 20) {
|
||||||
|
printf("FAIL: pop() should remove 30\n");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
printf("PASS: pop\n");
|
||||||
|
|
||||||
|
// Clear
|
||||||
|
clearStack(s);
|
||||||
|
printf("PASS: clearStack (no crash)\n");
|
||||||
|
|
||||||
|
printf("ALL STACK TESTS PASSED\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
BIN
test_stack.exe
Normal file
BIN
test_stack.exe
Normal file
Binary file not shown.
Loading…
x
Reference in New Issue
Block a user