Initial data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 72d218235f
commit f23f22d71c
199087 changed files with 3378941 additions and 0 deletions

View file

@ -0,0 +1,52 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int DATA; /* type of data to store in queue */
typedef struct {
DATA *buf;
size_t head, tail, alloc;
} queue_t, *queue;
queue q_new()
{
queue q = malloc(sizeof(queue_t));
q->buf = malloc(sizeof(DATA) * (q->alloc = 4));
q->head = q->tail = 0;
return q;
}
int empty(queue q)
{
return q->tail == q->head;
}
void enqueue(queue q, DATA n)
{
if (q->tail >= q->alloc) q->tail = 0;
q->buf[q->tail++] = n;
// Fixed bug where it failed to resizes
if (q->tail == q->alloc) { /* needs more room */
q->buf = realloc(q->buf, sizeof(DATA) * q->alloc * 2);
if (q->head) {
memcpy(q->buf + q->head + q->alloc, q->buf + q->head,
sizeof(DATA) * (q->alloc - q->head));
q->head += q->alloc;
} else
q->tail = q->alloc;
q->alloc *= 2;
}
}
int dequeue(queue q, DATA *n)
{
if (q->head == q->tail) return 0;
*n = q->buf[q->head++];
if (q->head >= q->alloc) { /* reduce allocated storage no longer needed */
q->head = 0;
if (q->alloc >= 512 && q->tail < q->alloc / 2)
q->buf = realloc(q->buf, sizeof(DATA) * (q->alloc/=2));
}
return 1;
}

View file

@ -0,0 +1,43 @@
#include <stdio.h>
#include <stdlib.h>
typedef struct node_t node_t, *node, *queue;
struct node_t { int val; node prev, next; };
#define HEAD(q) q->prev
#define TAIL(q) q->next
queue q_new()
{
node q = malloc(sizeof(node_t));
q->next = q->prev = 0;
return q;
}
int empty(queue q)
{
return !HEAD(q);
}
void enqueue(queue q, int n)
{
node nd = malloc(sizeof(node_t));
nd->val = n;
if (!HEAD(q)) HEAD(q) = nd;
nd->prev = TAIL(q);
if (nd->prev) nd->prev->next = nd;
TAIL(q) = nd;
nd->next = 0;
}
int dequeue(queue q, int *val)
{
node tmp = HEAD(q);
if (!tmp) return 0;
*val = tmp->val;
HEAD(q) = tmp->next;
if (TAIL(q) == tmp) TAIL(q) = 0;
free(tmp);
return 1;
}

View file

@ -0,0 +1,22 @@
int main()
{
int i, n;
queue q = q_new();
for (i = 0; i < 100000000; i++) {
n = rand();
if (n > RAND_MAX / 2) {
// printf("+ %d\n", n);
enqueue(q, n);
} else {
if (!dequeue(q, &n)) {
// printf("empty\n");
continue;
}
// printf("- %d\n", n);
}
}
while (dequeue(q, &n));// printf("- %d\n", n);
return 0;
}

View file

@ -0,0 +1,47 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/queue.h>
struct entry {
int value;
TAILQ_ENTRY(entry) entries;
};
typedef struct entry entry_t;
TAILQ_HEAD(FIFOList_s, entry);
typedef struct FIFOList_s FIFOList;
bool m_enqueue(int v, FIFOList *l)
{
entry_t *val;
val = malloc(sizeof(entry_t));
if ( val != NULL ) {
val->value = v;
TAILQ_INSERT_TAIL(l, val, entries);
return true;
}
return false;
}
bool m_dequeue(int *v, FIFOList *l)
{
entry_t *e = l->tqh_first;
if ( e != NULL ) {
*v = e->value;
TAILQ_REMOVE(l, e, entries);
free(e);
return true;
}
return false;
}
bool isQueueEmpty(FIFOList *l)
{
if ( l->tqh_first == NULL ) return true;
return false;
}