#include<stdio.h>
#include<stdlib.h>
struct data {
struct data *next;
char key;
};
struct queue {
struct data *top,*rear;
};
void enqueue(struct queue *q, char key);
char dequeue(struct queue *q);
void print_queue_list(struct queue q);
int main() {
struct queue q;
q.top = NULL;
q.rear = NULL;
enqueue(&q, 'a');
print_queue_list(q);
enqueue(&q, 'b');
print_queue_list(q);
dequeue(&q);
print_queue_list(q);
dequeue(&q);
}
void enqueue(struct queue *q, char key) {
struct data *d;
d
= (struct data
*) malloc(sizeof(struct data
)); d->key = key;
d->next = NULL;
if (q->top == NULL) {
q->top = d;
q->rear = d;
} else {
q->rear->next = d;
q->rear = d;
}
}
char dequeue(struct queue *q) {
char key;
struct data *d;
d = q->top;
key = d->key;
if (q->top == q->rear) {
q->top = NULL;
q->rear = NULL;
} else {
q->top = d->next;
}
return key;
}
void print_queue_list(struct queue q) {
while(q.top != NULL) {
q.top = q.top->next;
}
}
I2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8c3RkbGliLmg+CiAKc3RydWN0IGRhdGEgewogIHN0cnVjdCBkYXRhICpuZXh0OwogIGNoYXIga2V5Owp9OwogCnN0cnVjdCBxdWV1ZSB7CiAgc3RydWN0IGRhdGEgKnRvcCwqcmVhcjsKfTsKCnZvaWQgZW5xdWV1ZShzdHJ1Y3QgcXVldWUgKnEsIGNoYXIga2V5KTsKY2hhciBkZXF1ZXVlKHN0cnVjdCBxdWV1ZSAqcSk7CnZvaWQgcHJpbnRfcXVldWVfbGlzdChzdHJ1Y3QgcXVldWUgcSk7CgppbnQgbWFpbigpIHsKICBzdHJ1Y3QgcXVldWUgcTsKICBxLnRvcCA9IE5VTEw7CiAgcS5yZWFyID0gTlVMTDsKICAKICBlbnF1ZXVlKCZxLCAnYScpOwoKICBwcmludF9xdWV1ZV9saXN0KHEpOwogIHByaW50ZigiXG4iKTsKCiAgZW5xdWV1ZSgmcSwgJ2InKTsKCiAgcHJpbnRfcXVldWVfbGlzdChxKTsKICBwcmludGYoIlxuIik7Cgog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