nyhryan

Untitled

Jul 25th, 2023
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.16 KB | Source Code | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4.  
  5. #define MAX_QUEUE_SIZE 8
  6.  
  7. typedef int element;
  8.  
  9. // 덱 구조체
  10. typedef struct _Deque
  11. {
  12.     element arr[MAX_QUEUE_SIZE];
  13.     int front;
  14.     int rear;
  15. } Deque;
  16.  
  17. // 덱 초기화 함수
  18. void Deque_Init(Deque* dq)
  19. {
  20.     dq->front = dq->rear = 0;
  21.     for (int i = 0; i < MAX_QUEUE_SIZE; ++i)
  22.         dq->arr[i] = 0;
  23. }
  24.  
  25. // 덱이 비었으면 1, 아니면 0 리턴
  26. int Deque_IsEmpty(Deque* dq)
  27. {
  28.     return dq->front == dq->rear;
  29. }
  30.  
  31. // 덱이 꽉 찼으면 1, 아니면 0 리턴
  32. int Deque_IsFull(Deque* dq)
  33. {
  34.     return (dq->rear + 1) % MAX_QUEUE_SIZE == dq->front;
  35. }
  36.  
  37. // 뒤에 삽입 : Enqueue
  38. void Deque_InsertRear(Deque* dq, element e)
  39. {
  40.     if (Deque_IsFull(dq))
  41.     {
  42.         fprintf(stderr, "Cannot insert rear : Deque is full!\n");
  43.         exit(1);
  44.     }
  45.     // rear를 뒤로 한칸 땡기고
  46.     dq->rear = (dq->rear + 1) % MAX_QUEUE_SIZE;
  47.    
  48.     // 그 자리에 새 요소 삽입
  49.     dq->arr[dq->rear] = e;
  50. }
  51.  
  52. // 앞에 제거 : Dequeue
  53. element Deque_RemoveFront(Deque* dq)
  54. {
  55.     if (Deque_IsEmpty(dq))
  56.     {
  57.         fprintf(stderr, "Cannot remove front : Deque is empty!\n");
  58.         exit(1);
  59.     }
  60.  
  61.     // front를 뒤로 한칸 땡기고
  62.     dq->front = (dq->front + 1) % MAX_QUEUE_SIZE;
  63.    
  64.     // 그 자리에 있는 요소를 리턴
  65.     return dq->arr[dq->front];
  66. }
  67.  
  68. // 앞에 삽입
  69. void Deque_InsertFront(Deque* dq, element e)
  70. {
  71.     if (Deque_IsFull(dq))
  72.     {
  73.         fprintf(stderr, "Cannot insert rear : Deque is full!\n");
  74.         exit(1);
  75.     }
  76.     // front 자리에 새 요소 저장
  77.     dq->arr[dq->front] = e;
  78.     // front를 앞으로 한칸 땡기기
  79.     dq->front = (dq->front - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
  80. }
  81.  
  82. // 뒤에 제거
  83. element Deque_RemoveRear(Deque* dq)
  84. {
  85.     if (Deque_IsEmpty(dq))
  86.     {
  87.         fprintf(stderr, "Cannot remove front : Deque is empty!\n");
  88.         exit(1);
  89.     }
  90.     // rear자리에 있는 값을 임시저장
  91.     element temp = dq->arr[dq->rear];
  92.     // rear를 앞으로 한칸 땡기기
  93.     dq->rear = (dq->rear - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
  94.  
  95.     // 임시저장한 rear값 리턴
  96.     return temp;
  97. }
  98.  
  99. // 덱 출력
  100. void Deque_Print(Deque* dq)
  101. {
  102.     if (Deque_IsEmpty(dq))
  103.     {
  104.         printf("Deque is empty!\n");
  105.         return;
  106.     }
  107.     // front 자리부터 rear를 향해 출력
  108.     int i = dq->front;
  109.     for (; i != dq->rear; i = (i + 1) % MAX_QUEUE_SIZE)
  110.     {
  111.         // 다만 front는 비어있는 것으로 하기로 했기때문에
  112.         // 빈칸으로 출력
  113.         if (i == dq->front)  
  114.             printf("   | ");
  115.         else
  116.             printf("%2d | ", dq->arr[i]);
  117.     }
  118.     // 맨 마지막 rear값 출력
  119.     printf("%2d\n",dq->arr[i]);
  120. }
  121.  
  122.  
  123. int main()
  124. {
  125.     Deque dq;
  126.     Deque_Init(&dq);
  127.  
  128.     Deque_InsertRear(&dq, 10);
  129.     Deque_InsertRear(&dq, 20);
  130.     Deque_InsertRear(&dq, 30);
  131.  
  132.     Deque_InsertFront(&dq, 99);
  133.     Deque_InsertFront(&dq, 88);
  134.  
  135.     Deque_Print(&dq);
  136.  
  137.     printf("removed %2d from rear...\n", Deque_RemoveRear(&dq));
  138.     printf("removed %2d from front...\n", Deque_RemoveFront(&dq));
  139.  
  140.     Deque_Print(&dq);
  141.  
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment