공부/자료구조

큐~

choryDev 2019. 5. 31. 10:39

수업시간에 한 걸 정리 한 것입니다.

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 100
 
typedef int element;
 
typedef struct {
    element queue[MAX_QUEUE_SIZE];
    int rear;
    int front;
}QueueType;
 
void init(QueueType *tmp) {
    tmp->rear = 0;
    tmp->front = 0;
}
 
// 공백 상태 검출 함수
int is_empty(QueueType *q)
{
    return (q->front == q->rear);
}
 
// 포화 상태 검출 함수
int is_full(QueueType *q)
{
    return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}
 
// 삽입 함수
void enqueue(QueueType *q, element item)
{
    if (is_full(q)) {
        printf("큐가 포화상태입니다");
        exit(1);
    }
    q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
    q->queue[q->rear] = item;
}
 
// 삭제 함수
element dequeue(QueueType *q)
{
    if (is_empty(q)) {
        printf("큐가 공백상태입니다");
        exit(1);
    }
    q->front = (q->front + 1) % MAX_QUEUE_SIZE;
    return q->queue[q->front];
}
 
int get_count(QueueType *q)
{
    int count = q->rear - q->front;
    if (count < 0) count += MAX_QUEUE_SIZE;
    return count;
}
 
void main() {
    QueueType A;
    int i;
    init(&A);
 
    for (i = 0; i < 3; i++) {
        enqueue(&A, i);
        printf("size : %d\n", get_count(&A));
    }
 
    for (i = 0; i < 3; i++) {
        printf("뺀 요소 값 : %d", dequeue(&A));
        printf("size : %d\n", get_count(&A));
    }
}
cs

'공부 > 자료구조' 카테고리의 다른 글

스택 괄호검사  (0) 2019.05.24
더블링크드 리스트 c 언어  (0) 2019.05.17
링크드 리스트 2개 오름차순으로 병합하기  (0) 2019.05.10
C로 리스트 구현하기~  (0) 2019.05.03