void *Ring_add(T ring, int pos, void *x) { assert(ring); assert(pos >= -ring->length && pos<=ring->length+1); if (pos == 1 || pos == -ring->length) return Ring_addlo(ring, x); else if (pos == 0 || pos == ring->length + 1) return Ring_addhi(ring, x); else { struct node *p, *q; int i = pos < 0 ? pos + ring->length : pos - 1; { int n; q = ring->head; if (i <= ring->length/2) for (n = i; n-- > 0; ) q = q->rlink; else for (n = ring->length - i; n-- > 0; ) q = q->llink; } NEW(p); { p->llink = q->llink; q->llink->rlink = p; p->rlink = q; q->llink = p; } ring->length++; return p->value = x; } }
void *Ring_addlo(T ring, void *x){ assert(ring); Ring_addhi(ring, x); ring->head = ring->head->l_link; return x; }
T Ring_ring(void *x, ...) { va_list ap; T ring = Ring_new(); va_start(ap, x); for ( ; x; x = va_arg(ap, void *)) Ring_addhi(ring, x); va_end(ap); return ring; }
void *Ring_add(T ring, int pos, void *x){ assert(ring); assert(pos >= -ring->length && pos <= ring->length); if (pos == 1 || pos == -ring->length){ return Ring->addlo(ring, x); } else if (pos == 0 || pos == ring->length + 1){ return Ring_addhi(ring, x); } else{ struct node *p; struct node *q; int i = pos < 0 ? pos + ring->length : pos - 1; // q <- ith node { int n; q = ring->head; if (i <= ring->length / 2){ for (n = i; n-- > 0; ){ q = q->r_link; } } else{ for (n = ring->length - i; n-- > 0; ){ q = q->l_link; } } } NEW(p); // insert p to the left of q p->r_link = q; p->l_link = q->l_link; q->l_link->r_link = p; q->l_link = p; ring->length++; return p->value = x; } }