Linked List - Question 5 Answers

5.a, 5.b, and 5.c

\caption{Doubly Linked Lists}
\REQUIRE item = item to insert
\REQUIRE predecessor = linked list item that should precede item
\FUNCTION{insert}{item, predecessor}
  \STATE successor $\gets \text{predecessor}_{\text{next}}$
  \STATE $\text{item}_{next} \gets$ address of successor
  \STATE $\text{item}_{previous} \gets$ address of predecessor
  \STATE $\text{predecessor}_{\text{next}} \gets$ address of item
  \STATE $\text{successor}_{\text{previous}} \gets$ address of item
\REQUIRE item = item to delete
  \STATE successor $\gets \text{item}_{\text{next}}$
  \STATE predecessor $\gets \text{item}_{\text{previous}}$
  \STATE $\text{successor}_{\text{previous}} \gets$ address of predecessor
  \STATE $\text{predecessor}_{\text{next}} \gets$ address of successor
\REQUIRE head = head of the linked list
\REQUIRE item = item to insert
\REQUIRE index = zero based index indicating where to insert the item
\FUNCTION{insertAt}{head, item, index}
  \IF{index = 0}
    \STATE $\text{item}_{next} \gets$ address of head
    \STATE $\text{head}_{previous} \gets$ address of item
    \STATE head = item
  \STATE predecessor $\gets$ head
  \FOR{$i \gets 0$ to index - 1}
      \STATE predecessor $\gets \text{predecessor}_{next}$
  \STATE \CALL{insert}{item, predecessor}

Return to Exercises