<?xml version='1.0' encoding='UTF-8'?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearchrss/1.0/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-4070434216759110691</atom:id><lastBuildDate>Wed, 04 Mar 2026 20:08:00 +0000</lastBuildDate><category>Linux</category><category>How to</category><category>C</category><category>DataStructures</category><category>Linked List</category><category>Interview Question</category><category>Blogger</category><category>Computer Organization</category><category>Memory</category><category>Signals</category><category>Open Source</category><category>GCC</category><category>Comic</category><category>Hardware</category><category>Kernel</category><category>Stack</category><category>Tin Tin</category><category>Windows</category><category>Announcement</category><category>Facts</category><category>Fun</category><category>Hacking</category><category>India</category><category>Laptop</category><category>Mobile</category><category>Programming</category><category>Security</category><category>Software</category><category>Algorithms</category><category>Audio</category><category>Bit Operations</category><category>Bubble Sorting</category><category>Buffer overflow</category><category>Debugging</category><category>Google</category><category>Google Reader</category><category>Networks</category><category>Sorting</category><category>Video</category><category>Video Series</category><category>cheat-sheet</category><category>ctags</category><category>git</category><category>iptables</category><category>tcpdump</category><title>codingfreak</title><description></description><link>http://codingfreak.blogspot.com/</link><managingEditor>noreply@blogger.com (Unknown)</managingEditor><generator>Blogger</generator><openSearch:totalResults>84</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2315906110485676290</guid><pubDate>Mon, 02 Dec 2013 11:38:00 +0000</pubDate><atom:updated>2014-03-02T03:49:50.474-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Blogger</category><category domain="http://www.blogger.com/atom/ns#">How to</category><title>Displaying MATH formulas in blogger</title><description>Introducing math formulas in blogger was a daunting task in olden days. Now using Mathjax we can include them without any issue. Let us see steps to add support for Mathjax to blogger template.&lt;br /&gt;
&lt;br /&gt;
1. In your blog default home page under &quot;template&quot; section select &quot;Edit HTML&quot; option. Add following line after &amp;lt;head&amp;gt; tag &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;&amp;lt;script type=&quot;text/javascript&quot;
   src=&quot;http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML&quot;&amp;gt;
&amp;lt;/script&amp;gt;&lt;/pre&gt;
&lt;br /&gt;
2. Now we can include math formulas as simple ASCII Math notation.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; We should use ∖( ...∖) for inline math and ∖[ ... ∖] for displayed math.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For inline math&lt;br /&gt;
&lt;br /&gt;
To display \(\left(x-1\right)\left(x+3\right) \sqrt{a^2+b^2}\) as inline formula&lt;br /&gt;
&lt;br /&gt;
Similarly for displayed math&lt;br /&gt;
&lt;br /&gt;
&amp;nbsp;\[\sqrt{a^2+b^2}x = a_0 + \frac{1}x = a_0 + \frac{1}{\displaystyle a_1 + \frac{1}{\displaystyle a_2 + \frac{1}{\displaystyle a_3 + a_4}}}{a_1 + \frac{1}{a_2 + \frac{1}{a_3 + a_4}}}\]
&lt;br /&gt;
References:&lt;br /&gt;
&lt;br /&gt;
1. &lt;a href=&quot;http://docs.mathjax.org/en/v1.1-latest/start.html&quot; target=&quot;_blank&quot;&gt;Mathjax&lt;/a&gt;&lt;br /&gt;
2. &lt;a href=&quot;http://www.codecogs.com/latex/eqneditor.php&quot; target=&quot;_blank&quot;&gt;Online Latex Editor&lt;/a&gt;&amp;nbsp; &lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/12/displaying-math-formulas-in-blogger.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-950935976698756359</guid><pubDate>Mon, 04 Nov 2013 10:32:00 +0000</pubDate><atom:updated>2014-03-03T19:01:43.246-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Algorithms</category><category domain="http://www.blogger.com/atom/ns#">Bubble Sorting</category><category domain="http://www.blogger.com/atom/ns#">Sorting</category><title>Bubble Sorting</title><description>Bubble sort is one of the simple sorting algorithms and also popularly known as a &lt;b&gt;Brute Force Approach&lt;/b&gt;. The logic of the algorithm is very simple as it works by repeatedly iterating through a list of elements, comparing two elements at a time and swapping them if necessary until all the elements are swapped to an order. &lt;br /&gt;
&lt;br /&gt;
For e.g. if we have a list of 10 elements, bubble sort starts by comparing the first two elements in the list. If the second element is smaller than the first element then it exchanges them. Then it compares the current second element with the third element in the list. This continues until the second last and the last element is compared which completes one iteration through the list. By the time it completes the first iteration the largest element in the list comes to the rightmost position.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5-WCW6Kihyphenhyphen2q1AZ8DLoT6y59TgzChNlmJOdPUi0Rxf0aHRC6KORqkP6YR9Pj7tJxeffaW9RmqpTDarI64SkligkuVb2j-mIy_SrCnL9M5cuHYbzuXBX6ay_rrcWAXNKC2iS44hjbtAE0/s1600/BubbleBlowing.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5-WCW6Kihyphenhyphen2q1AZ8DLoT6y59TgzChNlmJOdPUi0Rxf0aHRC6KORqkP6YR9Pj7tJxeffaW9RmqpTDarI64SkligkuVb2j-mIy_SrCnL9M5cuHYbzuXBX6ay_rrcWAXNKC2iS44hjbtAE0/s1600/BubbleBlowing.jpg&quot; height=&quot;364&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Image courtesy from Annieink.com&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The algorithm gets its name as we start from lowest point and “bubble up” the higher elements to the highest point in the list. We can also follow other approach where we start from highest point and “bubble down” lowest elements in the list. Since it only uses comparisons to operate on elements, it is a comparison sort.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h4&gt;
Graphical Example&lt;/h4&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX8dkAZn7NGEjSqcxdSU66kLA07uLH1CM1Kz1TvSISBzIoTDmAjxoWi0na6_3DZ1DTCZSIz7F6hlFhMID6lmXvSNtXjW64mXtE0Q_20VEF79SjO4LMfx0T53EwUEGze4DlTN0w2lEvx70/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX8dkAZn7NGEjSqcxdSU66kLA07uLH1CM1Kz1TvSISBzIoTDmAjxoWi0na6_3DZ1DTCZSIz7F6hlFhMID6lmXvSNtXjW64mXtE0Q_20VEF79SjO4LMfx0T53EwUEGze4DlTN0w2lEvx70/s1600/01.png&quot; height=&quot;400&quot; width=&quot;362&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim0MNapn5Fd54VqF1SSbTcs3140Zj416QDyQNn_0tlNYtYlJif6UQ3q-WEyUqxeTU-kMAUczipXK1epfJq9R5Y4C12Z7M60eHwz1BRwb7qy2OwlYhiWUV9HBYIGtzbfFePy5WIOwG7QiI/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEim0MNapn5Fd54VqF1SSbTcs3140Zj416QDyQNn_0tlNYtYlJif6UQ3q-WEyUqxeTU-kMAUczipXK1epfJq9R5Y4C12Z7M60eHwz1BRwb7qy2OwlYhiWUV9HBYIGtzbfFePy5WIOwG7QiI/s1600/01.png&quot; height=&quot;307&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiHEYTZuLl0rXMIFaNumriZnbBDAjYDHv-7lQj1SlQAIHNCBzpnrB_s7T7h2KKmhlmX1qjDsX3h3C5nQsqfnex19ZkACrw2ui15_Ew3-1KMC_-g9NYTJ8Rwu6eqU7xGKbH80HGvMB0dEw/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiHEYTZuLl0rXMIFaNumriZnbBDAjYDHv-7lQj1SlQAIHNCBzpnrB_s7T7h2KKmhlmX1qjDsX3h3C5nQsqfnex19ZkACrw2ui15_Ew3-1KMC_-g9NYTJ8Rwu6eqU7xGKbH80HGvMB0dEw/s1600/01.png&quot; height=&quot;237&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwmXwtDH6Emjv3AvlX6FldNUeXoXJ1R9uxd0hN2rDNvXeM75mIc_CHWuZZe6-C3rWHMvvLLJ8rcbTdBKeYIDhZ2dUwTJkg0h8DnAlh8TxBdXYKw5cyeu7WRHbweWAM9Fo2dMCL7fsfYVU/s1600/02.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiwmXwtDH6Emjv3AvlX6FldNUeXoXJ1R9uxd0hN2rDNvXeM75mIc_CHWuZZe6-C3rWHMvvLLJ8rcbTdBKeYIDhZ2dUwTJkg0h8DnAlh8TxBdXYKw5cyeu7WRHbweWAM9Fo2dMCL7fsfYVU/s1600/02.png&quot; height=&quot;240&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHAOt4pLHaFk1_v8eFonkd1EZotU8EPjxpXO947wUz0OGSGFTKw3V1zH8RAWiltg7m1yIcETO04d67KGW5ti5TqCzdaSIa7xj22Q9qPUY3sP491W1R3LEJEBCKb38EEFNAgmF8Z0U7Kl0/s1600/03.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgHAOt4pLHaFk1_v8eFonkd1EZotU8EPjxpXO947wUz0OGSGFTKw3V1zH8RAWiltg7m1yIcETO04d67KGW5ti5TqCzdaSIa7xj22Q9qPUY3sP491W1R3LEJEBCKb38EEFNAgmF8Z0U7Kl0/s1600/03.png&quot; height=&quot;243&quot; width=&quot;400&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
As we can see in above example the list sorted by third iteration and it is useless to go for 4th and 5th iterations. So we use a flag which determines whether a swap operation is done in last iteration or not. If a swap operation is not done in last iteration we will stop remaining iterations since the list is already in sorted order. For the above example we will stop iterating once third iteration is done as the whole list is sorted by then.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;
Pseudo-code&lt;/h4&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;procedure bubbleSort( A : list of sortable items )
   repeat     
     swapped = false
     for i = 0 to length(A) - 1 
       if A[i] &amp;gt; A[i+1] then
         swap( A[i], A[i+1] )
         swapped = true
       end if
     end for
   until not swapped
end procedure&lt;/pre&gt;
&lt;br /&gt;
&lt;h4&gt;
Code Snippet
&lt;/h4&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;void bubbleSort(int arr[], int numberOfElements) 
{
  char swapped = 1;
  int tmp, i;

  while (swapped) 
    {
      swapped = 0;

      for (i = 0; i &amp;lt; (numberOfElements - 1); i++) 
        {
          if (arr[i] &amp;gt; arr[i + 1]) 
            {
              tmp = arr[i];
              arr[i] = arr[i + 1];
              arr[i + 1] = tmp;
              swapped = 1;
            }
        }
    }
}&lt;/pre&gt;
As we know, every iteration bubbles up the highest element to the end of the list. So there is no need to compare the same in next iteration. Above logic can be further enhanced by ignoring last n-1 elements when running for the nth iteration. &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;void bubbleSort(int arr[], int numberOfElements) 
{
  char swapped = 1;
  int numberOfElementsToSwap = numberOfElements;
  int tmp, i;

  while (swapped) 
    {
      swapped = 0;
      numberOfElementsToSwap = numberOfElementsToSwap - 1;

      for (i = 0; i &amp;lt; numberOfElementsToSwap; i++) 
        {
          if (arr[i] &amp;gt; arr[i + 1]) 
            {
              tmp = arr[i];
              arr[i] = arr[i + 1];
              arr[i + 1] = tmp;
              swapped = 1;
            }
        }
    }
}&lt;/pre&gt;
&lt;h4&gt;
Complexity Analysis&lt;/h4&gt;
&lt;br /&gt;
Worst case performance - \(O(n^2)\)&lt;br /&gt;
Best case performance - O(n)&lt;br /&gt;
Average case performance - \(O(n^2)\)&lt;br /&gt;
Worst case space complexity - O(1) auxiliary &lt;br /&gt;
&lt;br /&gt;
Although bubble sort is one of the simplest sorting algorithms to understand and implement, its \(O(n^2)\) complexity means that its efficiency decreases dramatically on lists of more than a small number of elements. Even among simple \(O(n^2)\) sorting algorithms, algorithms like insertion sort are
usually considerably more efficient.&lt;br /&gt;
&lt;br /&gt;
&lt;h4&gt;
Rabbits and turtles&lt;/h4&gt;
&lt;br /&gt;
The positions of the elements in bubble sort plays a large part in determining its performance. Large elements (Rabbits) at the beginning of the list do not pose a problem, as they are quickly swapped. But small elements (Turtle) towards the end, however, move to the beginning extremely slowly. This has led to these types of elements being named rabbits and turtles, respectively.&lt;br /&gt;
&lt;br /&gt;
For e.g. In case of {2, 3, 4, 5, 1} list it takes a complexity of \(O(n^2)\) to sort the list as smallest element (Turtle) 1 is at the end of the list while {6, 2, 3, 4, 5} list takes a complexity of \(O(n)\) to sort the list.&lt;br /&gt;
&lt;br /&gt;
Various efforts have been made to eliminate turtles to improve upon the speed of bubble sort. Cocktail sort achieves this goal fairly well, but it retains \(O(n^2)\) worst-case complexity.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/12/bubble-sorting.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh5-WCW6Kihyphenhyphen2q1AZ8DLoT6y59TgzChNlmJOdPUi0Rxf0aHRC6KORqkP6YR9Pj7tJxeffaW9RmqpTDarI64SkligkuVb2j-mIy_SrCnL9M5cuHYbzuXBX6ay_rrcWAXNKC2iS44hjbtAE0/s72-c/BubbleBlowing.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-93289881075660882</guid><pubDate>Wed, 14 Aug 2013 02:16:00 +0000</pubDate><atom:updated>2014-01-28T18:18:57.820-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Facts</category><category domain="http://www.blogger.com/atom/ns#">Fun</category><title>Reality of a Software Project</title><description>That&#39;s the reality I faced in real world crappy planning of Software Project. As a Developer we never have &quot;FREE TIME&quot; ..&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEip9-DYUfn7JIW-hzN9v8fnJt1VIkPL7xnRofa_ZhkhZoxkBbYU6iW_ajVJk-dc8gq5rnzWeiIdtyJlEgrsIsNYb53-rspeaO6FIa02ecGM-j-U-P3t2BsmsVCsrKhkf9HLoIvJPXElSmw/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEip9-DYUfn7JIW-hzN9v8fnJt1VIkPL7xnRofa_ZhkhZoxkBbYU6iW_ajVJk-dc8gq5rnzWeiIdtyJlEgrsIsNYb53-rspeaO6FIa02ecGM-j-U-P3t2BsmsVCsrKhkf9HLoIvJPXElSmw/s1600/01.png&quot; height=&quot;400&quot; width=&quot;395&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Reality of a Software Project&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/08/reality-of-softwarre-project.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEip9-DYUfn7JIW-hzN9v8fnJt1VIkPL7xnRofa_ZhkhZoxkBbYU6iW_ajVJk-dc8gq5rnzWeiIdtyJlEgrsIsNYb53-rspeaO6FIa02ecGM-j-U-P3t2BsmsVCsrKhkf9HLoIvJPXElSmw/s72-c/01.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5577600529246975517</guid><pubDate>Tue, 11 Jun 2013 02:10:00 +0000</pubDate><atom:updated>2014-01-29T18:11:51.412-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Fun</category><category domain="http://www.blogger.com/atom/ns#">How to</category><title>How to narrate a story to computer scientist</title><description>&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0g3oOHlOd5BYYDS7fxcyGteZlpLeb3Mk9UcYeqgmpyeYWSDzbCSXq-wS-bRVZBafrwe6IT-71T1oqAkYxh_do24nQRNVg8fYXEHSm0XDYM6kV8ycWk2FvNiXlFNiEJElKtoKP6uctUYA/s1600/protocol.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0g3oOHlOd5BYYDS7fxcyGteZlpLeb3Mk9UcYeqgmpyeYWSDzbCSXq-wS-bRVZBafrwe6IT-71T1oqAkYxh_do24nQRNVg8fYXEHSm0XDYM6kV8ycWk2FvNiXlFNiEJElKtoKP6uctUYA/s1600/protocol.png&quot; height=&quot;400&quot; width=&quot;278&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
If it brings back old memories or a smile then I hope you understood the characters in STORY.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/06/how-to-narrate-story-to-computer.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg0g3oOHlOd5BYYDS7fxcyGteZlpLeb3Mk9UcYeqgmpyeYWSDzbCSXq-wS-bRVZBafrwe6IT-71T1oqAkYxh_do24nQRNVg8fYXEHSm0XDYM6kV8ycWk2FvNiXlFNiEJElKtoKP6uctUYA/s72-c/protocol.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-4751024012384112702</guid><pubDate>Sat, 16 Mar 2013 06:14:00 +0000</pubDate><atom:updated>2013-03-16T11:58:18.919-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Google Reader</category><category domain="http://www.blogger.com/atom/ns#">How to</category><title>Death of Google Reader - Journey towards alternatives</title><description>&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Flashback&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Long Long ago when INTERNET is booming up with so many websites its really hard to follow the interesting one. Its quite clumsy to go around miliions of sites to check for new posts.&lt;br /&gt;
&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Am I Doomed ?&lt;/blockquote&gt;
&lt;br /&gt;
No, you are not. RSS feed mechanism is the answer. &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;iframe allowfullscreen=&#39;allowfullscreen&#39; webkitallowfullscreen=&#39;webkitallowfullscreen&#39; mozallowfullscreen=&#39;mozallowfullscreen&#39; width=&#39;320&#39; height=&#39;266&#39; src=&#39;https://www.youtube.com/embed/75qEvW5PANI?feature=player_embedded&#39; frameborder=&#39;0&#39;&gt;&lt;/iframe&gt;&lt;/div&gt;
&lt;br /&gt;
Just follow the RSS feed of your favourite site and the RSS reader will maintain the unread articles, favourite articles and so on. Voilla simple and effective solution to follow various sites.&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;blockquote class=&quot;tr_bq&quot;&gt;
Which RSS reader to use ?&amp;nbsp;&lt;/blockquote&gt;
&lt;br /&gt;
I started my journey with Google Reader just because &lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Service from GOOGLE (Truly .. those were the days everyone crazy to use Google Services. Even having a GMAIL account is symbol of techy saviness.)&lt;/li&gt;
&lt;li&gt;Simplicity&lt;/li&gt;
&lt;li&gt;Cleaner interface&lt;/li&gt;
&lt;li&gt;Meets all my basic needs like tagging, starring, sharing&lt;/li&gt;
&lt;li&gt;No extra signup trash (Just use your google account)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7-0wzulbuF0Y5wcB8ahutgYENWak8yI26kC6KG_Iiv-yrmA16hFUs-sjlOkOsPepM6RqHhp4PWeYr4tY35W1S1n1Gsdawyw3wwbCQV5FhHZDenlm7glaxa4qg3D_k43SPIUPg3jcWbHI/s1600/01.jpeg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7-0wzulbuF0Y5wcB8ahutgYENWak8yI26kC6KG_Iiv-yrmA16hFUs-sjlOkOsPepM6RqHhp4PWeYr4tY35W1S1n1Gsdawyw3wwbCQV5FhHZDenlm7glaxa4qg3D_k43SPIUPg3jcWbHI/s1600/01.jpeg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
I am already using it for last 6 years and I simply love it. I have around 1000+ subscriptions on various topics in it.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Present&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
As part of cleanup process which GOOGLE follows for its services they decided to ditch Google reader with only reason saying - it doesnt meets their expectation. Being a Loyal user of Google from last 6 years I have really lost my trust on them when they scrapped Google Reader. I really need to think alternatives for their services.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKhs3qIYg6q_5iUFOHdMCcf1Cx2jRXrat7bWoQtYX8M5NeFxh-nzW3M1y1MEvQzrUJ7kRlrGblRbMIiJRnK_x_PJqv2AnqXR-EgaNqitMJ595Yh0t0Qe_hsVnpjLmVPkelJGkRyaQ7kRM/s1600/01.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhKhs3qIYg6q_5iUFOHdMCcf1Cx2jRXrat7bWoQtYX8M5NeFxh-nzW3M1y1MEvQzrUJ7kRlrGblRbMIiJRnK_x_PJqv2AnqXR-EgaNqitMJ595Yh0t0Qe_hsVnpjLmVPkelJGkRyaQ7kRM/s1600/01.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Uproar started every where as users backlashed at google&#39;s decision in various popular social networking sites. Even I am one among them. Nothing can be done but looking for alternatives.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;&quot;&gt;Future&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Before I really jump into alternatives for Google reader I decided to sort down my requirements. &lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Web based RSS reader like Google Reader not a application only one. Its main advantage is I can read from anywhere from browser.&lt;/li&gt;
&lt;li&gt;Simple, Cleaner Interface.&lt;/li&gt;
&lt;li&gt;Support 1000+ subscriptions. &lt;/li&gt;
&lt;li&gt;Tagging of Feeds.&lt;/li&gt;
&lt;li&gt;Support for various views like List view, Expanded view, Full view so on. &lt;/li&gt;
&lt;li&gt;Automatic marking of items as read as they are scrolled past. This option really helps in marking posts as read the moment we scroll past them.&lt;/li&gt;
&lt;li&gt;Sharing options with various social networking sites (Not so mandatory).&lt;/li&gt;
&lt;li&gt;Free Service.&amp;nbsp; &lt;/li&gt;
&lt;li&gt;Extra goodies&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Alternatives I am gonna try&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;1. &lt;a href=&quot;http://www.feedly.com/&quot; target=&quot;_blank&quot;&gt;Feedly&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV4Oh5U-wVSNhBjp0L-S88gGS0n_K_2Uy-WHXtFgBn0BjpPpYzSE5-_MxTqTaVK2rSmPkweYzE7NRUKCP4KylhEtnLlrR8aITvN9X8C3mc_74_DOcRV8w2VnaXMDH4LAeZoqauciZhRaw/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiV4Oh5U-wVSNhBjp0L-S88gGS0n_K_2Uy-WHXtFgBn0BjpPpYzSE5-_MxTqTaVK2rSmPkweYzE7NRUKCP4KylhEtnLlrR8aITvN9X8C3mc_74_DOcRV8w2VnaXMDH4LAeZoqauciZhRaw/s320/01.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Quite popular among the alternatives I decided to start with Feedly. Feedly already started to face huge traffic from users migrating from Google Reader as they promise to provide seamless migration for Google Reader users. Just signup with your google account and all your feeds are imported automatically.&lt;br /&gt;
&lt;br /&gt;
Important Tips for users migrating from Google Reader to Feedly is shared in their &lt;a href=&quot;http://blog.feedly.com/2013/03/14/tips-for-google-reader-users-migrating-to-feedly/&quot; target=&quot;_blank&quot;&gt;Blog Link&lt;/a&gt;. These tips are really helpful.Even though initial look is clumsy we can customize it in preferences tab making it look simple and cleaner as shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbSDeJsrJ2rqJfusZ3EHDcpS0ExTuc30aFfeFpbb0zGvyPR1Gu9JWLiJXCddtv4AVIEZxhpQggkoUItPaxUzeCXW-f6WRR7pgLbANF_UFdotQSvlthJhAtJXuN0UrYgR13H7mVG0FgmzQ/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;268&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbSDeJsrJ2rqJfusZ3EHDcpS0ExTuc30aFfeFpbb0zGvyPR1Gu9JWLiJXCddtv4AVIEZxhpQggkoUItPaxUzeCXW-f6WRR7pgLbANF_UFdotQSvlthJhAtJXuN0UrYgR13H7mVG0FgmzQ/s640/01.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Pros:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Simple, Cleaner Interface&lt;/li&gt;
&lt;li&gt;Transition from Google Reader is very simple. &lt;/li&gt;
&lt;li&gt;Meets all my requirements mentioned above.&lt;/li&gt;
&lt;li&gt;Web Based RSS reader and even supports apps for various platforms.&lt;/li&gt;
&lt;li&gt;Support cleaner background themes option.&lt;/li&gt;
&lt;li&gt;Save option is really helpful&amp;nbsp; to check the old and interesting articles later.&lt;/li&gt;
&lt;/ul&gt;
&lt;b&gt;Cons:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;None currently.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
I would highly recommend this service for google readers as you gonna feel back home.I would call Feedly as &lt;b&gt;Google Reader on steroids&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
Eventhough I am very happy with Feedly I decided to try other popular services available.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;2. &lt;a href=&quot;http://www.bloglovin.com/&quot; target=&quot;_blank&quot;&gt;Bloglovin&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC7RFBkIVLV3qZWun9IWXmwweddPgCW-SaolyNpUx1lGAkkWeDEaeyZYrWVjcA6ch8mDVmRNvR7CbM120YVQf4ebHts8g0OcXaradV8a_aiNVy-0iyjsvBshaKvEAnv9d7BIdvnmN1JZ0/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgC7RFBkIVLV3qZWun9IWXmwweddPgCW-SaolyNpUx1lGAkkWeDEaeyZYrWVjcA6ch8mDVmRNvR7CbM120YVQf4ebHts8g0OcXaradV8a_aiNVy-0iyjsvBshaKvEAnv9d7BIdvnmN1JZ0/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
A simple signup procedure which accepts your Facebook account or Email sign-up. Once sign-up is done it asks for importing feeds from Google reader making transition from Google Reader very simple. &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEie9pMk-sCMo1K6NRub7xiQzO0WTucrDfuN1Tk9F1Fd9W_1EJUcDbsyk84glSHClXNE2ZUoDCzJtetyQF0nm9nq8rrU-3PikHy8PHBrQrUqVKud4Nw3CVbhSNhH3oE0WmPeqSV23h56Ftk/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;373&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEie9pMk-sCMo1K6NRub7xiQzO0WTucrDfuN1Tk9F1Fd9W_1EJUcDbsyk84glSHClXNE2ZUoDCzJtetyQF0nm9nq8rrU-3PikHy8PHBrQrUqVKud4Nw3CVbhSNhH3oE0WmPeqSV23h56Ftk/s640/01.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Pros:&lt;/b&gt; &lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Simple, Cleaner interface.&lt;/li&gt;
&lt;li&gt;Transition from Google Reader is very simple.&lt;/li&gt;
&lt;li&gt;Creates personal profile. &lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Cons:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Only supports magazine view.&lt;/li&gt;
&lt;li&gt;No support for automatic marking of items as read as they are scrolled past.&lt;/li&gt;
&lt;li&gt;No Tagging option.&lt;/li&gt;
&lt;li&gt;Very basic sorting option. &lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;3. &lt;a href=&quot;http://www.netvibes.com/en&quot; target=&quot;_blank&quot;&gt;Netvibes&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWA9R22Wg2wV35YCH_UteyBP_E2BZlm9XDktKlard1gggU_4uctPohJ2zHau1ufk3FJq7YjQBbestcjUYNsdYw8sgdPtIj_0FKpEq69GkAYzWB2TCIPxpnG10DpL0Weqy4j6Ua2IIJ80Y/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWA9R22Wg2wV35YCH_UteyBP_E2BZlm9XDktKlard1gggU_4uctPohJ2zHau1ufk3FJq7YjQBbestcjUYNsdYw8sgdPtIj_0FKpEq69GkAYzWB2TCIPxpnG10DpL0Weqy4j6Ua2IIJ80Y/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Netvibes is a personalized dashboard publishing platform like iGoogle. It provides simple signup procedure which accepts your Facebook account or Email sign-up. Once signed-in initial feel is just like iGoogle service with its widgets look of various sites. &lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjG1iku24Xy-3_wMPHIEQAAak1LijLFbQWxA1kOx41HI3DmIbLcOsuNyvJMQub3UVD6Y2bmh5PJW0KuqCHJj6WZfwYWUdhH7Lhk3XUHZF9IBM2bXAZW9iq-1zew7VKptZi4g10ROOciizU/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;252&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjG1iku24Xy-3_wMPHIEQAAak1LijLFbQWxA1kOx41HI3DmIbLcOsuNyvJMQub3UVD6Y2bmh5PJW0KuqCHJj6WZfwYWUdhH7Lhk3XUHZF9IBM2bXAZW9iq-1zew7VKptZi4g10ROOciizU/s640/01.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
Steps for importing feeds from Google Reader to netvibes is shared in their &lt;a href=&quot;http://blog.netvibes.com/easily-migrate-from-google-reader-to-netvibes/&quot; target=&quot;_blank&quot;&gt;blog link&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOHuolYX-V3VYSb-GSwTkdKpuennyQtivhyAxImM_RxdbD_e7s8tSm58swuOyadxE6zUAXKxS4vQ-d9mlIVImbepAT7Mv6YAjp4LUGkgezJcWZKSKc7-L1E-UIkfiCFYRYDCAnnglOc1g/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;188&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjOHuolYX-V3VYSb-GSwTkdKpuennyQtivhyAxImM_RxdbD_e7s8tSm58swuOyadxE6zUAXKxS4vQ-d9mlIVImbepAT7Mv6YAjp4LUGkgezJcWZKSKc7-L1E-UIkfiCFYRYDCAnnglOc1g/s640/01.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Pros:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Simple, Cleaner Interface.&lt;/li&gt;
&lt;li&gt;Support various options like iGoogle service.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Cons: &lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Its a dashboard publishing service. I just need a RSS reader.&lt;/li&gt;
&lt;li&gt;Free edition doesn&#39;t support automatic marking of items as read as they are scrolled past.&lt;/li&gt;
&lt;li&gt;Premium Services at whopping 499$/month. Are you kidding me lol.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;4. &lt;a href=&quot;http://feedspot.com/&quot; target=&quot;_blank&quot;&gt;Feedspot&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFvHQM9BXLc_770YWiJHbMmPL0eUpB0He9bmODBnKc71LHMw64oF0CfS0gLxpJgzJw9ERKhqWUODWGM8sPoqryt7gIax5ow3vF-zVccFW6WDAe1o1QJ9gMp6IxoQDEhDiP_QCWoOHF-Yo/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgFvHQM9BXLc_770YWiJHbMmPL0eUpB0He9bmODBnKc71LHMw64oF0CfS0gLxpJgzJw9ERKhqWUODWGM8sPoqryt7gIax5ow3vF-zVccFW6WDAe1o1QJ9gMp6IxoQDEhDiP_QCWoOHF-Yo/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
After simple signup procedure users are provided with a link to transition feeds from Google Reader.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbL-0YKJjF3mn5NYkPJm99DS-uNDqecjTeTPZSegCpAzn9jAeHdPV36JYwn1hU3jwU73XNKT6C3rZDwY7qLTATsUt1Wb7pE-Kgg32Xc11asdFk8Ar8xq-NwyvvkYWKQSdMY8XMdx0pAyM/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;292&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgbL-0YKJjF3mn5NYkPJm99DS-uNDqecjTeTPZSegCpAzn9jAeHdPV36JYwn1hU3jwU73XNKT6C3rZDwY7qLTATsUt1Wb7pE-Kgg32Xc11asdFk8Ar8xq-NwyvvkYWKQSdMY8XMdx0pAyM/s640/01.png&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Pros:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Simple, Cleaner interface.&lt;/li&gt;
&lt;li&gt;Easy transition of feeds from Google Reader.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Cons:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Doesn&#39;t support automatic marking of items as read as they are scrolled past. &lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/03/death-of-google-reader-journey-towards.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh7-0wzulbuF0Y5wcB8ahutgYENWak8yI26kC6KG_Iiv-yrmA16hFUs-sjlOkOsPepM6RqHhp4PWeYr4tY35W1S1n1Gsdawyw3wwbCQV5FhHZDenlm7glaxa4qg3D_k43SPIUPg3jcWbHI/s72-c/01.jpeg" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5117875310534613661</guid><pubDate>Thu, 14 Mar 2013 04:35:00 +0000</pubDate><atom:updated>2014-10-22T02:47:19.198-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Bit Operations</category><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><title>Divide a number by 3 without using any arithmetic operators</title><description>How to divide a number by 3 without using operators + - / * %&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; I am just trying to collect various answers down in this post available across multiple sites in internet as mentioned in references.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Solution 01:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main()
{
    FILE *fp=fopen(&quot;temp.dat&quot;,&quot;w+b&quot;);
    int number=12346;
    int divisor=3;
    char *buf = calloc(number,1);
    fwrite(buf,number,1,fp);
    rewind(fp);
    int result=fread(buf,divisor,number,fp);
    printf(&quot;%d / %d = %d&quot;, number, divisor, result);
    free(buf);
    fclose(fp);
    return 0;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Above solution is a bit tricky so let us simplify it&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Open a temporary file i.e. temp.dat in write mode.&lt;/li&gt;
&lt;li&gt;Allocate a temporary &quot;buf&quot; of size &quot;number&quot;.&lt;/li&gt;
&lt;li&gt;Now &lt;b&gt;fwrite&lt;/b&gt; writes &quot;number&quot; of bytes from &quot;buf&quot; into FILE STREAM &quot;fp&quot;. (In the above code since the value of &quot;number&quot; variable is 12346, we write 12346 bytes from &quot;buf&quot; into &quot;temp.dat&quot; file).&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;rewind&lt;/b&gt; will reset the file pointer &quot;fp&quot; to the start of the file.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;fread&lt;/b&gt; reads &quot;number&quot; of elements of data, each &quot;divisor&quot; bytes long, from FILE STREAM &quot;fp&quot; and writes into &quot;buf&quot;. Hence the return value of fread is 4115 in the above example. If you write 30 bytes and read back from the file in units of 3 then fread returns 10 which equivalent to 30 / 3.&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Solution 02:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main(int argc, char *argv[])
{
    int num = 1234567;
    int den = 3;
    div_t r = div(num,den); // div() is a standard C function.
    printf(&quot;%dn&quot;, r.quot);

    return 0;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Solution 03:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int add(int x, int y) {
  int a, b;
  do {
    a = x &amp;amp; y;
    b = x ^ y;
    x = a &amp;lt;&amp;lt; 1;
    y = b;
  } while (a);
  return b;
}

int divideby3 (int num) {
  int sum = 0;
  while (num &amp;gt; 3) {
    sum = add(num &amp;gt;&amp;gt; 2, sum);
    num = add(num &amp;gt;&amp;gt; 2, num &amp;amp; 3);
  }
  if (num == 3)
    sum = add(sum, 1);
  return sum;   
}

int main()
{
  int number=30;

  printf(&quot;%d n&quot;, divideby3(number));
  return 0;
}&lt;/pre&gt;
&lt;b&gt;References:&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://stackoverflow.com/questions/11694546/divide-a-number-by-3-without-using-operators&quot; target=&quot;_blank&quot;&gt;Stackoverflow&lt;/a&gt;.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/03/Divide-number-by-3-without-using-any-arithmetic-operators.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2806620986975517277</guid><pubDate>Wed, 13 Mar 2013 16:49:00 +0000</pubDate><atom:updated>2013-06-29T21:41:52.431-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><category domain="http://www.blogger.com/atom/ns#">Stack</category><title>Checking Balance of Symbols in a expression</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
A balanced expression contains right number of closing and open braces.&lt;br /&gt;
&lt;br /&gt;
For example:&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;[[ - unbalanced expression&lt;/li&gt;
&lt;li&gt;[]{}() - balanced expression &lt;/li&gt;
&lt;li&gt;[(*)] - balanced expression&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
Let us see how to find if an expression is balanced or not by checking for following operators [ ] { } and ( ) in the given expression.&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt;
Using Stacks &lt;/h3&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg608etBqw_bVpTkUjAFqZmd14aaPe7v4EzpElPan-Pzu6MrIIG8EBM3Aw8WNOoaCL3Wb13y6aW0_xZJy8rt6USo0AgkdtwtcvASrPfm0xO_RA1wB7xJB6wCfDersFtVPaVtzKe9MJJ2Jg/s1479/01.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;400&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg608etBqw_bVpTkUjAFqZmd14aaPe7v4EzpElPan-Pzu6MrIIG8EBM3Aw8WNOoaCL3Wb13y6aW0_xZJy8rt6USo0AgkdtwtcvASrPfm0xO_RA1wB7xJB6wCfDersFtVPaVtzKe9MJJ2Jg/s400/01.jpg&quot; width=&quot;255&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h4&gt;
Pseudocode:&lt;/h4&gt;
&lt;br /&gt;
We divide [ , ] , { , } , ( , ) operators into LEFT and RIGHT sets where LEFT set contains ( , { ,&amp;nbsp; [ operators while RIGHT set contains ) , } , ] operators. So we have a one to one mapping between RIGHT and LEFT set operators i,e&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;{ --- }&lt;/li&gt;
&lt;li&gt;[ ---]&amp;nbsp;&lt;/li&gt;
&lt;li&gt;( --- )&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; Stack related operations like create stack, delete stack, push into stack and pop from stack are considered generic and we are not including its logic in below pseudocode.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;********************************************************
 Function  : isExprBalanced 

 Calls     : popFromStack 

 Called by : main

 Input 
 Parameters: stack, var
  
 Returns   : balanced : TRUE 
             unbalanced: FALSE
********************************************************

CASE var

  &#39;)&#39; : IF stackTop equals &#39;(&#39; THEN
          CALL popFromStack (stack)
          RETURN TRUE
        ENDIF

  &#39;}&#39; : IF stackTop equals &#39;{&#39; THEN
          CALL popFromStack (stack)
          RETURN TRUE
        ENDIF

  &#39;]&#39; : IF stackTop equals &#39;[&#39; THEN
          CALL popFromStack (stack)
          RETURN TRUE
        ENDIF
  
ENDCASE

RETURN FALSE

********************************************************
 Function  : main

 Calls     : createStack
             isExprBalanced              
             pushIntoStack 
             popFromStack 

 Called by : NONE 

 Input 
 Parameters: Accepts input expression for processing
  
 Returns   : If input expression is balanced or not.
********************************************************

SET i to 0
GET input expression from user into input[20]
SET var with input[i]

CALL createStack

WHILE var != end of string

  IF var belongs to (RIGHT set) THEN

    CALL isExprBalanced (stack,var)

    IF not balanced THEN
      PRINT expression is not balanced
      RETURN
    ENDIF 

  ELSE IF var belongs to (LEFT set) THEN

    CALL pushIntoStack (stack,var)

  ENDIF

  SET var with input[INCREMENT i]

ENDWHILE

IF stack is empty THEN
  PRINT expression is balanced
ELSE
  PRINT expression is un-balanced
ENDIF

CALL freeStack (stack)&lt;/pre&gt;
&lt;br /&gt;
&lt;h4&gt;
Complexity:&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;TIme Complexity&lt;/b&gt;: O(n)&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Space Complexity&lt;/b&gt;: O(n)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h4&gt;
Sample Code:&lt;/h4&gt;
&lt;br /&gt;
&lt;b&gt;stackSLL.h&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#define MAX_STACK_SIZE 10
#define RESULT_ERROR -1
#define RESULT_OK     0

struct sllNode
{
  struct sllNode *next;
  int data;
};

struct sllHead
{
  struct sllNode *next;
  u_int32_t count;
  u_int32_t maxStackSize;
};

#define IS_RIGHT(_i)  ((_i == &#39;)&#39;) || (_i == &#39;}&#39;) || (_i == &#39;]&#39;))

#define IS_LEFT(_i)  ((_i == &#39;(&#39;) || (_i == &#39;{&#39;) || (_i == &#39;[&#39;))&lt;/pre&gt;
&lt;b&gt;stackSLL.c&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &quot;stackSLL.h&quot;

/*Poping from a Stack*/
int
popFromStack(struct sllHead *head)
{
  struct sllNode *tmpNode_ptr = NULL;

  if (!head)
  {
    return RESULT_ERROR;
  }

  if (head-&amp;gt;next)
  {
    tmpNode_ptr = head-&amp;gt;next;
    head-&amp;gt;next = head-&amp;gt;next-&amp;gt;next;
    free(tmpNode_ptr);
    head-&amp;gt;count--;
  }    
  else  
    printf(&quot;Stack is Empty\n&quot;);

  return RESULT_OK;
} 

/*Pushing into Stack*/
int
pushIntoStack(struct sllHead *head, int data)
{
  struct sllNode *tmpNode_ptr = NULL;

  if (!head)
  {
    return RESULT_ERROR;
  }

  if (head-&amp;gt;count == head-&amp;gt;maxStackSize)
  {
    printf (&quot;Stack is FILLED to MAX level\n&quot;);
    return RESULT_ERROR;
  }

  tmpNode_ptr = (struct sllNode *)calloc(1, sizeof(struct sllNode));

  if (tmpNode_ptr)
    tmpNode_ptr-&amp;gt;data = data;
  else
  {
    printf(&quot;Memory Allocation Failedn&quot;);
    return RESULT_ERROR;
  }

  tmpNode_ptr-&amp;gt;next = head-&amp;gt;next;
  head-&amp;gt;next = tmpNode_ptr;
  head-&amp;gt;count++;

  return RESULT_OK;
}

struct sllHead *
createStack()
{
  struct sllHead *head = struct sllHead *)calloc(1, sizeof(struct sllHead));

  if (head)
    head-&amp;gt;maxStackSize = MAX_STACK_SIZE;

  return head;
}

void
freeStack(struct sllHead **head)
{
  if (*head)
  {
    while((*head)-&amp;gt;count)
      popFromStack(*head);

    free(*head);
    *head = NULL;
  }
}

int
isExprBalanced (struct sllHead *head, char val)
{
  if (head == NULL || head-&amp;gt;next == NULL)
    return RESULT_ERROR;

  if (!IS_RIGHT(val))
    return RESULT_ERROR;

  switch(val)
  {
    case &#39;)&#39;:
      if (head-&amp;gt;next-&amp;gt;data == &#39;(&#39;)
      {
        popFromStack(head);
        return RESULT_OK;
      }
    break;

    case &#39;}&#39;:
      if (head-&amp;gt;next-&amp;gt;data == &#39;{&#39;)
      {
        popFromStack(head);
        return RESULT_OK;
      }
    break;

    case &#39;]&#39;:
      if (head-&amp;gt;next-&amp;gt;data == &#39;[&#39;)
      {
        popFromStack(head);
        return RESULT_OK;
      }
    break;
  }

  return RESULT_ERROR;
}

int
main(int argc, char *argv[])
{
  char in[20];
  short i = 0;
  struct sllHead *stack = NULL;

  memset(in, 0, 20);

  printf(&quot;Enter expression containing ( { [ ) { ]: &quot;);
  scanf(&quot;%s&quot;, in);

  stack = createStack();

  while(i &amp;lt; 20 &amp;amp;&amp;amp; in[i] != &#39;\0&#39;)
  {
    if (IS_RIGHT(in[i]))
    {
      if (isExprBalanced(stack, in[i]) != RESULT_OK)
      {
        printf(&quot;Expression is not balanced\n&quot;);
        goto EXIT;
      }
    }
    else if (IS_LEFT(in[i]))
    {
      if (pushIntoStack(stack, in[i]) == RESULT_ERROR)
        goto EXIT;
    }

  if (stack-&amp;gt;count)
    printf (&quot;Expression is not balanced\n&quot;);
  else
    printf (&quot;Expression is balanced\n&quot;);

EXIT:

  freeStack (&amp;amp;stack);

  return RESULT_OK;
}&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/03/checking-balance-of-symbols-in.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg608etBqw_bVpTkUjAFqZmd14aaPe7v4EzpElPan-Pzu6MrIIG8EBM3Aw8WNOoaCL3Wb13y6aW0_xZJy8rt6USo0AgkdtwtcvASrPfm0xO_RA1wB7xJB6wCfDersFtVPaVtzKe9MJJ2Jg/s72-c/01.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-1567985323434024218</guid><pubDate>Tue, 05 Feb 2013 10:42:00 +0000</pubDate><atom:updated>2013-06-30T05:05:23.137-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><category domain="http://www.blogger.com/atom/ns#">Stack</category><title>Conversion from Infix to Postfix</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;h3&gt; Converting a Fully Parenthesized Infix expression into Postfix expression&amp;nbsp;&amp;nbsp;&lt;/h3&gt;
&lt;br /&gt;
&lt;h4&gt;
Analysis:&lt;/h4&gt;
Five types of input characters&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Opening parentheses&lt;/li&gt;
&lt;li&gt;Operands&lt;/li&gt;
&lt;li&gt;Operators&lt;/li&gt;
&lt;li&gt;Closing parentheses&lt;/li&gt;
&lt;li&gt;New line character (\n)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h4&gt;
Pseudocode:&lt;/h4&gt;
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;********************************************************  
 Function  : main  
  
 Calls     : createStack  
             freeStack                
             pushIntoStack   
             popFromStack   
  
 Called by : NONE   
  
 Input   
 Parameters: Accepts input expression for processing  
    
 Returns   : Converts Fully paranthesized INFIX 
             expression into POSTFIX expression
******************************************************** 

SET i to 0
GET infix expression from user into input_array
SET var with input_array[i]

CALL createStack

WHILE var != end of string

  IF var equals to &#39;(&#39; THEN
  
    CALL pushIntoStack (stack, var)  

  ELSE IF var is a number THEN        
  
    PRINT var  

  ELSE IF var is an arithmetic operator THEN
  
    CALL pushIntoStack (stack, var) 
    
  ELSE IF var equals to &#39;)&#39; THEN

    WHILE stackTop != &#39;(&#39;

      IF stackTop is an arithmetic operator THEN

        PRINT stackTop
        popFromStack (stack)

      ENDIF
    
    ENDWHILE

    popFromStack (stack)

  ENDIF

  SET var with input_expression[INCREMENT i]

ENDWHILE

CALL freeStack (stack)&lt;/pre&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;h4&gt;
Complexity:&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;Time Complexity&lt;/b&gt;: O(n)&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Space Complexity&lt;/b&gt;: O(n)&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;h4&gt;
Explanation: &lt;/h4&gt;
&lt;br /&gt;
We will try out above pseudocode with input expression - (((8+1)-(7-4))/(11-9))&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;WordSection1&quot;&gt;
&lt;table border=&quot;1&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;MsoTableMediumGrid3Accent3&quot; style=&quot;border-collapse: collapse; border: none;&quot;&gt;
 &lt;tbody&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: solid white 3.0pt; border: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Input&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: solid white 3.0pt; border-left: none; border-right: solid white 1.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Operation&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: solid white 3.0pt; border-left: none; border-right: solid white 1.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Stack
  (after op)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: solid white 3.0pt; border-left: none; border-right: solid white 1.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Output
  on monitor&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  operand into stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  operand into stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  operand into stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Print
  it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;+&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  operator into stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( ( +&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;1&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Print
  it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;+&#39; print it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 +&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;(&#39; we ignore it and read next
  character&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 +&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;-&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  operator into stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( -&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  operand into stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( - (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;7&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Print
  it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;-&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  the operator in the stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( - ( -&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;4&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Print
  it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7 4&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;-&#39; print it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( - (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7 4 -&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;(&#39; we ignore it and read next
  character&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  ( -&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;-&#39; print it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7 4 - -&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;(&#39; we ignore it and read next
  character&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;/&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  the operand into the stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  /&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  into the stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  / (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;11&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Print
  it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7 4 - - 11&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;-&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Push
  the operand into the stack&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  / ( -&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;9&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Print
  it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7 4 - - 11 9&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;-&#39; print it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  / (&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7 4 - - 11 9 -&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;(&#39; we ignore it and read next
  character&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(
  /&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;)&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;/&#39; print it&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;(&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;8
  1 + 7 4 - - 11 9 - /&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-bottom: none; border-left: solid white 1.0pt; border-right: solid white 3.0pt; border-top: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Pop
  from the stack: Since popped element is &#39;(&#39; we ignore it and read next
  character&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;Stack
  is empty&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #E6EED5; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;tr&gt;
  &lt;td style=&quot;background: #9BBB59; border-right: solid white 3.0pt; border: solid white 1.0pt; padding: 0in 5.4pt 0in 5.4pt; width: 58.0pt;&quot; valign=&quot;top&quot; width=&quot;97&quot;&gt;&lt;div align=&quot;center&quot; class=&quot;MsoNormal&quot; style=&quot;text-align: center;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;color: white; font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;New
  line character&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 293.9pt;&quot; valign=&quot;top&quot; width=&quot;490&quot;&gt;&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: &amp;quot;Verdana&amp;quot;,&amp;quot;sans-serif&amp;quot;; font-size: 10.0pt;&quot;&gt;STOP&lt;/span&gt;&lt;/div&gt;
&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 58.9pt;&quot; valign=&quot;top&quot; width=&quot;98&quot;&gt;&lt;/td&gt;
  &lt;td style=&quot;background: #CDDDAC; border-bottom: solid white 1.0pt; border-left: none; border-right: solid white 1.0pt; border-top: none; padding: 0in 5.4pt 0in 5.4pt; width: 92.3pt;&quot; valign=&quot;top&quot; width=&quot;154&quot;&gt;&lt;/td&gt;
 &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;h4&gt;
Sample Code:&lt;/h4&gt;
&lt;br /&gt;
&lt;b&gt;stack01.h&lt;/b&gt; &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#define MAX_ARRAY_SIZE 50
#define MAX_STACK_SIZE 10
#define RESULT_ERROR -1
#define RESULT_OK     0

struct sllNode
{
  struct sllNode *next;
  char data;
};


struct sllHead
{
  struct sllNode *next;
  u_int32_t count;
  u_int32_t maxStackSize;
};

#define IS_ARITHMETIC_OPERATOR(_i)                                          
   ((_i == &#39;*&#39;) || (_i == &#39;+&#39;) || (_i == &#39;-&#39;) || (_i == &#39;/&#39;) || (_i == &#39;%&#39;))

#define IS_OPERAND(_i)                                                      
   ( (_i &amp;gt;= &#39;0&#39; &amp;amp;&amp;amp; _i &amp;lt;= &#39;9&#39;) ||                                            
     (_i &amp;gt;= &#39;a&#39; &amp;amp;&amp;amp; _i &amp;lt;= &#39;z&#39;) ||                                            
     (_i &amp;gt;= &#39;A&#39; &amp;amp;&amp;amp; _i &amp;lt;= &#39;Z&#39;))

#define IS_STACK_EMPTY(_stack)  ((_stack) ? _stack-&amp;gt;count : 0)    

#define STACK_TOP(_stack) ((_stack) ?                                       
                           ((_stack-&amp;gt;next) ?                                
                            (_stack-&amp;gt;next-&amp;gt;data) : 0) : 0) &lt;/pre&gt;
&lt;b&gt;stack01.c&lt;/b&gt; &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;/*FULLY PARANTHESIZED INFIX to POSTFIX*/
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;string.h&amp;gt;
#include &quot;stack01.h&quot;

/*Poping from a Stack*/
int popFromStack(struct sllHead *head)
{
  struct sllNode *tmpNode_ptr = NULL;

  if (!head)
  {
    return RESULT_ERROR;
  }

  if (head-&amp;gt;next)
  {
    tmpNode_ptr = head-&amp;gt;next;
    head-&amp;gt;next = head-&amp;gt;next-&amp;gt;next;
    free(tmpNode_ptr);
    head-&amp;gt;count--;
  }    
  else  
   printf(&quot;Stack is Emptyn&quot;);

  return RESULT_OK;
}   

/*Pushing into Stack*/
int pushIntoStack(struct sllHead *head, char data)
{
  struct sllNode *tmpNode_ptr = NULL;

  if (!head)
  {
    return RESULT_ERROR;
  }

  if (head-&amp;gt;count == head-&amp;gt;maxStackSize)
  {
    printf (&quot;Stack is FILLED to MAX leveln&quot;);
    return RESULT_ERROR;
  }

  tmpNode_ptr = (struct sllNode *)calloc(1, sizeof(struct sllNode));

  if (tmpNode_ptr)
    tmpNode_ptr-&amp;gt;data = data;
  else
  {
    printf(&quot;Memory Allocation Failedn&quot;);
    return RESULT_ERROR;
  }

  tmpNode_ptr-&amp;gt;next = head-&amp;gt;next;
  head-&amp;gt;next = tmpNode_ptr;
  head-&amp;gt;count++;

  return RESULT_OK;
}

struct sllHead * createStack()
{
  struct sllHead *head = NULL;
  head = (struct sllHead *)calloc(1, sizeof(struct sllHead));
  if (head)
    head-&amp;gt;maxStackSize = MAX_STACK_SIZE;

  return head;
}

void freeStack(struct sllHead **head)
{
  if (*head)
  {
    while((*head)-&amp;gt;count)
      popFromStack(*head);

    free(*head);
    *head = NULL;
  }
}

int main(int argc, char *argv[])
{
  char in[MAX_ARRAY_SIZE];
  short i = 0;
  struct sllHead *stack;
  u_char operand_before = 0;

  memset(in, 0, MAX_ARRAY_SIZE);

  printf(&quot;Enter INFIX expression : &quot;);
  scanf(&quot;%s&quot;, in);

  stack = createStack();

  while(i &amp;lt; MAX_ARRAY_SIZE &amp;amp;&amp;amp; in[i] != &#39;\0&#39;)
  {
    if (in[i] == &#39;(&#39; ||
        IS_ARITHMETIC_OPERATOR(in[i]))
    {
      pushIntoStack (stack,in[i]);
      operand_before = 0;
    }
    else if (IS_OPERAND(in[i]))
    {
      if (operand_before)
      printf (&quot;%c&quot;, in[i]);
      else
      printf (&quot; %c&quot;, in[i]);

      operand_before = 1;
    }
    else if (in[i] == &#39;)&#39;)
    {
      operand_before = 0;
      while (IS_STACK_EMPTY(stack) &amp;amp;&amp;amp;
             STACK_TOP(stack) != &#39;(&#39;)
      {
        if (IS_ARITHMETIC_OPERATOR(STACK_TOP(stack)))
        {
          printf(&quot; %c&quot;, STACK_TOP(stack));
          popFromStack(stack);
        }
      }

      if (STACK_TOP(stack) == &#39;(&#39;)
        popFromStack(stack);
    }

    i++;
  }

  printf (&quot;\n&quot;);

EXIT:

  freeStack (&amp;amp;stack);

  return RESULT_OK;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;h4&gt;
References:&lt;/h4&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Stack_%28abstract_data_type%29&quot; target=&quot;_blank&quot;&gt;wikipedia&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2013/02/conversion-from-infix-to-postfix.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-1757637782340738675</guid><pubDate>Tue, 25 Dec 2012 12:55:00 +0000</pubDate><atom:updated>2013-01-19T03:02:44.336-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><title>Deleting a Node from a Singly Linked List</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.in/2009/08/implementation-of-singly-linked-list-in.html&quot; target=&quot;_blank&quot;&gt;Implementation of Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Reversing a Singly Linked List&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Deletion of a Node from a Singly Linked List&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Similar to insertion we have three cases for deleting a Node from a Singly Linked List.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Deleting First Node in Singly Linked List&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
To complete deletion of &lt;b&gt;firstNode&lt;/b&gt; in the list we have to change &lt;b&gt;Head&lt;/b&gt; pointing to &lt;b&gt;Next&lt;/b&gt; of &lt;b&gt;firstNode&lt;/b&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Pseudocode&lt;/b&gt;:
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;firstNode = Head

Head = firstNode-&amp;gt;Next

free firstNode&lt;/pre&gt;
&lt;b&gt;Complexity&lt;/b&gt;:&lt;br /&gt;
Time Complexity: O(1)&lt;br /&gt;
Space Complexity: O(1)&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;Sourcecode&lt;/b&gt;:
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt; int delNodeData(int num)
 {
    struct Node *prev_ptr, *cur_ptr;

    cur_ptr=Head;

    while(cur_ptr != NULL)
    {
       if(cur_ptr-&amp;gt;Data == num)
       {
          if(cur_ptr==Head)
          {
             Head=cur_ptr-&amp;gt;Next;
             free(cur_ptr);
             return 0;
          }
          else
          {
             prev_ptr-&amp;gt;Next=cur_ptr-&amp;gt;Next;
             free(cur_ptr);
             return 0;
          }
       }
       else
       {
          prev_ptr=cur_ptr;
          cur_ptr=cur_ptr-&amp;gt;Next;
       }
    }

    printf(&quot;\nElement %d is not found in the List&quot;, num);
    return 1;
 }&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Deleting Last Node in the Singly Linked List&lt;/b&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;br /&gt;
Traverse to Last Node in the List using two pointers namely prevNode and curNode. Once curNode reaches the last Node in the list point Next in prevNode to NULL and free the curNode.&lt;br /&gt;
&lt;br /&gt;&lt;b&gt;
Pseudocode&lt;/b&gt;:
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curNode = head

forever:

 if curNode-&amp;gt;Next == NULL
 break

 prevNode = curNode
 curNode = curNode-&amp;gt;Next

prevNode-&amp;gt;Next = NULL

free curNode&lt;/pre&gt;
&lt;b&gt;Complexity&lt;/b&gt;:&lt;br /&gt;
Time Complexity: O(n)&lt;br /&gt;
Space Complexity: O(1)&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Deleting Node from position &#39;p&#39; in the List&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
To delete a Node at the position &#39;p&#39; we have to first traverse the list until we reach the position &#39;p&#39;. For this case have to maintain two pointers namely prevNode and curNode. Since Singly Linked Lists are uni-directional we have to maintain the information about previous Node in prevNode. Once we reach the position &#39;p&#39; we have to modify prevNode Next pointing to curNode Next and free curNode.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Pseudocode&lt;/b&gt;:&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curNode = head
curPos = 1

forever:

 if curPos == P || curNode == NULL
 break

 prevNode = curNode
 curNode = curNode-&amp;gt;Next
 curPos++

if curNode != NULL:
 
 prevNode-&amp;gt;Next = curNode-&amp;gt;Next
 free curNode&lt;/pre&gt;
&lt;b&gt;Complexity&lt;/b&gt;:&lt;br /&gt;
Time Complexity: O(n) worst case&lt;br /&gt;
Space Complexity: O(3)&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Sourcecode&lt;/b&gt;:&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;int delNodeLoc(int loc)
 {
    struct Node *prev_ptr, *cur_ptr;
    int i;

    cur_ptr=Head;

    if(loc &amp;gt; (length()) || loc &amp;lt;= 0)
    {
        printf(&quot;\nDeletion of Node at given location is not possible\n &quot;);
    }
    else
    {
        // If the location is starting of the list
        if (loc == 1)
        {
            Head=cur_ptr-&amp;gt;Next;
            free(cur_ptr);
            return 0;
        }
        else
        {
            for(i=1;i&amp;lt;loc;i++)
            {
                prev_ptr=cur_ptr;
                cur_ptr=cur_ptr-&amp;gt;Next;
            }

            prev_ptr-&amp;gt;Next=cur_ptr-&amp;gt;Next;
            free(cur_ptr);
        }
    }
    return 1;
 }&lt;/pre&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;br/&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.in/2009/08/implementation-of-singly-linked-list-in.html&quot; target=&quot;_blank&quot;&gt;Implementation of Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Reversing a Singly Linked List&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/12/deleting-node-from-singly-linked-list.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-851442953449411364</guid><pubDate>Tue, 25 Dec 2012 09:06:00 +0000</pubDate><atom:updated>2013-02-14T16:16:08.275-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><title>Detecting First Node in a Loop in the List</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html&quot; target=&quot;_blank&quot;&gt;Detecting a Loop in Singly Linked List - Tortoise and Hare&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article:&lt;/b&gt; &lt;a href=&quot;http://codingfreak.blogspot.com/2012/11/finding-nth-node-from-end-of-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Finding Nth node from end of a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Once we confirm that there is a Loop in a Singly Linked 
List we will see how to determine first node of the loop.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiQXue9eKVvjPQQZoRGu03rbIj_SgQKeXFPcufm8-HOOQF_GS9TZktfThhs5xrQP-jYCeNBF1cdgx34BC9J4yyS9LldPN-6Ny4hLp_cHH6N2MdrTwbM4IDEKLs5TUaUMQNXBtlO0whTUY/s1600/01.gif&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiQXue9eKVvjPQQZoRGu03rbIj_SgQKeXFPcufm8-HOOQF_GS9TZktfThhs5xrQP-jYCeNBF1cdgx34BC9J4yyS9LldPN-6Ny4hLp_cHH6N2MdrTwbM4IDEKLs5TUaUMQNXBtlO0whTUY/s320/01.gif&quot; width=&quot;311&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhazl0uUySXTv1tU6kYJXzEPr269xmPqCr8R3cFNiQWoXT1bD5yYibSjDpLgfgV-v8WKVYh65EOdu2XVSNtJTyOFDhcTw75696OuuLnoVmN04jZPcAkphyd0gVhzjPenQyns5_5gn0otBE/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhazl0uUySXTv1tU6kYJXzEPr269xmPqCr8R3cFNiQWoXT1bD5yYibSjDpLgfgV-v8WKVYh65EOdu2XVSNtJTyOFDhcTw75696OuuLnoVmN04jZPcAkphyd0gVhzjPenQyns5_5gn0otBE/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure-1: Singly Linked list with a loop which we have detected using Floyd&#39;s Cycle Detection Algorithm&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: red;&quot;&gt;&lt;b&gt;NOTE:&lt;/b&gt;&lt;/span&gt; Example in Figure-1 is the output of Floyd&#39;s Cycle detection Algorithm which we have seen in &lt;a href=&quot;http://codingfreak.blogspot.in/2012/09/detecting-loop-in-singly-linked-list_22.html&quot; target=&quot;_blank&quot;&gt;Floyd&#39;s Cycle Detection Algorithm&lt;/a&gt;. We are not randomly choosing Hare and Tortoise at Node-6.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Solution 01: Brute-force Approach&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;&lt;span style=&quot;font-size: small;&quot;&gt;
&lt;li&gt;Start at the Head of the Singly Linked List (i.e. &lt;b&gt;curNode&lt;/b&gt;).&lt;/li&gt;
&lt;li&gt;If &lt;b&gt;Tortoise&lt;/b&gt; and &lt;b&gt;Hare&lt;/b&gt; are at the same Node move 
the &lt;b&gt;curNode&lt;/b&gt; forward by one position.&lt;/li&gt;
&lt;li&gt;If &lt;b&gt;Tortoise&lt;/b&gt; and &lt;b&gt;curNode&lt;/b&gt; are at the same Node return, we have found first Node in the loop.&lt;/li&gt;
&lt;li&gt;Move &lt;b&gt;Tortoise&lt;/b&gt; forward by one position and start with STEP-2.&lt;/li&gt;
&lt;/span&gt;&lt;/ol&gt;
&lt;b&gt;Pseudocode:&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curNode = head

forever:

  if tortoise == hare
    tortoise = tortoise.next
    curNode = curNode.next

  if tortoise == curNode
    return curNode

  tortoise = tortoise.next &lt;/pre&gt;
&lt;b&gt;Complexity:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Time Complexity: &lt;b&gt;O(n*p) -&lt;/b&gt; where &#39;p&#39; is the length of the loop and &#39;n&#39; is the length of the List.&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;b&gt;O(1)&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Let us try above algorithm on example in Figure-1.The pattern for Hare, Tortoise and Culprit is given below&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Hare&amp;nbsp; Tortoise&amp;nbsp; Culprit&lt;/b&gt;&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; 7 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; HEAD&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br /&gt;
&lt;b&gt;7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;/b&gt;&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;br /&gt;
&lt;b&gt;7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/b&gt;&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqCTH_j5CCuARqZqaZ4rBjSO5nzvG-Laa1uGnTfL8U3W36sJ36bK12rZVb4KSojTigtX0G8xxm7wRpLBYKyFNSUEvDTySqJtm4xOxxTSVl5B7ryrKdD-4lbsDuZtsGr8X7vAnaVpShrWk/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgqCTH_j5CCuARqZqaZ4rBjSO5nzvG-Laa1uGnTfL8U3W36sJ36bK12rZVb4KSojTigtX0G8xxm7wRpLBYKyFNSUEvDTySqJtm4xOxxTSVl5B7ryrKdD-4lbsDuZtsGr8X7vAnaVpShrWk/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Solution 02:&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Let use see how to improve the Time Complexity of the solution 01.&lt;br /&gt;
&lt;br /&gt;
But before we go into the solution I would like to mention that the below solution can be used only after detecting a loop in the singly linked list. As mentioned Figure-1 is the output of Floyd&#39;s cycle detection algorithm and we havent randomly choosen Node-7. As pointed out many users if we start from Node-6 we might end up in loop.&lt;br /&gt;
&lt;ol&gt;&lt;span style=&quot;font-size: small;&quot;&gt;
&lt;li&gt;Start &lt;b&gt;Tortoise&lt;/b&gt; at first node.&lt;/li&gt;
&lt;li&gt;Move &lt;b&gt;Hare&lt;/b&gt; and &lt;b&gt;Tortoise&lt;/b&gt; by one position.&lt;/li&gt;
&lt;li&gt;If &lt;b&gt;Hare&lt;/b&gt; and &lt;b&gt;Tortoise&lt;/b&gt; points to same node then return (its the first Node in the loop)&lt;/li&gt;
&lt;li&gt;Start again from STEP-2 &lt;/li&gt;
&lt;/span&gt;&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;tortoise := firstNode

forever:

  if tortoise == hare
    return tortoise

  tortoise := tortoise.next
  hare := hare.next&lt;/pre&gt;
&lt;b&gt;Complexity:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Time Complexity: &lt;b&gt;O(n)&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;b&gt;O(1)&lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Example:&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
Let us try above algorithm on same example in Figure-1. Now let us start the Tortoise from the first node in the list.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHTQE0NddSa65Y5BIMjwibCiTGzUrhGnRgO_dAr0sxTPoFqjFTyun2yW9QYe_ZHx0kls_wV8QKlWUfKzC9Piq2mM_lZ9agZARHpvmYs4xnYhKGfYZXulwaLXpEvvbP06Fh70MSM25m-MU/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiHTQE0NddSa65Y5BIMjwibCiTGzUrhGnRgO_dAr0sxTPoFqjFTyun2yW9QYe_ZHx0kls_wV8QKlWUfKzC9Piq2mM_lZ9agZARHpvmYs4xnYhKGfYZXulwaLXpEvvbP06Fh70MSM25m-MU/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
The pattern for Hare and Tortoise is given below&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Hare&amp;nbsp; Tortoise&lt;/b&gt;&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp; 1 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
8&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2 &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9gKTJmoQ6FL48FfscL24BjEqGJ_QAt0ARvZlij8z40281RO8eFQLG2DOXm1uJJWkgjsvNHphv1EB80k1YQ9YRElpONtnlUvvy2Y7ly_HGF1Cap4BopQi32YeGlJnprqD9TAiBcMGosrg/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh9gKTJmoQ6FL48FfscL24BjEqGJ_QAt0ARvZlij8z40281RO8eFQLG2DOXm1uJJWkgjsvNHphv1EB80k1YQ9YRElpONtnlUvvy2Y7ly_HGF1Cap4BopQi32YeGlJnprqD9TAiBcMGosrg/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html&quot; target=&quot;_blank&quot;&gt;Detecting a Loop in Singly Linked List - Tortoise and Hare&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article:&lt;/b&gt; &lt;a href=&quot;http://codingfreak.blogspot.com/2012/11/finding-nth-node-from-end-of-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Finding Nth node from end of a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiiQXue9eKVvjPQQZoRGu03rbIj_SgQKeXFPcufm8-HOOQF_GS9TZktfThhs5xrQP-jYCeNBF1cdgx34BC9J4yyS9LldPN-6Ny4hLp_cHH6N2MdrTwbM4IDEKLs5TUaUMQNXBtlO0whTUY/s72-c/01.gif" height="72" width="72"/><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5905191137999281125</guid><pubDate>Wed, 14 Nov 2012 04:17:00 +0000</pubDate><atom:updated>2013-02-13T19:43:41.509-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><title>Finding Nth node from end of a Singly Linked List</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html&quot; target=&quot;_blank&quot;&gt;Finding first node in a Loop in Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgl_DFy_gLIyouSGyrrE_ScTx6sp_dB7HbBGwxnHg_Ys77v3pYt90lPQool-taknqCrTIiKv0lnppx01JVBIyVjcgv3rl94zK3h_nHey69caCe2PlzcPrLuMy27NWsK2wjbkKTtFQD3ABo/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgl_DFy_gLIyouSGyrrE_ScTx6sp_dB7HbBGwxnHg_Ys77v3pYt90lPQool-taknqCrTIiKv0lnppx01JVBIyVjcgv3rl94zK3h_nHey69caCe2PlzcPrLuMy27NWsK2wjbkKTtFQD3ABo/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: Normal;&quot;&gt;Figure 1: Singly Linked List&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Solution 01 - Brute Force Approach:&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Start at First Node of the List (call it &lt;b&gt;curNodePtr&lt;/b&gt;).&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Assign &lt;b&gt;curNodePtr&lt;/b&gt; to&lt;span style=&quot;font-size: small;&quot;&gt; &lt;b&gt;tmpPtr&lt;/b&gt; and count &lt;/span&gt;number of nodes after the &lt;b&gt;curNodePtr&lt;/b&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If number of nodes after &lt;b&gt;curNodePtr&lt;/b&gt; are equal to N nodes or &lt;b&gt;tmpPtr&lt;/b&gt; reaches END then break. If &lt;b&gt;tmPtr&lt;/b&gt; reaches END but &lt;span style=&quot;font-size: small;&quot;&gt;count not equal to N then return &lt;/span&gt;since we can&#39;t find the Nth node from the end of the Singly Linked List.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Mo&lt;/span&gt;ve the &lt;b&gt;curNodePtr&lt;/b&gt; one step forward in the Linked List i.e &lt;b&gt;curNodePtr&lt;/b&gt; now points to its next node in the list and start again from STEP-2&lt;/span&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curNodePtr := FirstNode

if curNodePtr == NULL
  return &#39;Less Nodes in the List&#39;

forever:

  tmpPtr := curNodePtr
  count := 0

  forever:

    if tmpPtr == NULL || count == N
      break

    tmpPtr := tmpPtr.NEXT
    count++

  if tmpPtr == NULL
    if count == n
      return curNodePtr
    else
      return &#39;Less Nodes in the List&#39;

  curNodePtr := curNodePtr.NEXT &lt;/pre&gt;
&lt;b&gt;Complexity&lt;/b&gt;:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Time Complexity: &lt;b&gt;O(n^2)&lt;/b&gt; - For traversing each node after &lt;b&gt;curNode&lt;/b&gt;.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Space Complexity: &lt;b&gt;O(1)&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
Let us try above brute-force approach on example in Figure-1&lt;br /&gt;
&lt;br /&gt;
Find 2nd Node: It re&lt;span style=&quot;font-size: small;&quot;&gt;turns Node 4.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;curNode &amp;nbsp; &amp;nbsp; tmpNode &amp;nbsp; Count&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1&lt;/span&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;01&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 03&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 03 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 04 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 2 &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;03 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 03 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/span&gt;&lt;/span&gt; &lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;03 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 04 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;03 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 05 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 2&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;04&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 04&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;04&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 05&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;04 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; NULL &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 2&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Find 6th Node: It returns &#39;Less Nodes in the List&#39;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: small;&quot;&gt;curNode &amp;nbsp; &amp;nbsp; tmpNode &amp;nbsp; Count&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 0&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 02 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 1 &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 03 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 2&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 04 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 3&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 05 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; 4&lt;/span&gt; &lt;/span&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;01 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; NULL &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 5&lt;/span&gt;&lt;/span&gt;&amp;nbsp;&lt;/span&gt; &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Solution 02:&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Let us improve the time complexity when compared to Solution-01.&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Find the length of the Singly Linked List (Let us say &lt;b&gt;P&lt;/b&gt;).&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If Length of the Singly linked List (P) is lesser than N return, since we can&#39;t find the Nth node from the end of the Singly Linked List.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If Length of the Singly Linked List (P) greater than N then compute (&lt;b&gt;P-N+1&lt;/b&gt;) value, which points to Nth node from the end of Singly Linked List.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Traverse to (&lt;b&gt;P-N+1&lt;/b&gt;)th Node in the Singly Linked List.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Pseudocode&lt;/b&gt;: &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curNodePtr := FirstNode
P = 0

forever:

  if curNodePtr == NULL
    break

  P++
  curNodePtr := curNodePtr.NEXT

if P &amp;lt; N
  return &#39;Less Nodes in the List&#39;

curNodePtr := FirstNode
count = 1

forever:

  if count == (P-N+1)
    return curNodePtr

  count++
  curNodePtr = curNodePtr.NEXT&lt;/pre&gt;
&lt;b&gt;Complexity&lt;/b&gt;:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Time Complexity: &lt;b&gt;O(n)&lt;/b&gt; - Time for finding the length of a Singly Linked List O(n) + Time for finding (P-N+1) node in the List O(n) = O(n)&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Space Complexity: &lt;b&gt;O(1)&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Solution 03:&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Let us see one more simple and cleaner approach when compared to Solution 02 which doesn&#39;t need to traverse all nodes in the list twice to find the Nth node from the end of a Singly Linked List.&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Let us take two pointers &lt;b&gt;tmpPtr&lt;/b&gt; and &lt;b&gt;nthNodePtr&lt;/b&gt; starting at the First Node of the Singly Linked List. &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;b&gt;tmpPtr&lt;/b&gt; will start first by traversing N positions in the list. If it doesn&#39;t reach N positions from start of the singly linked list it means list is small. Return as we can&#39;t find the Nth node from the end of the list.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Once &lt;b&gt;tmpPtr&lt;/b&gt; traverses N positions successfully &lt;b&gt;nthNodePtr&lt;/b&gt; starts traversing along with &lt;b&gt;tmpPtr&lt;/b&gt; one position forward until &lt;b&gt;tmpPtr&lt;/b&gt; reaches the end of the List.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Once &lt;b&gt;tmpPtr&lt;/b&gt; reaches end of the Singly Linked List &lt;b&gt;nthNodePtr&lt;/b&gt; will be pointing to Nth node from the end of the Singly Linked List&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;tmpPtr := FirstNode
nthNodePtr := FirstNode
count := 1

forever:
  
  if count == N || tmpPtr == NULL
    break

  tmpPtr := tmpPtr.NEXT
  count++

if tmpPtr == NULL &amp;amp;&amp;amp; count &amp;lt; N
  return &#39;Cannot find Nth element in the list&#39;

tmpPtr := tmpPtr.NEXT

forever:

  if tmpPtr == NULL
    return nthNodePtr

  tmpPtr := tmpPtr.NEXT
  nthNodePtr := nthNodePtr.NEXT 
&lt;/pre&gt;
&lt;b&gt;Complexity&lt;/b&gt;:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Time Complexity: &lt;b&gt;O(n)&lt;/b&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Space Complexity: &lt;b&gt;O(1)&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;:&lt;br /&gt;
&lt;br /&gt;
Let us try the above solution on example in Figure-1&lt;br /&gt;
&lt;br /&gt;
Find 2nd Node from end of the List&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;tmpPtr&amp;nbsp; nthNodePtr&amp;nbsp; count&lt;/b&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - &amp;nbsp;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; 5 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; 3 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; - &lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; NULL &amp;nbsp; &amp;nbsp;&amp;nbsp; 4 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; -&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/11/finding-nth-node-from-end-of-singly-linked-list.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgl_DFy_gLIyouSGyrrE_ScTx6sp_dB7HbBGwxnHg_Ys77v3pYt90lPQool-taknqCrTIiKv0lnppx01JVBIyVjcgv3rl94zK3h_nHey69caCe2PlzcPrLuMy27NWsK2wjbkKTtFQD3ABo/s72-c/01.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-572320054389403673</guid><pubDate>Wed, 07 Nov 2012 04:24:00 +0000</pubDate><atom:updated>2013-02-12T19:51:19.334-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><title>Reversing a Singly Linked List</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/12/deleting-node-from-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Deleting a Node from a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html&quot; target=&quot;_blank&quot;&gt;Detecting a Loop in Singly Linked List - Tortoise and Hare&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8Akk_Y8ZxzKailNy31BhP3Zvb7Yd4lUgTGaBUyV-EqPXkaXdHF301cEi-uLe5Uj0fwzJCTcf4kJwwS32I2u4GQRuEOn-UJRZrr1vnHFgbHLkcc6EkaS7q1JzvAFj2_BzT-49GWWvbpgU/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; height=&quot;308&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8Akk_Y8ZxzKailNy31BhP3Zvb7Yd4lUgTGaBUyV-EqPXkaXdHF301cEi-uLe5Uj0fwzJCTcf4kJwwS32I2u4GQRuEOn-UJRZrr1vnHFgbHLkcc6EkaS7q1JzvAFj2_BzT-49GWWvbpgU/s320/01.png&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
Let us see how to reverse a Singly Linked List.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd1c7ftojDKsxiFaWb-kGrDlrC1Tna3qQVHSBKKQdQk_bs69ejl8gmoOI5uzfFUVc-utKgsP7pa4aAYAZWmMrdMhEe2r8yweeR8JXwjXMl22mCB8ezr9qCLmdmKYfMIbdCNvM_gBHYmVc/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhd1c7ftojDKsxiFaWb-kGrDlrC1Tna3qQVHSBKKQdQk_bs69ejl8gmoOI5uzfFUVc-utKgsP7pa4aAYAZWmMrdMhEe2r8yweeR8JXwjXMl22mCB8ezr9qCLmdmKYfMIbdCNvM_gBHYmVc/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure-1: Singly Linked List&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;b&gt;Pseudocode:&lt;/b&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;cur_ptr = HEAD-&amp;gt;NEXT
prev_ptr = NULL

forever:

   if cur_ptr == NULL
   break

   tmp_ptr  = prev_ptr
   prev_ptr = cur_ptr
   cur_ptr  = cur_ptr-&amp;gt;NEXT
   
   prev_ptr-&amp;gt;NEXT = tmp_ptr

HEAD-&amp;gt;NEXT = prev_ptr&lt;/pre&gt;
&lt;b&gt;Complexity&lt;/b&gt;:&lt;br /&gt;
Time Complexity: &lt;b&gt;O(n)&lt;/b&gt;&lt;br /&gt;
Space Complexity: &lt;b&gt;O(3)&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
If we try on the example in Figure-1 we get the output as shown below&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhseqb6qnS7fhZv2ow4N5I-GtxKtQgDID2lcYxxiFmFGHkBmgNyS5g75uwqatrENUVvnpSixHbLyrvOhN27GIbNHDooo4K6krlhu6Q9sYsVBvKkJjmTfZFEwXilIknwee1OpsyupZTde5E/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhseqb6qnS7fhZv2ow4N5I-GtxKtQgDID2lcYxxiFmFGHkBmgNyS5g75uwqatrENUVvnpSixHbLyrvOhN27GIbNHDooo4K6krlhu6Q9sYsVBvKkJjmTfZFEwXilIknwee1OpsyupZTde5E/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure-2: After reversing the Singly Linked List&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/12/deleting-node-from-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Deleting a Node from a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html&quot; target=&quot;_blank&quot;&gt;Detecting a Loop in Singly Linked List - Tortoise and Hare&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg8Akk_Y8ZxzKailNy31BhP3Zvb7Yd4lUgTGaBUyV-EqPXkaXdHF301cEi-uLe5Uj0fwzJCTcf4kJwwS32I2u4GQRuEOn-UJRZrr1vnHFgbHLkcc6EkaS7q1JzvAFj2_BzT-49GWWvbpgU/s72-c/01.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5690511211198480784</guid><pubDate>Sat, 22 Sep 2012 06:07:00 +0000</pubDate><atom:updated>2013-02-15T10:01:30.938-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><title>Detecting a Loop in Singly Linked List - Tortoise &amp; Hare</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.com/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;&lt;b&gt;Datastructures&lt;/b&gt;&lt;/a&gt;&quot;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Reversing a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html&quot; target=&quot;_blank&quot;&gt;Finding first node in a Loop in Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Eventhough there are multiple algorithms available we start with&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Floyd&#39;s Cycle-Finding Algorithm&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
In simple terms it is also known as &quot;&lt;b&gt;Tortoise and Hare Algorithm&lt;/b&gt;&quot; or &quot;&lt;b&gt;Floyd&#39;s Cycle Detection Algorithm&lt;/b&gt;&quot; named after its inventor Robert Floyd. It is one of the simple cycle detection algorithm. It&#39;s a simple pointers based approach.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUDnY1bnMSV5C93RPknuxLINkuRBQAaAg5KdqPx9YLnsILsCohG7e4fwjL6rxOQ4calVPokB1ZRPKdx_EvRqxmpKA_DvH2ORtRtSH7NzoFIPeiqdRGxudiF0H4Lf94fF6CXiCw8XPiGhk/s1600/01.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUDnY1bnMSV5C93RPknuxLINkuRBQAaAg5KdqPx9YLnsILsCohG7e4fwjL6rxOQ4calVPokB1ZRPKdx_EvRqxmpKA_DvH2ORtRtSH7NzoFIPeiqdRGxudiF0H4Lf94fF6CXiCw8XPiGhk/s1600/01.jpg&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Robert Floyd&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Let us take 2 pointers namely slow Pointer and fast Pointer to traverse a Singly Linked List at different speeds. A slow Pointer (Also called &lt;b&gt;Tortoise&lt;/b&gt;) moves one step forward while fast Pointer (Also called &lt;b&gt;Hare&lt;/b&gt;) moves 2 steps forward&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Start &lt;b&gt;Tortoise&lt;/b&gt; and &lt;b&gt;Hare&lt;/b&gt; at the f&lt;span style=&quot;font-size: small;&quot;&gt;irst node &lt;/span&gt;of the List.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If &lt;b&gt;Hare&lt;/b&gt; reaches end of the List&lt;span style=&quot;font-size: small;&quot;&gt;, &lt;/span&gt;return&lt;span style=&quot;font-size: small;&quot;&gt; as there is &lt;span style=&quot;font-size: small;&quot;&gt;n&lt;/span&gt;&lt;/span&gt;o loop in the list.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Else &lt;span style=&quot;font-size: small;&quot;&gt;m&lt;/span&gt;ove &lt;b&gt;Hare&lt;/b&gt; one step forward.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If &lt;b&gt;Hare&lt;/b&gt; reaches end of the List, return as there &lt;span style=&quot;font-size: small;&quot;&gt;is &lt;span style=&quot;font-size: small;&quot;&gt;n&lt;/span&gt;&lt;/span&gt;o loop in the list.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Else&lt;span style=&quot;font-size: small;&quot;&gt; move&lt;/span&gt; &lt;b&gt;Hare&lt;/b&gt; and &lt;b&gt;Tortoise&lt;/b&gt; one step forward.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;If &lt;b&gt;Hare&lt;/b&gt; and &lt;b&gt;Tortoise&lt;/b&gt; pointing to same Node return, we found loop in the List.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style=&quot;font-size: small;&quot;&gt;Else &lt;span style=&quot;font-size: small;&quot;&gt;s&lt;/span&gt;tart with STEP-2.&lt;/span&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;b&gt;Pseudocode&lt;/b&gt;:
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;tortoise := firstNode
hare := firstNode

forever:

  if hare == end 
    return &#39;No Loop Found&#39;

  hare := hare.next

  if hare == end
    return &#39;No Loop Found&#39;

  hare = hare.next
  tortoise = tortoise.next

  if hare == tortoise
    return &#39;Loop Found&#39;&lt;/pre&gt;
&lt;b&gt;Why can&#39;t we let the Hare go by itself like Tortoise &lt;/b&gt;?&lt;br /&gt;
If there is a loop, Hare would just go forever. Tortoise ensures that you will only take &#39;n&#39; steps atmost.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;How to find the length of the Loop ?&lt;/b&gt;&lt;br /&gt;
Once Hare and Tortoise finds the loop in a Singly linked List move Tortoise one step forward everytime maintaining the count of the nodes until it reaches the Hare again.
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Example&lt;/b&gt;:&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEi7gW8SYWCUU1FRxWEPvWv9Q0GVcIckGuJj5BHewCXEXyUQXI_vTMUndPg92gImcmMDBqYw-wwayK-ZLMzRPyUNqSsGMh5l2P6cseZ_v2nSQqpacRDG0vw4X_d9KQARqVsyBZX5RcPpo/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhEi7gW8SYWCUU1FRxWEPvWv9Q0GVcIckGuJj5BHewCXEXyUQXI_vTMUndPg92gImcmMDBqYw-wwayK-ZLMzRPyUNqSsGMh5l2P6cseZ_v2nSQqpacRDG0vw4X_d9KQARqVsyBZX5RcPpo/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Singly Linked List with a Loop&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Both Tortoise and Hare starts at First node of the list and Tortoise moves 1 step forward while Hare moves 2 steps forward.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeCBzAFNCEjt9V2cod9b7fMAAud1moToJl88iP6dOwZ3jzS2zSi_25KX3sWXANFUF1xkbhoUyq077MZve8380oYX70_7UqGpXxcYRpNT20aLKdVmfVig6KNpbQqCUTK7osPV0p7i0YJ2s/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgeCBzAFNCEjt9V2cod9b7fMAAud1moToJl88iP6dOwZ3jzS2zSi_25KX3sWXANFUF1xkbhoUyq077MZve8380oYX70_7UqGpXxcYRpNT20aLKdVmfVig6KNpbQqCUTK7osPV0p7i0YJ2s/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Hare and Tortoise starts at First Node of the List&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
The pattern of Hare and Tortoise movements are shown below.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Hare&amp;nbsp;&amp;nbsp; Tortoise&lt;/b&gt;&lt;br /&gt;
1&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 1&lt;br /&gt;
3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 2 &lt;br /&gt;
5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 3&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 4&lt;br /&gt;
3&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 5&lt;br /&gt;
5&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 6&lt;br /&gt;
7&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 7&lt;br /&gt;
&lt;br /&gt;
Both Hare and Tortoise meet up at Node 7. That proves there is a loop in the list&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhazl0uUySXTv1tU6kYJXzEPr269xmPqCr8R3cFNiQWoXT1bD5yYibSjDpLgfgV-v8WKVYh65EOdu2XVSNtJTyOFDhcTw75696OuuLnoVmN04jZPcAkphyd0gVhzjPenQyns5_5gn0otBE/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhazl0uUySXTv1tU6kYJXzEPr269xmPqCr8R3cFNiQWoXT1bD5yYibSjDpLgfgV-v8WKVYh65EOdu2XVSNtJTyOFDhcTw75696OuuLnoVmN04jZPcAkphyd0gVhzjPenQyns5_5gn0otBE/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;b&gt;Complexity:&lt;/b&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Time Complexity: &lt;b&gt;O(n)&lt;/b&gt;&lt;/li&gt;
&lt;li&gt;Space Complexity: &lt;b&gt;O(1) &lt;/b&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
To calculate the Time Complexity the shape of the cycle doesn&#39;t matter. It can have a long tail, and a loop towards the end or just a loop from the beginning to the end without a tail. Irrespective of the shape of the cycle, one thing is clear - that the Tortoise can never catch up with the Hare. If the two has to meet, the Hare has to catch up with the Tortoise from behind.&lt;br /&gt;
&lt;br /&gt;
With that established, consider the two possibilities&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Hare is one step behind Tortoise&lt;/li&gt;
&lt;li&gt;Hare is two step behind Tortoise&lt;/li&gt;
&lt;/ul&gt;
All greater distances will reduce to One or Two. Let us assume always Tortoise moves first&amp;nbsp; (it could be even other way).&lt;br /&gt;
&lt;br /&gt;
In the first case were the distance between Hare and Tortoise is one step. Tortoise moves one step forward and the distance between Hare and Tortoise becomes 2. Now Hare moves 2 steps forward meeting up with Tortoise.&lt;br /&gt;
&lt;br /&gt;
In the second case were the distance between Hare and Tortoise is two steps. Tortoise moves one step forward and the distance between Hare and Tortoise becomes 3. Now Hare moved 2 steps forward which makes the distance between Hare and Tortoise as 1. It is similar to first case which we already proved that both Hare and Tortoise will meet up in next step.&lt;br /&gt;
&lt;br /&gt;
Let the length of the loop be &#39;n&#39; and there are &#39;p&#39; variables before the loop. Hare traverses the loop twice in &#39;n&#39; moves, they are guaranteed to meet in O(n).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Reversing a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html&quot; target=&quot;_blank&quot;&gt;Finding first node in a Loop in Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiUDnY1bnMSV5C93RPknuxLINkuRBQAaAg5KdqPx9YLnsILsCohG7e4fwjL6rxOQ4calVPokB1ZRPKdx_EvRqxmpKA_DvH2ORtRtSH7NzoFIPeiqdRGxudiF0H4Lf94fF6CXiCw8XPiGhk/s72-c/01.jpg" height="72" width="72"/><thr:total>11</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-8125301596330290867</guid><pubDate>Thu, 23 Aug 2012 15:19:00 +0000</pubDate><atom:updated>2013-02-25T07:24:35.649-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><title>Write a C Program without main function</title><description>In a C program &lt;b&gt;main&lt;/b&gt; function is the entry point so it is mandatory to have a main function.&lt;br /&gt;
&lt;br /&gt;
But let us see how can we write a program without &lt;b&gt;main&lt;/b&gt; (Kind of hiding main in some obfuscated code). This post is purely out of interest to know that we can do something weird like this, just for learning.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb1sqBqqfho6XiFh7cBXDlUFd2So-Bj8raokMm4dW3bCzyaucnr-ty7LhxLaVc5GOulYtuxUKaaNAqJ0k2tMPvqJjB5FsMwCxi5c3ixMYJyk9OrumwAcgQ2pBbW1-EhnW7rwzwpcjYs4E/s1600/01.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb1sqBqqfho6XiFh7cBXDlUFd2So-Bj8raokMm4dW3bCzyaucnr-ty7LhxLaVc5GOulYtuxUKaaNAqJ0k2tMPvqJjB5FsMwCxi5c3ixMYJyk9OrumwAcgQ2pBbW1-EhnW7rwzwpcjYs4E/s1600/01.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;Solution 01: Using simple macro substitution &lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

#define START main

int START()
{
   printf(&quot;HELLO WORLD&quot;);
}&lt;/pre&gt;
During the preprocessing stage of compilation of above code, HELLO is replaced with main. &lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Solution 02: Using Token Merging Operator&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;##&lt;/b&gt; is a Token merging operator. It can merge two or more characters. &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

#define START m##a##i##n

int START()
{
   printf(&quot;HELLO WORLD&quot;);
}&lt;/pre&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Solution 03: Using Argumented Macro&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

#define decode(l,i,g,h,t) h##l##g##i
#define START decode(a,n,i,m,e)

int START()
{
   printf(&quot;HELLO WORLD&quot;);
}&lt;/pre&gt;
During the preprocessing stage START is replaced with decode which takes 5 arguments namely a n i m e and then decode is replaced h##l##g##i which means main.&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/08/write-a-c-program-without-main-function.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhb1sqBqqfho6XiFh7cBXDlUFd2So-Bj8raokMm4dW3bCzyaucnr-ty7LhxLaVc5GOulYtuxUKaaNAqJ0k2tMPvqJjB5FsMwCxi5c3ixMYJyk9OrumwAcgQ2pBbW1-EhnW7rwzwpcjYs4E/s72-c/01.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-819243095199038007</guid><pubDate>Thu, 09 Aug 2012 15:14:00 +0000</pubDate><atom:updated>2015-07-13T17:53:07.896-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">Interview Question</category><category domain="http://www.blogger.com/atom/ns#">Linux</category><category domain="http://www.blogger.com/atom/ns#">Programming</category><title>Implementing ls command in C</title><description>We know that &lt;b&gt;ls&lt;/b&gt; command in unix displays the list of files in a directory. It has various options to display the data in various styles and formats.&lt;br /&gt;
&lt;br /&gt;
By default its implementation is part of &lt;a href=&quot;http://www.gnu.org/software/coreutils/&quot; target=&quot;_blank&quot;&gt;Coreutils&lt;/a&gt; package, which is default package in all Linux flavours.&lt;br /&gt;
&lt;br /&gt;
I want to see how we can implement its basic functionality with a simple C program.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU-nhW_E21HLWuOVy1941hMQb2lR2Sgk02zJYqd1e46k938Z9sJTMCXYgY0vd-2oucr3hwpT10iiYU14B5JK3Jrr8zjXYLWG5WixlvIFmFBEeNbtTveuBXQqt_yUK8UvlNXDdIP5UlK1k/s1600/01.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU-nhW_E21HLWuOVy1941hMQb2lR2Sgk02zJYqd1e46k938Z9sJTMCXYgY0vd-2oucr3hwpT10iiYU14B5JK3Jrr8zjXYLWG5WixlvIFmFBEeNbtTveuBXQqt_yUK8UvlNXDdIP5UlK1k/s1600/01.jpg&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;/*Listing the Files in a directory */

#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/dir.h&amp;gt;
#include &amp;lt;sys/param.h&amp;gt;
#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;sys/stat.h&amp;gt;
#include &amp;lt;dirent.h&amp;gt;
#include &amp;lt;pwd.h&amp;gt;
#include &amp;lt;grp.h&amp;gt;
#include &amp;lt;time.h&amp;gt;
#include &amp;lt;locale.h&amp;gt;
#include &amp;lt;langinfo.h&amp;gt;
#include &amp;lt;stdint.h&amp;gt;
#include &amp;lt;fcntl.h&amp;gt;

static char perms_buff[30];

const char *get_perms(mode_t mode)
{
  char ftype = &#39;?&#39;;

  if (S_ISREG(mode)) ftype = &#39;-&#39;;
  if (S_ISLNK(mode)) ftype = &#39;l&#39;;
  if (S_ISDIR(mode)) ftype = &#39;d&#39;;
  if (S_ISBLK(mode)) ftype = &#39;b&#39;;
  if (S_ISCHR(mode)) ftype = &#39;c&#39;;
  if (S_ISFIFO(mode)) ftype = &#39;|&#39;;

  sprintf(perms_buff, &quot;%c%c%c%c%c%c%c%c%c%c %c%c%c&quot;, ftype,
  mode &amp;amp; S_IRUSR ? &#39;r&#39; : &#39;-&#39;,
  mode &amp;amp; S_IWUSR ? &#39;w&#39; : &#39;-&#39;,
  mode &amp;amp; S_IXUSR ? &#39;x&#39; : &#39;-&#39;,
  mode &amp;amp; S_IRGRP ? &#39;r&#39; : &#39;-&#39;,
  mode &amp;amp; S_IWGRP ? &#39;w&#39; : &#39;-&#39;,
  mode &amp;amp; S_IXGRP ? &#39;x&#39; : &#39;-&#39;,
  mode &amp;amp; S_IROTH ? &#39;r&#39; : &#39;-&#39;,
  mode &amp;amp; S_IWOTH ? &#39;w&#39; : &#39;-&#39;,
  mode &amp;amp; S_IXOTH ? &#39;x&#39; : &#39;-&#39;,
  mode &amp;amp; S_ISUID ? &#39;U&#39; : &#39;-&#39;,
  mode &amp;amp; S_ISGID ? &#39;G&#39; : &#39;-&#39;,
  mode &amp;amp; S_ISVTX ? &#39;S&#39; : &#39;-&#39;);

  return (const char *)perms_buff;
}

char pathname[MAXPATHLEN];

void die(char *msg)
{
  perror(msg);
  exit(0);
}

static int
one (const struct dirent *unused)
{
  return 1;
}

int main()
{
  int count,i;
  struct direct **files;
  struct stat statbuf;
  char datestring[256];
  struct passwd pwent;
  struct passwd *pwentp;
  struct group grp;
  struct group *grpt;
  struct tm time;
  char buf[1024];

  if(!getcwd(pathname, sizeof(pathname)))
    die(&quot;Error getting pathnamen&quot;);

  count = scandir(pathname, &amp;amp;files, one, alphasort);

  if(count &amp;gt; 0)
  {
    printf(&quot;total %d\n&quot;,count);

    for (i=0; i&amp;lt;count; ++i)
    {
      if (stat(files[i]-&amp;gt;d_name, &amp;amp;statbuf) == 0)
      {
        /* Print out type, permissions, and number of links. */
        printf(&quot;%10.10s&quot;, get_perms(statbuf.st_mode));
        printf(&quot; %d&quot;, statbuf.st_nlink);

        if (!getpwuid_r(statbuf.st_uid, &amp;amp;pwent, buf, sizeof(buf), &amp;amp;pwentp))
          printf(&quot; %s&quot;, pwent.pw_name);
        else
          printf(&quot; %d&quot;, statbuf.st_uid);

        if (!getgrgid_r (statbuf.st_gid, &amp;amp;grp, buf, sizeof(buf), &amp;amp;grpt))
          printf(&quot; %s&quot;, grp.gr_name);
        else
          printf(&quot; %d&quot;, statbuf.st_gid);

        /* Print size of file. */
        printf(&quot; %5d&quot;, (int)statbuf.st_size);

        localtime_r(&amp;amp;statbuf.st_mtime, &amp;amp;time);
        /* Get localized date string. */
        strftime(datestring, sizeof(datestring), &quot;%F %T&quot;, &amp;amp;time);

        printf(&quot; %s %s\n&quot;, datestring, files[i]-&amp;gt;d_name);
      }

      free (files[i]);
    }

    free(files);
  }
}&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/08/implementing-ls-command-in-c.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU-nhW_E21HLWuOVy1941hMQb2lR2Sgk02zJYqd1e46k938Z9sJTMCXYgY0vd-2oucr3hwpT10iiYU14B5JK3Jrr8zjXYLWG5WixlvIFmFBEeNbtTveuBXQqt_yUK8UvlNXDdIP5UlK1k/s72-c/01.jpg" height="72" width="72"/><thr:total>6</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-8983264111824311653</guid><pubDate>Thu, 12 Jul 2012 06:45:00 +0000</pubDate><atom:updated>2012-12-09T20:01:19.670-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">git</category><title>GIT - Adding diffmerge as visual merge in git</title><description>GIT is one of the popular distributed code repositories in opensource community, especially with developers working on opensource projects like linux kernel and others.&lt;br /&gt;
&lt;br /&gt;
Operations like Merge and Diff are the most irritating &amp;amp; tricky tasks via command line when working on large code changes. So let us see how can we add some graphical stuff for these operations when using on GIT so that we can do merging and other options very easily.&amp;nbsp;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcelebPu059S2ukLCi2ITZDEpRkvjGiOKgGb7xL5r7h-dbnkIRvxjVxbP3sClGNJGhkp6LHb_Uf5aXQ81bdmTxSB4MRxgd8KBw6AtMokI2edSaJmENz0m0peTpll_f95cM9efzfdBzR48/s1600/git.jpg&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; height=&quot;272&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcelebPu059S2ukLCi2ITZDEpRkvjGiOKgGb7xL5r7h-dbnkIRvxjVxbP3sClGNJGhkp6LHb_Uf5aXQ81bdmTxSB4MRxgd8KBw6AtMokI2edSaJmENz0m0peTpll_f95cM9efzfdBzR48/s320/git.jpg&quot; title=&quot;git&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;By following below steps you can install and configure diffmerge tool for git.&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Based on the operating system you are using download respective &lt;a href=&quot;http://www.sourcegear.com/diffmerge/downloads.php&quot; target=&quot;_blank&quot;&gt;diffmerge&lt;/a&gt; tool. &lt;/li&gt;
&lt;br /&gt;
&lt;li&gt;Now go to the source code directory which have been cloned from a git repository. Add the following commands to make git support diffmerge in diff and merge operations.&lt;/li&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd &#39;diffmerge &quot;$LOCAL&quot; &quot;$REMOTE&quot;&#39;
git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd &#39;diffmerge --merge --result=&quot;$MERGED&quot; &quot;$LOCAL&quot; &quot;$(if test -f &quot;$BASE&quot;; then echo &quot;$BASE&quot;; else echo &quot;$LOCAL&quot;; fi)&quot; &quot;$REMOTE&quot;&#39;
git config --global mergetool.diffmerge.trustExitCode true&lt;/pre&gt;
&lt;/ol&gt;
Configuration is done.&lt;br /&gt;
&lt;br /&gt;
Now let us see how can we use the diffmerge tool. &lt;br /&gt;
&lt;br /&gt;
Whenever you want to launch diff use difftool and similarly mergetool to launch merge as shown below.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;# To diff a local file against the checked-in version
git difftool file

# to diff the local file against the version in some-feature-branch
git difftool some-feature-branch file

# to diff the file under the Build-54 tag to the Build-55 tag
git difftool Build-54..Build-55 file

# to do merge operations and resolve conflicts via diffmerge tool use below command.
git mergetool&lt;/pre&gt;
&lt;br /&gt;
Start using it now. &lt;br /&gt;
&lt;h4&gt;
&lt;b&gt;References&amp;nbsp;&lt;/b&gt;&lt;/h4&gt;
1. &lt;a href=&quot;http://twobitlabs.com/2011/08/install-diffmerge-git-mac-os-x/&quot;&gt;Twobit Labs&lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/07/git-adding-diffmerge-as-visual-merge-in.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcelebPu059S2ukLCi2ITZDEpRkvjGiOKgGb7xL5r7h-dbnkIRvxjVxbP3sClGNJGhkp6LHb_Uf5aXQ81bdmTxSB4MRxgd8KBw6AtMokI2edSaJmENz0m0peTpll_f95cM9efzfdBzR48/s72-c/git.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-7379765917753379539</guid><pubDate>Thu, 14 Jun 2012 09:29:00 +0000</pubDate><atom:updated>2014-10-13T19:58:13.437-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">cheat-sheet</category><category domain="http://www.blogger.com/atom/ns#">How to</category><category domain="http://www.blogger.com/atom/ns#">tcpdump</category><title>TCPDUMP cheat sheet</title><description>&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkLJK1IML2irwJNI7xkzINvfss9PPOtdCHIs13I4Yvfhs5C6nvQMVrhWrbdYnL5uKFOB1xKeFtQzRjaMAYgvVgTs5V9PcTrrwFH40ZSsYKj5A5kE_1dc56oZ79baJzDH02DkWXLXuOviE/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkLJK1IML2irwJNI7xkzINvfss9PPOtdCHIs13I4Yvfhs5C6nvQMVrhWrbdYnL5uKFOB1xKeFtQzRjaMAYgvVgTs5V9PcTrrwFH40ZSsYKj5A5kE_1dc56oZ79baJzDH02DkWXLXuOviE/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;ol&gt;
&lt;li&gt;Listing possible network interfaces on the system&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;tcpdump -D &lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
E.g. &lt;br /&gt;
$ tcpdump -D&lt;br /&gt;
1.eth0&lt;br /&gt;
2.eth1&lt;br /&gt;
3.usbmon1 (USB bus number 1)&lt;br /&gt;
4.eth2&lt;br /&gt;
&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Capture packets from a particular interface &lt;br /&gt;&lt;br /&gt;&lt;b&gt;tcpdump -i interface-name&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;E.g. To capture packets from interface eth1 - 
tcpdump -i eth1&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Capture only N number of packets&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -c N &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. To capture 10 packets from interface eth1 - tcpdump -i eth1 -c 10 &lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Capture the packets and write into a file &lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -w file.pcap&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 -w tmp.pcap&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;To capture and store network frames full-length&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -s 0&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 -w tmp.pcap -s 0&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Reading the packets from a saved file&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -r file.pcap&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g.tcpdump -tttt -r tmp.pcap&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Capture packets with proper readable timestamp&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -tttt&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 -tttt&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Filter Options&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ol&gt;
Possible qualifiers:&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;type&lt;/b&gt;: host, net , port and portrange. &lt;br /&gt;

E.g., ‘host foo’, ‘net 128.3’, ‘port 20’, ‘portrange 6000-6008’. &lt;br /&gt;&lt;br /&gt;

&lt;b&gt;dir&lt;/b&gt;: src, dst, src or dst and src and dst. &lt;br /&gt;

E.g., ‘src foo’, ‘dst net 128.3’, ‘src or dst port ftp-data’. If there is no dir qualifier, src or dst is assumed.&lt;br /&gt;&lt;br /&gt;

&lt;b&gt;proto&lt;/b&gt;: ether, fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp and udp. &lt;br /&gt;

E.g., ‘ether src foo’, ‘arp net 128.3’, ‘tcp port 21’, ‘udp portrange 7000-7009’. &lt;br /&gt;

If there is no proto qualifier, all protocols are assumed.&lt;br /&gt;&lt;br /&gt;
&lt;li&gt;Read packets longer than N bytes&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump greater N&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 -w tmp.pcap greater 1024&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Receive only the packets of a specific protocol type - fddi, tr, wlan, ip, ip6, arp, rarp, decnet, tcp and udp&lt;br /&gt;&lt;br /&gt;
E.g tcpdump -i eth1 arp&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Receive packets flows on a particular port&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump port PORT_NO&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 port 22&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Capture packets for particular destination IP and Port&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump dst IPADDRESS and port PORT-NO&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 dst 10.181.140.216 and port 22&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;span style=&quot;color: red;&quot;&gt;Display options&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Display more packet information &lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -vvv &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 -vvv&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Display link level header of every packet: -e &lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -e &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. &lt;br /&gt;&lt;br /&gt;
tcpdump -i eth1 -e -t&lt;br /&gt;
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes&lt;br /&gt;
&lt;b&gt;52:54:00:e1:1c:10 (oui Unknown) &amp;gt; 01:80:c2:00:00:00 (oui Unknown), 802.3, length 60: LLC, dsap STP (0x42) Individual, ssap STP (0x42) Command, ctrl 0x03&lt;/b&gt;: STP 802.1d, Config, Flags [none], bridge-id 8000.52:54:00:e1:1c:10.8003, length 43&lt;br /&gt;
&lt;b&gt;52:54:00:e1:1c:10 (oui Unknown) &amp;gt; 01:80:c2:00:00:00 (oui Unknown), 802.3, length 60: LLC, dsap STP (0x42) Individual, ssap STP (0x42) Command, ctrl 0x03&lt;/b&gt;: STP 802.1d, Config, Flags [none], bridge-id 8000.52:54:00:e1:1c:10.8003, length 43&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Don’t print a timestamp on each dump line: -t &lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -t &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. &lt;br /&gt;&lt;br /&gt;
Without using -t option we can see the below output timestamp is dumped (In bold letters).&lt;br /&gt;&lt;br /&gt;
tcpdump -i eth2&lt;br /&gt;&lt;br /&gt;
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes&lt;br /&gt;
&lt;b&gt;08:44:51.295229&lt;/b&gt; STP 802.1d, Config, Flags [none], bridge-id 8000.52:54:00:e1:1c:10.8003, length 43&lt;br /&gt;
&lt;b&gt;08:44:53.296795&lt;/b&gt; STP 802.1d, Config, Flags [none], bridge-id 8000.52:54:00:e1:1c:10.8003, length 43&lt;br /&gt;&lt;br /&gt;
tcpdump -i eth2 -t&lt;br /&gt;&lt;br /&gt;
listening on eth2, link-type EN10MB (Ethernet), capture size 65535 bytes&lt;br /&gt;
STP 802.1d, Config, Flags [none], bridge-id 8000.52:54:00:e1:1c:10.8003, length 43&lt;br /&gt;
STP 802.1d, Config, Flags [none], bridge-id 8000.52:54:00:e1:1c:10.8003, length 43&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Display packets with IP address instead of DNS names: -n&lt;br /&gt;&lt;br /&gt;
Basically tcpdump converts the plain address to DNS names. Using n option we can make tcpdump to display ip address.&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -n&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 -n&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Display Captured Packets in ASCII &lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -A &lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
E.g. tcpdump -i eth1 -A&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;li&gt;Display Captured Packets in HEX and ASCII&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;tcpdump -XX&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/06/tcpdump-cheat-sheet.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkLJK1IML2irwJNI7xkzINvfss9PPOtdCHIs13I4Yvfhs5C6nvQMVrhWrbdYnL5uKFOB1xKeFtQzRjaMAYgvVgTs5V9PcTrrwFH40ZSsYKj5A5kE_1dc56oZ79baJzDH02DkWXLXuOviE/s72-c/01.png" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-7374716353453789195</guid><pubDate>Wed, 02 May 2012 09:47:00 +0000</pubDate><atom:updated>2012-12-09T21:26:00.149-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Hardware</category><category domain="http://www.blogger.com/atom/ns#">How to</category><category domain="http://www.blogger.com/atom/ns#">Linux</category><title>Decoding hardware information of a PC</title><description>Checking out machine hardware information is no more a geeky thing of olden days where you need to go through the hardware specification documents or to open up a physical machine to find out the hardware details if the specification documents are missing. Now we have some really cool handy software tools to help us out.&lt;br /&gt;
&lt;br /&gt;
I thought to make a page with some important tools which help us in decoding the hardware related information on a Linux Box. Feel free to drop a comment with the tool names I missed out in this post.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE:&lt;/b&gt; All of them are ordered in alphabetical order and I am trying these tools on a Ubuntu machine running in virtual box. So some of the tool outputs might be displaying names likes VirtualBox and Oracle Corporation.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6RYJLSKUPiAkx1ndoeOHD-yyHODbl1831k-1y-CK6QZH3c3paIg-PTPC_OB6nKm3JUa9gMjVTgtGU0RjU6Q3NQVWCevvYuAlZlsiG53SlwepZJdIjMY_uifp_x-trCzhjxAubeA6_7QA/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Tweak&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6RYJLSKUPiAkx1ndoeOHD-yyHODbl1831k-1y-CK6QZH3c3paIg-PTPC_OB6nKm3JUa9gMjVTgtGU0RjU6Q3NQVWCevvYuAlZlsiG53SlwepZJdIjMY_uifp_x-trCzhjxAubeA6_7QA/s1600/01.png&quot; title=&quot;Tweak&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;biosdecode&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Also known as BIOS Information Decoder is one of the best tools to check the BIOS information. It parses the BIOS memory and displays the info about your system BIOS. For more detailed info checkout &lt;a href=&quot;http://linux.die.net/man/8/biosdecode&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;man-page&lt;/a&gt;.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;ACPI 2.0 present.
 OEM Identifier: VBOX  
 RSD Table 32-bit Address: 0x33FF0000
 XSD Table 64-bit Address: 0x0000000033FF0030
BIOS32 Service Directory present.
 Revision: 0
 Calling Interface Address: 0x000FDA00
PCI Interrupt Routing 1.0 present.
 Router ID: 00:01.0
 Exclusive IRQs: None
 Compatible Router: 8086:7000
.
.
SMBIOS 2.5 present.
 Structure Table Length: 352 bytes
 Structure Table Address: 0x000E1000
 Number Of Structures: 5
 Maximum Structure Size: 255 bytes
&lt;/pre&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;dmidecode&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
Also known as DMI Table Decoder. It just dumps the DMI or SMBIOS information, to screen, in a human-readable format. Quite verbose with the information it provides but this tool seems to be unreliable as per its users in some case. For more detailed info checkout &lt;a href=&quot;http://linux.die.net/man/8/dmidecode&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;man-page&lt;/a&gt;.
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;# dmidecode
# dmidecode 2.9
SMBIOS 2.5 present.
5 structures occupying 352 bytes.
Table at 0x000E1000.

Handle 0x0000, DMI type 0, 20 bytes
BIOS Information
 Vendor: innotek GmbH
 Version: VirtualBox
 Release Date: 12/01/2006
.
Handle 0x0001, DMI type 1, 27 bytes
System Information
 Manufacturer: innotek GmbH
 Product Name: VirtualBox
 Version: 1.2
 Serial Number: 0
 UUID: B220160E-6104-4C00-B1B4-D00F6384DD24
&lt;/pre&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;lspci&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
It displays complete information about all the PCI buses in the system and the devices connected to them. As you can see in below output we can see about type of ethernet nic cards being used and other stuff. For more detailed info checkout &lt;a href=&quot;http://linux.die.net/man/8/lspci&quot; rel=&quot;nofollow&quot; target=&quot;_blank&quot;&gt;man-page&lt;/a&gt;.
&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;# lspci
00:00.0 Host bridge: Intel Corporation 440FX - 82441FX PMC [Natoma] (rev 02)
00:01.0 ISA bridge: Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II]
00:01.1 IDE interface: Intel Corporation 82371AB/EB/MB PIIX4 IDE (rev 01)
00:02.0 VGA compatible controller: InnoTek Systemberatung GmbH VirtualBox Graphics Adapter
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:04.0 System peripheral: InnoTek Systemberatung GmbH VirtualBox Guest Service
00:06.0 USB Controller: Apple Computer Inc. KeyLargo/Intrepid USB
00:07.0 Bridge: Intel Corporation 82371AB/EB/MB PIIX4 ACPI (rev 08)
00:08.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
00:0d.0 SATA controller: Intel Corporation 82801HBM/HEM (ICH8M/ICH8M-E) SATA AHCI Controller (rev 02)
&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/05/decoding-hardware-information-of-pc.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6RYJLSKUPiAkx1ndoeOHD-yyHODbl1831k-1y-CK6QZH3c3paIg-PTPC_OB6nKm3JUa9gMjVTgtGU0RjU6Q3NQVWCevvYuAlZlsiG53SlwepZJdIjMY_uifp_x-trCzhjxAubeA6_7QA/s72-c/01.png" height="72" width="72"/><thr:total>1</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-374796060261492237</guid><pubDate>Thu, 19 Apr 2012 12:18:00 +0000</pubDate><atom:updated>2013-02-25T07:28:35.420-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Buffer overflow</category><category domain="http://www.blogger.com/atom/ns#">Linux</category><category domain="http://www.blogger.com/atom/ns#">Security</category><category domain="http://www.blogger.com/atom/ns#">Video Series</category><title>Uli Drepper - Basics you should know about buffer overflow</title><description>&lt;b&gt;Part 1: Buffer Overflows&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;360&quot; src=&quot;http://www.youtube.com/embed/AlgwqMH3Uss&quot; width=&quot;480&quot;&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b&gt;Part 2: Buffer overflow and libc attacks&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;360&quot; src=&quot;http://www.youtube.com/embed/3tG2I4uPQug&quot; width=&quot;480&quot;&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;b&gt;Part 3: Memory allocation errors&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;360&quot; src=&quot;http://www.youtube.com/embed/iLyEriwr2U4&quot; width=&quot;480&quot;&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Part 4: SELinux&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&amp;nbsp;&lt;/b&gt; 
&lt;br /&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;360&quot; src=&quot;http://www.youtube.com/embed/tE6XFSz1D8M&quot; width=&quot;480&quot;&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Part 5: Preventing exploits&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen=&quot;&quot; frameborder=&quot;0&quot; height=&quot;360&quot; src=&quot;http://www.youtube.com/embed/CrdoKKTyEgo&quot; width=&quot;480&quot;&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;br /&gt;
For direct REDHAT videos checkout this &lt;a href=&quot;http://magazine.redhat.com/2007/10/26/uli-drepper-paying-attention-to-the-basics/&quot; target=&quot;_blank&quot;&gt;link&lt;/a&gt;.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/04/uli-drepper-basics-you-should-know.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://img.youtube.com/vi/AlgwqMH3Uss/default.jpg" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-3943261768157389994</guid><pubDate>Fri, 06 Apr 2012 05:49:00 +0000</pubDate><atom:updated>2012-12-09T20:26:37.293-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">How to</category><category domain="http://www.blogger.com/atom/ns#">Linux</category><title>Connecting via SSH without password prompt</title><description>SSH is the common way to connect remote machines these days. Almost all kinds of applications use SSH in background for communicating with end machines.&lt;br /&gt;
&lt;br /&gt;
But sometimes typing password repeatedly for establishing a SSH connection with the trusted end machine is quite daunting task.&lt;br /&gt;
&lt;br /&gt;
Let us see how to automate the things in 3 simple steps where we can ssh to &quot;user@host&quot; without asking a password every time. Replace user with the username and host with the remote machine ip-address or hostname. For E.g. john@192.168.245.129&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;color: red;&quot;&gt;NOTE:&lt;/b&gt; Only do this with trusted machines.&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiW60DtizOek0crxIOrSfd14JfCBlOima4sa-v8u7x6DDeVkhPs9ad93kThqctHrUY4JcJJTuqjtNKeg8RxudI5YENAOTccpX7Q7b-yYEfrhttDm16P15VbQg3QsToSBsb_7WntLyXRAOk/s1600/Terminal.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiW60DtizOek0crxIOrSfd14JfCBlOima4sa-v8u7x6DDeVkhPs9ad93kThqctHrUY4JcJJTuqjtNKeg8RxudI5YENAOTccpX7Q7b-yYEfrhttDm16P15VbQg3QsToSBsb_7WntLyXRAOk/s320/Terminal.png&quot; title=&quot;&quot; width=&quot;320&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;ol&gt;
&lt;li&gt;Login to your local machine from which you want to ssh to remote machine. Go to your &lt;b&gt;.ssh&lt;/b&gt; folder.&lt;/li&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;cd ~/.ssh&lt;/pre&gt;
&lt;br /&gt;
&lt;li&gt;Check if you have a &#39;&lt;b&gt;id_rsa.pub&lt;/b&gt;&#39; (or &#39;&lt;b&gt;id_dsa.pub&lt;/b&gt;&#39;) file in .ssh folder. If not, try below command:&lt;/li&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;ssh-keygen -t rsa &lt;/pre&gt;
&lt;br /&gt;
To make it simple just keep on pressing the ENTER key a bunch of times, even when it asks for a password.&lt;br /&gt;&lt;br /&gt;
&lt;li&gt;Run ssh-copy-id&lt;/li&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;ssh-copy-id -i id_rsa.pub user@host &lt;/pre&gt;
If it asks for remote machine password then provide it so that it can create a security key successfully so that you dont need to remember password everytime.&lt;/ol&gt;
&lt;br /&gt;
Its done .. Now you can ssh to user@host without being prompted for password.&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/04/connectig-via-ssh-without-password.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiW60DtizOek0crxIOrSfd14JfCBlOima4sa-v8u7x6DDeVkhPs9ad93kThqctHrUY4JcJJTuqjtNKeg8RxudI5YENAOTccpX7Q7b-yYEfrhttDm16P15VbQg3QsToSBsb_7WntLyXRAOk/s72-c/Terminal.png" height="72" width="72"/><thr:total>2</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2678749949937143988</guid><pubDate>Tue, 13 Mar 2012 13:44:00 +0000</pubDate><atom:updated>2012-10-06T03:08:12.155-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Linux</category><title>Daemon-izing a Process in Linux</title><description>A Linux process works either in foreground or background.&lt;br /&gt;
&lt;br /&gt;
A process running in foreground can interact with the user in front of the terminal. To run &lt;b&gt;a.out&lt;/b&gt; in foreground we execute as shown below.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;./a.out&lt;/pre&gt;
When a process runs as background process then it runs by itself without any user interaction. The user can check its status but he doesn&#39;t (need to) know what it is doing. To run &lt;b&gt;a.out&lt;/b&gt; in background we execute as shown below.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;$ ./a.out &amp;amp;
[1] 3665&lt;/pre&gt;
As shown above when we run a process with &lt;b&gt;&amp;amp;&lt;/b&gt; at the end then the process runs in background and returns the process id (3665 in above example).&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;what is a DAEMON Process?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
A &#39;daemon&#39; process is a process that runs in background, begins execution at startup &lt;br /&gt;
(not neccessarily), runs forever, usually do not die or get restarted, waits for requests to arrive and respond to them and frequently spawn other processes to handle these requests.&lt;br /&gt;
&lt;br /&gt;
So running a process in BACKGROUND with a while loop logic in code to loop forever makes a Daemon ? &lt;b&gt;Yes and also No&lt;/b&gt;. But there are certain things to be considered when we create a daemon process. We follow a step-by-step procedure as shown below to create a daemon process.&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;1. Create a separate child process - fork() it.&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
Using fork() system call create a copy of our process(child), then let the parent process exit. Once the parent process exits the Orphaned child process will become the child of init process (this is the initial system process, in other words the parent of all processes). As a result our process will be completely detached from its parent and start operating in background.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;pid=fork();

if (pid&amp;lt;0) exit(1); /* fork error */

if (pid&amp;gt;0) exit(0); /* parent exits */

/* child (daemon) continues */&lt;/pre&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;div style=&quot;color: red;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;2.    Make child process In-dependent - setsid()&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
Before we see how we gonna make a child process independent let us talk Process group and Session ID.&lt;br /&gt;
&lt;br /&gt;
A &lt;b&gt;process group&lt;/b&gt; denotes a collection of one or more processes.
 Process groups are used to control the distribution of signals. A 
signal directed to a process group is delivered individually to all of 
the processes that are members of the group. &lt;br /&gt;
&lt;br /&gt;
Process groups are themselves grouped into &lt;b&gt;sessions&lt;/b&gt;. 
Process groups are not permitted to migrate from one session to another,
 and a process may only create new process groups belonging to the same 
session as it itself belongs to. Processes are not permitted to join 
process groups that are not in the same session as they themselves are.&lt;br /&gt;
&lt;br /&gt;
New process images created by a call to a function of the &lt;b&gt;exec&lt;/b&gt; family and &lt;b&gt;fork()&lt;/b&gt; inherit the process group membership and the session membership of the parent process image.&lt;br /&gt;
&lt;br /&gt;
A process receives signals from the terminal that it is connected to, and each process inherits its parent&#39;s controlling &lt;b&gt;tty&lt;/b&gt;. A daemon process should not receive signals from the process that started it, so it must detach itself from its controlling tty.&lt;br /&gt;
&lt;br /&gt;
In Unix systems, processes operates within a process group, so that all processes within a group is treated as a single entity. Process group or session is also inherited. A daemon process should operate independently from other processes.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;setsid();&lt;/pre&gt;
&lt;b&gt;setsid()&lt;/b&gt; system call is used to create a new session containing a single (new) process group, with the current process as both the session leader and the process group leader of that single process group. (&lt;b&gt;setpgrp()&lt;/b&gt; is an alternative for this).&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;NOTE&lt;/b&gt;: We have to create a child process and use &lt;b&gt;setsid()&lt;/b&gt; to make it independent. Trying on a parent process returns error saying &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;EPERM&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: red;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;3. Change current Running Directory - chdir()&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
A daemon process should run in a known directory. There are many advantages, in fact the opposite has many disadvantages: suppose that our daemon process is started in a user&#39;s home directory, it will not be able to find some input and output files. If the home directory is a mounted filesystem then it will even create many issues if the filesystem is accidentally un-mounted.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;chdir(&quot;/server/&quot;);&lt;/pre&gt;
The root &quot;/&quot; directory may not be appropriate for every server, it should be chosen carefully depending on the type of the server.&lt;br /&gt;
&lt;br /&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;4. Close Inherited Descriptors and Standard I/O Descriptors&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
A child process inherits default standard I/O descriptors and opened file descriptors from a parent process, this may cause the use of resources un-neccessarily. Unnecessary file descriptors should be closed before &lt;b&gt;fork()&lt;/b&gt; system call (so that they are not inherited) or close all open descriptors as soon as the child process starts running as shown below.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;for ( i=getdtablesize(); i&amp;gt;=0; --i) 
close(i); /* close all descriptors */&lt;/pre&gt;
There are three standard I/O descriptors: &lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;standard input &#39;stdin&#39; (0), &lt;/li&gt;
&lt;li&gt;standard output &#39;stdout&#39; (1), &lt;/li&gt;
&lt;li&gt;standard error &#39;stderr&#39; (2). &lt;/li&gt;
&lt;/ul&gt;
For safety, these descriptors should be opened and connected to a harmless I/O device (such as /dev/null).&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;int fd;

fd = open(&quot;/dev/null&quot;,O_RDWR, 0);

if (fd != -1) 
{   
  dup2 (fd, STDIN_FILENO);
  dup2 (fd, STDOUT_FILENO);
  dup2 (fd, STDERR_FILENO);
  
  if (fd &amp;gt; 2)
  close (fd);
}&lt;/pre&gt;
&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;5. Reset File Creation Mask - umask()&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
Most Daemon processes runs as super-user, for security reasons they should protect files that they create. Setting user mask will prevent unsecure file priviliges that may occur on file creation.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;umask(027);&lt;/pre&gt;
This will restrict file creation mode to 750 (complement of 027).&lt;br /&gt;
&lt;br /&gt;
Let us see a sample &#39;C&#39; code which creates a daemon. &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;
#include &amp;lt;unistd.h&amp;gt;
#include &amp;lt;sys/types.h&amp;gt;
#include &amp;lt;sys/stat.h&amp;gt;
#include &amp;lt;fcntl.h&amp;gt;

#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1

static void daemonize(void)
{
    pid_t pid, sid;
    int fd; 

    /* already a daemon */
    if ( getppid() == 1 ) return;

    /* Fork off the parent process */
    pid = fork();
    if (pid &amp;lt; 0)  
    {   
        exit(EXIT_FAILURE);
    }   

    if (pid &amp;gt; 0)  
    {   
        exit(EXIT_SUCCESS); /*Killing the Parent Process*/
    }   

    /* At this point we are executing as the child process */

    /* Create a new SID for the child process */
    sid = setsid();
    if (sid &amp;lt; 0)  
    {
        exit(EXIT_FAILURE);
    }

    /* Change the current working directory. */
    if ((chdir(&quot;/&quot;)) &amp;lt; 0)
    {
        exit(EXIT_FAILURE);
    }


    fd = open(&quot;/dev/null&quot;,O_RDWR, 0);

    if (fd != -1)
    {
        dup2 (fd, STDIN_FILENO);
        dup2 (fd, STDOUT_FILENO);
        dup2 (fd, STDERR_FILENO);

        if (fd &amp;gt; 2)
        {
            close (fd);
        }
    }

    /*resettign File Creation Mask */
    umask(027);
}

int main( int argc, char *argv[] )
{
    daemonize();

    while(1)
    {
      /* Now we are a daemon -- do the work for which we were paid */

      sleep(10);
    }

    return 0;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;References&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;http://www.enderunix.org/docs/eng/daemon.php&quot; target=&quot;_blank&quot;&gt;Daemon Server Programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://www.itp.uzh.ch/%7Edpotter/howto/daemonize&quot; target=&quot;_blank&quot;&gt;Daemonize in Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Process_group&quot; target=&quot;_blank&quot;&gt;Wiki&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/03/daemon-izing-process-in-linux.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>5</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-4945757918676549520</guid><pubDate>Sun, 04 Mar 2012 11:59:00 +0000</pubDate><atom:updated>2012-10-06T03:08:23.783-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><title>Memory Layout of a C program - Part 2</title><description>Continuation of &lt;a href=&quot;http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html&quot; target=&quot;_blank&quot;&gt;PART-1&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
As we have seen so much theory in the &lt;a href=&quot;http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html&quot; target=&quot;_blank&quot;&gt;PART-1&lt;/a&gt; now let us see a real-time example to understand about these segments.

we will use &lt;b&gt;size(1)&lt;/b&gt; command to list various section sizes in a C code.&lt;br /&gt;
&lt;br /&gt;
A simple C program is given below&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

int main()
{
    return 0;
}

$ gcc test.c 
$ size a.out 
   text    data     bss     dec     hex filename
    836     260       8    1104     450 a.out&lt;/pre&gt;
Now add a global variable as shown below&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

int global; /* Uninitialized variable stored in bss*/

int main()
{
    return 0;
}

$ gcc test.c 
$ size a.out 
   text    data     bss     dec     hex filename
    836     260      12    1108     454 a.out&lt;/pre&gt;
As you can see BSS is incremented by 4 bytes.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;Let us include a static variable as shown below.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

int global; /* Uninitialized variable stored in bss*/

int main()
{
    static int i; /* Uninitialized static variable stored in bss */
    return 0;
}

$ gcc test.c 
$ size a.out 
   text    data     bss     dec     hex filename
    836     260      16    1112     458 a.out&lt;/pre&gt;
As you can see BSS is incremented by 4 bytes.&lt;br /&gt;
&lt;br /&gt;
Now let us initialize the static variable so that it is saved in the initialized data segment.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

int global; /* Uninitialized variable stored in bss*/

int main()
{
    static int i=10; /* Initialized static variable stored in DS*/
    return 0;
}

$ gcc test.c 
$ size a.out 
   text    data     bss     dec     hex filename
    836     264      12    1112     458 a.out&lt;/pre&gt;
Now you can see that data section is incremented by 4 bytes and BSS is decremented by 4 bytes.&lt;br /&gt;
&lt;br /&gt;
Let us even initialize the Global variable which makes it part of Data Segment.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

int global=10; /* Initialized variable stored in DS*/

int main()
{
    static int i=10; /* Initialized static variable stored in DS*/
    return 0;
}

$ gcc test.c 
$ size a.out 
   text    data     bss     dec     hex filename
    836     268      8    1112     458 a.out&lt;/pre&gt;
We can see that data section is incremented by 4 bytes and BSS is decremented by 4 bytes.&lt;br /&gt;
&lt;br /&gt;
Let us see what happens when we add a &lt;b&gt;const&lt;/b&gt; variable. &lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;#include &amp;lt;stdio.h&amp;gt;

const int i = 1;

int global=10;

int main()
{
    static int i=10;
    return 0;
}

$ gcc test.c 
$ size a.out 
   text    data     bss     dec     hex filename
    840     268       8    1116     45c a.out&lt;/pre&gt;
Now we can see that TEXT segment is incremented by 4 bytes.&lt;br /&gt;
&lt;br /&gt;
References:&lt;br /&gt;
1. &lt;a href=&quot;http://www.geeksforgeeks.org/archives/14268&quot; target=&quot;_blank&quot;&gt;Narendra Kangralkar &lt;/a&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-2.html</link><author>noreply@blogger.com (Unknown)</author><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6200432886671870069</guid><pubDate>Sun, 04 Mar 2012 11:10:00 +0000</pubDate><atom:updated>2012-12-12T21:38:39.771-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><title>Memory Layout of a C program - Part 1</title><description>A running program is called a process.&lt;br /&gt;
&lt;br /&gt;
When we run a program, its executable image is loaded into memory area that normally called a process address space in an organized manner. It is organized into following areas of memory, called segments:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;text segment&amp;nbsp;&lt;/li&gt;
&lt;li&gt;data segment&amp;nbsp;&lt;/li&gt;
&lt;li&gt;stack segment&amp;nbsp;&lt;/li&gt;
&lt;li&gt;heap segment&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div style=&quot;color: red;&quot;&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQJ-tu3a9lHZEpahCuo0RdB4vWUqyolpYipJIeD5gI62XeG04KdcKY_uf4BYRJLjjBnuavP39c9smDylGA9zW45fSkeNVOauRUS7fZJxcsSPSLhIwXEiayhKCbSKfsmRmsfZ-4PkNWV2c/s1600/memory.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot; target=&quot;_blank&quot;&gt;&lt;img alt=&quot;Memory layout of a C program&quot; border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQJ-tu3a9lHZEpahCuo0RdB4vWUqyolpYipJIeD5gI62XeG04KdcKY_uf4BYRJLjjBnuavP39c9smDylGA9zW45fSkeNVOauRUS7fZJxcsSPSLhIwXEiayhKCbSKfsmRmsfZ-4PkNWV2c/s1600/memory.png&quot; title=&quot;Memory layout of a C program&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 1: Memory layout&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;text segment&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
It is also called the code segment.&lt;br /&gt;
&lt;br /&gt;
This is the area where the compiled code of the program itself resides. This is the machine language representation of the program steps to be carried out, including all functions making up the program, both user defined and system. &lt;br /&gt;
&lt;br /&gt;
For example, Linux/Unix arranges things so that multiple running instances of the same program share their code if possible. Only one copy of the instructions for the same program resides in memory at any time and also it is often read-only, to prevent a program from accidentally modifying its instructions.&lt;br /&gt;
&lt;br /&gt;
The portion of the executable file containing the text segment is the text section.&lt;br /&gt;
&lt;br /&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;b style=&quot;color: red;&quot;&gt;&lt;span style=&quot;font-size: large;&quot;&gt;data segment&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;initialized data&lt;/b&gt;&lt;br /&gt;
It contains both static and global data that are initialized with non-zero values. Each process running the same program has its own data segment.This segment can be further classified into initialized read-only area and initialized read-write area.&lt;br /&gt;
&lt;br /&gt;
For Eg: The global string defined by char s[] = “hello world” in C and a C statement like int debug=1 outside the main (i.e. global) would be stored in initialized read-write area. And a global C statement like const char* string = “hello world” makes the string literal “hello world” to be stored in initialized read-only area and the character pointer variable string in initialized read-write area.&lt;br /&gt;
&lt;br /&gt;
The portion of the executable file containing the data segment is the data section.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Uninitialized data - bss segment&lt;/b&gt;&lt;br /&gt;
BSS stands for ‘Block Started by Symbol’ named after an ancient assembler operator that stood for “block started by symbol&quot;.uninitialized data starts at the end of the data segment and contains all global variables and static variables that are initialized to zero or do not have explicit initialization in source code.&lt;br /&gt;
&lt;br /&gt;
For Eg: A variable declared static int i and a global variable declared int j would be contained in the BSS segment.&lt;br /&gt;
&lt;br /&gt;
Each process running the same program has its own BSS area. When running, the BSS data are placed in the data segment. &lt;br /&gt;
&lt;br /&gt;
For Linux/Unix the format of an executable, only variables that are initialized to a nonzero value occupy space in the executable’s disk file. In the executable file, they are stored in the BSS section.&lt;br /&gt;
&lt;br /&gt;
The remaining two areas of system memory are where storage may be allocated by the compiler for data storage. &lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: red;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;heap segment&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
&lt;br /&gt;
The heap is where dynamically allocated memory (obtained by malloc(), calloc(), realloc() and new for C++) comes from. As memory is allocated on the heap, the process’s address space grows.&amp;nbsp; Although it is possible to give memory back to the system and shrink a process’s address space, this is almost never done because it will be allocated to other process again.&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
Freed memory (free() and delete) goes back to the heap. Heap memory does not have to be returned in the same order in which it was acquired (it doesn&#39;t have to be returned at all), so unordered malloc/free&#39;s eventually cause heap fragmentation. The heap must keep track of different regions, and whether they are in use or available to malloc. One scheme is to have a linked list of available blocks (the &quot;free store&quot;), and each block handed to malloc is preceded by a size count that goes with it. Some people use the term arena to describe the set of blocks managed by a memory allocator (in SunOS, the area between the end of the data segment and the current position of the break).&lt;br /&gt;
&lt;br /&gt;
It is typical for the heap to grow upward. This means that successive items that are added to the heap are added at addresses that are numerically greater than previous items.&amp;nbsp; It is also typical for the heap to start immediately after the BSS area of the data segment. The end of the heap is marked by a pointer known as the &quot;break&quot; (Your programs will &quot;break&quot; if they reference past the break).&lt;br /&gt;
&lt;br /&gt;
When the heap manager needs more memory, it can push the break further away using the system calls &lt;b&gt;brk&lt;/b&gt; and &lt;b&gt;sbrk&lt;/b&gt;. We don&#39;t call &lt;b&gt;brk&lt;/b&gt; yourself explicitly, but if we malloc enough memory, &lt;b&gt;brk&lt;/b&gt; will eventually be called.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: red;&quot;&gt;
&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;stack segment&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
The stack segment is where local (automatic) variables are allocated.&amp;nbsp; In C program, local variables are all variables declared inside the opening left curly brace of a function body including the main() or other left curly brace that aren’t defined as static. The data is popped up or pushed into the stack following the Last in First out (LIFO) rule.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
On the standard PC x86 computer architecture it grows toward address zero; on some other architectures it grows the opposite direction.&lt;br /&gt;
&lt;br /&gt;
A “stack pointer” register tracks the top of the stack; it is adjusted each time a value is “pushed” onto the stack.When a function is called, a stack frame (or a procedure activation record) is created and PUSHed onto the top of the stack. This stack frame contains information such as the address from which the function was called and where to jump back to when the function is finished (return address), parameters, local variables, and any other information needed by the invoked function. The order of the information may vary by system and compiler.&lt;br /&gt;
&lt;br /&gt;
When a function returns, the stack frame is POPped from the stack.&amp;nbsp; Typically the stack grows downward, meaning that items deeper in the call chain are at numerically lower addresses and toward the heap.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFqAyixNQE4z2SGRe4PVGoU17Tl8JPXyrJe_enpwikHJlusnPzpxcYONDnyygXoXd5re97_1QQzqnA031CU8Xs4FOepPExxNAwjME64Zxk-IgX1JpzEYDHjVBkqGfxzvom476p7B-ehSk/s1600/memory.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjFqAyixNQE4z2SGRe4PVGoU17Tl8JPXyrJe_enpwikHJlusnPzpxcYONDnyygXoXd5re97_1QQzqnA031CU8Xs4FOepPExxNAwjME64Zxk-IgX1JpzEYDHjVBkqGfxzvom476p7B-ehSk/s1600/memory.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 2: Stack layout&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
When a program begins executing in the function main(), space is allocated on the stack for all variables declared within&amp;nbsp; main(), as seen in Figure (a). If main() calls a function, func1(), additional storage is allocated for the variables in&amp;nbsp; func1() at the top of the stack as shown in Figure (b). Notice that the parameters passed by main() to func1() are also stored on the stack. &lt;br /&gt;
&lt;br /&gt;
If func1() were to call any additional functions, storage would be allocated at the new Top of stack as seen in the figure. When func1() returns, storage for its local variables is deallocated, and the Top of the stack returns to position shown in Figure (c). If main() were to call another function, storage would be allocated for that function at the Top shown in the figure. As can be seen, the memory allocated in the stack area is used and reused during program execution. It should be clear that memory allocated in this area will contain garbage values left over from previous usage.&lt;br /&gt;
&lt;br /&gt;
Although it is theoretically possible for the stack and heap to grow into each other, the operating system prevents that event.&lt;br /&gt;
&lt;br /&gt;
Check the &lt;a href=&quot;http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-2.html&quot; target=&quot;_blank&quot;&gt;PART-2&lt;/a&gt; for detailed understanding of these sections with examples.&lt;br /&gt;
&lt;br /&gt;
The relationship among the different sections/segments is summarized below.&lt;br /&gt;
&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTwT2Hk8cqavMfJOwmH34Q5qDuH-4JlSUDY38BcfOzGXayEDOL2FyxMLemkELQWuPEIJMdY_HVktPet3rP4ft3fQdKFQJKOLZHMyplf3YDNFxjC8sW4c51jOu2NIFeToRchkdPv6Gjeno/s1600/memory.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjTwT2Hk8cqavMfJOwmH34Q5qDuH-4JlSUDY38BcfOzGXayEDOL2FyxMLemkELQWuPEIJMdY_HVktPet3rP4ft3fQdKFQJKOLZHMyplf3YDNFxjC8sW4c51jOu2NIFeToRchkdPv6Gjeno/s1600/memory.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 3: Relationship between Sections and Segments&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQJ-tu3a9lHZEpahCuo0RdB4vWUqyolpYipJIeD5gI62XeG04KdcKY_uf4BYRJLjjBnuavP39c9smDylGA9zW45fSkeNVOauRUS7fZJxcsSPSLhIwXEiayhKCbSKfsmRmsfZ-4PkNWV2c/s72-c/memory.png" height="72" width="72"/><thr:total>8</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-149401312334994450</guid><pubDate>Thu, 01 Mar 2012 14:49:00 +0000</pubDate><atom:updated>2012-12-25T01:34:54.207-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><title>Deletion of a Node from Doubly Linked List</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot;.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.in/2012/03/implementation-of-doubly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Inserting a Node in Doubly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: red;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Deletion of a Node&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
Let us say our current Doubly Linked List is as shown in Figure-1.&lt;br /&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyS2MwKsEduGUQhuamSsJZXK_fu8XeCbBZQI6qzlTfndO2WnQU-kZaByciF5xEAQ86aVODaVQzCBClc6voeZL8qELlMaYhUIjRmSzyDBeHYVpLJv8GGUh68wAIXrahyphenhyphen86_1TSkL9bIPVE/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyS2MwKsEduGUQhuamSsJZXK_fu8XeCbBZQI6qzlTfndO2WnQU-kZaByciF5xEAQ86aVODaVQzCBClc6voeZL8qELlMaYhUIjRmSzyDBeHYVpLJv8GGUh68wAIXrahyphenhyphen86_1TSkL9bIPVE/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 1: Current Doubly Linked List&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Deleting First Node of the List&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
Now we have to delete First Node from the List shown in Figure-1. Because of this operation HEAD and Node-2 are affected.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In HEAD&lt;/b&gt; - FIRST variable should now point to NODE-2 (i.e HEAD-&amp;gt;FIRST = HEAD-&amp;gt;FIRST-&amp;gt;NEXT). If you see HEAD-&amp;gt;FIRST-&amp;gt;NEXT actually HEAD-&amp;gt;FIRST currently points to NODE-1 so HEAD-&amp;gt;FIRST-&amp;gt;NEXT is equivalent to NODE-1-&amp;gt;NEXT which is NODE-2.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In NODE-2&lt;/b&gt; - NEXT variable remains unchanged and PREV variable should now point to NULL since it is the first Node in the List.&lt;/ul&gt;
&lt;ul&gt;Decrement LENGTH variable in HEAD so that it maintains proper count of Nodes in the List.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;HEAD-&amp;gt;FIRST = HEAD-&amp;gt;FIRST-&amp;gt;NEXT

NODE-2-&amp;gt;PREV = NULL

decrement(HEAD-&amp;gt;LENGTH)&lt;/pre&gt;
&lt;b&gt;Output:&lt;/b&gt;&lt;/ul&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYg5VaKxaLRKNVTpxLzyXN0FIYfx0RIGZQla447VTOidSwNmlcO-IuWW_Tg6Q9Se_oq2cOQZOPohlPDqZ3_fJZmdFoRdCWLPRmprElkwYqHaRFhyphenhyphen_I4ZH7wCt-Xl-XUKI8BZt9NqeX6-o/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgYg5VaKxaLRKNVTpxLzyXN0FIYfx0RIGZQla447VTOidSwNmlcO-IuWW_Tg6Q9Se_oq2cOQZOPohlPDqZ3_fJZmdFoRdCWLPRmprElkwYqHaRFhyphenhyphen_I4ZH7wCt-Xl-XUKI8BZt9NqeX6-o/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 2: After deleting the First Node in the List.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Deleting Middle Node from the List&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
Now let us delete NODE-2 from the List shown in Figure-1.Because of this operation NODE-1 and NODE-3 will be affected.&lt;/ul&gt;
&lt;ul&gt;
Using a temporary Node pointer curPtr we have to traverse until NODE-2.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In NODE-1&lt;/b&gt; - PREV variable remains unchanged and NEXT variable should point to NODE-3 (i.e. curPtr-&amp;gt;PREV-&amp;gt;NEXT = curPtr-&amp;gt;NEXT). As we know curPtr is at Node-2 so curPtr-&amp;gt;PREV points to NODE-1 and hence curPtr-&amp;gt;PREV-&amp;gt;NEXT is equivalent to NODE-1-&amp;gt;NEXT.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In NODE-3&lt;/b&gt; - NEXT variable remains unchanged and PREV variable should point to NODE-1 (i.e. curPtr-&amp;gt;NEXT-&amp;gt;PREV = curPtr-&amp;gt;PREV). Since curPtr is at NODE-2 so curPtr-&amp;gt;NEXT points to NODE-3 and hence curPtr-&amp;gt;NEXT-&amp;gt;PREV is equivalent to NODE-3-&amp;gt;PREV.&lt;/ul&gt;
&lt;ul&gt;Decrement LENGTH variable in HEAD so that it maintains the proper count of Nodes in the List.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curPtr-&amp;gt;PREV-&amp;gt;NEXT = curPtr-&amp;gt;NEXT

curPtr-&amp;gt;NEXT-&amp;gt;PREV = curPtr-&amp;gt;PREV

decrement(HEAD-&amp;gt;LENGTH)&lt;/pre&gt;
&lt;b&gt;Output:&lt;/b&gt;&lt;/ul&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6c1bLRQCwakuU8_FZz8hhODxt62pZ6jxY-nSlM8QbSGTVSUSEr-FT9muiyGeekzu5ZsrSbWsE7H8SGlrehGRE33yAbTSqontBH-bYI7JVW1JjHSubSyobd-VF33erKXUAGJbvAB_WX94/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6c1bLRQCwakuU8_FZz8hhODxt62pZ6jxY-nSlM8QbSGTVSUSEr-FT9muiyGeekzu5ZsrSbWsE7H8SGlrehGRE33yAbTSqontBH-bYI7JVW1JjHSubSyobd-VF33erKXUAGJbvAB_WX94/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 3: After Deleting NODE-2 in the List.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Deleting Last Node in the List&lt;/span&gt;&lt;/b&gt;&lt;/li&gt;
Now let us delete Last Node(NODE-3) from the List in Figure-1. Only NODE-2 is affected because of this operation.&lt;/ul&gt;
&lt;ul&gt;Using a temporary Node pointer curPtr we have to traverse until the end Node of the List. So curPtr is now pointing to NODE-3.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In NODE-2&lt;/b&gt;- PREV variable remains unchanged and NEXT variable has to point to NULL (i.e curPtr-&amp;gt;PREV-&amp;gt;NEXT = NULL). Since curPtr is pointing to NODE-3 curPtr-&amp;gt;PREV points to NODE-2 and hence curPtr-&amp;gt;PREV-&amp;gt;NEXT is equivalent to NODE-2 -&amp;gt; NEXT.&lt;/ul&gt;
&lt;ul&gt;Decrement the LENGTH variable in HEAD so that it maintains the proper count of Nodes in the List.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curPtr-&amp;gt;PREV-&amp;gt;NEXT = NULL

decrement(HEAD-&amp;gt;LENGTH)&lt;/pre&gt;
&lt;b&gt;Output:&lt;/b&gt;&lt;/ul&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE1d5mthvqSsGJ5KdEgOuVdFWQta30q0n-tfoZrtg0B4WvnHKPLc3IVppJViYdaV7skLoxBFnCuZGSKym6cuf7w6XKO8kSq9rvXjApTs8SYOPTCa7a5I-LWvNUd0Y12vioUgi0eUL-MHg/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjE1d5mthvqSsGJ5KdEgOuVdFWQta30q0n-tfoZrtg0B4WvnHKPLc3IVppJViYdaV7skLoxBFnCuZGSKym6cuf7w6XKO8kSq9rvXjApTs8SYOPTCa7a5I-LWvNUd0Y12vioUgi0eUL-MHg/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 4: After Deleting Last Node from the List.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Let us see sample &#39;C&#39; code to the deletion of a Node operation&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;retVal
delNodeAtLoc(listHead *head, UINT32 loc)
{
    retVal ret = SUCCESS;
    UINT32 i;

    listNode *curPtr;

    if(head &amp;amp;&amp;amp; head-&amp;gt;first &amp;amp;&amp;amp; loc &amp;amp;&amp;amp; loc &amp;lt;= (head-&amp;gt;length)) 
    {   
        curPtr = head-&amp;gt;first;

        if (loc == 1)
        {   
            head-&amp;gt;first = curPtr-&amp;gt;next;

            if(head-&amp;gt;first)
                head-&amp;gt;first-&amp;gt;prev = NULL;
        }   
        else 
        {   
            for(i=1; i&amp;lt;loc; i++) 
                curPtr = curPtr-&amp;gt;next;

            curPtr-&amp;gt;prev-&amp;gt;next = curPtr-&amp;gt;next;

            if(curPtr-&amp;gt;next)
                curPtr-&amp;gt;next-&amp;gt;prev = curPtr-&amp;gt;prev;
        }    

        dlist_freeNode(curPtr);
        head-&amp;gt;length--;
    }
    else
        ret = FAILURE;

    return ret;
}&lt;/pre&gt;
&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/03/implementation-of-doubly-linked-list.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjyS2MwKsEduGUQhuamSsJZXK_fu8XeCbBZQI6qzlTfndO2WnQU-kZaByciF5xEAQ86aVODaVQzCBClc6voeZL8qELlMaYhUIjRmSzyDBeHYVpLJv8GGUh68wAIXrahyphenhyphen86_1TSkL9bIPVE/s72-c/01.png" height="72" width="72"/><thr:total>0</thr:total></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6062941783072623096</guid><pubDate>Mon, 27 Feb 2012 12:30:00 +0000</pubDate><atom:updated>2013-02-12T09:09:47.476-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">C</category><category domain="http://www.blogger.com/atom/ns#">DataStructures</category><category domain="http://www.blogger.com/atom/ns#">Linked List</category><title>Inserting a Node in Doubly Linked List</title><description>This article is part of article series - &quot;&lt;a href=&quot;http://codingfreak.blogspot.in/p/data-structures.html&quot; target=&quot;_blank&quot;&gt;Datastructures&lt;/a&gt;&quot; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.in/2012/02/implementation-of-doubly-linked-list-in.html&quot; target=&quot;_blank&quot;&gt;Doubly Linked List&lt;/a&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.in/2012/03/implementation-of-doubly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Deletion of a Node from Doubly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;color: red;&quot;&gt;
&lt;b&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Insertion of a Node&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
Before we discuss about how to insert a NODE let us discuss few rules to follow at the time of insertion.&lt;br /&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Check the location into which the user want to insert a new NODE. The possible locations where an user can insert a new node is in the range of &lt;b&gt;1 &amp;lt;= loc &amp;lt;= (length of list)+1&lt;/b&gt;. Let us say the length of the list is 10 &amp;amp; the user want to insert at location 12 (sounds stupid).&lt;/li&gt;
&lt;br /&gt;
&lt;li&gt;As we know we can traverse Bi-Directional in case of Doubly Linked Lists so we have to take care of &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;PREV&lt;/span&gt; and &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;NEXT&lt;/span&gt; variables in the &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;NODE&lt;/span&gt; structure. We should also update the neighboring Nodes which are affected by this operation. If not we might break up the List somewhere or the other by creating a BROKEN LIST.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
We have following scenarios in the case of insertion of a NODE.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Adding a Node at the start of the Empty List &lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqFwLwhNTlclZYX9sovwmZV5vshXwC5RnWYtngIuuGSIwOrBY7y-bdvlbPKy0YRhbutXetjapRIvy7-p30h5XwtKtWldjwx_s6ig1hqMdiZ6fXBXLIuALT_RoqbwlRmgcOIBt3nmYLhIw/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqFwLwhNTlclZYX9sovwmZV5vshXwC5RnWYtngIuuGSIwOrBY7y-bdvlbPKy0YRhbutXetjapRIvy7-p30h5XwtKtWldjwx_s6ig1hqMdiZ6fXBXLIuALT_RoqbwlRmgcOIBt3nmYLhIw/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 1: Empty List and the newNode we want to add&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
As shown in Figure-1 we have a Empty List with LENGTH set to 0 and FIRST pointing to &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;NULL&lt;/span&gt;. Let us add newNode at Location 1.&amp;nbsp;&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In HEAD&lt;/b&gt; - FIRST variable points to newNode (head-&amp;gt;FIRST = newNode).&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In newNode&lt;/b&gt; - NEXT and&amp;nbsp; PREV points to &lt;span style=&quot;font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;&quot;&gt;NULL&lt;/span&gt; as we don&#39;t have any other Nodes in the List.&lt;/ul&gt;
&lt;ul&gt;Increment the LENGTH variable in HEAD once insertion is successful to maintain the count of number of Nodes in the List.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;HEAD-&amp;gt;FIRST = newNode

newNode-&amp;gt;PREV = NULL

newNode-&amp;gt;NEXT = NULL

increment(HEAD-&amp;gt;LENGTH)&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/ul&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDCBdb3mW-ArDz2fjE1M6BXwunruyEnIjTYAQWhsYJ0pM-nonw7BtSduTg8Ebw3ntOtNy222fptkVqC1ruifUeSx6qmQDRiMN_jnFFUn6rjOZzm0kWNyuf1enGQ4EBt_RtP9oxP9HaHG4/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgDCBdb3mW-ArDz2fjE1M6BXwunruyEnIjTYAQWhsYJ0pM-nonw7BtSduTg8Ebw3ntOtNy222fptkVqC1ruifUeSx6qmQDRiMN_jnFFUn6rjOZzm0kWNyuf1enGQ4EBt_RtP9oxP9HaHG4/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 2: After adding newNode in Empty List. (Changes in BLUE)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;a name=&#39;more&#39;&gt;&lt;/a&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Adding a Node at the start of the Non-Empty List&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdTIX3UPuUkHyrnnio9_qB4wP9c0Snmx_IQFU8i8lJEMTfmwT76S-R-u-j5QMC2MUiKi-FE4VbsHYpew7Vw2reqOlBQdMSSmVer0PXHQA1GLGlleNtFwL9UVYpbQdMlJUAPVdxQOSOdnk/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhdTIX3UPuUkHyrnnio9_qB4wP9c0Snmx_IQFU8i8lJEMTfmwT76S-R-u-j5QMC2MUiKi-FE4VbsHYpew7Vw2reqOlBQdMSSmVer0PXHQA1GLGlleNtFwL9UVYpbQdMlJUAPVdxQOSOdnk/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 3: A Doubly Linked List of Length 3.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
As shown in Figure-3 we have a list of length 3. Let us call current first Node as curFirstNode.&amp;nbsp;&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In newNode&lt;/b&gt; - NEXT points to curFirstNode (newNode-&amp;gt;NEXT = head-&amp;gt;FIRST) and PREV points to NULL as the newNode itself is the first in the List.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In curFirstNode&lt;/b&gt; - PREV points to newNode (HEAD-&amp;gt;FIRST-&amp;gt;PREV = newNode) and NEXT remains unchanged. (we have to update curFirstNode before HEAD since it holds the pointer to curFirstNode)&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In HEAD&lt;/b&gt; - FIRST points to newNode (HEAD-&amp;gt;FIRST = newNode).&lt;/ul&gt;
&lt;ul&gt;Increment the LENGTH variable in HEAD to maintain the count of number of Nodes in the List. &lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;newNode-&amp;gt;NEXT = HEAD-&amp;gt;FIRST

HEAD-&amp;gt;FIRST-&amp;gt;PREV = newNode

HEAD-&amp;gt;FIRST = newNode

newNode-&amp;gt;PREV = NULL

increment(HEAD-&amp;gt;LENGTH)&lt;/pre&gt;
&lt;b&gt;&lt;/b&gt;&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/ul&gt;
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD5Eno0YQPBPKpbR1mR7xIqBXSGScnwyQQPVuhNMm2JYTntf4YqoL1UKo8l-4GI3Y4LK8e-KjOlq_b2kET42qQ17lXJaHBxtWHiRMQTwck5zUtVdY56B5Zd8nTamwnM1hvON3lP-NVLfc/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiD5Eno0YQPBPKpbR1mR7xIqBXSGScnwyQQPVuhNMm2JYTntf4YqoL1UKo8l-4GI3Y4LK8e-KjOlq_b2kET42qQ17lXJaHBxtWHiRMQTwck5zUtVdY56B5Zd8nTamwnM1hvON3lP-NVLfc/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 4: After adding newNode at location 1. (Changes in BLUE)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Adding a Node at the middle of the list.&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
Now we will see how to add a newNode at the arbitrary location.&amp;nbsp; 
&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaRw6TYDoHlouCMnIOa7UiFmhFdoczPOpDEiWrRvo9ae55_QoV2XH_-a2NcVNhB9HMEedakNWXpBezNLwhLteCR81XoxD0odA3K2H968NrZBemc9goz_tZcij-ebVNRLGsGPf0wP1qFpA/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgaRw6TYDoHlouCMnIOa7UiFmhFdoczPOpDEiWrRvo9ae55_QoV2XH_-a2NcVNhB9HMEedakNWXpBezNLwhLteCR81XoxD0odA3K2H968NrZBemc9goz_tZcij-ebVNRLGsGPf0wP1qFpA/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 5: Current Preview of the Doubly Linked List.&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
Let us add a newNode at Location 3.&amp;nbsp;&lt;/ul&gt;
&lt;ul&gt;Traverse the Doubly Linked List until the Location 2. So our curPtr is pointing to Node 2.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In newNode&lt;/b&gt; - PREV points to NODE 2 (newNode-&amp;gt;PREV = curPtr) and NEXT points to NODE 3 (newNode-&amp;gt;NEXT = curPtr-&amp;gt;NEXT).&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In NODE3&lt;/b&gt; - PREV points to newNode (newNode-&amp;gt;NEXT-&amp;gt;PREV = newNode) and NEXT remains unchanged.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In NODE2&lt;/b&gt; - NEXT points to newNode (curPtr-&amp;gt;NEXT = newNode) and PREV remains unchanged.&lt;/ul&gt;
&lt;ul&gt;Increment the LENGTH variable in HEAD to maintain the count of number of Nodes in the List.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;newNode-&amp;gt;PREV = curPtr

newNode-&amp;gt;NEXT = curPtr-&amp;gt;NEXT

newNode-&amp;gt;NEXT-&amp;gt;PREV = newNode

curPtr-&amp;gt;NEXT = newNode

increment(HEAD-&amp;gt;LENGTH)&lt;/pre&gt;
&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Output:&lt;/b&gt;&lt;/ul&gt;
&lt;ul&gt;&lt;table align=&quot;center&quot; cellpadding=&quot;0&quot; cellspacing=&quot;0&quot; class=&quot;tr-caption-container&quot; style=&quot;margin-left: auto; margin-right: auto; text-align: center;&quot;&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style=&quot;text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhogQsGBbZNRhMF99_SVmZnz7mfjz9653SOrUr4ikptSoFft9PDkPc9omKhtsyqIkzG0XhXJAqRfQ8ZX5B6kxCNRHNJKKyY4O0O6gyYXiLUISxKDavC9GoMMOLlGrmFpaM8BCtYLa9OHPU/s1600/01.png&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: auto; margin-right: auto;&quot;&gt;&lt;img border=&quot;0&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhogQsGBbZNRhMF99_SVmZnz7mfjz9653SOrUr4ikptSoFft9PDkPc9omKhtsyqIkzG0XhXJAqRfQ8ZX5B6kxCNRHNJKKyY4O0O6gyYXiLUISxKDavC9GoMMOLlGrmFpaM8BCtYLa9OHPU/s1600/01.png&quot; /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class=&quot;tr-caption&quot; style=&quot;text-align: center;&quot;&gt;Figure 6: After adding newNode at Location 3 (Changes in BLUE)&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;/div&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style=&quot;font-size: large;&quot;&gt;&lt;b&gt;Adding a Node at the End of the List&lt;/b&gt;&lt;/span&gt;&lt;/li&gt;
Traverse until the end of the List so that the curPtr points to Last Node in the List.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In Last Node&lt;/b&gt; - NEXT points to newNode (curPtr-&amp;gt;NEXT = newNode) and PREV remains unchanged.&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;In newNode&lt;/b&gt; - PREV points to Last Node (newNode-&amp;gt;PREV = curPtr) and NEXT points to NULL since we are the last Node in the List.&lt;/ul&gt;
&lt;ul&gt;Increment the LENGTH variable in HEAD to maintain the count of number of Nodes in the List.
&lt;/ul&gt;
&lt;ul&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;curPtr-&amp;gt;NEXT = newNode

newNode-&amp;gt;PREV = curPtr

increment(HEAD-&amp;gt;LENGTH)&lt;/pre&gt;
&lt;/ul&gt;
Let us see the preview of the C code which does the insertion of a Node at a particular location.&lt;br /&gt;
&lt;br /&gt;
This code is generic for all the possible scenarios explained above.&lt;br /&gt;
&lt;pre class=&quot;cpp&quot; name=&quot;code&quot;&gt;addNodeAtLoc(listHead *head, listNode *newNode, UINT32 loc)
{
    UINT32 i;
    retVal ret = SUCCESS;

    listNode *curPtr;

    if(head &amp;amp;&amp;amp; newNode &amp;amp;&amp;amp; loc &amp;amp;&amp;amp; (loc &amp;lt;= (head-&amp;gt;length)+1))
    {   
        if(loc == 1)
        {   
            newNode-&amp;gt;prev = NULL;
            newNode-&amp;gt;next = head-&amp;gt;first;
    
            if(head-&amp;gt;first)
                head-&amp;gt;first-&amp;gt;prev = newNode;

            head-&amp;gt;first = newNode;
        }   
        else
        {   
            curPtr = head-&amp;gt;first;

            for(i=1; i&amp;lt;(loc-1); i++)
                curPtr = curPtr-&amp;gt;next;

            newNode-&amp;gt;prev = curPtr;
            newNode-&amp;gt;next = curPtr-&amp;gt;next;

            if(curPtr-&amp;gt;next)
                curPtr-&amp;gt;next-&amp;gt;prev = newNode;

            curPtr-&amp;gt;next = newNode;
        } 

        head-&amp;gt;length++;
    }
    else
        ret = FAILURE;

    return ret;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.in/2012/02/implementation-of-doubly-linked-list-in.html&quot; target=&quot;_blank&quot;&gt;Doubly Linked List&lt;/a&gt;&lt;b&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Next Article&lt;/b&gt;: &lt;a href=&quot;http://codingfreak.blogspot.in/2012/03/implementation-of-doubly-linked-list.html&quot; target=&quot;_blank&quot;&gt;Deletion of a Node from Doubly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class=&quot;blogger-post-footer&quot;&gt;&lt;hr/&gt;&lt;a href=&quot;http://codingfreak.blogspot.com&quot;&gt;CodingFreak&lt;/a&gt;

&lt;script type=&quot;text/javascript&quot;&gt;&lt;!--
google_ad_client = &quot;pub-3238413467491229&quot;;
/* 234x60, created 1/30/08 */
google_ad_slot = &quot;9553173798&quot;;
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = &quot;&quot;; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src=&quot;http://pagead2.googlesyndication.com/pagead/show_ads.js&quot; type=&quot;text/javascript&quot;&gt;
&lt;/script&gt;&lt;/div&gt;</description><link>http://codingfreak.blogspot.com/2012/02/implementation-of-doubly-linked-list.html</link><author>noreply@blogger.com (Unknown)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqFwLwhNTlclZYX9sovwmZV5vshXwC5RnWYtngIuuGSIwOrBY7y-bdvlbPKy0YRhbutXetjapRIvy7-p30h5XwtKtWldjwx_s6ig1hqMdiZ6fXBXLIuALT_RoqbwlRmgcOIBt3nmYLhIw/s72-c/01.png" height="72" width="72"/><thr:total>2</thr:total></item></channel></rss>