fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct _Queue {
  6. int front;
  7. int rear;
  8. int capacity;
  9. int* arr;
  10. } *Queue;
  11.  
  12. Queue createQueue();
  13. void push(Queue Q, int in);
  14. int pop(Queue Q);
  15. int size(Queue Q);
  16. int emptyQueue(Queue Q);
  17. int fullQueue(Queue Q);
  18. int frontQueue(Queue Q);
  19. int backQueue(Queue Q);
  20.  
  21. Queue createQueue() {
  22. Queue Q = (Queue)malloc(sizeof(Queue));
  23. Q->front = 0;
  24. Q->rear = 0;
  25. Q->capacity = 10002;
  26. Q->arr = malloc(sizeof(int) * Q->capacity);
  27. }
  28.  
  29. void push(Queue Q, int in) {//rear 하나 증가
  30. if (!Q) {
  31. return;
  32. }
  33. if (fullQueue(Q)) {
  34. return;
  35. }
  36. if (Q->rear == Q->capacity - 1) {
  37. Q->rear = 0;
  38. }
  39. else {
  40. Q->rear += 1;
  41. }
  42. Q->arr[Q->rear] = in;
  43. }
  44. int pop(Queue Q) {//front 하나증가
  45. if (!Q) {
  46. return -1;
  47. }
  48. if (emptyQueue(Q)) {
  49. return -1;
  50. }
  51. if (Q->front == Q->capacity - 1) {
  52. Q->front = 0;
  53. }
  54. else {
  55. Q->front += 1;
  56. }
  57. int data = Q->arr[Q->front];
  58. return data;
  59.  
  60. }
  61. int size(Queue Q) {
  62. if (!Q) {
  63. return -1;
  64. }
  65. return Q->rear - Q->front;
  66. }
  67. int emptyQueue(Queue Q) {
  68. if (!Q) return 1;
  69. return(Q->front == Q->rear); // 큐가 비어있으면 1 , 차있으면 0 반환
  70. }
  71. int fullQueue(Queue Q) {
  72. return(Q->front == (Q->rear + 1) % Q->capacity); // Q가 꽉찼으면 1. 아니면 0 반환
  73. }
  74. int frontQueue(Queue Q) {
  75. if (!Q) return -1;
  76. if (emptyQueue(Q)) return -1;
  77. if (Q->front == Q->capacity - 1) {
  78. return Q->arr[0];
  79. }
  80. else {
  81. return Q->arr[Q->front + 1];
  82. }
  83. }
  84. int backQueue(Queue Q) {
  85. if (!Q) return -1;
  86. if (emptyQueue(Q)) return -1;
  87. return Q->arr[Q->rear];
  88. }
  89.  
  90.  
  91. int main() {
  92. int count = 0;
  93. char *s = malloc(100);
  94. scanf("%d", &count);
  95. Queue Q = createQueue();
  96. for (int i = 0; i < count; i++) {
  97. scanf("%s", s);
  98. if (strcmp(s, "push") == 0) {
  99. int data;
  100. scanf("%d", &data);
  101. push(Q, data);
  102. }
  103. else if (strcmp(s, "pop") == 0) {
  104. printf("%d\n", pop(Q));
  105. }
  106. else if (strcmp(s, "size") == 0) {
  107. printf("%d\n", size(Q));
  108. }
  109. else if (strcmp(s, "back") == 0) {
  110. printf("%d\n", backQueue(Q));
  111. }
  112. else if (strcmp(s, "front") == 0) {
  113. printf("%d\n", frontQueue(Q));
  114. }
  115. else if (strcmp(s, "empty") == 0) {
  116. printf("%d\n", emptyQueue(Q));
  117. }
  118. }
  119. free(Q->arr);
  120. free(Q);
  121.  
  122. }
Runtime error #stdin #stdout 0s 4516KB
stdin
15
push 1
push 2
front
back
size
empty
pop
pop
pop
size
empty
pop
push 3
empty
front
stdout
Standard output is empty