/* Adds a link to the back of the deque param: q - pointer to the deque param: val - value for the link to be added pre: q is not null post: a link storing val is added to the back of the deque */ void addBackCirListDeque( struct cirListDeque *q, TYPE val ) { assert( q != NULL ); /* Update information about q itself */ q->size += 1; /* Create a new link with the value */ struct DLink *newLnk = _createLink( val ); /* Update pointers of new link */ _addLinkAfter( q, q->Sentinel->prev, newLnk ); }
/* Adds a link after another link param: q pointer to the deque param: lnk pointer to the existing link in the deque param: v value of the new link to be added after the existing link pre: q is not null pre: lnk is not null pre: lnk is in the deque post: the new link is added into the deque after the existing link */ void _addLinkAfter(struct cirListDeque *q, struct DLink *lnk, TYPE v) { /* DONE: you must write this */ assert(q != 0 && lnk != 0); struct DLink *newLink = _createLink(v); lnk->next->prev = newLink; newLink->next = lnk->next; lnk->next = newLink; newLink->prev = lnk; q->size++; }
/* Adds a link after another link param: q pointer to the deque param: lnk pointer to the existing link in the deque param: v value of the new link to be added after the existing link pre: q is not null pre: lnk is not null pre: lnk is in the deque post: the new link is added into the deque after the existing link */ void _addLinkAfter(struct cirListDeque *q, struct DLink *lnk, TYPE v){ struct DLink *newLink = _createLink(v); assert(q); assert(lnk); newLink->prev = lnk; newLink->next = lnk->next; lnk->next->prev = newLink; lnk->next = newLink; q->size++; }
/* Adds a link to the front of the deque param: q pointer to the deque param: val value for the link to be added pre: q is not null post: a link storing val is added to the front of the deque */ void addFrontCirListDeque(struct cirListDeque *q, TYPE val) { struct DLink * lnk = _createLink(val); if (isEmptyCirListDeque(q)) { /* if q is currently empty, the added link becomes the only link in the deque */ lnk->next = lnk; lnk->prev = lnk; q->size++; /* change last to the newly added link */ q->last = lnk; } else _addLinkAfter(q, q->last, lnk); }
/* Adds a link after another link param: q pointer to the deque param: lnk pointer to the existing link in the deque param: v value of the new link to be added after the existing link pre: q is not null pre: lnk is not null pre: lnk is in the deque post: the new link is added into the deque after the existing link */ void _addLinkAfter(struct cirListDeque *q, struct DLink *lnk, TYPE v) { /* FIXME: you must write this */ /* check null */ assert(q != NULL); assert(lnk != NULL); assert(lnk->next != 0); assert(lnk->prev != 0); /* create the link and update pointers */ struct DLink *newLink = _createLink(v); lnk->next->prev = newLink; newLink->next = lnk->next; lnk->next = newLink; newLink->prev = lnk; q->size++; }
/* Adds a link after another link param: q pointer to the deque param: lnk pointer to the existing link in the deque param: v value of the new link to be added after the existing link pre: q is not null pre: lnk is not null pre: lnk is in the deque post: the new link is added into the deque after the existing link */ void _addLinkAfter(struct cirListDeque *q, struct DLink *lnk, TYPE v) { /* FIXME: you must write this */ assert (q != 0); /* q is not null */ assert (lnk != 0); /* lnk is not null */ /* lnk is in the deque */ assert (lnk->next != 0); assert (lnk->prev != 0); /*assign a pointer to iterate through list*/ struct DLink *newLink = _createLink(v); newLink->next = lnk->next; /* pass lnk's next to the newLink */ newLink->prev = lnk; lnk->next->prev = newLink; lnk->next = newLink; q->size++; }