From 0763ca0cd43155f69ddcda597f5cb755937804b0 Mon Sep 17 00:00:00 2001 From: ergz Date: Wed, 28 Jun 2023 20:04:09 -0700 Subject: [PATCH] dumb implementations of each so far --- c/a.out | Bin 16800 -> 16904 bytes c/bubble_sort.c | 25 ++++++++++++++ c/queue.c | 74 +++++++++++++++++++++++++++++++++++++++++ ts/bubble_sort.test.ts | 2 +- ts/linked_list.ts | 0 ts/queue.test.ts | 55 ++++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 c/bubble_sort.c create mode 100644 c/queue.c create mode 100644 ts/linked_list.ts create mode 100644 ts/queue.test.ts diff --git a/c/a.out b/c/a.out index a3f6b931dafa01b8a808fda99b8c9f478b1f144a..887012c9a48a307aad39bef4efec07801ee340b8 100755 GIT binary patch delta 2086 zcmZWqe{54#6u$ShW9=Bwb|udu!UGXDU~M+bTKBVWgYlLuQk0?zno?+S4Rd4C21kIp z42&<$0_>o{1miGjhA~D`{{Uks%hW;C;9m>)D?lWvGI0ndNWka0@7>ypZ+ScCe&6}d zIq%%NyOBZq+J1VdQc&s-FMr?apF#A;fB2H8?3&3~Tr+`#x~&1hSO&mOD>BbfFST|X z2~@twZHntiP1f$PefM* z-mlK(T8t3(L1y@^Bn*pcDl*fKG3&7HjX8A~stnD_VrFHi={me;yO6r0sL5$1z2;6j zU^{R&>0+*u4oxent>6YN*XJ9nfW;t#X!C)FkNTk?WvCjo9eRHJ)|>@*LyCG$QTJlP zeE!l?tikJ|p(aWy%`>UCQxusZ-Qbih`w&98jel#JqOxlEPB1+&+<+zTPXzSYF!+3- z^5B%D?w!gqDrYZBk`7JrubW5}auf-9%S!sc{YZNAxlNkh&*f|xOMK8+<+3x`YQR|G zL_<03=CYzrD(XaTh*f7(EkL_{ z^u@oT3KZ$`^W)pxa~AGrlOo#=v0ag$=^ScmTTY0)v16e+DTg}LKjjUwdM~65ReKSC zy0hxOGB{<)_1?>*9DU`(k|PyKNQthP)D??FrJmksOp5QUmmZ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k3qfs>jrj=hcbxMPewnR~ z@GI=lBYc$YF~YA;XcgShDX_#K*tH~t_~kE|ECeFaPFa=>XBUI~Qs6sGdl|Q^gpD@- zE6a)`1RuTAEmWi^@rHHLEF`|aVAw%eUY;j9LJfO&65@HhJ@AvKt37brv(FRQ>=Hty zEj!WK-j0&Hn0Qij;%@j@#y|=lR~ntRr2C4xEz*!h>bDI0do13;LGO!omZEOViqIU* zZUHd~`>j7@c?_{|Lzdq|tVH}AaUUWxZn!DSR}e#pw-5{el;uo3ixY@X!)ulTa}+%| zkRdIJ%1sxdSc&}*T(&%D9z`}@0L&;8`>?_qttO8bqjWMg|3KOit~oVbojmWW%>?CcF|~4pG_k3 zgv}4v?2l0wlsI-!Te#hEo`S`ht2Us2%dgmG7{K@|I>XMLh?Xhf9yrD6ES<;oF7 z->6sYmHNGX(n~#CKFaM?VO#ApG<>9PmSSaHF3*7a`dm0!p9cQ=GNVhqo;a4lT)ji@ zSG9h)USFe+SE4O!ZD^w$&%twz_T=~-VX4hFw$c( + +// asume the array is sorted of course +void bubble_sort(int arr[], int len) { + int temp; + for (int i = 0; i < len; i++) { + for (int j = 0; j < len - 1; j++) { + if (arr[j] > arr[j+1]) { + temp = arr[j+1]; + arr[j+1] = arr[j]; + arr[j] = temp; + } + } + } +} + +int main() { + int arr[5] = {1, 3, 2, 4, 5}; + bubble_sort(arr, 5); + + for (int i = 0; i < 5; i++) { + printf("%d ", arr[i]); + } + printf("\n"); +} \ No newline at end of file diff --git a/c/queue.c b/c/queue.c new file mode 100644 index 0000000..32f03f2 --- /dev/null +++ b/c/queue.c @@ -0,0 +1,74 @@ +#include +#include +#include + +#define MAX_LEN 100 + +typedef struct Node { + int value; + struct Node* next; +} Node; + +typedef struct { + Node* head; + Node* tail; + int length; +} Queue; + +// void enqueue(Queue q) {} + +// void enqueue(Queue q) {} + +void enqueue(Queue* q, Node* n) { + if (q->length == 0) { + q->head = n; + q->tail = n; + } else { + q->tail->next = n; + q->tail = n; + } + q->length++; +} + +bool dequeue(Queue* q) { + if (q->length == 0) { + return false; + } else { + q->head = q->head->next; + return true; + } +} + +Queue* new_queue() { + Queue* q = malloc(sizeof(Queue)); + q->head = NULL; + q->tail = NULL; + q->length = 0; + return q; +} + +Node* new_node(int value) { + Node* n = malloc(sizeof(Node)); + n->value = value; + n->next = NULL; + return n; +} + +Node* peek(Queue* q) { return q->head; } + +int main() { + Queue* q = new_queue(); + Node* p = new_node(10); + Node* r = new_node(11); + // Node* n; + enqueue(q, p); + enqueue(q, r); + Node* n = peek(q); + printf("the value at the head node is: %d\n", n->value); + + dequeue(q); + n = peek(q); + printf("the value at the head node is: %d\n", n->value); + + return (0); +} diff --git a/ts/bubble_sort.test.ts b/ts/bubble_sort.test.ts index 197ad01..4d4d2fc 100644 --- a/ts/bubble_sort.test.ts +++ b/ts/bubble_sort.test.ts @@ -5,7 +5,7 @@ function bubble_sort(arr: number[]): void { const t = arr[j+1]; arr[j+1] = arr[j]; arr[j] = t; - + } } } diff --git a/ts/linked_list.ts b/ts/linked_list.ts new file mode 100644 index 0000000..e69de29 diff --git a/ts/queue.test.ts b/ts/queue.test.ts new file mode 100644 index 0000000..5af29f3 --- /dev/null +++ b/ts/queue.test.ts @@ -0,0 +1,55 @@ +type QNode = { + value: T, + next?: QNode +} + +class Queue { + public length: number; + private head?: QNode; + private tail?: QNode; + + constructor() { + this.head = this.tail = undefined; + this.length = 0; + } + + enqueue(item: T): void { + const node = { value: item } as QNode; + this.length++; + if (!this.tail) { + this.tail = this.head = node + return + } + + this.tail.next = node; + this.tail = node; + + } + + deque(): T | undefined { + if (!this.head) { + return undefined; + } + this.length--; + const head = this.head; + this.head = this.head.next; + + return head.value; + } + + peek(): T | undefined { + return this.head?.value + } +} + +test("test that queue works", () => { + const q = new Queue; + + q.enqueue(12); + console.log(q); + expect(q.length).toEqual(1); + + q.enqueue(13); + console.log(q); + expect(q.length).toEqual(2); +}) \ No newline at end of file