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 = 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. char *t = malloc(100);
  22. strcpy(t, s);
  23. q->array[q->tail] = t;
  24. if(q->tail == QUE_LENGTH)
  25. q->tail = 0;
  26. else
  27. q->tail++;
  28. }
  29.  
  30. char *dequeue(struct queue *q){
  31. char* s = q->array[q->head];
  32. if(q->head == QUE_LENGTH)
  33. q->head = 0;
  34. else
  35. q->head++;
  36. return s;
  37. }
  38.  
  39. int main(int argc, char *argv[]){
  40. int n = 10;
  41. char buffer[100];
  42. struct queue *q = create_queue();
  43.  
  44. if(argc > 1){
  45. ++argv;
  46. if(**argv == '-')
  47. n = atoi(++*argv);
  48. }
  49. printf("%d\n", n);
  50. while(fgets(buffer, 100, stdin)){
  51. enqueue(q, buffer);
  52. }
  53. while(n--){
  54. strcpy(buffer, dequeue(q));
  55. printf("%s\n", buffer);
  56. }
  57. return 0;
  58. }
Success #stdin #stdout 0s 2292KB
stdin
-8
aaa
bbb
ccc
ddd
eee
fff
ggg
hhh
iii
jjj
kkk
stdout
10
-8

aaa

bbb

ccc

ddd

eee

fff

ggg

hhh

iii