Exercise Show a code snippet for finding where to insert a new node in a doubly
ID: 3586508 • Letter: E
Question
Exercise
Show a code snippet for finding where to insert a new node in a doubly linked ordered list which uses a header and a footer node.
Assume:
list->pHeader points to a header node.
list->pFooter points to a footer node.
pNew already has been allocated and contains the element.
pPrecedes is used to point to the node that precedes where we want pNew inserted.
*program is in c*
Exercise
Show a code snippet for finding where to insert a new node in a doubly linked ordered list which uses a header and a footer node.
Assume:
list->pHeader points to a header node.
list->pFooter points to a footer node.
pNew already has been allocated and contains the element.
pPrecedes is used to point to the node that precedes where we want pNew inserted.
*program is in c*
Explanation / Answer
void insert(struct node **Head, struct node **Foot, struct node *pNew, struct node *pPrecedes)
{
struct node *pHeader = *Head, *pFooter = *Foot;
pNew->next = pPrecedes->next;
pNew->prev = pPrecedes;
if(pPrecedes!=NULL)
pPrecedes->next = pNew;
else
pHeader = pNew;
if(pPrecedes->next!=NULL)
pPrecedes->next->prev = pNew;
else
pFooter = pNew;
*Head = pHeader;
*Foot = pFooter;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.