<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" version="2.0"><channel><title>Codingfreak</title><link>http://codingfreak.blogspot.com/</link><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codingfreak" /><description></description><language>en</language><managingEditor>noreply@blogger.com (Ajith Adapa)</managingEditor><lastBuildDate>Tue, 21 May 2013 07:26:57 PDT</lastBuildDate><generator>Blogger</generator><atom:id xmlns:atom="http://www.w3.org/2005/Atom">tag:blogger.com,1999:blog-4070434216759110691</atom:id><openSearch:totalResults xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">81</openSearch:totalResults><openSearch:startIndex xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">1</openSearch:startIndex><openSearch:itemsPerPage xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/">25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/codingfreak" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="codingfreak" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><geo:lat>12.9833</geo:lat><geo:long>77.5833</geo:long><creativeCommons:license>http://creativecommons.org/licenses/by/3.0/</creativeCommons:license><feedburner:emailServiceId xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">codingfreak</feedburner:emailServiceId><feedburner:feedburnerHostname xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0">http://feedburner.google.com</feedburner:feedburnerHostname><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://add.my.yahoo.com/rss?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://us.i1.yimg.com/us.yimg.com/i/us/my/addtomyyahoo4.gif">Subscribe with My Yahoo!</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.newsgator.com/ngs/subscriber/subext.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.newsgator.com/images/ngsub1.gif">Subscribe with NewsGator</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://feeds.my.aol.com/add.jsp?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://o.aolcdn.com/favorites.my.aol.com/webmaster/ffclient/webroot/locale/en-US/images/myAOLButtonSmall.gif">Subscribe with My AOL</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bloglines.com/sub/http://feeds.feedburner.com/codingfreak" src="http://www.bloglines.com/images/sub_modern11.gif">Subscribe with Bloglines</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.netvibes.com/subscribe.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.netvibes.com/img/add2netvibes.gif">Subscribe with Netvibes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://fusion.google.com/add?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://buttons.googlesyndication.com/fusion/add.gif">Subscribe with Google</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.pageflakes.com/subscribe.aspx?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.pageflakes.com/ImageFile.ashx?instanceId=Static_4&amp;fileName=ATP_blu_91x17.gif">Subscribe with Pageflakes</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.plusmo.com/add?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://plusmo.com/res/graphics/fbplusmo.gif">Subscribe with Plusmo</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://my.feedlounge.com/external/subscribe?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://static.feedlounge.com/buttons/subscribe_0.gif">Subscribe with FeedLounge</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.thefreedictionary.com/_/hp/AddRSS.aspx?http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://img.tfd.com/hp/addToTheFreeDictionary.gif">Subscribe with The Free Dictionary</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.bitty.com/manual/?contenttype=rssfeed&amp;contentvalue=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.bitty.com/img/bittychicklet_91x17.gif">Subscribe with Bitty Browser</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.live.com/?add=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://tkfiles.storage.msn.com/x1piYkpqHC_35nIp1gLE68-wvzLZO8iXl_JMledmJQXP-XTBOLfmQv4zhj4MhcWEJh_GtoBIiAl1Mjh-ndp9k47If7hTaFno0mxW9_i3p_5qQw">Subscribe with Live.com</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.yourminis.com/subscribe.aspx?u=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.yourminis.com/images/addtoyourminisbadge.gif">Subscribe with Yourminis.com</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://download.attensa.com/app/get_attensa.html?feedurl=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.attensa.com/blogs/attensa/WindowsLiveWriter/BadgeredintoBadges_10C02/attensa_feed_button5.gif">Subscribe with Attensa for Outlook</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.webwag.com/wwgthis.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.webwag.com/images/wwgthis.gif">Subscribe with Webwag</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.podcastready.com/oneclick_bookmark.php?url=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak" src="http://www.podcastready.com/images/podcastready_button.gif">Subscribe with Podcast Ready</feedburner:feedFlare><feedburner:feedFlare xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" href="http://www.addtoany.com/?linkname=Codingfreak&amp;linkurl=http%3A%2F%2Ffeeds.feedburner.com%2Fcodingfreak&amp;type=feed" src="http://www.addtoany.com/addfr-b.gif">Add to Any Feed Reader</feedburner:feedFlare><item><title>Death of Google Reader - Journey towards alternatives</title><link>http://codingfreak.blogspot.com/2013/03/death-of-google-reader-journey-towards.html</link><category>Google Reader</category><category>How to</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Fri, 15 Mar 2013 23:14:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-4751024012384112702</guid><description>&lt;span style="color: red;"&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&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="tr_bq"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;object class="BLOGGER-youtube-video" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" data-thumbnail-src="http://img.youtube.com/vi/75qEvW5PANI/0.jpg" height="266" width="320"&gt;&lt;param name="movie" value="http://youtube.googleapis.com/v/75qEvW5PANI&amp;source=uds" /&gt;&lt;param name="bgcolor" value="#FFFFFF" /&gt;&lt;param name="allowFullScreen" value="true" /&gt;&lt;embed width="320" height="266"  src="http://youtube.googleapis.com/v/75qEvW5PANI&amp;source=uds" type="application/x-shockwave-flash" allowfullscreen="true"&gt;&lt;/embed&gt;&lt;/object&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='more'&gt;&lt;/a&gt;&lt;br /&gt;
&lt;blockquote class="tr_bq"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-Szk0ghOkxSU/UUP5ksVThVI/AAAAAAAACkI/5sIpaMZdkUQ/s1600/01.jpeg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-Szk0ghOkxSU/UUP5ksVThVI/AAAAAAAACkI/5sIpaMZdkUQ/s1600/01.jpeg" /&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="color: red;"&gt;&lt;b&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;&lt;span style="font-size: large;"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-ghwyGOk37Gg/UUP6QSNcKRI/AAAAAAAACkQ/9e6UQroBO94/s1600/01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-ghwyGOk37Gg/UUP6QSNcKRI/AAAAAAAACkQ/9e6UQroBO94/s1600/01.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
Uproar started every where as users backlashed at google'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="color: red;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&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="color: red;"&gt;&lt;b&gt;&lt;span style="font-family: &amp;quot;Trebuchet MS&amp;quot;,sans-serif;"&gt;&lt;span style="font-size: large;"&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="font-size: large;"&gt;1. &lt;a href="http://www.feedly.com/" target="_blank"&gt;Feedly&lt;/a&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-VmyMC1TBDQY/UUQAll9m2NI/AAAAAAAACkg/eYodYEguZTI/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://1.bp.blogspot.com/-VmyMC1TBDQY/UUQAll9m2NI/AAAAAAAACkg/eYodYEguZTI/s320/01.png" width="320" /&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="http://blog.feedly.com/2013/03/14/tips-for-google-reader-users-migrating-to-feedly/" target="_blank"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-DxYSJdQtSpY/UUQNGtmYnlI/AAAAAAAACkw/T614bGUfhIM/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="268" src="http://2.bp.blogspot.com/-DxYSJdQtSpY/UUQNGtmYnlI/AAAAAAAACkw/T614bGUfhIM/s640/01.png" width="640" /&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="font-family: inherit;"&gt;&lt;span style="font-size: large;"&gt;2. &lt;a href="http://www.bloglovin.com/" target="_blank"&gt;Bloglovin&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-0e5f1z2TlQs/UUSmMqJX_iI/AAAAAAAAClA/_3iYY5eL1mI/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-0e5f1z2TlQs/UUSmMqJX_iI/AAAAAAAAClA/_3iYY5eL1mI/s1600/01.png" /&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-NXSTXmcpUO4/UUSpoJ74tqI/AAAAAAAAClI/GocBmCe8AaA/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="373" src="http://1.bp.blogspot.com/-NXSTXmcpUO4/UUSpoJ74tqI/AAAAAAAAClI/GocBmCe8AaA/s640/01.png" width="640" /&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="font-size: large;"&gt;&lt;span style="font-family: inherit;"&gt;3. &lt;a href="http://www.netvibes.com/en" target="_blank"&gt;Netvibes&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-2RMgXyoy2C8/UUSxWCFWNwI/AAAAAAAAClQ/m_RjGD75ogg/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-2RMgXyoy2C8/UUSxWCFWNwI/AAAAAAAAClQ/m_RjGD75ogg/s1600/01.png" /&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-TtJxW4RcE7U/UUSzapXszWI/AAAAAAAAClY/77fKfNpRxKk/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="252" src="http://1.bp.blogspot.com/-TtJxW4RcE7U/UUSzapXszWI/AAAAAAAAClY/77fKfNpRxKk/s640/01.png" width="640" /&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="http://blog.netvibes.com/easily-migrate-from-google-reader-to-netvibes/" target="_blank"&gt;blog link&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-I5VrWOhgzsw/UUS4ZxvAh-I/AAAAAAAAClg/jkXFnZSQRhI/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="188" src="http://4.bp.blogspot.com/-I5VrWOhgzsw/UUS4ZxvAh-I/AAAAAAAAClg/jkXFnZSQRhI/s640/01.png" width="640" /&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'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="font-size: large;"&gt;&lt;b&gt;4. &lt;a href="http://feedspot.com/" target="_blank"&gt;Feedspot&lt;/a&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-efbBLd2G_5o/UUS5kPoEVYI/AAAAAAAAClo/9ies5oORlvg/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-efbBLd2G_5o/UUS5kPoEVYI/AAAAAAAAClo/9ies5oORlvg/s1600/01.png" /&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-_VJpamrCh1c/UUS9D9ibF9I/AAAAAAAAClw/RTiWPygWOqs/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="292" src="http://4.bp.blogspot.com/-_VJpamrCh1c/UUS9D9ibF9I/AAAAAAAAClw/RTiWPygWOqs/s640/01.png" width="640" /&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't support automatic marking of items as read as they are scrolled past. &lt;/li&gt;
&lt;/ul&gt;
&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kby7s4TR_bQ:t5awY4UN1Ng:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kby7s4TR_bQ:t5awY4UN1Ng:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kby7s4TR_bQ:t5awY4UN1Ng:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=kby7s4TR_bQ:t5awY4UN1Ng:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kby7s4TR_bQ:t5awY4UN1Ng:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=kby7s4TR_bQ:t5awY4UN1Ng:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/kby7s4TR_bQ" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-03-17T00:28:18.919+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-Szk0ghOkxSU/UUP5ksVThVI/AAAAAAAACkI/5sIpaMZdkUQ/s72-c/01.jpeg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><title>Divide a number by 3 without using any arithmetic operators</title><link>http://codingfreak.blogspot.com/2013/03/Divide-number-by-3-without-using-any-arithmetic-operators.html</link><category>C</category><category>Interview Question</category><category>Bit Operations</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Wed, 13 Mar 2013 21:35:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5117875310534613661</guid><description>How to divide a number by 3 without using + - / * %&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="font-size: large;"&gt;&lt;b&gt;Solution 01:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

int main()
{
    FILE *fp=fopen("temp.dat","w+b");
    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("%d / %d = %d", number, divisor, result);
    free(buf);
    fclose(fp);
    return 0;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;Explanation of how it works&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;The &lt;b&gt;fwrite&lt;/b&gt; writes number bytes (number being 123456 in the example above).&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;rewind&lt;/b&gt; resets the file pointer to the front of the file.&amp;nbsp;&lt;/li&gt;
&lt;li&gt;&lt;b&gt;fread&lt;/b&gt; reads a maximum of number "records" that are divisor in length from the file, and returns the number of elements it read.&amp;nbsp;&lt;/li&gt;
&lt;/ol&gt;
If you write 30 bytes then read back the file in units of 3, you get 10 "units". 30 / 3 = 10&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution 02:&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&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("%dn", r.quot);

    return 0;
}&lt;/pre&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;Solution 03:&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&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("%d n", 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="http://stackoverflow.com/questions/11694546/divide-a-number-by-3-without-using-operators" target="_blank"&gt;Stackoverflow&lt;/a&gt;.
&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=thT3ZMNGR20:N2fD2NuV4Bw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=thT3ZMNGR20:N2fD2NuV4Bw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=thT3ZMNGR20:N2fD2NuV4Bw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=thT3ZMNGR20:N2fD2NuV4Bw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=thT3ZMNGR20:N2fD2NuV4Bw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=thT3ZMNGR20:N2fD2NuV4Bw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/thT3ZMNGR20" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-03-22T09:59:51.388+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Deleting a Node from a Singly Linked List</title><link>http://codingfreak.blogspot.com/2012/12/deleting-node-from-singly-linked-list.html</link><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Tue, 25 Dec 2012 04:55:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-1757637782340738675</guid><description>This article is part of article series - "&lt;a href="http://codingfreak.blogspot.in/p/data-structures.html" target="_blank"&gt;Datastructures&lt;/a&gt;"&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.in/2009/08/implementation-of-singly-linked-list-in.html" target="_blank"&gt;Implementation of Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html" target="_blank"&gt;Reversing a Singly Linked List&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&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="font-size: large;"&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="cpp" name="code"&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='more'&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;Sourcecode&lt;/b&gt;:
&lt;pre class="cpp" name="code"&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("\nElement %d is not found in the List", num);
    return 1;
 }&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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="font-size: large;"&gt;Deleting Node from position 'p' in the List&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;&lt;br /&gt;
To delete a Node at the position 'p' we have to first traverse the list until we reach the position 'p'. 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 'p' 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="cpp" name="code"&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="cpp" name="code"&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("\nDeletion of Node at given location is not possible\n ");
    }
    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="http://codingfreak.blogspot.in/2009/08/implementation-of-singly-linked-list-in.html" target="_blank"&gt;Implementation of Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html" target="_blank"&gt;Reversing a Singly Linked List&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=2I4cz3KsGWA:qCa7JSB0UZ4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=2I4cz3KsGWA:qCa7JSB0UZ4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=2I4cz3KsGWA:qCa7JSB0UZ4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=2I4cz3KsGWA:qCa7JSB0UZ4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=2I4cz3KsGWA:qCa7JSB0UZ4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=2I4cz3KsGWA:qCa7JSB0UZ4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/2I4cz3KsGWA" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-01-19T16:32:44.336+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Detecting First Node in a Loop in the List</title><link>http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html</link><category>Interview Question</category><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Tue, 25 Dec 2012 01:06:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-851442953449411364</guid><description>This article is part of article series - "&lt;a href="http://codingfreak.blogspot.in/p/data-structures.html" target="_blank"&gt;Datastructures&lt;/a&gt;"&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html" target="_blank"&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="http://codingfreak.blogspot.com/2012/11/finding-nth-node-from-end-of-singly-linked-list.html" target="_blank"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-eYwYLrlLWZs/UNlpG3goYgI/AAAAAAAACaI/MdQ2-CPcJYY/s1600/01.gif" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="320" src="http://3.bp.blogspot.com/-eYwYLrlLWZs/UNlpG3goYgI/AAAAAAAACaI/MdQ2-CPcJYY/s320/01.gif" width="311" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-cg4vVSVVhyI/URsA1H8AWII/AAAAAAAACiY/FKpQGr7Cxo4/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-cg4vVSVVhyI/URsA1H8AWII/AAAAAAAACiY/FKpQGr7Cxo4/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Figure-1: Singly Linked list with a loop which we have detected using Floyd's Cycle Detection Algorithm&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;span style="background-color: red;"&gt;&lt;b&gt;NOTE:&lt;/b&gt;&lt;/span&gt; Example in Figure-1 is the output of Floyd's Cycle detection Algorithm which we have seen in &lt;a href="http://codingfreak.blogspot.in/2012/09/detecting-loop-in-singly-linked-list_22.html" target="_blank"&gt;Floyd'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="color: red;"&gt;&lt;span style="font-size: large;"&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="font-size: small;"&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="cpp" name="code"&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 'p' is the length of the loop and 'n' 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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-xH-E-xXO2E8/URsD15Eb4FI/AAAAAAAACis/EjD1HHLrl4w/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-xH-E-xXO2E8/URsD15Eb4FI/AAAAAAAACis/EjD1HHLrl4w/s1600/01.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style="color: red;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&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'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="font-size: small;"&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="cpp" name="code"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-b5FBhN5cz0g/URsJZm4onZI/AAAAAAAACjA/2mzTaJNqosM/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-b5FBhN5cz0g/URsJZm4onZI/AAAAAAAACjA/2mzTaJNqosM/s1600/01.png" /&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-a-Iy2yhxGzU/URsKC8znIdI/AAAAAAAACjI/2_z5UIe-XF8/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-a-Iy2yhxGzU/URsKC8znIdI/AAAAAAAACjI/2_z5UIe-XF8/s1600/01.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html" target="_blank"&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="http://codingfreak.blogspot.com/2012/11/finding-nth-node-from-end-of-singly-linked-list.html" target="_blank"&gt;Finding Nth node from end of a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=dyG3NaS0-60:qCYpHpCq2mI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=dyG3NaS0-60:qCYpHpCq2mI:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=dyG3NaS0-60:qCYpHpCq2mI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=dyG3NaS0-60:qCYpHpCq2mI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=dyG3NaS0-60:qCYpHpCq2mI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=dyG3NaS0-60:qCYpHpCq2mI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/dyG3NaS0-60" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-15T05:46:08.275+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-eYwYLrlLWZs/UNlpG3goYgI/AAAAAAAACaI/MdQ2-CPcJYY/s72-c/01.gif" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>Finding Nth node from end of a Singly Linked List</title><link>http://codingfreak.blogspot.com/2012/11/finding-nth-node-from-end-of-singly-linked-list.html</link><category>Interview Question</category><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Tue, 13 Nov 2012 20:17:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5905191137999281125</guid><description>This article is part of article series - "&lt;a href="http://codingfreak.blogspot.in/p/data-structures.html" target="_blank"&gt;Datastructures&lt;/a&gt;"&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html" target="_blank"&gt;Finding first node in a Loop in Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-l0NKRJDXLJs/UOetLvq7PsI/AAAAAAAACbA/CH5cPZ4Yqcw/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-l0NKRJDXLJs/UOetLvq7PsI/AAAAAAAACbA/CH5cPZ4Yqcw/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;&lt;span style="color: black;"&gt;&lt;b&gt;&lt;span style="font-size: Normal;"&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="color: red;"&gt;&lt;span style="font-size: large;"&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="font-size: small;"&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="font-size: small;"&gt;Assign &lt;b&gt;curNodePtr&lt;/b&gt; to&lt;span style="font-size: small;"&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="font-size: small;"&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="font-size: small;"&gt;count not equal to N then return &lt;/span&gt;since we can'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="font-size: small;"&gt;&lt;span style="font-size: small;"&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='more'&gt;&lt;/a&gt;&lt;b&gt;Pseudocode:&lt;/b&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;curNodePtr := FirstNode

if curNodePtr == NULL
  return 'Less Nodes in the List'

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 'Less Nodes in the List'

  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="font-size: small;"&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="font-size: small;"&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="font-size: small;"&gt;turns Node 4.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: small;"&gt;curNode &amp;nbsp; &amp;nbsp; tmpNode &amp;nbsp; Count&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&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="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&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 'Less Nodes in the List'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: small;"&gt;curNode &amp;nbsp; &amp;nbsp; tmpNode &amp;nbsp; Count&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;span style="font-size: small;"&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="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&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="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&gt;&lt;span style="font-size: small;"&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="color: red;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&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="font-size: small;"&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="font-size: small;"&gt;If Length of the Singly linked List (P) is lesser than N return, since we can'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="font-size: small;"&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="font-size: small;"&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="cpp" name="code"&gt;curNodePtr := FirstNode
P = 0

forever:

  if curNodePtr == NULL
    break

  P++
  curNodePtr := curNodePtr.NEXT

if P &amp;lt; N
  return 'Less Nodes in the List'

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="font-size: small;"&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="font-size: small;"&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="color: red;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&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'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="font-size: small;"&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="font-size: small;"&gt;&lt;b&gt;tmpPtr&lt;/b&gt; will start first by traversing N positions in the list. If it doesn't reach N positions from start of the singly linked list it means list is small. Return as we can't find the Nth node from the end of the list.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&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="font-size: small;"&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="cpp" name="code"&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 'Cannot find Nth element in the list'

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="font-size: small;"&gt;Time Complexity: &lt;b&gt;O(n)&lt;/b&gt; &lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&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="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=3TNKPrpeNOY:g9KRL7vpPyA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=3TNKPrpeNOY:g9KRL7vpPyA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=3TNKPrpeNOY:g9KRL7vpPyA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=3TNKPrpeNOY:g9KRL7vpPyA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=3TNKPrpeNOY:g9KRL7vpPyA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=3TNKPrpeNOY:g9KRL7vpPyA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/3TNKPrpeNOY" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-14T09:13:41.509+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-l0NKRJDXLJs/UOetLvq7PsI/AAAAAAAACbA/CH5cPZ4Yqcw/s72-c/01.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Reversing a Singly Linked List</title><link>http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html</link><category>Interview Question</category><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Tue, 06 Nov 2012 20:24:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-572320054389403673</guid><description>This article is part of article series - "&lt;a href="http://codingfreak.blogspot.in/p/data-structures.html" target="_blank"&gt;Datastructures&lt;/a&gt;"&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/12/deleting-node-from-singly-linked-list.html" target="_blank"&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="http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html" target="_blank"&gt;Detecting a Loop in Singly Linked List - Tortoise and Hare&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-tqOrV10awXk/UOfUB58541I/AAAAAAAACbY/L-WRpkkqCE0/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" height="308" src="http://1.bp.blogspot.com/-tqOrV10awXk/UOfUB58541I/AAAAAAAACbY/L-WRpkkqCE0/s320/01.png" width="320" /&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-BirdLLKpSJE/UPp5ybh0RQI/AAAAAAAACeY/Keu_DeE0TTw/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-BirdLLKpSJE/UPp5ybh0RQI/AAAAAAAACeY/Keu_DeE0TTw/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="cpp" name="code"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-deVKU12R-Oc/UPp8UEeXjnI/AAAAAAAACes/pi11dACZW04/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-deVKU12R-Oc/UPp8UEeXjnI/AAAAAAAACes/pi11dACZW04/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="http://codingfreak.blogspot.com/2012/12/deleting-node-from-singly-linked-list.html" target="_blank"&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="http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html" target="_blank"&gt;Detecting a Loop in Singly Linked List - Tortoise and Hare&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=GDm2UQFO1E8:uzUrSomKucQ:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=GDm2UQFO1E8:uzUrSomKucQ:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=GDm2UQFO1E8:uzUrSomKucQ:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=GDm2UQFO1E8:uzUrSomKucQ:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=GDm2UQFO1E8:uzUrSomKucQ:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=GDm2UQFO1E8:uzUrSomKucQ:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/GDm2UQFO1E8" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-13T09:21:19.334+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-tqOrV10awXk/UOfUB58541I/AAAAAAAACbY/L-WRpkkqCE0/s72-c/01.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Detecting a Loop in Singly Linked List - Tortoise &amp; Hare</title><link>http://codingfreak.blogspot.com/2012/09/detecting-loop-in-singly-linked-list_22.html</link><category>C</category><category>Interview Question</category><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Fri, 21 Sep 2012 23:07:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-5690511211198480784</guid><description>This article is part of article series - "&lt;a href="http://codingfreak.blogspot.com/p/data-structures.html" target="_blank"&gt;&lt;b&gt;Datastructures&lt;/b&gt;&lt;/a&gt;"&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html" target="_blank"&gt;Reversing a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html" target="_blank"&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="color: red;"&gt;&lt;span style="font-size: large;"&gt;Floyd's Cycle-Finding Algorithm&lt;/span&gt;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
In simple terms it is also known as "&lt;b&gt;Tortoise and Hare Algorithm&lt;/b&gt;" or "&lt;b&gt;Floyd's Cycle Detection Algorithm&lt;/b&gt;" named after its inventor Robert Floyd. It is one of the simple cycle detection algorithm. It's a simple pointers based approach.&lt;br /&gt;
&lt;br /&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-mINZJ2C35KE/UMVxiXE6JXI/AAAAAAAACVk/WaeTaxFjKlY/s1600/01.jpg" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-mINZJ2C35KE/UMVxiXE6JXI/AAAAAAAACVk/WaeTaxFjKlY/s1600/01.jpg" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Robert Floyd&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;a name='more'&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="font-size: small;"&gt;Start &lt;b&gt;Tortoise&lt;/b&gt; and &lt;b&gt;Hare&lt;/b&gt; at the f&lt;span style="font-size: small;"&gt;irst node &lt;/span&gt;of the List.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span style="font-size: small;"&gt;If &lt;b&gt;Hare&lt;/b&gt; reaches end of the List&lt;span style="font-size: small;"&gt;, &lt;/span&gt;return&lt;span style="font-size: small;"&gt; as there is &lt;span style="font-size: small;"&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="font-size: small;"&gt;Else &lt;span style="font-size: small;"&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="font-size: small;"&gt;If &lt;b&gt;Hare&lt;/b&gt; reaches end of the List, return as there &lt;span style="font-size: small;"&gt;is &lt;span style="font-size: small;"&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="font-size: small;"&gt;Else&lt;span style="font-size: small;"&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="font-size: small;"&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="font-size: small;"&gt;Else &lt;span style="font-size: small;"&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="cpp" name="code"&gt;tortoise := firstNode
hare := firstNode

forever:

  if hare == end 
    return 'No Loop Found'

  hare := hare.next

  if hare == end
    return 'No Loop Found'

  hare = hare.next
  tortoise = tortoise.next

  if hare == tortoise
    return 'Loop Found'&lt;/pre&gt;
&lt;b&gt;Why can'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 'n' 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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-fubT0ph-Kys/UMWMdoMFgHI/AAAAAAAACV8/wjZj1vDfYX4/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-fubT0ph-Kys/UMWMdoMFgHI/AAAAAAAACV8/wjZj1vDfYX4/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/--TxUSEUFAyI/URsASBKdvhI/AAAAAAAACiQ/9zZ6-SPE7QQ/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/--TxUSEUFAyI/URsASBKdvhI/AAAAAAAACiQ/9zZ6-SPE7QQ/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-cg4vVSVVhyI/URsA1H8AWII/AAAAAAAACiY/FKpQGr7Cxo4/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-cg4vVSVVhyI/URsA1H8AWII/AAAAAAAACiY/FKpQGr7Cxo4/s1600/01.png" /&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'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 'n' and there are 'p' variables before the loop. Hare traverses the loop twice in 'n' 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="http://codingfreak.blogspot.com/2012/11/reversing-a-singly-linked-list.html" target="_blank"&gt;Reversing a Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;b&gt;Next Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.com/2012/12/detecting-first-node-in-a-loop.html" target="_blank"&gt;Finding first node in a Loop in Singly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=v5ZwwzEKSo0:axHqCItpuKE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=v5ZwwzEKSo0:axHqCItpuKE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=v5ZwwzEKSo0:axHqCItpuKE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=v5ZwwzEKSo0:axHqCItpuKE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=v5ZwwzEKSo0:axHqCItpuKE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=v5ZwwzEKSo0:axHqCItpuKE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/v5ZwwzEKSo0" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-15T23:31:30.938+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-mINZJ2C35KE/UMVxiXE6JXI/AAAAAAAACVk/WaeTaxFjKlY/s72-c/01.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total></item><item><title>Write a C Program without main function</title><link>http://codingfreak.blogspot.com/2012/08/write-a-c-program-without-main-function.html</link><category>C</category><category>Interview Question</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Thu, 23 Aug 2012 08:19:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-8125301596330290867</guid><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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://1.bp.blogspot.com/-yb89nYwjwrQ/USuBjSaUCyI/AAAAAAAACjw/YgQT3paRTDo/s1600/01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-yb89nYwjwrQ/USuBjSaUCyI/AAAAAAAACjw/YgQT3paRTDo/s1600/01.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;span style="font-size: large;"&gt;&lt;span style="color: red;"&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="cpp" name="code"&gt;#include &amp;lt;stdio.h&amp;gt;

#define START main

int START()
{
   printf("HELLO WORLD");
}&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="color: red;"&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&gt;#include &amp;lt;stdio.h&amp;gt;

#define START m##a##i##n

int START()
{
   printf("HELLO WORLD");
}&lt;/pre&gt;
&lt;span style="color: red;"&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Solution 03: Using Argumented Macro&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&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("HELLO WORLD");
}&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="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=qBA_gsMFUKc:3d0FJTiZv2k:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=qBA_gsMFUKc:3d0FJTiZv2k:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=qBA_gsMFUKc:3d0FJTiZv2k:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=qBA_gsMFUKc:3d0FJTiZv2k:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=qBA_gsMFUKc:3d0FJTiZv2k:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=qBA_gsMFUKc:3d0FJTiZv2k:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/qBA_gsMFUKc" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-25T20:54:35.649+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://1.bp.blogspot.com/-yb89nYwjwrQ/USuBjSaUCyI/AAAAAAAACjw/YgQT3paRTDo/s72-c/01.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Implementing ls command in C</title><link>http://codingfreak.blogspot.com/2012/08/implementing-ls-command-in-c.html</link><category>C</category><category>Programming</category><category>Interview Question</category><category>Linux</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Thu, 09 Aug 2012 08:14:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-819243095199038007</guid><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="http://www.gnu.org/software/coreutils/" target="_blank"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-3OuARNOCnBs/URpdYuIoFZI/AAAAAAAACh8/ZreXfSvwZDw/s1600/01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-3OuARNOCnBs/URpdYuIoFZI/AAAAAAAACh8/ZreXfSvwZDw/s1600/01.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;pre class="cpp" name="code"&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 = '?';

  if (S_ISREG(mode)) ftype = '-';
  if (S_ISLNK(mode)) ftype = 'l';
  if (S_ISDIR(mode)) ftype = 'd';
  if (S_ISBLK(mode)) ftype = 'b';
  if (S_ISCHR(mode)) ftype = 'c';
  if (S_ISFIFO(mode)) ftype = '|';

  sprintf(perms_buff, "%c%c%c%c%c%c%c%c%c%c %c%c%c", ftype,
  mode &amp;amp; S_IRUSR ? 'r' : '-',
  mode &amp;amp; S_IWUSR ? 'w' : '-',
  mode &amp;amp; S_IXUSR ? 'x' : '-',
  mode &amp;amp; S_IRGRP ? 'r' : '-',
  mode &amp;amp; S_IWGRP ? 'w' : '-',
  mode &amp;amp; S_IXGRP ? 'x' : '-',
  mode &amp;amp; S_IROTH ? 'r' : '-',
  mode &amp;amp; S_IWOTH ? 'w' : '-',
  mode &amp;amp; S_IXOTH ? 'x' : '-',
  mode &amp;amp; S_ISUID ? 'U' : '-',
  mode &amp;amp; S_ISGID ? 'G' : '-',
  mode &amp;amp; S_ISVTX ? 'S' : '-');

  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("Error getting pathnamen");

  count = scandir(pathname, &amp;amp;files, one, alphasort);

  if(count &amp;gt; 0)
  {
    printf("total %dn",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("%10.10s", get_perms(statbuf.st_mode));
        printf(" %d", statbuf.st_nlink);

        if (!getpwuid_r(statbuf.st_uid, &amp;amp;pwent, buf, sizeof(buf), &amp;amp;pwentp))
          printf(" %s", pwent.pw_name);
        else
          printf(" %d", statbuf.st_uid);

        if (!getgrgid_r (statbuf.st_gid, &amp;amp;grp, buf, sizeof(buf), &amp;amp;grpt))
          printf(" %s", grp.gr_name);
        else
          printf(" %d", statbuf.st_gid);

        /* Print size of file. */
        printf(" %5d", (int)statbuf.st_size);

        localtime_r(&amp;amp;statbuf.st_mtime, &amp;amp;time);
        /* Get localized date string. */
        strftime(datestring, sizeof(datestring), "%F %T", &amp;amp;time);

        printf(" %s %sn", datestring, files[i]-&amp;gt;d_name);
      }

      free (files[i]);
    }

    free(files);
  }
}&lt;/pre&gt;
&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kP_dudKTOw8:lxTl7u0xe-Y:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kP_dudKTOw8:lxTl7u0xe-Y:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kP_dudKTOw8:lxTl7u0xe-Y:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=kP_dudKTOw8:lxTl7u0xe-Y:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=kP_dudKTOw8:lxTl7u0xe-Y:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=kP_dudKTOw8:lxTl7u0xe-Y:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/kP_dudKTOw8" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-12T22:37:39.901+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-3OuARNOCnBs/URpdYuIoFZI/AAAAAAAACh8/ZreXfSvwZDw/s72-c/01.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><title>GIT - Adding diffmerge as visual merge in git</title><link>http://codingfreak.blogspot.com/2012/07/git-adding-diffmerge-as-visual-merge-in.html</link><category>git</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Wed, 11 Jul 2012 23:45:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-8983264111824311653</guid><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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-J9xpqKs7k08/UMVeb-788AI/AAAAAAAACVA/6Tt8EFv450Y/s1600/git.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" border="0" height="272" src="http://3.bp.blogspot.com/-J9xpqKs7k08/UMVeb-788AI/AAAAAAAACVA/6Tt8EFv450Y/s320/git.jpg" title="git" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&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="http://www.sourcegear.com/diffmerge/downloads.php" target="_blank"&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="cpp" name="code"&gt;git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd 'diffmerge "$LOCAL" "$REMOTE"'
git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd 'diffmerge --merge --result="$MERGED" "$LOCAL" "$(if test -f "$BASE"; then echo "$BASE"; else echo "$LOCAL"; fi)" "$REMOTE"'
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="cpp" name="code"&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="http://twobitlabs.com/2011/08/install-diffmerge-git-mac-os-x/"&gt;Twobit Labs&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=nbtmlsKXWxQ:xGDbhegqBTA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=nbtmlsKXWxQ:xGDbhegqBTA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=nbtmlsKXWxQ:xGDbhegqBTA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=nbtmlsKXWxQ:xGDbhegqBTA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=nbtmlsKXWxQ:xGDbhegqBTA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=nbtmlsKXWxQ:xGDbhegqBTA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/nbtmlsKXWxQ" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-12-10T09:31:19.670+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-J9xpqKs7k08/UMVeb-788AI/AAAAAAAACVA/6Tt8EFv450Y/s72-c/git.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>TCPDUMP cheat sheet</title><link>http://codingfreak.blogspot.com/2012/06/tcpdump-cheat-sheet.html</link><category>tcpdump</category><category>How to</category><category>cheat-sheet</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Thu, 14 Jun 2012 02:29:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-7379765917753379539</guid><description>&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://2.bp.blogspot.com/-pTPD-rDnSyg/UPpn973x2pI/AAAAAAAACeE/yD8V4Mi4UV8/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-pTPD-rDnSyg/UPpn973x2pI/AAAAAAAACeE/yD8V4Mi4UV8/s1600/01.png" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&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="color: red;"&gt;&lt;span style="font-size: large;"&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="font-size: large;"&gt;&lt;span style="color: red;"&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="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=R3wavME-4v0:APbDyNlULBo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=R3wavME-4v0:APbDyNlULBo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=R3wavME-4v0:APbDyNlULBo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=R3wavME-4v0:APbDyNlULBo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=R3wavME-4v0:APbDyNlULBo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=R3wavME-4v0:APbDyNlULBo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/R3wavME-4v0" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-12T08:48:22.810+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-pTPD-rDnSyg/UPpn973x2pI/AAAAAAAACeE/yD8V4Mi4UV8/s72-c/01.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Decoding hardware information of a PC</title><link>http://codingfreak.blogspot.com/2012/05/decoding-hardware-information-of-pc.html</link><category>Hardware</category><category>Linux</category><category>How to</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Wed, 02 May 2012 02:47:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-7374716353453789195</guid><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="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-StxuntJtgKM/UMVyUiFSjMI/AAAAAAAACVs/pC6TbRwgHuQ/s1600/01.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="Tweak" border="0" src="http://3.bp.blogspot.com/-StxuntJtgKM/UMVyUiFSjMI/AAAAAAAACVs/pC6TbRwgHuQ/s1600/01.png" title="Tweak" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
&lt;span style="font-size: large;"&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="http://linux.die.net/man/8/biosdecode" rel="nofollow" target="_blank"&gt;man-page&lt;/a&gt;.&lt;br /&gt;
&lt;pre class="cpp" name="code"&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="font-size: large;"&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="http://linux.die.net/man/8/dmidecode" rel="nofollow" target="_blank"&gt;man-page&lt;/a&gt;.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&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="font-size: large;"&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="http://linux.die.net/man/8/lspci" rel="nofollow" target="_blank"&gt;man-page&lt;/a&gt;.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&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="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NzLPVTivwJA:JlmP5x4rJPo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NzLPVTivwJA:JlmP5x4rJPo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NzLPVTivwJA:JlmP5x4rJPo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=NzLPVTivwJA:JlmP5x4rJPo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=NzLPVTivwJA:JlmP5x4rJPo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=NzLPVTivwJA:JlmP5x4rJPo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/NzLPVTivwJA" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-12-10T10:56:00.149+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-StxuntJtgKM/UMVyUiFSjMI/AAAAAAAACVs/pC6TbRwgHuQ/s72-c/01.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item><item><title>Uli Drepper - Basics you should know about buffer overflow</title><link>http://codingfreak.blogspot.com/2012/04/uli-drepper-basics-you-should-know.html</link><category>Buffer overflow</category><category>Security</category><category>Linux</category><category>Video Series</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Thu, 19 Apr 2012 05:18:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-374796060261492237</guid><description>&lt;b&gt;Part 1: Buffer Overflows&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;iframe allowfullscreen="" frameborder="0" height="360" src="http://www.youtube.com/embed/AlgwqMH3Uss" width="480"&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;a name='more'&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="" frameborder="0" height="360" src="http://www.youtube.com/embed/3tG2I4uPQug" width="480"&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="" frameborder="0" height="360" src="http://www.youtube.com/embed/iLyEriwr2U4" width="480"&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="" frameborder="0" height="360" src="http://www.youtube.com/embed/tE6XFSz1D8M" width="480"&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="" frameborder="0" height="360" src="http://www.youtube.com/embed/CrdoKKTyEgo" width="480"&gt;&lt;/iframe&gt;

&lt;br /&gt;
&lt;br /&gt;
For direct REDHAT videos checkout this &lt;a href="http://magazine.redhat.com/2007/10/26/uli-drepper-paying-attention-to-the-basics/" target="_blank"&gt;link&lt;/a&gt;.&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6vp46qLa2dM:rXaWLkYb3SE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6vp46qLa2dM:rXaWLkYb3SE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6vp46qLa2dM:rXaWLkYb3SE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=6vp46qLa2dM:rXaWLkYb3SE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6vp46qLa2dM:rXaWLkYb3SE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=6vp46qLa2dM:rXaWLkYb3SE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/6vp46qLa2dM" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-25T20:58:35.420+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://img.youtube.com/vi/AlgwqMH3Uss/default.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Connecting via SSH without password prompt</title><link>http://codingfreak.blogspot.com/2012/04/connectig-via-ssh-without-password.html</link><category>Linux</category><category>How to</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Thu, 05 Apr 2012 22:49:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-3943261768157389994</guid><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 "user@host" 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="color: red;"&gt;NOTE:&lt;/b&gt; Only do this with trusted machines.&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-00zpQVItY64/UMVkUgec_yI/AAAAAAAACVQ/HlioUQ1ejP0/s1600/Terminal.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img alt="" border="0" height="320" src="http://3.bp.blogspot.com/-00zpQVItY64/UMVkUgec_yI/AAAAAAAACVQ/HlioUQ1ejP0/s320/Terminal.png" title="" width="320" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;a name='more'&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="cpp" name="code"&gt;cd ~/.ssh&lt;/pre&gt;
&lt;br /&gt;
&lt;li&gt;Check if you have a '&lt;b&gt;id_rsa.pub&lt;/b&gt;' (or '&lt;b&gt;id_dsa.pub&lt;/b&gt;') file in .ssh folder. If not, try below command:&lt;/li&gt;
&lt;pre class="cpp" name="code"&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="cpp" name="code"&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="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=DlxxNwPHep8:FjWPLwoHFU4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=DlxxNwPHep8:FjWPLwoHFU4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=DlxxNwPHep8:FjWPLwoHFU4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=DlxxNwPHep8:FjWPLwoHFU4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=DlxxNwPHep8:FjWPLwoHFU4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=DlxxNwPHep8:FjWPLwoHFU4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/DlxxNwPHep8" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-12-10T09:56:37.293+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-00zpQVItY64/UMVkUgec_yI/AAAAAAAACVQ/HlioUQ1ejP0/s72-c/Terminal.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>Daemon-izing a Process in Linux</title><link>http://codingfreak.blogspot.com/2012/03/daemon-izing-process-in-linux.html</link><category>Linux</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Tue, 13 Mar 2012 06:44:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2678749949937143988</guid><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="cpp" name="code"&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'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="cpp" name="code"&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="color: red;"&gt;&lt;span style="font-size: large;"&gt;what is a DAEMON Process?&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
A 'daemon' 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="color: red;"&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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='more'&gt;&lt;/a&gt;&lt;div style="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&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'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="cpp" name="code"&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="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;EPERM&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&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'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="cpp" name="code"&gt;chdir("/server/");&lt;/pre&gt;
The root "/" 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="color: red;"&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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 'stdin' (0), &lt;/li&gt;
&lt;li&gt;standard output 'stdout' (1), &lt;/li&gt;
&lt;li&gt;standard error 'stderr' (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="cpp" name="code"&gt;int fd;

fd = open("/dev/null",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="color: red;"&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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 'C' code which creates a daemon. &lt;br /&gt;
&lt;pre class="cpp" name="code"&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("/")) &amp;lt; 0)
    {
        exit(EXIT_FAILURE);
    }


    fd = open("/dev/null",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="font-size: large;"&gt;References&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href="http://www.enderunix.org/docs/eng/daemon.php" target="_blank"&gt;Daemon Server Programming&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.itp.uzh.ch/%7Edpotter/howto/daemonize" target="_blank"&gt;Daemonize in Linux&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://en.wikipedia.org/wiki/Process_group" target="_blank"&gt;Wiki&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=YMkuEbLxdFg:b1G5t5GVD8E:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=YMkuEbLxdFg:b1G5t5GVD8E:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=YMkuEbLxdFg:b1G5t5GVD8E:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=YMkuEbLxdFg:b1G5t5GVD8E:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=YMkuEbLxdFg:b1G5t5GVD8E:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=YMkuEbLxdFg:b1G5t5GVD8E:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/YMkuEbLxdFg" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-10-06T15:38:12.155+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">2</thr:total></item><item><title>Memory Layout of a C program - Part 2</title><link>http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-2.html</link><category>C</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Sun, 04 Mar 2012 03:59:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-4945757918676549520</guid><description>Continuation of &lt;a href="http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html" target="_blank"&gt;PART-1&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
As we have seen so much theory in the &lt;a href="http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html" target="_blank"&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="cpp" name="code"&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="cpp" name="code"&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='more'&gt;&lt;/a&gt;Let us include a static variable as shown below.&lt;br /&gt;
&lt;pre class="cpp" name="code"&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="cpp" name="code"&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="cpp" name="code"&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="cpp" name="code"&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="http://www.geeksforgeeks.org/archives/14268" target="_blank"&gt;Narendra Kangralkar &lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=1oUO0qK1C8w:3KU74HNPawA:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=1oUO0qK1C8w:3KU74HNPawA:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=1oUO0qK1C8w:3KU74HNPawA:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=1oUO0qK1C8w:3KU74HNPawA:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=1oUO0qK1C8w:3KU74HNPawA:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=1oUO0qK1C8w:3KU74HNPawA:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/1oUO0qK1C8w" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-10-06T15:38:23.783+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Memory Layout of a C program - Part 1</title><link>http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-1.html</link><category>C</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Sun, 04 Mar 2012 03:10:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6200432886671870069</guid><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="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div style="color: red;"&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-ohkIShIRrps/UMloODjJNhI/AAAAAAAACWc/d55WEizat_Y/s1600/memory.png" imageanchor="1" style="margin-left: auto; margin-right: auto;" target="_blank"&gt;&lt;img alt="Memory layout of a C program" border="0" src="http://2.bp.blogspot.com/-ohkIShIRrps/UMloODjJNhI/AAAAAAAACWc/d55WEizat_Y/s1600/memory.png" title="Memory layout of a C program" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="font-size: large;"&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='more'&gt;&lt;/a&gt;&lt;b style="color: red;"&gt;&lt;span style="font-size: large;"&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".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="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&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't have to be returned at all), so unordered malloc/free'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 "free store"), 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 "break" (Your programs will "break" 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'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="color: red;"&gt;
&lt;span style="font-size: large;"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/--2wQ8vHfbMM/UMlpHyznv2I/AAAAAAAACWk/XP9c95aHYeI/s1600/memory.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/--2wQ8vHfbMM/UMlpHyznv2I/AAAAAAAACWk/XP9c95aHYeI/s1600/memory.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Figure 2: Stack layout&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&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="http://codingfreak.blogspot.com/2012/03/memory-layout-of-c-program-part-2.html" target="_blank"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-tWpWom-gOSs/UMlpsvbugnI/AAAAAAAACWs/GNyxH13MQHc/s1600/memory.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-tWpWom-gOSs/UMlpsvbugnI/AAAAAAAACWs/GNyxH13MQHc/s1600/memory.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Figure 3: Relationship between Sections and Segments&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=k2CTLZlUreI:PhqAfJllpXI:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=k2CTLZlUreI:PhqAfJllpXI:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=k2CTLZlUreI:PhqAfJllpXI:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=k2CTLZlUreI:PhqAfJllpXI:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=k2CTLZlUreI:PhqAfJllpXI:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=k2CTLZlUreI:PhqAfJllpXI:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/k2CTLZlUreI" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-12-13T11:08:39.771+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-ohkIShIRrps/UMloODjJNhI/AAAAAAAACWc/d55WEizat_Y/s72-c/memory.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">3</thr:total></item><item><title>Deletion of a Node from Doubly Linked List</title><link>http://codingfreak.blogspot.com/2012/03/implementation-of-doubly-linked-list.html</link><category>C</category><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Thu, 01 Mar 2012 06:49:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-149401312334994450</guid><description>This article is part of article series - "&lt;a href="http://codingfreak.blogspot.in/p/data-structures.html" target="_blank"&gt;Datastructures&lt;/a&gt;".&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.in/2012/03/implementation-of-doubly-linked-list.html" target="_blank"&gt;Inserting a Node in Doubly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-oaG8lMO3YXg/UNgy2Eei5FI/AAAAAAAACYM/njLGqXs1f0g/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-oaG8lMO3YXg/UNgy2Eei5FI/AAAAAAAACYM/njLGqXs1f0g/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;Figure 1: Current Doubly Linked List&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-nVTouWArITw/UNg1TZFccBI/AAAAAAAACYg/8-NBcJRKWrw/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-nVTouWArITw/UNg1TZFccBI/AAAAAAAACYg/8-NBcJRKWrw/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-nHAsxOdyMVc/UNg2SqHzMGI/AAAAAAAACYs/9xUCBGPWWuc/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-nHAsxOdyMVc/UNg2SqHzMGI/AAAAAAAACYs/9xUCBGPWWuc/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-Oqvulna8sxE/UNg23aM0_AI/AAAAAAAACY4/MuaA8bkMKJY/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-Oqvulna8sxE/UNg23aM0_AI/AAAAAAAACY4/MuaA8bkMKJY/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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 'C' code to the deletion of a Node operation&lt;br /&gt;
&lt;pre class="cpp" name="code"&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="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K4OxdbH782Y:a3VqKpkbjMo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K4OxdbH782Y:a3VqKpkbjMo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K4OxdbH782Y:a3VqKpkbjMo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=K4OxdbH782Y:a3VqKpkbjMo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K4OxdbH782Y:a3VqKpkbjMo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=K4OxdbH782Y:a3VqKpkbjMo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/K4OxdbH782Y" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-12-25T15:04:54.207+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-oaG8lMO3YXg/UNgy2Eei5FI/AAAAAAAACYM/njLGqXs1f0g/s72-c/01.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Inserting a Node in Doubly Linked List</title><link>http://codingfreak.blogspot.com/2012/02/implementation-of-doubly-linked-list.html</link><category>C</category><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Mon, 27 Feb 2012 04:30:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6062941783072623096</guid><description>This article is part of article series - "&lt;a href="http://codingfreak.blogspot.in/p/data-structures.html" target="_blank"&gt;Datastructures&lt;/a&gt;" &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Previous Article&lt;/b&gt;: &lt;a href="http://codingfreak.blogspot.in/2012/02/implementation-of-doubly-linked-list-in.html" target="_blank"&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="http://codingfreak.blogspot.in/2012/03/implementation-of-doubly-linked-list.html" target="_blank"&gt;Deletion of a Node from Doubly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&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="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;PREV&lt;/span&gt; and &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NEXT&lt;/span&gt; variables in the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&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="font-size: large;"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-KhX8bcXGNV8/UNguqSexTHI/AAAAAAAACXQ/bDIAIZ2K6ko/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-KhX8bcXGNV8/UNguqSexTHI/AAAAAAAACXQ/bDIAIZ2K6ko/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&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="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&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="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NULL&lt;/span&gt; as we don'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="cpp" name="code"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-3Jo763raAJo/UNgvgrf8fKI/AAAAAAAACXc/iAUOYWW6AIQ/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-3Jo763raAJo/UNgvgrf8fKI/AAAAAAAACXc/iAUOYWW6AIQ/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: large;"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-TPcRAx9dyZc/UNgwK1P_EbI/AAAAAAAACXo/vpvC-Jsotwo/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-TPcRAx9dyZc/UNgwK1P_EbI/AAAAAAAACXo/vpvC-Jsotwo/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&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="cpp" name="code"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://1.bp.blogspot.com/-jK8fhDWULys/UNgwtniu4vI/AAAAAAAACXw/Zbdm2Z3zR-Y/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://1.bp.blogspot.com/-jK8fhDWULys/UNgwtniu4vI/AAAAAAAACXw/Zbdm2Z3zR-Y/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: large;"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-oLd1hfZhXOk/UNgxWcwi8wI/AAAAAAAACX4/DgMY9fgixRk/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-oLd1hfZhXOk/UNgxWcwi8wI/AAAAAAAACX4/DgMY9fgixRk/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&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="cpp" name="code"&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="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://3.bp.blogspot.com/-qg0hcykaIHA/UNgxvKlUSFI/AAAAAAAACYA/1LnqTmPe4ik/s1600/01.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-qg0hcykaIHA/UNgxvKlUSFI/AAAAAAAACYA/1LnqTmPe4ik/s1600/01.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&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="separator" style="clear: both; text-align: center;"&gt;
&lt;/div&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-size: large;"&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="cpp" name="code"&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="cpp" name="code"&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="http://codingfreak.blogspot.in/2012/02/implementation-of-doubly-linked-list-in.html" target="_blank"&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="http://codingfreak.blogspot.in/2012/03/implementation-of-doubly-linked-list.html" target="_blank"&gt;Deletion of a Node from Doubly Linked List&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K_FfJd3Urvc:p9rKnUKyvXE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K_FfJd3Urvc:p9rKnUKyvXE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K_FfJd3Urvc:p9rKnUKyvXE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=K_FfJd3Urvc:p9rKnUKyvXE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=K_FfJd3Urvc:p9rKnUKyvXE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=K_FfJd3Urvc:p9rKnUKyvXE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/K_FfJd3Urvc" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-12T22:39:47.476+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-KhX8bcXGNV8/UNguqSexTHI/AAAAAAAACXQ/bDIAIZ2K6ko/s72-c/01.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Implementation of Doubly Linked List in C</title><link>http://codingfreak.blogspot.com/2012/02/implementation-of-doubly-linked-list-in.html</link><category>C</category><category>Linked List</category><category>DataStructures</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Fri, 24 Feb 2012 19:58:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6272150419287458954</guid><description>In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called Nodes. Each Node contains two fields, called Links, that are references to the Previous and to the Next Node in the sequence of Nodes as well as field named Data.&lt;br /&gt;
For every Linked List we have something called Head which marks the starting of a list. So we have two main structures namely&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;Node&lt;/li&gt;
&lt;li&gt;Head&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;span style="color: #3d85c6;"&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Why we need a new structure for HEAD variable ?&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
Just for convenience I decided to have HEAD its own structure. You can even use the Node structure.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red; font-size: large;"&gt;&lt;b&gt;Node&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Every Node in a Doubly Linked List has three main members namely&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;PREV &lt;/li&gt;
&lt;li&gt;DATA &lt;/li&gt;
&lt;li&gt;NEXT &lt;/li&gt;
&lt;/ul&gt;
As their names say&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;PREV&lt;/b&gt; - holds the memory location of the Previous Node in the List. If there are none we point towards &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NULL&lt;/span&gt;. For the First Node in the List &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;PREV&lt;/span&gt; points to &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NULL&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;NEXT&lt;/b&gt; - holds the memory location of the Next Node in the List. If there are none we point towards &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NULL&lt;/span&gt;. For the Last Node in the List &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NEXT&lt;/span&gt; points to &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NULL&lt;/span&gt;.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;DATA&lt;/b&gt; - In simple words it holds Data. In our case it holds the memory location to the actual data to be held by the Node.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="cpp" name="code"&gt;typedef struct node
{
    struct node *prev;
    void        *data;
    struct node *next;
}NODE;&lt;/pre&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;Head&lt;/b&gt;&lt;/span&gt;&lt;/div&gt;
Head acts as the&amp;nbsp; "head" of the List. Head structure has two members namely&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;&lt;b&gt;LENGTH&lt;/b&gt; - holds the count of number of Nodes in the List. &lt;/li&gt;
&lt;li&gt;&lt;b&gt;FIRST&lt;/b&gt; - hold the memory location of the first Node in the List. If the List is EMPTY it points to &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NULL&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;pre class="cpp" name="code"&gt;typedef struct head 
{
    unsigned int length;
    struct node  *first;
}HEAD;&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="color: red;"&gt;NOTE&lt;/span&gt;:&lt;/b&gt; Our Head structure doesn't contain any pointer to the Tail of the List. Eventhough its a best way to include a pointer to Tail Node we decided to cover that implementation in Circular Doubly Linked List.&lt;br /&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-bzCsXSkLUw4/UMlrnJOKqKI/AAAAAAAACW0/PDF0hvMwDqs/s1600/memory.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://2.bp.blogspot.com/-bzCsXSkLUw4/UMlrnJOKqKI/AAAAAAAACW0/PDF0hvMwDqs/s1600/memory.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;&lt;span style="font-size: large;"&gt;&lt;span style="color: blue;"&gt;&lt;span style="font-size: small;"&gt;Figure 1: Basic structures used for Doubly Linked List Example&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
Pictorially, a Doubly Linked List can be depicted as shown Figure-2.&lt;br /&gt;
&lt;table align="center" cellpadding="0" cellspacing="0" class="tr-caption-container" style="margin-left: auto; margin-right: auto; text-align: center;"&gt;&lt;tbody&gt;
&lt;tr&gt;&lt;td style="text-align: center;"&gt;&lt;a href="http://4.bp.blogspot.com/-01hfY1o5NrE/UMlsMuEFK4I/AAAAAAAACW8/bO0bsfrEPlc/s1600/memory.png" imageanchor="1" style="margin-left: auto; margin-right: auto;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-01hfY1o5NrE/UMlsMuEFK4I/AAAAAAAACW8/bO0bsfrEPlc/s1600/memory.png" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="tr-caption" style="text-align: center;"&gt;&lt;span style="font-size: large;"&gt;&lt;span style="color: blue;"&gt;&lt;span style="font-size: small;"&gt;Figure 2: Doubly Linked List&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;
&lt;br /&gt;
In this introductory article let use see very basic operations which we don't discuss much in later articles.&lt;br /&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Creating a Node&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
Whenever we have to add a new Node we have to create one dynamically.So we will have a function called createNode which creates a new Node and returns the pointer to it.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;NODE* createNode()
{
    NODE *newNode = (NODE *)calloc(sizeof(NODE));

    return newNode;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Creating a Head&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
Since I have decided to have my own head structure I need to create a HEAD variable for every Doubly Linked List. So we have a to create HEAD variable which acts as the index to which we have add or delete a Node.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;HEAD* createHead()
{
    HEAD *head = (HEAD *)calloc(sizeof(HEAD));

    return head;
}&lt;/pre&gt;
&lt;br /&gt;
&lt;div style="color: red;"&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Traversing a Doubly Linked List&lt;/span&gt;&lt;/b&gt;&lt;/div&gt;
Doubly Linked List are more convenient than Singly Linked List since we maintain links for bi-directional traversing. If we are given a pointer to a Node which is at a arbitrary position we can traverse in both directions and display the contents in the whole List.&lt;br /&gt;
&lt;br /&gt;
But in a Singly Linked List we can only traverse in one direction from Head to Tail where as in Doubly Linked List we can traverse from Head to Tail as well as Tail to Head.&lt;br /&gt;
&lt;br /&gt;
As usual the logic would be a simple while loop and traversing using the NEXT and PREV pointers in the Node to traverse to the desired location in the List.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://codingfreak.blogspot.com/2012/02/implementation-of-doubly-linked-list.html" target="_blank"&gt;Part-1&lt;/a&gt; : Creating a Node in Doubly Linked List&lt;br /&gt;
&lt;a href="http://codingfreak.blogspot.com/2012/03/implementation-of-doubly-linked-list.html" target="_blank"&gt;Part-2&lt;/a&gt; : Deleting a Node in Doubly Linked List&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=KasVfA-SVjY:mkt71WBn0XE:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=KasVfA-SVjY:mkt71WBn0XE:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=KasVfA-SVjY:mkt71WBn0XE:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=KasVfA-SVjY:mkt71WBn0XE:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=KasVfA-SVjY:mkt71WBn0XE:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=KasVfA-SVjY:mkt71WBn0XE:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/KasVfA-SVjY" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2013-02-13T06:54:33.413+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://2.bp.blogspot.com/-bzCsXSkLUw4/UMlrnJOKqKI/AAAAAAAACW0/PDF0hvMwDqs/s72-c/memory.png" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">8</thr:total></item><item><title>Notification Chains in Linux Kernel - Part 03</title><link>http://codingfreak.blogspot.com/2012/02/notification-chains-in-linux-kernel.html</link><category>Linux</category><category>Kernel</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Sat, 11 Feb 2012 21:44:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-509028808842592750</guid><description>Continuation after &lt;a href="http://codingfreak.blogspot.in/2012/01/notification-chains-in-linux-kernel.html" target="_blank"&gt;PART-2&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: large;"&gt;&lt;b&gt;Notifying Events on a Chain&amp;nbsp;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
Notifications are generated with &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_call_chain&lt;/span&gt;. This function simply invokes, in order of priority, all the callback routines registered against the chain. Note that callback routines are executed in the context of the process that calls &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_call_chain&lt;/span&gt;. A callback routine could, however, be implemented so that it queues the notification somewhere and wakes up a process that will look at it.&lt;br /&gt;
&lt;br /&gt;
&lt;b style="color: #cc0000;"&gt;NOTE:&lt;/b&gt; Similar to register and unregister functions we don't directly call &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_call_chain&lt;/span&gt; function as we have wrapper functions for respective chains.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt; &amp;lt;kernel/notifier.c&amp;gt;

 58 static int __kprobes notifier_call_chain(struct notifier_block **nl,
 59                     unsigned long val, void *v,
 60                     int nr_to_call, int *nr_calls)
 61 {
 62     int ret = NOTIFY_DONE;
 63     struct notifier_block *nb, *next_nb;
 64 
 65     nb = rcu_dereference(*nl);
 66 
 67     while (nb &amp;amp;&amp;amp; nr_to_call) {
 68         next_nb = rcu_dereference(nb-&amp;gt;next);
 69         ret = nb-&amp;gt;notifier_call(nb, val, v);
 .
 76         nb = next_nb;
 77         nr_to_call--;
 78     }
 79     return ret;
 80 }&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;nl&lt;/span&gt; &lt;br /&gt;Notification chain.&amp;nbsp;&lt;/li&gt;
&lt;br /&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val&lt;/span&gt;&lt;br /&gt;
Event type. The chain itself identifies a class of events; &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;val &lt;/span&gt;unequivocally identifies an event type (i.e., &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NETDEV_REGISTER&lt;/span&gt;).&amp;nbsp;&lt;/li&gt;
&lt;br /&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;v&lt;/span&gt;&lt;br /&gt;
Input parameter that can be used by the handlers registered by the various clients. This can be used in different ways under different circumstances. For instance, when a new network device is registered with the kernel, the associated notification uses v to identify the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;net_device&lt;/span&gt; data structure.&lt;/li&gt;
&lt;br /&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;nr_to_call&lt;/span&gt; &lt;br /&gt;   
Number of notifier functions to be called. Don't care value of this parameter is -1.&amp;nbsp;&lt;/li&gt;
&lt;br /&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;nr_calls&lt;/span&gt; &lt;br /&gt; 
Records the number of notifications sent. Don't care value of this field is &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;NULL&lt;/span&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;span style="font-size: large;"&gt;&lt;b&gt;Example of notifier chains in Network Subsystem&lt;/b&gt;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
Let us see an example of using the notifier chains in bridge subsystem.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;net/bridge/br_notify.c&amp;gt;

 24 struct notifier_block br_device_notifier = {
 25     .notifier_call = br_device_event
 26 };

&amp;lt;net/bridge/br.c&amp;gt;

 30 static int __init br_init(void)
 31 {
 .
 48     err = register_netdevice_notifier(&amp;amp;br_device_notifier);
 .
 72 }&lt;/pre&gt;
Once we fill the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;br_device_notifier&lt;/span&gt; we are registering to &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;netdevice &lt;/span&gt;chain by calling the function&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt; register_netdevice_notifier()&lt;/span&gt;. &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;netdev_chain&lt;/span&gt; is the head of the netdevice chain.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;net/core/dev.c&amp;gt;

245 static RAW_NOTIFIER_HEAD(netdev_chain);
.
1129 int register_netdevice_notifier(struct notifier_block *nb)
1130 {
.
1136     rtnl_lock();
1137     err = raw_notifier_chain_register(&amp;amp;netdev_chain, nb);
1138     if (err)
1139         goto unlock;
.
1177 }&lt;/pre&gt;
When any events occur we invoke all the registered functions via &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_call_chain&lt;/span&gt; function.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;net/core/dev.c&amp;gt;

1208 int call_netdevice_notifiers(unsigned long val, struct net_device *dev)
1209 {
1210     return raw_notifier_call_chain(&amp;amp;netdev_chain, val, dev);
1211 }     &lt;/pre&gt;
Below is the list of event types supported by netdevice:
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;include/linux/notifier.h&amp;gt;

181 /* netdevice notifier chain */
182 #define NETDEV_UP   0x0001  /* For now you can't veto a device up/down */
183 #define NETDEV_DOWN 0x0002
184 #define NETDEV_REBOOT   0x0003  
.
188 #define NETDEV_CHANGE   0x0004  /* Notify device state change */
189 #define NETDEV_REGISTER 0x0005
190 #define NETDEV_UNREGISTER   0x0006
191 #define NETDEV_CHANGEMTU    0x0007
192 #define NETDEV_CHANGEADDR   0x0008
193 #define NETDEV_GOING_DOWN   0x0009
194 #define NETDEV_CHANGENAME   0x000A
195 #define NETDEV_FEAT_CHANGE  0x000B&lt;/pre&gt;
Now it is upto to the notified to write a switch case to invoke the appropriate internal function in their subsystem for processing the respective event.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt; &amp;lt;net/bridge/br_notify.c&amp;gt;

 34 static int br_device_event(struct notifier_block *unused, unsigned long event, void *ptr)
 35 {
 .
 43     /* not a port of a bridge */
 44     if (p == NULL)
 45         return NOTIFY_DONE;
 .
 49     switch (event) {
 50     case NETDEV_CHANGEMTU:
 51         dev_set_mtu(br-&amp;gt;dev, br_min_mtu(br));
 52         break;
 53 
 54     case NETDEV_CHANGEADDR:
 55         spin_lock_bh(&amp;amp;br-&amp;gt;lock);
 56         br_fdb_changeaddr(p, dev-&amp;gt;dev_addr);
 57         br_stp_recalculate_bridge_id(br);
 58         spin_unlock_bh(&amp;amp;br-&amp;gt;lock);
 59         break;
 60 
 61     case NETDEV_CHANGE:
 62         br_port_carrier_check(p);
 63         break;
 .
 72     case NETDEV_DOWN:
 73         spin_lock_bh(&amp;amp;br-&amp;gt;lock);
 74         if (br-&amp;gt;dev-&amp;gt;flags &amp;amp; IFF_UP)
 75             br_stp_disable_port(p);
 76         spin_unlock_bh(&amp;amp;br-&amp;gt;lock);
 77         break;
 78 
 79     case NETDEV_UP:
 80         if (netif_carrier_ok(dev) &amp;amp;&amp;amp; (br-&amp;gt;dev-&amp;gt;flags &amp;amp; IFF_UP)) {
 81             spin_lock_bh(&amp;amp;br-&amp;gt;lock);
 82             br_stp_enable_port(p);
 83             spin_unlock_bh(&amp;amp;br-&amp;gt;lock);
 .
 98 }&lt;/pre&gt;
We can say the whole notification mechanism is much more like call-back functions used to trigger appropriate functions when certain operation or an even happens.&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6roBJX0EiQ0:J7uz7HEfiWM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6roBJX0EiQ0:J7uz7HEfiWM:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6roBJX0EiQ0:J7uz7HEfiWM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=6roBJX0EiQ0:J7uz7HEfiWM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=6roBJX0EiQ0:J7uz7HEfiWM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=6roBJX0EiQ0:J7uz7HEfiWM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/6roBJX0EiQ0" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-03-03T10:13:47.112+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Notification Chains in Linux Kernel - Part 02</title><link>http://codingfreak.blogspot.com/2012/01/notification-chains-in-linux-kernel.html</link><category>Linux</category><category>Kernel</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Mon, 16 Jan 2012 19:32:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-2998264715813985257</guid><description>Continuation after &lt;a href="http://codingfreak.blogspot.in/2012/01/notification-chains-in-linux-part-01.html" target="_blank"&gt;PART-1&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Check the &lt;a href="http://codingfreak.blogspot.com/2012/02/notification-chains-in-linux-kernel.html" target="_blank"&gt;PART-3&lt;/a&gt;.&amp;nbsp; &lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Blocking Notifier chains&lt;/b&gt;&lt;br /&gt;
A blocking notifier chain runs in the process context. The calls in the notification list could be blocked as it runs in the process context. Notifications that are not highly time critical could use blocking notifier chains.&lt;br /&gt;
&lt;br /&gt;
Linux modules use blocking notifier chains to inform the modules on a change in QOS value or the addition of a new device.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

186 int blocking_notifier_chain_register(struct blocking_notifier_head *nh,
187         struct notifier_block *n)
188 {
.
199     down_write(&amp;amp;nh-&amp;gt;rwsem);
200     ret = notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
201     up_write(&amp;amp;nh-&amp;gt;rwsem);
202     return ret;
203 }
204 EXPORT_SYMBOL_GPL(blocking_notifier_chain_register)
.
216 int blocking_notifier_chain_unregister(struct blocking_notifier_head *nh,
217         struct notifier_block *n)
218 {
.
229     down_write(&amp;amp;nh-&amp;gt;rwsem);
230     ret = notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
231     up_write(&amp;amp;nh-&amp;gt;rwsem);
232     return ret;
233 }
234 EXPORT_SYMBOL_GPL(blocking_notifier_chain_unregister);&lt;/pre&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;b&gt;Raw Notifier chains&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
A raw notifier chain does not manage the locking and protection of the callers. Also, there are no restrictions on callbacks, registration, or de-registration. It provides flexibility to the user to have individual lock and protection mechanisms.&lt;br /&gt;
&lt;br /&gt;
Linux uses the raw notifier chain in low-level events. &lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

297 int raw_notifier_chain_register(struct raw_notifier_head *nh,
298         struct notifier_block *n)
299 {
300     return notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
301 }
302 EXPORT_SYMBOL_GPL(raw_notifier_chain_register);
.
314 int raw_notifier_chain_unregister(struct raw_notifier_head *nh,
315         struct notifier_block *n)
316 {
317     return notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
318 }
319 EXPORT_SYMBOL_GPL(raw_notifier_chain_unregister);&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;SRCU Notifier chains&amp;nbsp;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;Sleepable Read Copy Update (SRCU)&lt;/b&gt; notifier chains are similar to the blocking notifier chain and run in the process context. It differs in the way it handles locking and protection. The SRCU methodology brings in less overhead when we notify the registered callers. On the flip side, it consumes more resource while unregistering. So it is advisable to choose this methodology where we use the notifier call often and where there’s very little requirement for removing from the chain.&amp;nbsp;
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

370 int srcu_notifier_chain_register(struct srcu_notifier_head *nh,
371         struct notifier_block *n)
372 {
.
383     mutex_lock(&amp;amp;nh-&amp;gt;mutex);
384     ret = notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
385     mutex_unlock(&amp;amp;nh-&amp;gt;mutex);
386     return ret;
387 }
388 EXPORT_SYMBOL_GPL(srcu_notifier_chain_register)
.
400 int srcu_notifier_chain_unregister(struct srcu_notifier_head *nh,
401         struct notifier_block *n)
402 {
.
413     mutex_lock(&amp;amp;nh-&amp;gt;mutex);
414     ret = notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
415     mutex_unlock(&amp;amp;nh-&amp;gt;mutex);
416     synchronize_srcu(&amp;amp;nh-&amp;gt;srcu);
417     return ret;
418 }
419 EXPORT_SYMBOL_GPL(srcu_notifier_chain_unregister);&lt;/pre&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;Registering and Un-registering with a Chain&amp;nbsp;&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
When a kernel component is interested in the events of a given notification chain, it can register it with the general function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_chain_register&lt;/span&gt; and to unregister we can use the function &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_chain_unregister&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;b style="color: red;"&gt;NOTE:&lt;/b&gt; The kernel also provides a set of wrappers around &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_chain_register&lt;/span&gt;. We might have to use those wrapper functions instead of directly using the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notification_chain_register.&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

 20 static int notifier_chain_register(struct notifier_block **nl,
 21         struct notifier_block *n)
 22 {
 23     while ((*nl) != NULL) {
 24         if (n-&amp;gt;priority &amp;gt; (*nl)-&amp;gt;priority)
 25             break;
 26         nl = &amp;amp;((*nl)-&amp;gt;next);
 27     }
 28     n-&amp;gt;next = *nl;
 29     rcu_assign_pointer(*nl, n);
 30     return 0;
 31 }&lt;/pre&gt;
&lt;br /&gt;
For each chain, the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_block&lt;/span&gt; instances are inserted into a list, which is sorted by priority. Elements with the same priority are sorted based on insertion time: new ones go to the tail.

Accesses to the notification chains are protected by the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_lock&lt;/span&gt; lock. The use of a single lock for all the notification chains is not a big constraint and does not affect performance, because subsystems usually register their &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_call&lt;/span&gt; functions only at boot time or at module load time, and from that moment on access the lists in a read-only manner (that is, shared).&lt;br /&gt;
&lt;br /&gt;
Because the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_chain_register&lt;/span&gt; function is called to insert callbacks into all lists, it requires that the list be specified as an input parameter. However, this function is rarely called directly; generic wrappers are used instead.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

 33 static int notifier_chain_unregister(struct notifier_block **nl,
 34         struct notifier_block *n)
 35 {
 36     while ((*nl) != NULL) {
 37         if ((*nl) == n) {
 38             rcu_assign_pointer(*nl, n-&amp;gt;next);
 39             return 0;
 40         }
 41         nl = &amp;amp;((*nl)-&amp;gt;next);
 42     }
 43     return -ENOENT;
 44 }&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=OfnjyIlDWfM:g236sv3ybiM:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=OfnjyIlDWfM:g236sv3ybiM:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=OfnjyIlDWfM:g236sv3ybiM:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/OfnjyIlDWfM" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-03-03T10:14:22.416+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Notification Chains in Linux Kernel - Part 01</title><link>http://codingfreak.blogspot.com/2012/01/notification-chains-in-linux-part-01.html</link><category>Linux</category><category>Kernel</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Mon, 16 Jan 2012 05:03:00 PST</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-6847357699726064053</guid><description>Linux is a monolithic kernel. Its subsystems or modules help to keep the kernel light by being flexible enough to load and unload at runtime. In most cases, the kernel modules are interconnected to one another. An event captured by a certain module might be of interest to another module. &lt;br /&gt;
&lt;br /&gt;
Typically, communication systems implement request-reply messaging, or polling. In such models, a program that receives a request will have to send the data available since the last transaction. Such methods sometimes require high bandwidth or they waste polling cycles.&lt;br /&gt;
&lt;br /&gt;
To fulfill the need for interaction, Linux uses so called &lt;b&gt;notification chains&lt;/b&gt;. These notifier chains work in a &lt;b&gt;Publish-Subscribe model&lt;/b&gt;. This model is more effective when compared to polling or the request-reply model.&lt;br /&gt;
&lt;br /&gt;
For each notification chain there is a passive side (&lt;b&gt;the notified&lt;/b&gt;) and an active side (&lt;b&gt;the notifier&lt;/b&gt;), as in the so-called publish-and-subscribe model:&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;The notified are the subsystems that ask to be notified about the event and that provide a callback function to invoke.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;The notifier is the subsystem that experiences an event and calls the callback function.&lt;/li&gt;
&lt;/ul&gt;
&lt;b style="color: #cc0000;"&gt;NOTE:&lt;/b&gt; All the code samples are taken from Linux 2.6.24 kernel.
&lt;br /&gt;&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: large;"&gt;struct notifier_block&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
The elements of the notification chain's list are of type &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_block&lt;/span&gt;:&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;include/linux/notifier.h&amp;gt;

 50 struct notifier_block {
 51     int (*notifier_call)
(struct notifier_block *, unsigned long, void *);
 52     struct notifier_block *next;
 53     int priority;
 54 };
&lt;/pre&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_call&lt;/span&gt; - function to execute.&amp;nbsp;&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;next&lt;/span&gt; - used to link together the elements of the list.&lt;/li&gt;
&lt;/ul&gt;
&lt;ul&gt;
&lt;li&gt;&lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;priority&lt;/span&gt; - the priority of the function. Functions with higher priority are executed first. But in practice, almost all registrations leave the priority out of the notifier_block definition, which means it gets the default value of 0 and execution order ends up depending only on the registration order (i.e., it is a semirandom order). 
&lt;/li&gt;
&lt;/ul&gt;
The &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;notifier_block&lt;/span&gt; data structure is a simple linked list of function pointers. The function pointers are registered with ‘functions’ that are to be called when an event occurs. Each module needs to maintain a notifier list. The functions are registered to this notification list.

The notification module (publisher) maintains a list head that is used to manage and traverse the notifier block list. The function that subscribes to a module is added to the head of the module’s list by using the &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;register_xxxxxx_notifier&lt;/span&gt; API and deletion from the list is done using &lt;span style="font-family: &amp;quot;Courier New&amp;quot;,Courier,monospace;"&gt;unregister_xxxxxx_notifier&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;&lt;b&gt;&lt;span style="font-size: large;"&gt;Types of Notifier Chains&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
Notifier chains are broadly classified based on the context in which they are executed and the lock/protect mechanism of the calling chain. Based on the need of the module, the notifiers can be executed in the process context or interrupt/atomic context. Thus, notifier chains are classified into four types:
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;span style="font-size: small;"&gt;Atomic Notifier Chains&lt;/span&gt;&lt;/b&gt;&lt;br /&gt;
As the name indicates, this notifier chain is executed in interrupt or atomic context. Normally, events that are time critical use this notifier. This also means it is a non-blockable call. Linux modules use atomic notifier chains to inform watchdog timers or message handlers.
&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&amp;lt;kernel/notifier.c&amp;gt;

 96 int atomic_notifier_chain_register(struct atomic_notifier_head *nh,
 97         struct notifier_block *n)
 98 {
.
102     spin_lock_irqsave(&amp;amp;nh-&amp;gt;lock, flags);
103     ret = notifier_chain_register(&amp;amp;nh-&amp;gt;head, n);
104     spin_unlock_irqrestore(&amp;amp;nh-&amp;gt;lock, flags);
105     return ret;
106 }
107 EXPORT_SYMBOL_GPL(atomic_notifier_chain_register);
.
118 int atomic_notifier_chain_unregister(struct atomic_notifier_head *nh,
119         struct notifier_block *n)
120 {
.
124     spin_lock_irqsave(&amp;amp;nh-&amp;gt;lock, flags);
125     ret = notifier_chain_unregister(&amp;amp;nh-&amp;gt;head, n);
126     spin_unlock_irqrestore(&amp;amp;nh-&amp;gt;lock, flags);
127     synchronize_rcu();
128     return ret;
129 }
130 EXPORT_SYMBOL_GPL(atomic_notifier_chain_unregister);&lt;/pre&gt;
Check the &lt;a href="http://codingfreak.blogspot.in/2012/01/notification-chains-in-linux-kernel.html"&gt;PART-2&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=7PZHlz24aEY:tVbCQOSzAjo:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=7PZHlz24aEY:tVbCQOSzAjo:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=7PZHlz24aEY:tVbCQOSzAjo:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/7PZHlz24aEY" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-02-12T10:42:15.125+05:30</atom:updated><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Printing logs based on log levels in C</title><link>http://codingfreak.blogspot.com/2010/08/printing-logs-based-on-log-levels-in-c.html</link><category>C</category><category>Programming</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Sat, 07 Aug 2010 07:35:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-7501749357954447631</guid><description>&lt;span style="color: red; font-weight: bold;"&gt;LOG LEVELS ??&lt;/span&gt;&lt;br /&gt;
As per my definition LOG LEVEL means a way to differentiate the importance of logs in our application. We can divide the logs into categories based on their importance and effect for e.g. &lt;span style="font-weight: bold;"&gt;ERROR&lt;/span&gt; logs are more important than &lt;span style="font-weight: bold;"&gt;DEBUG&lt;/span&gt; logs.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://3.bp.blogspot.com/-zeyRkAkwpoE/UNg3x7sTTyI/AAAAAAAACZE/sKxV5m785kU/s1600/01.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://3.bp.blogspot.com/-zeyRkAkwpoE/UNg3x7sTTyI/AAAAAAAACZE/sKxV5m785kU/s1600/01.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;span style="color: red; font-weight: bold;"&gt;Why do we need to print logs based on LOG LEVEL ??&lt;/span&gt;&lt;br /&gt;
It is really helpful in projects with millions of lines of source code where the user can't use &lt;span style="font-weight: bold;"&gt;#defines&lt;/span&gt; or #ifdef's in order to maintain DEBUG prints. It is really tiresome to maintain &lt;span style="font-weight: bold;"&gt;#defines&lt;/span&gt; and #ifdef atleast for printing logs.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;printk&lt;/span&gt; which is a part of LINUX KERNEL supports printing logs based on LOG LEVEL and it is really helpful in debugging kernel.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-weight: bold;"&gt;printf&lt;/span&gt; or any of its brothers &amp;amp; sisters don't support the option to print logs depending upon the log levels.&lt;br /&gt;
&lt;br /&gt;
&lt;a name='more'&gt;&lt;/a&gt;I have written a sample program where we can create our own printf function with LOG LEVEL. At present I am just supporting the option to print a character, string and a integer.&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdarg.h&amp;gt;

//LOG LEVELS
typedef enum
{
  LOG_DEFAULT,
  LOG_INFO,
  LOG_ERROR,
  LOG_DEBUG
}LOG_LEVEL;
 
void LOG_TRACE(LOG_LEVEL lvl, char *fmt, ... );

int main()
{
  int i =10;
  char *string="Hello World";
  char c='a';
       
  LOG_TRACE(LOG_INFO, "String - %s\n", string);
  LOG_TRACE(LOG_DEBUG, "Integer - %d\n", i);
  LOG_TRACE(LOG_INFO, "Character - %c\n", c);
       
  LOG_TRACE(LOG_INFO, "\nTOTAL DATA: %s - %d - %c\n", string, i, c);
  return 1;
}

/* LOG_TRACE(log level, format, args ) */
void LOG_TRACE(LOG_LEVEL lvl, char *fmt, ... )
{
  va_list  list;
  char *s, c;
  int i;

  if( (lvl==LOG_INFO) || (lvl==LOG_ERROR))
  {
     va_start( list, fmt );

     while(*fmt)
     {
        if ( *fmt != '%' )
           putc( *fmt, stdout );
        else
        {
           switch ( *++fmt )
           {
              case 's':
                 /* set r as the next char in list (string) */
                 s = va_arg( list, char * );
                 printf("%s", s);
                 break;

              case 'd':
                 i = va_arg( list, int );
                 printf("%d", i);
                 break;

              case 'c':
                 c = va_arg( list, int);
                 printf("%c",c);
                 break;

              default:
                 putc( *fmt, stdout );
                 break;
           }
        }
        ++fmt;
     }
     va_end( list );
  }
  fflush( stdout );
}&lt;/pre&gt;
&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Pawu7Y-PZT0:IYzQVWbVWQ4:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=Pawu7Y-PZT0:IYzQVWbVWQ4:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=Pawu7Y-PZT0:IYzQVWbVWQ4:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/Pawu7Y-PZT0" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-12-24T16:39:32.334+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://3.bp.blogspot.com/-zeyRkAkwpoE/UNg3x7sTTyI/AAAAAAAACZE/sKxV5m785kU/s72-c/01.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">0</thr:total></item><item><title>Implementation of Stack using Singly Linked Lists</title><link>http://codingfreak.blogspot.com/2010/07/implementation-of-stack-using-singly.html</link><category>C</category><category>Linked List</category><category>DataStructures</category><category>Stack</category><author>noreply@blogger.com (Ajith Adapa)</author><pubDate>Wed, 07 Jul 2010 00:02:00 PDT</pubDate><guid isPermaLink="false">tag:blogger.com,1999:blog-4070434216759110691.post-1125104915856385785</guid><description>&lt;span style="font-size: 130%;"&gt;S&lt;/span&gt;tacks are linear data structures which means the data is stored in what looks like a line (although vertically). In simple words we can say&lt;br /&gt;
&lt;blockquote&gt;
&lt;span style="font-style: italic;"&gt;A stack is a last in, first out (LIFO) abstract data type and data structure.&lt;/span&gt;&lt;/blockquote&gt;
Basic usage of stack at the Architecture level is as a means of allocating and accessing memory.&lt;br /&gt;
&lt;br /&gt;
&lt;div class="separator" style="clear: both; text-align: center;"&gt;
&lt;a href="http://4.bp.blogspot.com/-kcNBSSIB6E8/UNg4ybZwipI/AAAAAAAACZU/C30N-G0u0_o/s1600/92.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"&gt;&lt;img border="0" src="http://4.bp.blogspot.com/-kcNBSSIB6E8/UNg4ybZwipI/AAAAAAAACZU/C30N-G0u0_o/s1600/92.jpg" /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
We can only perform two fundamental operations on a stack: &lt;span style="font-weight: bold;"&gt;push&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;pop&lt;/span&gt;.&lt;br /&gt;
&lt;br /&gt;
The push operation adds to the top of the list, hiding any items already on the stack, or initializing the stack if it is empty. The pop operation removes an item from the top of the list, and returns this value to the caller. A pop either reveals previously concealed items, or results in an empty list.&lt;br /&gt;
&lt;br /&gt;
A stack is a &lt;span style="font-style: italic;"&gt;restricted data structure&lt;/span&gt;, because only a small number of operations are performed on it.&lt;br /&gt;
&lt;span class="fullpost"&gt;&lt;br /&gt;Let us 'C' code for implementing a stack using Singly Linked Lists.&lt;/span&gt;&lt;br /&gt;
&lt;pre class="cpp" name="code"&gt;&lt;span class="fullpost"&gt;#include &amp;lt;stdio.h&amp;gt;
#include &amp;lt;stdlib.h&amp;gt;

//Structure containing a Data part &amp;amp; a
//Link part to the next node in the List
struct Node
{
int Data;
struct Node *Next;
}*Head;

//Poping from a Stack
void popStack()
{
struct Node *tmp_ptr=NULL, *cur_ptr=Head;

if(cur_ptr)
{
   Head = Head-&amp;gt;Next;
   free(cur_ptr);
}     
else  
   printf("\nStack is Empty");
}   

//Pushing into Stack
void pushIntoStack(int num)
{
struct Node *tmp_ptr=NULL;

if((tmp_ptr=(struct Node *)malloc(sizeof(struct Node))) != NULL)
   tmp_ptr-&amp;gt;Data=num;
else
   printf("\nMemory Allocation Failed");

if(Head)
{
   tmp_ptr-&amp;gt;Next=Head;
   Head=tmp_ptr;
}
else { //List is Empty
   Head=tmp_ptr;
   Head-&amp;gt;Next=NULL;
}
}

//Displaying Stack
void displayStack()
{
struct Node *cur_ptr=Head;

if(cur_ptr)
{
    printf("\nElements in Stack:\n");
    while(cur_ptr)
    {
        printf("\t%d\n",cur_ptr-&amp;gt;Data);
        cur_ptr=cur_ptr-&amp;gt;Next;
    }
    printf("\n");
}
else
   printf("\nStack is Empty");
}

int main(int argc, char *argv[])
{
int i=0;

//Set HEAD as NULL
Head=NULL;

while(1)
{
  printf("\n####################################################\n");
  printf("MAIN MENU\n");
  printf("####################################################\n");
  printf(" \n1. Push into stack");
  printf(" \n2. Pop from Stack");
  printf(" \n3. Display Stack");
  printf(" \n4. Exit\n");
  printf(" \nChoose Option: ");
  scanf("%d",&amp;amp;i);

  switch(i)
  {
    case 1:
    {
        int num;
        printf("\nEnter a Number to push into Stack: ");
        scanf("%d",&amp;amp;num);
        pushIntoStack(num);
        displayStack();
        break;
    }

    case 2:
    {
        popStack();
        displayStack();
        break;
    }

    case 3:
    {
        displayStack();
        break;
    }

    case 4:
    {
        struct Node *temp;

        while(Head!=NULL)
        {
            temp = Head-&amp;gt;Next;
            free(Head);
            Head=temp;
        }
        exit(0);
    }

    default:
    {
        printf("\nWrong Option choosen");
    }
  }/* end if switch */
}/* end of while */
}/* end of main */&lt;/span&gt;&lt;/pre&gt;
&lt;span class="fullpost"&gt;&lt;span style="font-weight: bold;"&gt;References:&lt;/span&gt;&lt;br /&gt;1. &lt;a href="http://en.wikipedia.org/wiki/Stack_%28data_structure%29"&gt;Wikipedia&lt;/a&gt;&lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;hr/&gt;&lt;a href="http://codingfreak.blogspot.com"&gt;CodingFreak&lt;/a&gt;

&lt;script type="text/javascript"&gt;&lt;!--
google_ad_client = "pub-3238413467491229";
/* 234x60, created 1/30/08 */
google_ad_slot = "9553173798";
google_ad_width = 234;
google_ad_height = 60;
google_cpa_choice = ""; // on file
//--&gt;
&lt;/script&gt;
&lt;script 
src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"&gt;
&lt;/script&gt;&lt;/div&gt;&lt;div class="feedflare"&gt;
&lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:yIl2AUoC8zA"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=yIl2AUoC8zA" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:qj6IDK7rITs"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?d=qj6IDK7rITs" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:F7zBnMyn0Lo"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=SsLRfx9qm5s:wFMV06UCxaw:F7zBnMyn0Lo" border="0"&gt;&lt;/img&gt;&lt;/a&gt; &lt;a href="http://feeds.feedburner.com/~ff/codingfreak?a=SsLRfx9qm5s:wFMV06UCxaw:V_sGLiPBpWU"&gt;&lt;img src="http://feeds.feedburner.com/~ff/codingfreak?i=SsLRfx9qm5s:wFMV06UCxaw:V_sGLiPBpWU" border="0"&gt;&lt;/img&gt;&lt;/a&gt;
&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/codingfreak/~4/SsLRfx9qm5s" height="1" width="1"/&gt;</description><atom:updated xmlns:atom="http://www.w3.org/2005/Atom">2012-12-24T16:43:54.849+05:30</atom:updated><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-kcNBSSIB6E8/UNg4ybZwipI/AAAAAAAACZU/C30N-G0u0_o/s72-c/92.jpg" height="72" width="72" /><thr:total xmlns:thr="http://purl.org/syndication/thread/1.0">1</thr:total></item></channel></rss>
