diff --git a/semester3/exercises/exercise2.13.c b/semester3/exercises/2-Algorighm-Analysis/exercise2.13.c similarity index 100% rename from semester3/exercises/exercise2.13.c rename to semester3/exercises/2-Algorighm-Analysis/exercise2.13.c diff --git a/semester3/exercises/exercise2.6.c b/semester3/exercises/2-Algorighm-Analysis/exercise2.6.c similarity index 100% rename from semester3/exercises/exercise2.6.c rename to semester3/exercises/2-Algorighm-Analysis/exercise2.6.c diff --git a/semester3/exercises/homework2.c b/semester3/exercises/2-Algorighm-Analysis/homework2.c similarity index 100% rename from semester3/exercises/homework2.c rename to semester3/exercises/2-Algorighm-Analysis/homework2.c diff --git a/semester3/exercises/exercise3.10.c b/semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.10.c similarity index 100% rename from semester3/exercises/exercise3.10.c rename to semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.10.c diff --git a/semester3/exercises/exercise3.21.c b/semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.21.c similarity index 100% rename from semester3/exercises/exercise3.21.c rename to semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.21.c diff --git a/semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.26.c b/semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.26.c new file mode 100644 index 0000000..194a310 --- /dev/null +++ b/semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.26.c @@ -0,0 +1,100 @@ +#include +#include + +typedef struct deque { + int capacity; + int front; + int rear; + int* arr; +} deque; + +deque* new_deque(int capacity) { + // capacity > 1 + deque* res = malloc(sizeof(deque)); + int* arrray = malloc(sizeof(int) * capacity); + + res->arr = arrray; + res->rear = 0; + res->front = 1; + res->capacity = capacity; + + return res; +} + +void remove_deque(deque *de) { + free(de->arr); + free(de); +} + +int front_it (deque *de, int step) { + int after = (de->front + step + de->capacity) % de->capacity; + // check for after + if (after == de->rear) { + if (step == 1) { + printf("ERROR: capacity overload\n"); + exit(1); + } else { + printf("ERROR: The array is already empty\n"); + exit(2); + } + } + de->front = after; + return after; +} + +void push (int x, deque* de) { + // insert item x to the front end of deque + de->arr[front_it(de, 1)] = x; +} + +int pop (deque* de) { + // remove the front item and return it. + int val = de->arr[de->front]; + front_it(de, -1); + return val; +} + +int rear_it (deque *de, int step) { + int after = (de->rear + step + de->capacity) % de->capacity; + // check for after + if (after == de->front) { + if (step == -1) { + printf("ERROR: capacity overload\n"); + exit(1); + } else { + printf("ERROR: The array is already empty\n"); + exit(2); + } + } + de->rear = after; + return after; +} + +void inject (int x, deque* de) { + // rear end + de->arr[rear_it(de, -1)] = x; +} + +int eject (deque *de) { + // rear end + int val = de->arr[de->rear]; + rear_it(de, 1); + return val; +} + +int main(void) { + deque* test = new_deque(5); + push(1, test); + push(2, test); + push(3, test); + printf("%d\n", pop(test)); + printf("%d\n", pop(test)); + printf("%d\n", pop(test)); + inject(4, test); + inject(5, test); + printf("REAR: %d\n", eject(test)); + printf("REAR: %d\n", eject(test)); + + remove_deque(test); + return 0; +} diff --git a/semester3/exercises/exercise3.6.c b/semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.6.c similarity index 100% rename from semester3/exercises/exercise3.6.c rename to semester3/exercises/3-Lists-Stacks-and-Queues/exercise3.6.c diff --git a/semester3/exercises/4-Trees/exercise4.35.cpp b/semester3/exercises/4-Trees/exercise4.35.cpp new file mode 100644 index 0000000..ce30043 --- /dev/null +++ b/semester3/exercises/4-Trees/exercise4.35.cpp @@ -0,0 +1,38 @@ +#include +#include + +typedef struct TreeNode { + struct TreeNode *l; + struct TreeNode *r; + int val; +} TreeNode; + +// answer begins here +void BFS(TreeNode *node) { + std::queue todo; + if (node) { + todo.push(node); + } + + while (!todo.empty()) { + for (int i = 0, size = todo.size(); i < size; i++) { + auto ptr = todo.front(); + std::printf("%d ", ptr->val); + todo.pop(); + + if (ptr->l) { + todo.push(ptr->l); + } + if (ptr->r) { + todo.push(ptr->r); + } + } + printf("\n"); + } +} + +// answer ends +int main(void) { + BFS(nullptr); + return 0; +}