Trouble inserting into a linked list object

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jonathan Moss

    Trouble inserting into a linked list object

    I have (almost) succesfully created a linked list data structure class. It
    will quite happily add items to to the end of the list (once I found that
    objects are not passed by reference :). What I am having problems with is
    inserting an item into the middle of the list.

    The code below is the addAfter function. As It's name suggests it is
    supposed to add a new list item after the one specified by $prev with what
    ever was after $prev now coming after the new item. Any suggestions would be
    much appreciated.

    regards,
    Jon

    function addAfter($prev, $content){
    $b = true;
    $current = &$this->first;
    while($b){
    if ($prev == $current){
    $item = new ListItem();
    $item->content = $content;
    $item->next = &$current->next;
    $current->next = &$item;
    $this->count++;
    }
    if (is_null($curre nt->next)){
    $b = false;
    }else{
    $current = &$current->next;
    }
    }
    $this->resetLast();
    }


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.566 / Virus Database: 357 - Release Date: 22/01/2004


Working...