UVa-133-The Dole Queue
AOAPC I: Beginning Algorithm Contests (Rujia Liu) :: Volume 2. Data Structures :: Lists
// 133 - The Dole Queue #include <cstdio> #include <cstdlib> typedef struct node { int num; struct node* pre; struct node* next; }*ptr; ptr creatnode(int elem) { ptr temp; temp = (struct node*)malloc(sizeof(struct node)); if(temp == NULL) exit(EXIT_FAILURE); temp->num = elem; temp->pre = NULL; temp->next = NULL; return temp; } void creatlist(ptr* x, ptr* y, int n) { ptr head, temp; head = creatnode(1); temp = head; for(int i = 2; i <= n; i++) { temp->next = creatnode(i); temp->next->pre = temp; temp = temp->next; } temp->next = head; head->pre = temp; *x = head; *y = temp; } void deletenode(ptr t) { ptr temp = t; temp->next->pre = t->pre; temp->pre->next = t->next; printf("%3d", t->num); free(t); } int main(void) { int i, n, k, m, cnt; ptr x, y, x_t, y_t; while(scanf("%d%d%d", &n, &k, &m) && n) { cnt = 0; creatlist(&x, &y, n); while(cnt != n) { for(i = 1; i < k; i++) x = x->next; for(i = 1; i < m; i++) y = y->pre; if(x == y) { x_t = x->next; y_t = y->pre; deletenode(x); cnt++; } else if(x->next == y) { x_t = y->next; y_t = x->pre; deletenode(x); deletenode(y); cnt += 2; } else { x_t = x->next; y_t = y->pre; deletenode(x); deletenode(y); cnt += 2; } x = x_t; y = y_t; if(cnt != n) printf(","); } printf("\n"); } return 0; }