<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Doubly-linked list &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/doubly-linked-list/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>on web development</description>
	<lastBuildDate>Tue, 13 Feb 2018 08:18:15 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=5.0.3</generator>
	<item>
		<title>Construct a Sorted PHP Linked List</title>
		<link>/2010/09/29/construct-a-sorted-php-linked-list/</link>
		<comments>/2010/09/29/construct-a-sorted-php-linked-list/#comments</comments>
		<pubDate>Wed, 29 Sep 2010 18:24:22 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[B-tree]]></category>
		<category><![CDATA[Binary trees]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Doubly-linked list]]></category>
		<category><![CDATA[Graph theory]]></category>
		<category><![CDATA[Kd-tree]]></category>
		<category><![CDATA[Linked lists]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Node]]></category>
		<category><![CDATA[Technology/Internet]]></category>

		<guid isPermaLink="false">/?p=2006</guid>
		<description><![CDATA[This is really a draft, but I somehow decided to post it. Here&#8217;s a simple linked list in PHP, where only the add() method is slightly different. When you have an item to add it&#8217;s placed on the &#8220;right&#8221; place &#8211; the result is a sorted list. Few notes before the code! This list is &#8230; <a href="/2010/09/29/construct-a-sorted-php-linked-list/" class="more-link">Continue reading <span class="screen-reader-text">Construct a Sorted PHP Linked List</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2012/06/14/computer-algorithms-linked-list-data-structure/" rel="bookmark" title="Computer Algorithms: Linked List">Computer Algorithms: Linked List </a></li>
<li><a href="/2012/07/17/computer-algorithms-detecting-and-breaking-a-loop-in-a-linked-list/" rel="bookmark" title="Computer Algorithms: Detecting and Breaking a Loop in a Linked List">Computer Algorithms: Detecting and Breaking a Loop in a Linked List </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2010/08/27/php-what-is-more-powerful-than-list/" rel="bookmark" title="PHP: What is More Powerful Than list()">PHP: What is More Powerful Than list() </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<p>This is really a draft, but I somehow decided to post it. Here&#8217;s a simple linked list in PHP, where only the add() method is slightly different. When you have an item to add it&#8217;s placed on the &#8220;right&#8221; place &#8211; the result is a sorted list.</p>
<p>Few notes before the code! This list is designed for integers, but I plan to &#8220;extend&#8221; it somehow and it can be improved a lot. In my future posts I&#8217;ll post something more about the complexity of the algorithm and an analysis of it.</p>
<pre lang="php">
class Node
{
    public $data;
    public $next;
    public $prev;

    public function __construct($data, $prev, $next)
    {
        $this->data = $data;
        $this->prev = $prev;
        $this->next = $next;
    }
}

class LinkedList
{
    protected $front = null;

    public function add($data)
    {
        if (!$this->front) {
            $node = new Node($data, null, null);
            $this->front = &$node;
        } else {
            if ($data < $this->front->data) {
                $node = new Node($data, null, $this->front);
                $this->front = $node;
                return;
            }

            $current = $this->front;
            while ($current) {
                if ($current->data < $data &#038;&#038; isset($current->next) && $current->next->data > $data) {
                    $node = new Node($data, $current, $current->next);
                    $current->next = $node;
                }
                if ($current->data < $data &#038;&#038; !isset($current->next)) {
                    $node = new Node($data, $current, $current->next);
                    $current->next = $node;
                }
                $current = $current->next;
            }
        }
    }

    public function printl()
    {
        $current = &$this->front;
        while($current) {
            echo $current->data, '<br />';
            $current = $current->next;
        }
    }
}

$list = new LinkedList();
$list->add(13);
$list->add(14);
$list->add(15);
$list->add(11);
$list->add(12);
$list->add(17);
$list->add(10);
$list->add(16);

$list->printl();
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2012/06/14/computer-algorithms-linked-list-data-structure/" rel="bookmark" title="Computer Algorithms: Linked List">Computer Algorithms: Linked List </a></li>
<li><a href="/2012/07/17/computer-algorithms-detecting-and-breaking-a-loop-in-a-linked-list/" rel="bookmark" title="Computer Algorithms: Detecting and Breaking a Loop in a Linked List">Computer Algorithms: Detecting and Breaking a Loop in a Linked List </a></li>
<li><a href="/2012/07/24/php-arrays-or-linked-lists/" rel="bookmark" title="PHP: Arrays or Linked Lists?">PHP: Arrays or Linked Lists? </a></li>
<li><a href="/2010/08/27/php-what-is-more-powerful-than-list/" rel="bookmark" title="PHP: What is More Powerful Than list()">PHP: What is More Powerful Than list() </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/09/29/construct-a-sorted-php-linked-list/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
