feat: add stack
This commit is contained in:
23
test/stack/s1.c
Normal file
23
test/stack/s1.c
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../../stack/mystack.h"
|
||||
|
||||
|
||||
void test_s1(void) {
|
||||
stack_s *stack = stack_new(32, sizeof(int));
|
||||
int num = 10;
|
||||
stack_push(stack, &num);
|
||||
|
||||
num = 13;
|
||||
stack_push(stack, &num);
|
||||
|
||||
int *rv = (int *)stack_top(stack);
|
||||
assert(*rv == 13);
|
||||
free(rv);
|
||||
|
||||
rv = (int *)stack_pop(stack);
|
||||
assert(*rv == 13);
|
||||
free(rv);
|
||||
|
||||
stack_free(stack);
|
||||
19
test/test.c
19
test/test.c
@@ -5,15 +5,17 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void test_hm1();
|
||||
void test_hm1(void);
|
||||
|
||||
void test_q1();
|
||||
void test_q1(void);
|
||||
|
||||
void test_str1();
|
||||
void test_str2();
|
||||
void test_str3();
|
||||
void test_str1(void);
|
||||
void test_str2(void);
|
||||
void test_str3(void);
|
||||
|
||||
void test_v1();
|
||||
void test_v1(void);
|
||||
|
||||
void test_s1(void);
|
||||
|
||||
int main(void) {
|
||||
puts("==== [Running Hashmap tests] ====");
|
||||
@@ -38,5 +40,10 @@ int main(void) {
|
||||
puts("OK!");
|
||||
puts("");
|
||||
|
||||
puts("==== [Running Stack tests] ====");
|
||||
test_s1();
|
||||
puts("OK!");
|
||||
puts("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "../../vector/myvector.h"
|
||||
@@ -15,11 +14,12 @@ static void multiply(size_t index, void *elem) {
|
||||
e->age = e->age * 2;
|
||||
}
|
||||
|
||||
/* Another way to use foreach */
|
||||
/* Another way to use foreach
|
||||
static void print(size_t index, void *elem) {
|
||||
my_elem_s *e = (my_elem_s *)elem;
|
||||
printf("%s (%d)\n", e->name, e->age);
|
||||
}
|
||||
*/
|
||||
|
||||
/* Compare function used to sort */
|
||||
int my_cmp(const void *a, const void *b) {
|
||||
@@ -70,13 +70,10 @@ void test_v1() {
|
||||
/* Iterate for each element */
|
||||
vec_foreach(v, multiply);
|
||||
/* Print each element */
|
||||
puts("Before sort:");
|
||||
vec_foreach(v, print);
|
||||
// vec_foreach(v, print);
|
||||
|
||||
/* Sort */
|
||||
vec_sort(v, my_cmp);
|
||||
puts("After sort:");
|
||||
vec_foreach(v, print);
|
||||
|
||||
/* Deallocate the vector */
|
||||
vec_free(v);
|
||||
|
||||
Reference in New Issue
Block a user