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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | #include <stdio.h> //배열을 이용한 큐 만들기 // 큐의 MAX 값 설정 #define MAX_N 100 // front, rear 설정 int front; int rear; int queue[MAX_N]; // 큐를 초기화 -> front 와 rear 를 0으로 초기화. void queueInit(void) { front = 0; rear = 0; } // 큐가 비어있는지 확인 -> front 와 rear가 같으면 비어있음. int queueIsEmpty(void) { return (front == rear); } // 큐가 꽉 차있는지 확인 -> (rear+1)%MAX 값이 front와 같은지 확인 int queueIsFull(void) { if ((rear + 1) % MAX_N == front) { return 1; } else { return 0; } } // PUSH 큐 -> 꽉차있는지 확인. 비어있으면 queue[rear++]=data , rear=MAX 면 0으로 재설정 int queueEnqueue(int value) { if (queueIsFull()) { printf("queue is full!"); return 0; } queue[rear] = value; rear++; if (rear == MAX_N) { rear = 0; } return 1; } // POP 큐 -> 큐가 비어있는지 확인, 차있으면 data = queue[front++], front=MAX면 0으로 재설정. int queueDequeue(int *value) { if (queueIsEmpty()) { printf("queue is empty!"); return 0; } *value = queue[front]; front++; if (front == MAX_N) { front = 0; } return 1; } int main(int argc, char* argv[]) { int T; int N; scanf("%d", &T); for (int test_case = 1; test_case <= T; test_case++) { scanf("%d", &N); queueInit(); for (int i = 0; i < N; i++) { int value; scanf("%d", &value); queueEnqueue(value); printf("setValue"); } printf("#%d ", test_case); while (!queueIsEmpty()) { int value; if (queueDequeue(&value) == 1) { printf("%d ", value); } } printf("\n"); } return 0; } | cs |
반응형
'잡다한 IT > 자료구조' 카테고리의 다른 글
연결 리스트로 완전 이진 탐색(BST) 구현 (0) | 2018.04.24 |
---|---|
연결리스트를 통한 큐 구현 (0) | 2018.04.24 |
연결리스트를 통한 스택 구현 (0) | 2018.04.24 |
링크드 리스트 구현 (0) | 2018.04.23 |
배열을 이용한 스택 (0) | 2018.04.22 |