キュー
キュー
#include<stdio.h> #include<stdlib.h> typedef struct cell{ int id; struct cell *next; }Cell; typedef struct que{ Cell* front; Cell* rear; }Queue; Queue *make_queue() { Queue *que = malloc(sizeof(Queue)); que->front = NULL; que->rear = NULL; return que; } int enqueue(Queue *que,int item) { Cell *cel = malloc(sizeof(Cell)); cel->next = NULL; cel->id = item; if(!(que->rear)){ que->front = cel; que->rear = cel; }else{ que->rear->next = cel; que->rear = cel; return 1; } return 0; } Cell* dequeue(Queue *que) { Cell* cel = que->front; if(que->front) que->front = que->front->next; if(que->front == NULL) que->rear = NULL; return cel; } int main() { Queue *q = make_queue(); int i; Cell* c; for(i=0;i<10;i++) enqueue(q,i); for(;c=dequeue(q);) printf("%d\n",c->id); return 0; }
実行結果
0 1 2 3 4 5 6 7 8 9
キュー