728x90
반응형
#include<stdio.h>
#include<stdlib.h>
#define MAX_QUEUE_SIZE 100
int data[MAX_QUEUE_SIZE];
int front;
int rear;
void init_queue() { front = rear = 0; }
void add_rear(int val) { enqueue(val); }
int delete_front() { return dequeue(); }
int get_front() { return peek(); }
int is_empty() { return front == rear; }
int is_full() { return front == (rear + 1) % MAX_QUEUE_SIZE; }
int size() { return (rear - front + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE; }
void enqueue(int val) {
if (!is_full()) {
rear = (rear + 1) % MAX_QUEUE_SIZE;
data[rear] = val;
}
}
int dequeue() {
if (!is_empty()) {
front = (front + 1) % MAX_QUEUE_SIZE;
return data[front];
}
}
int peek() {
if (!is_empty()) {
return data[(front + 1) % MAX_QUEUE_SIZE];
}
}
void add_front(int val) {
if (!is_full()) {
data[front] = val;
front = (front - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}
}
int delete_rear() {
}
void print_queue(char msg[]) {
int i, maxi = rear;
if (front >= rear) maxi += MAX_QUEUE_SIZE;
printf("%s[%2d]= ", msg, size());
for (i = front + 1; i <= maxi; i++) {
printf("%2d ", data[i % MAX_QUEUE_SIZE]);
}
printf("\n");
}
void main() {
int i;
init_queue();
for (int i = 1; i < 10; i++) {
enqueue(i);
}
print_queue("선형큐 equeue 9회");
printf("\tdequeue() --> %d\n", dequeue());
printf("\tdequeue() --> %d\n", dequeue());
printf("\tdequeue() --> %d\n", dequeue());
print_queue("선형큐 equeue 3회");
}
728x90
반응형