#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);
}
}
*/