dumb implementations of each so far
This commit is contained in:
parent
0b2c71d97d
commit
0763ca0cd4
|
@ -0,0 +1,25 @@
|
|||
#include <stdio.h>
|
||||
|
||||
// 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");
|
||||
}
|
|
@ -0,0 +1,74 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
|
@ -5,7 +5,7 @@ function bubble_sort(arr: number[]): void {
|
|||
const t = arr[j+1];
|
||||
arr[j+1] = arr[j];
|
||||
arr[j] = t;
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
type QNode<T> = {
|
||||
value: T,
|
||||
next?: QNode<T>
|
||||
}
|
||||
|
||||
class Queue<T> {
|
||||
public length: number;
|
||||
private head?: QNode<T>;
|
||||
private tail?: QNode<T>;
|
||||
|
||||
constructor() {
|
||||
this.head = this.tail = undefined;
|
||||
this.length = 0;
|
||||
}
|
||||
|
||||
enqueue(item: T): void {
|
||||
const node = { value: item } as QNode<T>;
|
||||
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<number>;
|
||||
|
||||
q.enqueue(12);
|
||||
console.log(q);
|
||||
expect(q.length).toEqual(1);
|
||||
|
||||
q.enqueue(13);
|
||||
console.log(q);
|
||||
expect(q.length).toEqual(2);
|
||||
})
|
Loading…
Reference in New Issue