Skip to content

Instantly share code, notes, and snippets.

@boxp
Created June 2, 2014 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boxp/489666fab28a0a524e1d to your computer and use it in GitHub Desktop.
Save boxp/489666fab28a0a524e1d to your computer and use it in GitHub Desktop.
C言語でキュー
#include <stdio.h>
#define QUEUESIZE 3000
struct Queue {
int buffer[QUEUESIZE];
int length;
};
int enqueue(struct Queue *queue, int data) {
queue->buffer[queue->length] = data;
queue->length++;
return 0;
};
int dequeue(struct Queue *queue) {
int res;
int i;
if (queue->length > 0) {
res = queue->buffer[0];
// 配列を先頭へずらす
for (i = 0; i < queue->length - 1; i++) {
queue->buffer[i] = queue->buffer[i+1];
};
queue->length--;
} else {
res = 0;
}
return res;
};
int main(int argc, char const* argv[])
{
struct Queue alice;
alice.length = 0;
enqueue(&alice, 1);
enqueue(&alice, 2);
enqueue(&alice, 3);
printf("%d\n",dequeue(&alice));
printf("%d\n",dequeue(&alice));
printf("%d\n",dequeue(&alice));
return 0;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment