åå¿è ãªè³ªåã§ããã¾ããããã¥ã¼ã«ã¤ãã¦æãã¦ãã ããã
ãã¥ã¼ã«ã¤ãã¦èããã
çµã¿è¾¼ã¿ã§ä½¿ãã®ãæ³å®ãã¦ãããmallocã¯ä½¿ããªããæåã«æ大ãµã¤ãºã決ã¾ã£ã¦ã¦ããã以ä¸ã¯ã©ãã§ãããããã®ã¨ããã
struct q { char *buf; int buflen; int head; int tail; }; void init( struct q *q, char *buf, int len ) { q->head = 0; q->tail = 0; q->buf = buf; q->buflen = len; } void push( struct q *q, char c ) { if ( q->head == (tail-1) ) { /* ç¥ããªã */ return; } /* ... 端ã£ãã®å¦çã¨ã ... */ q->buf[q->head] = c; q->head++; } int pop( struct q *q ) { int ret; if ( q->head == q->tail ) return -1 /* ... 端ã£ãã®å¦çã¨ã ... */ ret = q->buf[q->tail]; q->tail++; return ret; }
ãããªæããããã¼ã ãªã¼ãã¨æã£ã¦ãã£ã¦ããã ãã©ããã®ããããã ã¨ããã¿ã¼ãªåé¡ãããããããã¡ã®ãµã¤ãºå使ããªãã®ã ã
char buf[5]; init( &q, buf, 5 ); /* ããã ã¨ååã¾ã§ããå ¥ããããªã⦠*/
ã¯ããã¦ããããç°¡åã«è§£æ±ºããæ¹æ³ã¯ããã ãããã
â
ã¨ãæã£ããã©ãããã¾ãé¢ç½ããªããªã
æè¿æ¹åæ§ãããããªããªã£ã¦ãã¦ãã®ã§ã¡ãã£ã¨ä¼ãã