tasks a-s
This commit is contained in:
parent
47bf37c096
commit
b83f433714
12433 changed files with 156208 additions and 123 deletions
50
Task/Queue-Definition/C/queue-definition-1.c
Normal file
50
Task/Queue-Definition/C/queue-definition-1.c
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
#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;
|
||||
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;
|
||||
}
|
||||
43
Task/Queue-Definition/C/queue-definition-2.c
Normal file
43
Task/Queue-Definition/C/queue-definition-2.c
Normal 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;
|
||||
}
|
||||
22
Task/Queue-Definition/C/queue-definition-3.c
Normal file
22
Task/Queue-Definition/C/queue-definition-3.c
Normal 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;
|
||||
}
|
||||
47
Task/Queue-Definition/C/queue-definition-4.c
Normal file
47
Task/Queue-Definition/C/queue-definition-4.c
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue