Created
June 2, 2014 01:14
-
-
Save boxp/489666fab28a0a524e1d to your computer and use it in GitHub Desktop.
C言語でキュー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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