Commit cf6fd19
Changed files (6)
assignments/01/priority_queue.c
@@ -0,0 +1,15 @@
+#include "priority_queue.h"
+/*#include <stdio.h>*/
+/*#include <stdlib.h>*/
+/*#include <stdint.h>*/
+/*#include <ctype.h>*/
+/*#include <string.h>*/
+
+struct PriorityQueue initialize() {
+ struct PriorityQueue queue;
+ return queue;
+}
+
+int count(struct PriorityQueue queue) {
+ return 0;
+}
assignments/01/priority_queue.h
@@ -0,0 +1,5 @@
+struct PriorityQueue {
+};
+
+struct PriorityQueue initialize();
+int count(struct PriorityQueue queue);
assignments/01/priority_queue_test.c
@@ -0,0 +1,30 @@
+#include <cgreen/cgreen.h>
+#include <string.h>
+
+#include "priority_queue.h"
+
+/*
+Implement the methods of the priority queue interface using a singly-linked list.
+
+* `add(x)`
+* `deleteMin()`
+* `size()`
+
+Analyze the running time of the `add(x)` and `deletMin()` operations based on this implementation.
+*/
+
+Describe(PriorityQueue);
+BeforeEach(PriorityQueue){ }
+AfterEach(PriorityQueue){ }
+
+Ensure(PriorityQueue, returns_size) {
+ struct PriorityQueue queue = initialize();
+
+ assert_that(count(queue), is_equal_to(0));
+}
+
+TestSuite *priority_queue_tests() {
+ TestSuite *suite = create_test_suite();
+ add_test_with_context(suite, PriorityQueue, returns_size);
+ return suite;
+}
assignments/01/README.md
@@ -7,11 +7,13 @@ You must score at least 50 to pass the assignment.
queue and priority queue, stack, list and linked list, sequence,
and unordered set, and you understand the concept of interface or abstract data type that defines the set of operations supported by a data structure and the semantics, or meaning, of those operations.
You can use the interface of one particular data structure to define or implement the operations of a different data structure.
- * a. Describe the meaning of the essential methods `add(x)`, `deleteMin()`, and `size()` that are supported by the priority queue interface.
- Implement those methods using a singly-linked list. Analyze the running time of the `add(x)` and `deletMin()` operations based on this implementation.
+ * a. Describe the meaning of the essential methods `add(x)`, `deleteMin()`, and `size()` that are supported by the priority queue interface
* The `add(x)` method is used to add an element to the queue with a priority.
* The `deleteMin()` method is used to remove the element with the lowest priority from the queue and return it.
* The `size()` method is used to determine how many items are in the queue.
+
+ Implement those methods using a singly-linked list.
+ Analyze the running time of the `add(x)` and `deletMin()` 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.
main.c
@@ -1,12 +1,15 @@
#include <cgreen/cgreen.h>
TestSuite *words_tests();
+TestSuite *priority_queue_tests();
int main(int argc, char **argv) {
TestSuite *suite = create_test_suite();
+
add_suite(suite, words_tests());
- if (argc > 1) {
+ add_suite(suite, priority_queue_tests());
+
+ if (argc > 1)
return run_single_test(suite, argv[1], create_text_reporter());
- }
return run_test_suite(suite, create_text_reporter());
}
Makefile
@@ -10,8 +10,8 @@ ci : main
run : main
./main
-main : main.o words_test.o words.o
- $(CC) main.o words_test.o words.o -lcgreen -o 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.o : main.c
$(CC) -c main.c
@@ -22,5 +22,11 @@ words.o : words.c
words_test.o : words_test.c
$(CC) -c words_test.c
+priority_queue.o : assignments/01/priority_queue.c
+ $(CC) -c assignments/01/priority_queue.c
+
+priority_queue_test.o : assignments/01/priority_queue_test.c
+ $(CC) -c assignments/01/priority_queue_test.c
+
clean:
rm main *.o