#include<stdio.h>
#include<stdlib.h>

typedef int element;
typedef struct QueueNode{
        element item;
        struct QueueNode *link;
}QueueNode;

typedef struct {
        QueueNode *front,*rear;
}QueueType;

void error(char *message);
void init(QueueType *q);
int is_empty(QueueType *q);
void enqueue(QueueType *q,element item);
element dequeue(QueueType *q);
//void display_linked_queue(QueueType *q);

int main(void){
    QueueType q;
   
    init(&q);
 
    enqueue(&q,1);
    enqueue(&q,2);
    enqueue(&q,3);
 
   // display_linked_queue(&q);
    printf("dequeue()=%d\n",dequeue(&q));

    printf("dequeue()=%d\n",dequeue(&q));
 
    printf("dequeue()=%d\n",dequeue(&q));
 
   
    return 0;
}

void error(char *message){
     fprintf(stderr,"%s",message);
     exit(1);
}

void init(QueueType *q){
      q->front = q->rear = NULL;
}

int is_empty(QueueType *q){
    return (q->front == NULL);
}

void enqueue(QueueType *q, element item){
     QueueNode *temp = (QueueNode *)malloc(sizeof(QueueNode));
     if(temp == NULL)
        error("MEMORY ALLOC ERROR");
     else{
          temp->item = item;
          temp->link = NULL;
          if( is_empty(q)){
              q->front = temp;
              q->rear = temp;
          }
          else{
               q->rear->link = temp;
               q->rear = temp;
          }
     }
}

element dequeue(QueueType *q){
        QueueNode *temp = q->front;
        element item;
       
        if( is_empty(q)){
            error("Queue is empty");
        }
        else{
             item = temp -> item;
             q->front = q->front-> link;
             if( q->front == NULL ){
                 q->rear = NULL;
             }
             free(temp);
             return item;
        }
}
/*
void display_linked_queue(QueueType *q){
 
     for( q = q->front ; q != q->rear ; q = q ->front->link){
          printf("%d<-",q->front->item);
     }
}


*/

'이전것 > Algorithm' 카테고리의 다른 글

배열 트리  (0) 2014.05.26
배열 큐 전체출력  (0) 2014.05.21
배열 큐  (0) 2014.05.14
후위 표기식 계산  (0) 2014.05.07
괄호 검사 프로그램  (1) 2014.05.07
블로그 이미지

잉비니

,