Time for an 2014 update…
This commit is contained in:
parent
372c577f83
commit
09687c4926
2520 changed files with 34227 additions and 7318 deletions
|
|
@ -4,47 +4,47 @@
|
|||
|
||||
typedef int DATA; /* type of data to store in queue */
|
||||
typedef struct {
|
||||
DATA *buf;
|
||||
size_t head, tail, alloc;
|
||||
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;
|
||||
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;
|
||||
return q->tail == q->head;
|
||||
}
|
||||
|
||||
void enqueue(queue q, DATA n)
|
||||
{
|
||||
if (q->tail >= q->alloc) q->tail = 0;
|
||||
q->buf[q->tail++] = n;
|
||||
if (q->tail == q->head) { /* 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;
|
||||
}
|
||||
if (q->tail >= q->alloc) q->tail = 0;
|
||||
q->buf[q->tail++] = n;
|
||||
if (q->tail == q->head) { /* 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;
|
||||
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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,36 +8,36 @@ struct node_t { int val; node prev, next; };
|
|||
#define TAIL(q) q->next
|
||||
queue q_new()
|
||||
{
|
||||
node q = malloc(sizeof(node_t));
|
||||
q->next = q->prev = 0;
|
||||
return q;
|
||||
node q = malloc(sizeof(node_t));
|
||||
q->next = q->prev = 0;
|
||||
return q;
|
||||
}
|
||||
|
||||
int empty(queue q)
|
||||
{
|
||||
return !HEAD(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;
|
||||
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;
|
||||
node tmp = HEAD(q);
|
||||
if (!tmp) return 0;
|
||||
*val = tmp->val;
|
||||
|
||||
HEAD(q) = tmp->next;
|
||||
if (TAIL(q) == tmp) TAIL(q) = 0;
|
||||
free(tmp);
|
||||
HEAD(q) = tmp->next;
|
||||
if (TAIL(q) == tmp) TAIL(q) = 0;
|
||||
free(tmp);
|
||||
|
||||
return 1;
|
||||
return 1;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,22 +1,22 @@
|
|||
int main()
|
||||
{
|
||||
int i, n;
|
||||
queue q = q_new();
|
||||
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);
|
||||
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;
|
||||
return 0;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue