#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_QUEUE_SIZE 10
#define MAX_SIZE 100
#define RANDOM_SIZE 30000
typedef int element;
typedef struct{
element queue[MAX_QUEUE_SIZE];
int front, rear;
}QueueType;
void error(char *message){
fprintf(stderr,"%s\n",message);
exit(1);
}
void init(QueueType *q){
q->front = q->rear = 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))
error("Q is fUll");
q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
q->queue[q->rear] = item;
}
element dequeue(QueueType *q){
if (is_empty(q))
error("Q is empty");
q->front = (q->front+1) % MAX_QUEUE_SIZE;
return q->queue[q->front];
}
int main(void){
QueueType q;
int n, i, r, temp;
init(&q);
n = MAX_SIZE;
r = RANDOM_SIZE;
srand((unsigned int)time(NULL));
for( i = 0 ;i<n; i++){
temp = rand() % r;
if( temp % 2 == 0 && is_full(&q) == 0){
enqueue(&q,temp);
printf("enqueue [%d] << %d\n", i, temp);
} else if (temp % 2 == 1 && is_empty(&q) == 0){
printf("dequeue [%d] >> %d, rand : %d \n", i, dequeue(&q),temp);
}
}
}
'이전것 > Algorithm' 카테고리의 다른 글
자체참조 구조체 스택을 활용한 트리의 순회 (0) | 2015.06.03 |
---|---|
Delegate Memorial Code (0) | 2015.05.26 |
All New 이중연결리스트 (0) | 2015.05.11 |
실습과제#2 (0) | 2014.09.26 |
Insertion_Sort (0) | 2014.09.23 |