📄 q.c
字号:
#include "q.h"q_t *qtmalloc(int initl) { //initl should be 64... q_t *q; q = (q_t *)malloc(sizeof(q_t)); q->front=0; q->end=0; q->len=0; q->size = initl; q->q = (void **)malloc(sizeof(void *)*initl); q->get = qtget; q->put = qtput; q->peek = qtpeek; q->putmore = qtputmore;}void qtfree(q_t *q) { free(q->q); free(q);}void qtgrow(q_t *q, int len) { int i, j; void **p; printf("growing\n"); p = (void **)malloc(sizeof(void *)*(q->size)*2); if (q->len<=0) goto qtgrowfinish; if (q->front >= q->end) { //its wrapped memcpy(p, &q->q[q->front], (i=(q->size - q->front))*sizeof(void *)); memcpy(&p[i], q->q, (q->end+1)*sizeof(void *)); } else { memcpy(p, &(q->q[q->front]), q->len*sizeof(void *)); } qtgrowfinish: free(q->q); q->q = p; q->size*=2; q->front=0; q->end=q->len;}void *qtget(q_t *q) { void *t; if ((q->len)<=0) { return(NULL); } t = q->q[q->front]; (q->front)++; q->front %= q->size; (q->len)--; return(t);}void *qtput(q_t *q, void *p) { if ((q->len)>=(q->size)) { //grow qtgrow(q, 1); } q->q[q->end]=p; q->end++; q->end %= q->size; q->len++; return(p);}void *qtpeek(q_t *q) { if ((q->len)<=0) { return(NULL); } return(q->q[q->front]);}void *qtputmore(q_t *q, void **p, int l) { int i, t, j; if ( ((q->len)+l) > (q->size)) { //grow qtgrow(q, l); } if ((q->end) + l >= q->size) { //it's wrapped i=q->end + l - q->size; j=l-i; memcpy(&(q->q[q->end]), p, i*sizeof(void *)); memcpy(q->q, &(p[i]), j*sizeof(void *)); } else memcpy(&(q->q[q->end]), p, l*sizeof(void *)); q->end+=l; q->end%=q->size; q->len+=l; return(&(q->q[q->front]));}void qtprint(q_t *q) { int i; printf("f: %i, e: %i, l: %i, s: %i\n", q->front, q->end, q->len, q->size); for (i=0; i<q->len; i++) { printf("%i ", q->q[(q->front+i)%(q->size)]); } printf("\n");}/*int main(void) { q_t *q; void *a; int s[10], i, j; q = qtmalloc(10); qtprint(q); qtput(q, 1); qtput(q, 2); qtprint(q); for (i=0; i<6; i++) { s[i]=6; } qtputmore(q, s, 6); qtprint(q); a = (int)qtget(q); printf("%i\n", (int)a); a = qtget(q); printf("%i\n", (int)a); for (i=0; i<4; i++) { s[i]=4; } qtputmore(q, s, 4); qtprint(q); qtput(q, 9); qtprint(q); return(0);}*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -