Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. /**
  2. * ~~~ Pointer magic ~~~
  3. * The address of the next pointer of the previous element
  4. * In case of index == 0: Address of the head
  5. * @param index the index
  6. * @return the address of the next pointer of the previous element
  7. */
  8. std::shared_ptr<ListNode<T>> *prevElementNextPointer(unsigned int index) const {
  9. auto *to_return = const_cast<std::shared_ptr<ListNode<T>> *>(&head);
  10.  
  11. if (2 * index <= length + 3) {
  12. for (unsigned int i = 0; i < index; i++)
  13. to_return = &((*to_return)->next);
  14. } else {
  15. ListNode<T> *curr_pointer = (*to_return).get();
  16. for (unsigned int i = length + 1; i >= index; i--)
  17. curr_pointer = (curr_pointer->prev).lock().get();
  18. to_return = &curr_pointer->next;
  19. }
  20.  
  21. return to_return;
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement