fork download
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #define QLEN 100
  4. #define TRUE 1
  5. #define FALSE 0
  6. typedef int Data;
  7. typedef struct _Qu
  8. {
  9. int front;
  10. int rear;
  11. Data queArr[QLEN];
  12. }Qu;
  13.  
  14. typedef Qu Queue;
  15.  
  16. void QInit(Queue *p); //큐를 초기화 한다. 제일먼저 호출되야한다.
  17.  
  18. int QIsEmpty(Queue *p); //큐가 비었는지 아닌지 판단.True or False
  19.  
  20. void QPush(Queue *p, Data data); //큐에 데이터 저장.
  21.  
  22. Data QPop(Queue *p); //큐에서 가장 먼저 push 한 데이터를 이때 반환
  23. //if (QIsEmpty(Queue *p)
  24. // printf("큐가 비었습니다.");
  25. // return false; //데이터가 하나 있는지 보장되야됨.
  26.  
  27. Data QPeek(Queue *p);//데이터
  28. //if (QIsEmpty(Queue *p)
  29. // printf("큐가 비었습니다.");
  30. // return false; //데이터가 하나 있는지 보장되야됨.
  31.  
  32.  
  33. int main(void)
  34. {
  35. Queue q;
  36. QInit(&q);
  37.  
  38. QPush(&q,1);
  39. QPush(&q,3);
  40. while(QIsEmpty(&q)!=0)
  41. printf("%d",QPop(&q));
  42. return 0;
  43. }
  44.  
  45. int NextPoint(int point)
  46. {
  47. if(point==QLEN-1)
  48. return 0;
  49. else
  50. return point+1;
  51. }
  52. void QInit(Queue *p)
  53. {
  54. p->front = 0; //0은 비워져
  55. p->rear = 0;
  56. }
  57.  
  58. int QIsEmpty(Queue *p)
  59. {
  60. if(p->rear==p->front)
  61. return TRUE;
  62. else
  63. return FALSE;
  64. }
  65.  
  66. void QPush(Queue *p,Data data)
  67. {
  68. if(NextPoint(p->rear)==p->front)//다음을 가리키는것을 뽑아내야될 필요성.
  69. exit(-1);
  70.  
  71. p->rear = NextPoint(p->rear); //하나
  72. p->queArr[p->rear] = data;
  73. }
  74.  
  75. Data QPop(Queue *p)
  76. {
  77. if(QIsEmpty(&p)==1)
  78. exit(-1);
  79. printf("Empty not!");
  80. p->front=NextPoint(p->front);
  81. return p->queArr[p->front];
  82. }
  83.  
  84. Data QPeek(Queue *p)
  85. {
  86. int tmp=0;
  87. if(QIsEmpty(&p)==1)
  88. exit(-1);
  89. tmp = NextPoint(p->front);
  90. return p->queArr[tmp];
  91. }
  92.  
Success #stdin #stdout 0s 9296KB
stdin
Standard input is empty
stdout
Standard output is empty