#include<stdio.h>

#include<stdlib.h>


#define MAX_QUEUE_SIZE 6

typedef char 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];

}


void display_everything(QueueType *q){

int i,len;

len = (q->front)-(q->rear);

i=(q->front)+1;

while(i<MAX_QUEUE_SIZE){

printf("%c\n", q->queue[i]);

i++;

}

i=0;

while(i<len){

printf("%c\n", q->queue[i]);

i++;

}

}


int main(void){

QueueType q;

//int test1,test2;


init(&q);


enqueue(&q,'a');

enqueue(&q,'b');

enqueue(&q,'c');

enqueue(&q,'d');

enqueue(&q,'e');

printf("출력 : %c\n",dequeue(&q));

printf("출력 : %c\n",dequeue(&q));

printf("출력 : %c\n",dequeue(&q));

enqueue(&q,'f');

enqueue(&q,'g');


/*test1 = q.front;

test2 = q.rear;

printf("front :%d\n", test1);

printf("rear : %d\n", test2);

printf("0 : %c\n",q.queue[0]);

printf("1 : %c\n",q.queue[1]);

printf("2 : %c\n",q.queue[2]);

printf("3 : %c\n",q.queue[3]);

printf("4 : %c\n",q.queue[4]);

printf("5 : %c\n",q.queue[5]);*/

display_everything(&q);

return 0;

}

'개발 > Algorithm' 카테고리의 다른 글

정적->동적 트리 미완성  (0) 2014.05.28
배열 트리  (0) 2014.05.26
연결 큐 미완성  (0) 2014.05.19
배열 큐  (0) 2014.05.14
후위 표기식 계산  (0) 2014.05.07
블로그 이미지

잉비니

,