<?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>Tree traversal &#8211; stoimen&#039;s web log</title>
	<atom:link href="/tag/tree-traversal/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>Friday Algorithms: A Data Structure: JavaScript Stack</title>
		<link>/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/</link>
		<comments>/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/#respond</comments>
		<pubDate>Fri, 16 Jul 2010 11:02:40 +0000</pubDate>
		<dc:creator><![CDATA[Stoimen]]></dc:creator>
				<category><![CDATA[algorithms]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Abstract data type]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Control flow]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Linked list]]></category>
		<category><![CDATA[search algorithms]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Stack]]></category>
		<category><![CDATA[Subroutine]]></category>
		<category><![CDATA[Technology/Internet]]></category>
		<category><![CDATA[Tree traversal]]></category>

		<guid isPermaLink="false">/?p=1713</guid>
		<description><![CDATA[Algorithms and Data Structures Instead of writing about &#8220;pure&#8221; algorithms this time I&#8217;ve decided to write about data structures and to start with a stack implementation in JavaScript. However algorithms and data structures live together since the very beginning of the computer sciences. Another reason to write about data structures is that many algorithms need &#8230; <a href="/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/" class="more-link">Continue reading <span class="screen-reader-text">Friday Algorithms: A Data Structure: JavaScript Stack</span> <span class="meta-nav">&#8594;</span></a><div class='yarpp-related-rss'>

Related posts:<ol>
<li><a href="/2010/06/18/friday-algorithms-iterative-quicksort/" rel="bookmark" title="Friday Algorithms: Iterative Quicksort">Friday Algorithms: Iterative Quicksort </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2012/06/05/computer-algorithms-stack-and-queue-data-structure/" rel="bookmark" title="Computer Algorithms: Stack and Queue">Computer Algorithms: Stack and Queue </a></li>
<li><a href="/2010/07/02/friday-algorithms-javascript-merge-sort/" rel="bookmark" title="Friday Algorithms: JavaScript Merge Sort">Friday Algorithms: JavaScript Merge Sort </a></li>
</ol>
</div>
]]></description>
				<content:encoded><![CDATA[<h2>Algorithms and Data Structures</h2>
<p><a href="/wp-content/uploads/2010/06/stack.jpg"><img class="aligncenter size-full wp-image-1721" title="stack" src="/wp-content/uploads/2010/06/stack.jpg" alt="stack" width="430" height="286" srcset="/wp-content/uploads/2010/06/stack.jpg 430w, /wp-content/uploads/2010/06/stack-300x199.jpg 300w" sizes="(max-width: 430px) 100vw, 430px" /></a></p>
<p>Instead of writing about &#8220;pure&#8221; algorithms this time I&#8217;ve decided to write about data structures and to start with a stack implementation in JavaScript. However algorithms and data structures live together since the very beginning of the computer sciences. Another reason to write about data structures is that many algorithms need a specific data structure to be implemented. Most of the search algorithms are data structure dependent. You know that searching into a tree is different from searching into a linked list.</p>
<p>I&#8217;d like to write more about searching in my future algorithm posts, but first we need some data structure examples. The first one is, as I mentioned &#8211; <a href="http://en.wikipedia.org/wiki/Stack_%28data_structure%29">stack</a>.</p>
<p>Implemented in JavaScript this is really a simple example, which don&#8217;t need much to be understood. But in first place what is a stack?</p>
<p>You can thing of the &#8220;computer science&#8221; stack as a stack of sheets of paper, as it&#8217;s shown on the picture. You&#8217;ve three basic operations. You can add new element to the stack by putting it on the top of the stack, so the previous top of the stack becomes the second element in the stack. Another operation is to &#8220;pop&#8221; from the stack &#8211; remove the &#8220;first&#8221; element in the stack &#8211; the top most element, and to return it. And finally print the stack. This is difficult to define as an operation, but let say you&#8217;ve to show somehow every element from the stack.</p>
<p>Here&#8217;s a little diagram:</p>
<p><a href="/wp-content/uploads/2010/07/stack.png"><img class="alignleft size-full wp-image-1837" title="stack" src="/wp-content/uploads/2010/07/stack.png" alt="Stack" width="378" height="130" srcset="/wp-content/uploads/2010/07/stack.png 378w, /wp-content/uploads/2010/07/stack-300x103.png 300w" sizes="(max-width: 378px) 100vw, 378px" /></a></p>
<h2>Source Code</h2>
<p>At the end some source code:</p>
<pre lang="javascript">
var node = function()
{
    var data;
    var next = null;
}

var stack = function()
{
    this.top = null;

    this.push = function(data) {
        if (this.top == null) {
            this.top = new node();
            this.top.data = data;
        } else {
            var temp = new node();
            temp.data = data;
            temp.next = this.top;
            this.top = temp;
        }
    }

    this.pop = function() {
        var temp = this.top;
        var data = this.top.data;
        this.top = this.top.next;
        temp = null;
        return data;
    }

    this.print = function() {
        var node = this.top;
        while (node != null) {
            console.log(node.data);
            node = node.next;
        }
    }
}

var s = new stack();

s.push(1);
s.push(2);
s.push(3);

s.print();

var a = s.pop();

s.print();
</pre>
<div class='yarpp-related-rss'>
<p>Related posts:<ol>
<li><a href="/2010/06/18/friday-algorithms-iterative-quicksort/" rel="bookmark" title="Friday Algorithms: Iterative Quicksort">Friday Algorithms: Iterative Quicksort </a></li>
<li><a href="/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/" rel="bookmark" title="Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript">Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript </a></li>
<li><a href="/2012/06/05/computer-algorithms-stack-and-queue-data-structure/" rel="bookmark" title="Computer Algorithms: Stack and Queue">Computer Algorithms: Stack and Queue </a></li>
<li><a href="/2010/07/02/friday-algorithms-javascript-merge-sort/" rel="bookmark" title="Friday Algorithms: JavaScript Merge Sort">Friday Algorithms: JavaScript Merge Sort </a></li>
</ol></p>
</div>
]]></content:encoded>
			<wfw:commentRss>/2010/07/16/friday-algorithms-a-data-structure-javascript-stack/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
