#include <stdio.h>
#include <stdlib.h>
struct part {
int value;
struct part* next;
};
struct part* tail;
int count;
void initialze() {
tail = NULL;
count = 0;
}
void enterq(int val) {
struct part* p;
p
= malloc(sizeof(struct part
)); p->value = val;
if (tail == NULL) {
tail = p;
tail->next = p;
} else {
p->next = tail->next;
tail->next = p;
tail = p;
}
count++;
}
int removeq(void) {
int val;
struct part* p;
if (tail == NULL) {
return -1;
} else {
p = tail->next;
if (p == tail) {
tail = NULL;
} else {
tail->next = p->next;
}
val = p->value;
count--;
return val;
}
}
void display() {
struct part* p;
p = tail;
if (p != NULL) {
do {
p = p->next;
} while (p != tail);
}
}
int main(void) {
int mode, id;
initialze();
mode = 1;
while (mode) {
printf("queue process ? enter(1) or remove(0) ="); if (mode == 1) {
enterq(id);
} else if (mode == 0) {
id = removeq();
if (id > 0)
printf("id = %d was removed\n", id
); }
display();
printf("?continue(1) or quit(0) = "); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCnN0cnVjdCBwYXJ0IHsKICAgIGludCB2YWx1ZTsKCXN0cnVjdCBwYXJ0KiBuZXh0Owp9OwoKc3RydWN0IHBhcnQqIHRhaWw7CmludCBjb3VudDsKCnZvaWQgaW5pdGlhbHplKCkgewoJdGFpbCA9IE5VTEw7Cgljb3VudCA9IDA7Cn0KCnZvaWQgZW50ZXJxKGludCB2YWwpIHsKCXN0cnVjdCBwYXJ0KiBwOwoJcCA9IG1hbGxvYyhzaXplb2Yoc3RydWN0IHBhcnQpKTsKCXAtPnZhbHVlID0gdmFsOwoJaWYgKHRhaWwgPT0gTlVMTCkgewoJCXRhaWwgPSBwOwoJCXRhaWwtPm5leHQgPSBwOwoJfSBlbHNlIHsKCQlwLT5uZXh0ID0gdGFpbC0+bmV4dDsKCQl0YWlsLT5uZXh0ID0gcDsKCQl0YWlsID0gcDsKCX0KCWNvdW50Kys7Cn0KCmludCByZW1vdmVxKHZvaWQpIHsKCWludCB2YWw7C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