fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define QUE_LENGTH 100
  6.  
  7. struct queue{
  8. int head;
  9. int tail;
  10. char *array[QUE_LENGTH];
  11. };
  12.  
  13. struct queue *create_queue(){
  14. struct queue* q = (struct queue *)malloc(sizeof(struct queue));
  15. q->head = 0;
  16. q->tail = 0;
  17. return q;
  18. }
  19.  
  20. void enqueue(struct queue *q, char *s){
  21. q->array[q->tail] = s;
  22. if(q->tail == QUE_LENGTH)
  23. q->tail = 0;
  24. else
  25. q->tail++;
  26. }
  27.  
  28. char *dequeue(struct queue *q){
  29. char* s = q->array[q->head];
  30. if(q->head == QUE_LENGTH)
  31. q->head = 0;
  32. else
  33. q->head++;
  34. return s;
  35. }
  36.  
  37. int main(int argc, char *argv[]){
  38. int n = 10;
  39. char buffer[100];
  40. struct queue *q = create_queue();
  41.  
  42. if(argc > 1){
  43. ++argv;
  44. if(**argv == '-')
  45. n = atoi(++*argv);
  46. }
  47. printf("%d\n", n);
  48. while(fgets(buffer, 100, stdin)){
  49. enqueue(q, buffer);
  50. }
  51. while(n--){
  52. strcpy(buffer, dequeue(q));
  53. printf("%s\n", buffer);
  54. }
  55. return 0;
  56. }
Success #stdin #stdout 0s 2292KB
stdin
-8
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
jjj
kkk
stdout
10
kkk
kkk
kkk
kkk
kkk
kkk
kkk
kkk
kkk
kkk