Commit 1d0cd78
Changed files (4)
assignments/01/README.md
@@ -13,7 +13,8 @@ You must score at least 50 to pass the assignment.
* The `size()` method is used to determine how many items are in the queue.
* The `add(x)` function is linear time O(n).
* The `deleteMin(x)` function is constant time O(1).
- * b. Implement the stack methods `push(x)` and `pop()` using two queues. Analyze the running time of the `push(x)` and `pop()` operations based on this implementation.
+ * b. Implement the stack methods `push(x)` and `pop()` using two queues.
+ Analyze the running time of the `push(x)` and `pop()` operations based on this implementation.
2. Swap two adjacent elements in a list by adjusting only the links (and not the data) using:
* a. singly-linked list.
* b. doubly-linked list.
assignments/01/stack_test.c
@@ -0,0 +1,53 @@
+#include <cgreen/cgreen.h>
+
+/*
+Implement the stack methods using two queues
+
+* `push(x)`
+* `pop()`
+
+Analyze the running time of the `push(x)` and `pop()` operations based on this implementation.
+*/
+
+typedef struct {
+} Stack;
+
+static Stack *initialize() {
+ Stack *stack = malloc(sizeof(Stack));
+ return stack;
+}
+
+static void push(Stack *stack, int data) {
+}
+
+static int pop(Stack *stack) {
+ return 0;
+}
+
+static void destroy(Stack *stack) {
+ free(stack);
+}
+
+Describe(Stack);
+BeforeEach(Stack){ }
+AfterEach(Stack){ }
+
+Ensure(Stack, returns_last_item_pushed_to_stack) {
+ Stack *stack = initialize();
+
+ push(stack, 1);
+ push(stack, 2);
+
+ assert_that(pop(stack), is_equal_to(2));
+ assert_that(pop(stack), is_equal_to(1));
+
+ destroy(stack);
+}
+
+TestSuite *stack_tests() {
+ TestSuite *suite = create_test_suite();
+
+ add_test_with_context(suite, Stack, returns_last_item_pushed_to_stack);
+
+ return suite;
+}
main.c
@@ -2,12 +2,14 @@
TestSuite *words_tests();
TestSuite *priority_queue_tests();
+TestSuite *stack_tests();
int main(int argc, char **argv) {
TestSuite *suite = create_test_suite();
add_suite(suite, words_tests());
add_suite(suite, priority_queue_tests());
+ add_suite(suite, stack_tests());
if (argc > 1)
return run_single_test(suite, argv[1], create_text_reporter());
Makefile
@@ -13,8 +13,8 @@ doc : doc/
run : main
./main
-main : main.o words_test.o words.o priority_queue.o priority_queue_test.o
- $(CC) main.o words_test.o words.o priority_queue.o priority_queue_test.o -lcgreen -o main
+main : main.o words_test.o words.o priority_queue.o priority_queue_test.o stack_test.o
+ $(CC) main.o words_test.o words.o priority_queue.o priority_queue_test.o stack_test.o -lcgreen -o main
main.o : main.c
$(CC) -c main.c
@@ -31,6 +31,9 @@ priority_queue.o : assignments/01/priority_queue.c
priority_queue_test.o : assignments/01/priority_queue_test.c
$(CC) -c assignments/01/priority_queue_test.c
+stack_test.o : assignments/01/stack_test.c
+ $(CC) -c assignments/01/stack_test.c
+
clean:
rm -f main *.o
rm -fr doc