fork(1) download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct data {
  5. struct data *next;
  6. char key;
  7. };
  8.  
  9. struct queue {
  10. struct data *top,*rear;
  11. };
  12.  
  13. void enqueue(struct queue *q, char key);
  14. char dequeue(struct queue *q);
  15. void print_queue_list(struct queue q);
  16.  
  17. int main() {
  18. struct queue q;
  19. q.top = NULL;
  20. q.rear = NULL;
  21.  
  22. enqueue(&q, 'a');
  23.  
  24. print_queue_list(q);
  25. printf("\n");
  26.  
  27. enqueue(&q, 'b');
  28.  
  29. print_queue_list(q);
  30. printf("\n");
  31.  
  32. dequeue(&q);
  33.  
  34. print_queue_list(q);
  35. printf("\n");
  36.  
  37. dequeue(&q);
  38. }
  39.  
  40. void enqueue(struct queue *q, char key) {
  41. struct data *d;
  42. d = (struct data *) malloc(sizeof(struct data));
  43. d->key = key;
  44. d->next = NULL;
  45. if (q->top == NULL) {
  46. q->top = d;
  47. q->rear = d;
  48. } else {
  49. q->rear->next = d;
  50. q->rear = d;
  51. }
  52. }
  53.  
  54. char dequeue(struct queue *q) {
  55. char key;
  56. struct data *d;
  57. d = q->top;
  58. key = d->key;
  59. if (q->top == q->rear) {
  60. q->top = NULL;
  61. q->rear = NULL;
  62. } else {
  63. q->top = d->next;
  64. }
  65. free(d);
  66. return key;
  67. }
  68.  
  69. void print_queue_list(struct queue q) {
  70. while(q.top != NULL) {
  71. printf("%c\n", q.top->key);
  72. q.top = q.top->next;
  73. }
  74. }
Runtime error #stdin #stdout 0.01s 1852KB
stdin
Standard input is empty
stdout
a

a
b

b