summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-06-15 20:42:51 -0600
committermo khan <mo.khan@gmail.com>2020-06-15 20:42:51 -0600
commitcf6fd19cd4affad1ee23bfe133e00e3e2cab4283 (patch)
treee1b303212aa83c9f08d3d21aa3da3f71c683c029
parentd0d72f679a6a5e7ffcc65d297ed2760a611c7084 (diff)
Start to build a priority queue
-rw-r--r--Makefile10
-rw-r--r--assignments/01/README.md6
-rw-r--r--assignments/01/priority_queue.c15
-rw-r--r--assignments/01/priority_queue.h5
-rw-r--r--assignments/01/priority_queue_test.c30
-rw-r--r--main.c7
6 files changed, 67 insertions, 6 deletions
diff --git a/Makefile b/Makefile
index 58231b0..ffb4232 100644
--- a/Makefile
+++ b/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
diff --git a/assignments/01/README.md b/assignments/01/README.md
index 1562d3d..004e128 100644
--- a/assignments/01/README.md
+++ b/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.
diff --git a/assignments/01/priority_queue.c b/assignments/01/priority_queue.c
new file mode 100644
index 0000000..ebeea0f
--- /dev/null
+++ b/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;
+}
diff --git a/assignments/01/priority_queue.h b/assignments/01/priority_queue.h
new file mode 100644
index 0000000..02be7cd
--- /dev/null
+++ b/assignments/01/priority_queue.h
@@ -0,0 +1,5 @@
+struct PriorityQueue {
+};
+
+struct PriorityQueue initialize();
+int count(struct PriorityQueue queue);
diff --git a/assignments/01/priority_queue_test.c b/assignments/01/priority_queue_test.c
new file mode 100644
index 0000000..f58f4c9
--- /dev/null
+++ b/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;
+}
diff --git a/main.c b/main.c
index 97d2aaa..78ba042 100644
--- a/main.c
+++ b/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());
}