#define MAXQUEUE 5
typedef struct {
char data[MAXQUEUE];
int rear;
int front;
}QUEUE;
void initial(QUEUE *q);
int enqueue(QUEUE *q,char item);
int dequeue(QUEUE *q,char *ret);
void initial(QUEUE *q){
q->front=-1;
q->rear=-1;
}
int enqueue(QUEUE *q,char item)
{
if (q->rear == MAXQUEUE){
printf("FULL QUEUE !!! \n");
return 0;
}
else
{
q->rear++;
q->data[q->rear] = item;
printf("\n\t enqueue <- %c",q->data[q->rear]);
}
return 1;
}
} int dequeue(QUEUE *q,char *ret)
{
if (q->front== q->rear)
{
printf("Empty QUEUE !!! \n");
return 0;
}
else
{
q->front++;
*ret = q->data[q->front];
}
return 1;
}
void main(){
QUEUE q;
char *element="ABCDE";
char r;
int i=0;
initial(&q);
// enqueue
printf("ENQUEUE \n");
while(enqueue(&q,element[i]))
i++;
// dequeue
printf("\n DEQUEUE \n");
while(dequeue(&q,&r))
{
printf("\n\t dequeue ->%c",r);
}
}
No comments:
Post a Comment