<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-1254145323936187668</id><updated>2026-10-15T00:17:31.467-07:00</updated><category term="#operatingsystem"/><category term="#datastructures"/><category term="#technology"/><category term="#Programming"/><category term="#gaming"/><category term="#google"/><category term="#webdevelopment"/><category term="#AI"/><category term="#Apple"/><category term="#datascience"/><category term="#malware"/><category term="#operating system"/><title type='text'>GeekTech</title><subtitle type='html'>Geektech The #1 place for all the latest in geek culture from technology, Programming, gaming, and more.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default?start-index=26&amp;max-results=25&amp;redirect=false'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>110</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-3309908604439515265</id><published>2021-07-09T00:02:00.007-07:00</published><updated>2021-07-10T23:18:56.182-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#technology"/><title type='text'>What Is Cryptocurrency? Here Is Everything You Need To Know About Cryptocurrency</title><content type='html'>&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9jKtXlFACbRz0FIUktSOIQCt_msHiMO-E8QJPw0gZo0pHuRDkKtQhy7ZDjpEs97lAS5AlX-xCpIeYNNM0JDvZCdIqTWpCvfwzvFhNR7zCdzB6r1YK-NxsbIPDaQ3Vi1JPB5AluAyXgRwi/s2048/pexels-worldspectrum-844124.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;What Is Cryptocurrency ? Here Is Everything You Need To Know About Cryptocurrency&quot; border=&quot;0&quot; data-original-height=&quot;1263&quot; data-original-width=&quot;2048&quot; height=&quot;394&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9jKtXlFACbRz0FIUktSOIQCt_msHiMO-E8QJPw0gZo0pHuRDkKtQhy7ZDjpEs97lAS5AlX-xCpIeYNNM0JDvZCdIqTWpCvfwzvFhNR7zCdzB6r1YK-NxsbIPDaQ3Vi1JPB5AluAyXgRwi/w640-h394/pexels-worldspectrum-844124.webp&quot; title=&quot;What Is Cryptocurrency ? Here Is Everything You Need To Know About Cryptocurrency&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;What Is Cryptocurrency? Here Is Everything You Need To Know About Cryptocurrency&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 16.1px;&quot;&gt;A cryptocurrency&amp;nbsp;is a form of currency but the difference between cryptocurrency and a regular currency which we use in our daily lives is that the former is digital and decentralized. What has led to cryptocurrencies gaining attention from everywhere around is the fact that they can be stored and witness growth exponentially in value. This might be a surprise for many – well, there are not one, not two, not tens but thousands of cryptocurrencies. But some popular cryptocurrencies are&amp;nbsp;&lt;/span&gt;&lt;a href=&quot;https://www.coindesk.com/price/bitcoin&quot; rel=&quot;noopener&quot; style=&quot;background: rgb(255, 255, 255); border: 0px; font-size: 16.1px; margin: 0px; outline: 0px; padding: 0px; text-decoration-line: none; transition: all 0.2s ease 0s; vertical-align: baseline;&quot; target=&quot;_blank&quot;&gt;Bitcoin&lt;/a&gt;, &lt;a href=&quot;https://www.coindesk.com/price/ethereum&quot; target=&quot;_blank&quot;&gt;Ethereum&lt;/a&gt;, &lt;a href=&quot;https://www.coindesk.com/price/xrp&quot; target=&quot;_blank&quot;&gt;XRP&lt;/a&gt;,&amp;nbsp; &lt;a href=&quot;https://www.coindesk.com/price/cardano&quot; target=&quot;_blank&quot;&gt;Cardano&lt;/a&gt;, &lt;a href=&quot;https://www.coindesk.com/price/litecoin&quot; target=&quot;_blank&quot;&gt;Litecoin&lt;/a&gt;&lt;span style=&quot;background-color: white; font-size: 16.1px;&quot;&gt;. Some of the cryptocurrencies are used instead of cash while some are used for private, direct transactions.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 16.1px;&quot;&gt;Cryptocurrencies work using a technology called the &lt;a href=&quot;https://geektech1717.blogspot.com/2019/05/what-is-blockchain-technology.html&quot; target=&quot;_blank&quot;&gt;Blockchain&lt;/a&gt;. Blockchain is a decentralized technology spread across many computers that manage and records transactions. Part of the appeal of Blockchain is its security and assurance.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 16.1px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 16.1px;&quot;&gt;&lt;b&gt;Learn More&lt;/b&gt;: &lt;a href=&quot;https://geektech1717.blogspot.com/2019/05/what-is-blockchain-technology.html&quot; target=&quot;_blank&quot;&gt;What is Blockchain Technology?&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white;&quot;&gt;
  
  &lt;script async src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-format=&quot;fluid&quot;
     data-ad-layout-key=&quot;-6t+ed+2i-1n-4w&quot;
     data-ad-client=&quot;ca-pub-6736665307191368&quot;
     data-ad-slot=&quot;6343911603&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
  
  
  
  &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;Why invest in cryptocurrencies?&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;b&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;Cryptocurrencies are similar to stocks and real estate. While all can be traded in a centralized fashion, they are different in a few significant ways. Cryptocurrencies, such as Bitcoin, are decentralized. While this may seem like a negative, it actually makes the market more efficient and secure, as there is no one entity (government, company, or individual) controlling and controlling the price. Trading and holding cryptocurrency has some major advantages. First, there are no transaction fees.&amp;nbsp; Transactions are very quick, very secure, and very cheap. It can be worth $0.01 to $0.02, making it a very easy decision to buy an extra $5 to $50 for yourself or to trade for a profit.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;Another common reason to invest in cryptocurrency is the desire for a reliable, long-term store of value. Unlike&amp;nbsp;&lt;/span&gt;&lt;a data-component=&quot;link&quot; data-ordinal=&quot;1&quot; data-source=&quot;inlineLink&quot; data-type=&quot;internalLink&quot; href=&quot;https://www.investopedia.com/terms/f/fiatmoney.asp&quot; style=&quot;background-color: white; box-sizing: border-box;&quot;&gt;fiat money&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;, most cryptocurrencies have a limited supply, capped by mathematical algorithms. This makes it impossible for any political body or government agency to dilute their value through&amp;nbsp;&lt;/span&gt;&lt;a data-component=&quot;link&quot; data-ordinal=&quot;2&quot; data-source=&quot;inlineLink&quot; data-type=&quot;internalLink&quot; href=&quot;https://www.investopedia.com/articles/investing/081415/understanding-how-federal-reserve-creates-money.asp&quot; style=&quot;background-color: white; box-sizing: border-box;&quot;&gt;inflation&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. Moreover, due to the cryptographic nature of cryptocurrencies, it is impossible for a government body to tax or confiscate tokens without the cooperation of the owner.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;b&gt;See Also&lt;/b&gt;: &lt;a href=&quot;https://geektech1717.blogspot.com/2019/11/the-z-guide-of-impact-of-5g-technology.html&quot; target=&quot;_blank&quot;&gt;Impact of 5G technology in our lives&amp;nbsp;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;

&lt;script async src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-format=&quot;fluid&quot;
     data-ad-layout-key=&quot;-6t+ed+2i-1n-4w&quot;
     data-ad-client=&quot;ca-pub-6736665307191368&quot;
     data-ad-slot=&quot;6343911603&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;


&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;b&gt;How to buy cryptocurrency&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;It’s important to note that there is no exchange that is 100% secure and 100% secure is also very hard to achieve. This is not to say that a trader should avoid using a reputable trading platform, but it is worth noting that a user’s identity and account credentials are always at risk. However, because cryptocurrencies are not regulated, the online exchanges you use to trade the various tokens are not required to use the most secure security practices. Here are some basic rules to follow when purchasing cryptocurrencies:&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;1.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;Choose an Exchange&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;span style=&quot;background-color: white;&quot;&gt;There are lots of&amp;nbsp;&lt;/span&gt;&lt;a data-component=&quot;link&quot; data-ordinal=&quot;1&quot; data-source=&quot;inlineLink&quot; data-type=&quot;internalLink&quot; href=&quot;https://www.hedgewithcrypto.com/best-cryptocurrency-exchanges-usa/&quot; style=&quot;background-color: white; box-sizing: border-box;&quot; target=&quot;_blank&quot;&gt;cryptocurrency exchanges&lt;/a&gt;&lt;span style=&quot;background-color: white;&quot;&gt;. Due to decentralization and individual privacy, some exchanges even allow users to remain anonymous and do not require users to submit their personal information. Such kinds of exchanges operate autonomously and, typically, are decentralized which means they do not have an origin of control.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;In today&#39;s scenario, however, the popular and renowned exchanges are not decentralized and they require KYC. In the United States&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Arvo; font-size: 17.6px;&quot;&gt;, these exchanges include &lt;a href=&quot;https://www.investopedia.com/tech/coinbase-what-it-and-how-do-you-use-it/&quot; target=&quot;_blank&quot;&gt;Coinbase&lt;/a&gt;, &lt;a href=&quot;https://www.investopedia.com/tech/gemini-winklevoss-bitcoin-exchange/&quot; target=&quot;_blank&quot;&gt;Gemini&lt;/a&gt;, &lt;a href=&quot;https://www.investopedia.com/tech/what-kraken/&quot; target=&quot;_blank&quot;&gt;Kraken&lt;/a&gt;. These exchanges have grown significantly over the last few years and in the number of benefits they offer.&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-family: Arvo; font-size: 17.6px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-family: Arvo; font-size: 17.6px;&quot;&gt;2. Connect Your Exchange to a Payment Gateway&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;Once you have chosen an exchange, now you have to submit your personal documents. These documents may include a driver&#39;s license, social security number, as well as information about your employment or other sources of income. The information you submit may need can depend on the country/region you live in and the laws within it.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;After the exchange verifies your documents and legitimacy you may now connect a payment option. At most exchanges, you can connect your bank account directly or you a debit or credit card.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;3. Place an Order&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;After completing the above steps now you can buy Bitcoin and other cryptocurrencies. In recent years, cryptocurrency exchanges have slowly become more mainstream. They have grown exponentially in terms of liquidity and their breadth of features. The operational changes at cryptocurrency exchanges parallel the change in perception of cryptocurrencies. An industry that was once thought of as a scam or one with questionable practices is slowly growing into a legit one that has drawn interest from all the big players in the financial industry.&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;4. Store Cryptocurrency in Wallet&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;Cryptocurrency wallets are a special kind of wallet that stores your cryptocurrency more securely. Having your crypto in your personal wallet ensures that only you have control over the private key to your funds. It also gives you the ability to store funds away from an exchange and prevent the risk of your cryptocurrency or exchange getting hacked. Some Popular wallets are:&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;/p&gt;&lt;ul style=&quot;text-align: left;&quot;&gt;&lt;li&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;Mycelium (&lt;a href=&quot;https://mycelium.com&quot;&gt;https://mycelium.com&lt;/a&gt;),&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;Coinbase (&lt;a href=&quot;https://www.coinbase.com&quot;&gt;https://www.coinbase.com&lt;/a&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;li&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;Bitstamp (&lt;a href=&quot;https://www.bitstamp.com&quot;&gt;https://www.bitstamp.com&lt;/a&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white; font-size: 17.6px;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&lt;b&gt;See Also&lt;/b&gt;:&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;a href=&quot;https://geektech1717.blogspot.com/2019/09/what-is-cloud-computing.html&quot; target=&quot;_blank&quot;&gt;What is Cloud Computing?&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white; font-size: medium;&quot;&gt;
  
  &lt;script async src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block&quot;
     data-ad-format=&quot;fluid&quot;
     data-ad-layout-key=&quot;-6t+ed+2i-1n-4w&quot;
     data-ad-client=&quot;ca-pub-6736665307191368&quot;
     data-ad-slot=&quot;6343911603&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
  
  &lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span color=&quot;var(--post-title-color)&quot; style=&quot;background-color: white; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;b&gt;How to Trade Cryptocurrency&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;How to Buy Cryptocurrency?&amp;nbsp; How to buy&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Arvo;&quot;&gt;Cryptocurrency&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&amp;nbsp;with a credit card? How to buy&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Arvo;&quot;&gt;Cryptocurrency&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&amp;nbsp;without a credit card? How to Sell&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white; font-family: Arvo;&quot;&gt;Cryptocurrency?&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;&amp;nbsp; &lt;/span&gt;&lt;span style=&quot;font-family: Arvo;&quot;&gt;The three main apps you will need are &lt;a href=&quot;https://www.coinbase.com/&quot; target=&quot;_blank&quot;&gt;Coinbase&lt;/a&gt; (the most popular and easiest to use), &lt;a href=&quot;https://www.bitgo.com/&quot; target=&quot;_blank&quot;&gt;BitGo&lt;/a&gt; (more secure, but more expensive), and &lt;a href=&quot;https://www.kraken.com/&quot; target=&quot;_blank&quot;&gt;Kraken Exchange&lt;/a&gt; (more user-friendly and cheap). Coinbase is by far the most popular and easiest to use exchange because it is one of the most popular and most used exchanges. Coinbase works directly with various banking institutions and exchanges to allow users to transfer money between their bank accounts, from their wallet to Coinbase, and to other exchanges. Coinbase also offers much more transparency, because you can see where all your money goes and what you’re investing in.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;background-color: white;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;b&gt;Should I buy cryptocurrency now?&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: inherit; margin: 0px 0px 0.75em;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;So you are interested in buying cryptocurrency, and you are confused about when&amp;nbsp;&lt;em style=&quot;box-sizing: inherit;&quot;&gt;should&amp;nbsp;&lt;/em&gt;you buy it? To be honest,&amp;nbsp; it doesn&#39;t really matter - until and unless you&#39;re strategic about it.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: inherit; margin: 0px 0px 0.75em;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;The key to earning money in stocks is to buy strong investments and hold them for a long time of period. If the investments were really good, they should grow over time, and their prices will increase automatically.&lt;/span&gt;&lt;/p&gt;&lt;p data-uw-rm-sr=&quot;&quot; style=&quot;background-color: white; box-sizing: inherit; margin: 0px 0px 0.75em;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;The same principle is applied to cryptocurrency. If you think cryptocurrency has a bright future, it doesn&#39;t necessarily matter whether you buy when Bitcoin costs $60,000 or $30,000 per token. If it ends up reaching, say,&amp;nbsp;$500,000 per token&amp;nbsp;someday, you&#39;ll make a hefty profit regardless.&lt;/span&gt;&lt;/p&gt;&lt;p style=&quot;background-color: white; box-sizing: inherit; margin: 0px 0px 0.75em;&quot;&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;There is no guarantee that Bitcoin or any cryptocurrency will succeed. But if you&#39;re going to invest in them, it should be because you believe in their potential and are willing to hold your investments for the long term. If you&#39;re only investing to try to make a quick buck, then my friend it is risky you&#39;ll most likely end up losing more than you earn.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-size: medium;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;b&gt;Conclusion&lt;/b&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;A new day has arrived and with that a new level of growth and awareness within the Bitcoin and cryptocurrency community. In the last few years, cryptocurrencies have grown at an exponential rate and have reached a tipping point in the industry. But it does not mean that if you buy cryptocurrency you will be Bill Gates or Jeff Bezos in a year or two. You need to be patient and you should invest in cryptocurrency if and only if you have the potential to hold your investments for the long term.&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;i&gt;Please share your thoughts about cryptocurrency below!!&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;span style=&quot;font-family: Arvo; font-size: medium;&quot;&gt;&lt;em style=&quot;border: 0px; box-sizing: border-box; color: #33475b; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;Want to learn more about Technology and Programming?&amp;nbsp;&lt;/em&gt;&lt;a href=&quot;https://geektech1717.blogspot.com/&quot; target=&quot;_blank&quot;&gt;Check out this amazing&amp;nbsp;blog&lt;em style=&quot;border: 0px; box-sizing: border-box; color: #33475b; font-stretch: inherit; font-variant-east-asian: inherit; font-variant-numeric: inherit; line-height: inherit; margin: 0px; padding: 0px; vertical-align: baseline;&quot;&gt;.&quot;&lt;/em&gt;&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/3309908604439515265/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2021/07/what-is-cryptocurrency-here-is.html#comment-form' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3309908604439515265'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3309908604439515265'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2021/07/what-is-cryptocurrency-here-is.html' title='What Is Cryptocurrency? Here Is Everything You Need To Know About Cryptocurrency'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj9jKtXlFACbRz0FIUktSOIQCt_msHiMO-E8QJPw0gZo0pHuRDkKtQhy7ZDjpEs97lAS5AlX-xCpIeYNNM0JDvZCdIqTWpCvfwzvFhNR7zCdzB6r1YK-NxsbIPDaQ3Vi1JPB5AluAyXgRwi/s72-w640-h394-c/pexels-worldspectrum-844124.webp" height="72" width="72"/><thr:total>4</thr:total><georss:featurename>Schaffer Rd, Rush Center, KS 67575, USA</georss:featurename><georss:point>38.3702208 -99.088085499999991</georss:point><georss:box>-11.838831960637485 -169.40058549999998 88.579273560637489 -28.775585499999991</georss:box></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-2210743156530946547</id><published>2020-09-30T06:54:00.002-07:00</published><updated>2021-06-09T23:53:28.247-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Program to Reverse a Queue - Code Explained </title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;Today we will be discussing the&amp;nbsp;&lt;strong&gt;Program to Reverse a Queue - Code Explained&lt;/strong&gt;. But before reading further if you are unfamiliar with the concept of queue then refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-are-queues-in-data-structures.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt;. Suppose we have a queue 1-&amp;gt;2-&amp;gt;3-&amp;gt;4 then after reversing it should look like 4-&amp;gt;3-&amp;gt;2-&amp;gt;1. Now let&#39;s discuss the procedure:&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;Procedure&lt;/h3&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;1. First we will make a queue and insert some values in it (&lt;b&gt;data structures types&lt;/b&gt;).&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;2. Now make a function to reverse the queue. In that function we will iterate the list from the rear until we reach the front of the queue.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;3. This will print the queue in reverse order.&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSARQ5KspJpAO8ODnu3ciaDlC1KVBexwzEDHxcd7D5XT37MboyEc5SMWJktud7KRODYe-5E6N3fDT1biz5Sv-Es616nm2L5XQDSm7YkoSaY2lHKy8AO6o19gZbLU53OOxvN08bGI1YTANZ/s859/ququ.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Program to Reverse a Queue - Code Explained&quot; border=&quot;0&quot; data-original-height=&quot;295&quot; data-original-width=&quot;859&quot; height=&quot;166&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSARQ5KspJpAO8ODnu3ciaDlC1KVBexwzEDHxcd7D5XT37MboyEc5SMWJktud7KRODYe-5E6N3fDT1biz5Sv-Es616nm2L5XQDSm7YkoSaY2lHKy8AO6o19gZbLU53OOxvN08bGI1YTANZ/w483-h166/ququ.webp&quot; title=&quot;Program to Reverse a Queue - Code Explained&quot; width=&quot;483&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/h3&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;
  &lt;script async=&quot;&quot; src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot; data-ad-client=&quot;ca-pub-6736665307191368&quot; data-ad-format=&quot;fluid&quot; data-ad-layout=&quot;in-article&quot; data-ad-slot=&quot;1898893421&quot; style=&quot;display: block; text-align: center;&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;&lt;/div&gt;
  
  
  
  
  &lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;Code&lt;/h3&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;/p&gt;&lt;form&gt;
&lt;textarea cols=&quot;75&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;70&quot;&gt;  
    #include&lt;iostream&gt;
using namespace std;

class Queue
{
	private:
		int front;
		int rear;
		int a[];
		int size;
	public:
		Queue(int s)   //constructor to set size of array
		{
			size=s;
			front=-1;
			rear=-1;
		}
		
		int empty()   //checks if queue is empty or not
		{
			if(front==-1 &amp;&amp; rear==-1)
			   return 1;
			else
			   return 0;
		}
		
		int full()    //checks if queue is full or not
		{
			if(rear==size-1)
			  return 1;
			else
			  return 0;
		}
		
		void enqueue(int data)  //insert data in queue
		{
			if(empty())
			{
				front=rear=0;
			}
			if(full())
			{
				return;
			}
			else
			{
				rear=rear+1;
			}
			a[rear]=data;
		}
		
		
		void reverse()
		{
			for(int i=rear;i&gt;=front+1;i--)
			{
				cout&lt;&lt;a[i]&lt;&lt;&quot; &quot;;
			}
		}
		
		void show()   //print data in the queue
		{
			for(int i=front+1;i&lt;=rear;i++)
			{
				cout&lt;&lt;a[i]&lt;&lt;&quot; &quot;;
			}
			cout&lt;&lt;endl;
		}
};

int main()
{
	Queue q(7);
	q.enqueue(2);
	q.enqueue(3);
	q.enqueue(-4);
	q.enqueue(5);
	q.enqueue(-6);
	cout&lt;&lt;&quot;data in queue: &quot;;
	q.show();
	cout&lt;&lt;endl;
	cout&lt;&lt;&quot;queue after reversing: &quot;;
	q.reverse();
	return 0;
}

  
  &lt;/textarea&gt;
  &lt;/form&gt;
  &lt;p&gt;&lt;/p&gt;&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px; font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/h3&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/h3&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/h3&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
  
 &lt;script async=&quot;&quot; src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot; data-ad-client=&quot;ca-pub-6736665307191368&quot; data-ad-format=&quot;fluid&quot; data-ad-layout=&quot;in-article&quot; data-ad-slot=&quot;1898893421&quot; style=&quot;display: block; text-align: center;&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;
  
  
  
  &lt;/h3&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/h3&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/h3&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/h3&gt;&lt;h3 style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;Explanation of code&lt;/h3&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;1. First we will write a simple code of&amp;nbsp;queue which will have:&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;ul style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;li&gt;Queue() : It is a constructor which will initialize the size of array, front and rear.&lt;/li&gt;&lt;li&gt;empty() : This function checks if the queue is empty or not.&lt;/li&gt;&lt;li&gt;full() : This function checks if the queue is full or not.&lt;/li&gt;&lt;li&gt;enqueue() : This function inserts the data in the queue.&lt;/li&gt;&lt;li&gt;show() : This function prints the queue.&lt;/li&gt;&lt;/ul&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;2. Insert some values in the&amp;nbsp;queue. (&lt;b&gt;data structures types&lt;/b&gt;)&lt;/div&gt;&lt;div&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;3.&amp;nbsp;Now we have created a function named&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;reverse()&lt;/strong&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&amp;nbsp;to&amp;nbsp;&lt;b&gt;Program to Reverse a Queue - Code Explained &lt;/b&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;4. In the reverse() we have written a for loop which starts from rear of the queue and prints the values of the queue until it reaches the front of the queue.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;for(int i=rear;i&amp;gt;=front+1;i--)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;cout&amp;lt;&amp;lt;a[i]&amp;lt;&amp;lt;&quot; &quot;;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;em&gt;&lt;strong&gt;**&lt;/strong&gt;&lt;/em&gt;&lt;strong&gt;&amp;nbsp;&lt;em data-mce-bogus=&quot;1&quot;&gt;&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;This was the&amp;nbsp;&lt;strong&gt;Program to Reverse a Queue - Code Explained.&amp;nbsp;&lt;/strong&gt;I hope you have understood the logic.&lt;/em&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/2210743156530946547/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/09/program-to-reverse-queue-code-explained.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2210743156530946547'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2210743156530946547'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/09/program-to-reverse-queue-code-explained.html' title='Program to Reverse a Queue - Code Explained '/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhSARQ5KspJpAO8ODnu3ciaDlC1KVBexwzEDHxcd7D5XT37MboyEc5SMWJktud7KRODYe-5E6N3fDT1biz5Sv-Es616nm2L5XQDSm7YkoSaY2lHKy8AO6o19gZbLU53OOxvN08bGI1YTANZ/s72-w483-h166-c/ququ.webp" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-168206835541795017</id><published>2020-09-23T07:47:00.006-07:00</published><updated>2021-06-11T04:57:15.695-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#technology"/><title type='text'>Rest API - Explained in an Easy, Quick and Simple Way</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;Hey Developer, how long have you been in your Computer Science profession ? Nevermind, but have you ever heard of Rest API&#39;s? I&#39;m sure you have no matter what language you use. But do you know what are these rest API&#39;s ? Don&#39;t worry today we&#39;ll be discussing about&amp;nbsp;&lt;strong&gt;Rest API - Explained in an Easy, Quick and Simple Way.&amp;nbsp;&lt;/strong&gt;Oh, and do tell me how long have you been in this coding profession in the comment section below 🙌.&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
  
  
  
  &lt;script async src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block; text-align:center;&quot;
     data-ad-layout=&quot;in-article&quot;
     data-ad-format=&quot;fluid&quot;
     data-ad-client=&quot;ca-pub-6736665307191368&quot;
     data-ad-slot=&quot;7599680777&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;





&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;Rest API&lt;br data-mce-bogus=&quot;1&quot; /&gt;&lt;/h3&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLgBpn__P2oHTRpGVlCU42lKS_CXHTStPY4LTDSVNDk-3fzOhPuPd4sI9lKArdNbcc_gIgSJriGMsSV2rnIb4OaOu7vlDKD7vMc7bOM-sjzJzYaJu_1RTPhBPq5iuweDSclpe8WtNhSkjb/s1584/representational-state-transfer-rest-api-application-program-interface-words-vector-large-text-illustration-1572017061212222222222222222222222222222222222.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Rest API - Explained in an Easy, Quick and Simple Way&quot; border=&quot;0&quot; data-original-height=&quot;502&quot; data-original-width=&quot;1584&quot; height=&quot;170&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLgBpn__P2oHTRpGVlCU42lKS_CXHTStPY4LTDSVNDk-3fzOhPuPd4sI9lKArdNbcc_gIgSJriGMsSV2rnIb4OaOu7vlDKD7vMc7bOM-sjzJzYaJu_1RTPhBPq5iuweDSclpe8WtNhSkjb/w541-h170/representational-state-transfer-rest-api-application-program-interface-words-vector-large-text-illustration-1572017061212222222222222222222222222222222222.webp&quot; title=&quot;Rest API - Explained in an Easy, Quick and Simple Way&quot; width=&quot;541&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;Let me explain with the help of an example. Suppose you want to make a weather application. Now in a weather app you can see that different locations have different temperatures and that temperature changes every now and then.&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;So, how to we get such huge amount of data from?&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;For that we have servers or clouds located around the globe. So when you use the weather app and try to search for weather of a location you will send a&amp;nbsp;&lt;b&gt;R&lt;/b&gt;&lt;strong&gt;equest&amp;nbsp;&lt;/strong&gt;to the server that you want the temperature of XYZ location and then the server will&amp;nbsp;&lt;strong&gt;Respond&lt;/strong&gt;&amp;nbsp;to your query.&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;But then how do you generate that data from server?&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;For that you will have to use a &lt;b&gt;&lt;a href=&quot;https://www.javatpoint.com/servlet-tutorial&quot; target=&quot;_blank&quot;&gt;servlet&lt;/a&gt;&lt;/b&gt;. So now a client will send you the data and the servlet will respond you with the data.&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;But what will the servlet return to the client?&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;The servlet will return you the data in form of XML or JSON format (based on your requirement). But to get data from servlet you will have to write some bulky code and to make remove this bulky code we use&amp;nbsp;&lt;strong&gt;Rest API.&amp;nbsp;&lt;/strong&gt;So what if you just have to create an object at server side and return the values associated with it or we can say you want to return just the state of the object.&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
  
  
  
  
  &lt;script async src=&quot;https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js&quot;&gt;&lt;/script&gt;
&lt;ins class=&quot;adsbygoogle&quot;
     style=&quot;display:block; text-align:center;&quot;
     data-ad-layout=&quot;in-article&quot;
     data-ad-format=&quot;fluid&quot;
     data-ad-client=&quot;ca-pub-6736665307191368&quot;
     data-ad-slot=&quot;7599680777&quot;&gt;&lt;/ins&gt;
&lt;script&gt;
     (adsbygoogle = window.adsbygoogle || []).push({});
&lt;/script&gt;






&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;Now whenever we use internet we use CURD operations ( C-Create U-Update R-Read D-Delete) . We CREATE the resource we UPDATE the resource we READ the resource and we DELETE the resource. So to perform these CURD operations we use Http methods like POST, GET, PUT,DELETE. So by using these operations we can perform a CURD operation. And that&#39;s how the Rest API works.&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkOPqx9VuqRXH3iBDYFMse8Hc6C-GCiQI63hyphenhyphenforSllNhyphenhyphenJZvMIx8Cgu8BLhvyBJtfnKThqkggOB3cGb0dQxOgxChC1Hkj0FQItQK8I5Pl16ghd9XuFL0LY9hQrEmaimp2GAIlDHccPwG4/s2000/6+Descriptive+properties+of+rEst+APi.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img border=&quot;0&quot; data-original-height=&quot;2000&quot; data-original-width=&quot;800&quot; height=&quot;943&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkOPqx9VuqRXH3iBDYFMse8Hc6C-GCiQI63hyphenhyphenforSllNhyphenhyphenJZvMIx8Cgu8BLhvyBJtfnKThqkggOB3cGb0dQxOgxChC1Hkj0FQItQK8I5Pl16ghd9XuFL0LY9hQrEmaimp2GAIlDHccPwG4/w377-h943/6+Descriptive+properties+of+rEst+APi.webp&quot; width=&quot;377&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;Use below Code to embed this Infographic:&lt;/b&gt;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;&lt;/b&gt;&lt;/p&gt;&lt;form&gt;&lt;b&gt;
&lt;textarea cols=&quot;60&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;15&quot;&gt;  
  &lt;p&gt;&lt;strong&gt;Please include attribution to https://geektech1717.blogspot.com/ with this graphic.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&#39;https://geektech1717.blogspot.com/2020/09/rest-api-explained-in-easy-quick-and.html&#39;&gt;&lt;img src=&#39;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhkOPqx9VuqRXH3iBDYFMse8Hc6C-GCiQI63hyphenhyphenforSllNhyphenhyphenJZvMIx8Cgu8BLhvyBJtfnKThqkggOB3cGb0dQxOgxChC1Hkj0FQItQK8I5Pl16ghd9XuFL0LY9hQrEmaimp2GAIlDHccPwG4/s2000/6+Descriptive+properties+of+rEst+APi.webp&#39; alt=&#39;Properties of Rest API&#39; 540px border=&#39;0&#39; /&gt;&lt;/a&gt;&lt;/p&gt;
  
  &lt;/textarea&gt;
  &lt;/b&gt;&lt;/form&gt;This was all about the basics of &lt;b&gt;Rest API&lt;/b&gt;. I hope you enjoyed reading.&lt;p&gt;&lt;/p&gt;



</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/168206835541795017/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/09/rest-api-explained-in-easy-quick-and.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/168206835541795017'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/168206835541795017'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/09/rest-api-explained-in-easy-quick-and.html' title='Rest API - Explained in an Easy, Quick and Simple Way'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgLgBpn__P2oHTRpGVlCU42lKS_CXHTStPY4LTDSVNDk-3fzOhPuPd4sI9lKArdNbcc_gIgSJriGMsSV2rnIb4OaOu7vlDKD7vMc7bOM-sjzJzYaJu_1RTPhBPq5iuweDSclpe8WtNhSkjb/s72-w541-h170-c/representational-state-transfer-rest-api-application-program-interface-words-vector-large-text-illustration-1572017061212222222222222222222222222222222222.webp" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-2877018984261405802</id><published>2020-09-16T07:22:00.002-07:00</published><updated>2020-09-26T05:39:47.274-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#technology"/><title type='text'>What is Docker ? Explained in an Easy, Quick and Simple Way</title><content type='html'>&lt;p&gt;&amp;nbsp;&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;Have you heard of Docker?&amp;nbsp; Do you know what it is or why it is used? Don&#39;t worry, Today you&#39;ll learn about &lt;b&gt;What is Docker ? Explained in an Easy, Quick and Simple Way.&lt;/b&gt;&amp;nbsp;So let&#39;s start:&lt;/p&gt;&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;What is the problem that Docker resolves?&lt;/h3&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQuYlj9pZjWUdNb4QLPPlVVJS8YUNsbvDWrskZF333N-DHtCVbXBB6W-MWg8F8nTHXIEmUXuwuzef3EBqEioioSLokgI4nTZ24dVbfQyrXKDvB0uR5dG93JBmpas-PcIcXDqhyEkqmDhVE/s2048/docker-1600265965641-784.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;What is Docker ? A Beginner&#39;s Guide&quot; border=&quot;0&quot; data-original-height=&quot;1152&quot; data-original-width=&quot;2048&quot; height=&quot;300&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQuYlj9pZjWUdNb4QLPPlVVJS8YUNsbvDWrskZF333N-DHtCVbXBB6W-MWg8F8nTHXIEmUXuwuzef3EBqEioioSLokgI4nTZ24dVbfQyrXKDvB0uR5dG93JBmpas-PcIcXDqhyEkqmDhVE/w534-h300/docker-1600265965641-784.webp&quot; title=&quot;What is Docker ? A Beginner&#39;s Guide&quot; width=&quot;534&quot; /&gt;&lt;/a&gt;&lt;/div&gt;&lt;br /&gt;&lt;div&gt;&lt;br /&gt;&lt;/div&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;There is a major problem that almost all developers face which is that whenever you build a project ( here we are talking about large-scale projects) it works absolutely fine on your machine but as soon as you move that project into your friend&#39;s computer or onto the server then the working or performance or optimization of your project decreases. Some&amp;nbsp;uncertainties are like maybe your images are not loading correctly or there are glitches in the workflow of the project. So, in layman terms&amp;nbsp;&lt;strong&gt;&lt;em&gt;&quot;the code works on my machine&quot;&lt;/em&gt;&lt;/strong&gt;. So Docker was made to resolve this problem of&amp;nbsp;&lt;strong&gt;&lt;em&gt;&quot;the code works on my machine&quot;&lt;/em&gt;&lt;/strong&gt;.&amp;nbsp;&lt;/p&gt;&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;Docker&lt;/h3&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;Docker allows you to wrap up your code in containers which are portable. Now if you place that container on any platform maybe on a server or a friend&#39;s PC these containers make sure that the code inside it works exactly the same as if it were to run on your machine. So these containers are the heart of Docker. A docker container can store your code, configurations, dependencies, networking information or even the processes that you are running.&lt;/p&gt;&lt;p style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;So to summarize Docker:&lt;br /&gt;&lt;/p&gt;&lt;ol style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;li&gt;It is a client side application. So you can just install it and perform all kind of operations on the containers and move these containers anywhere.&lt;/li&gt;&lt;li&gt;It also acts as a service and it can be deployed on any server so that you can take your container from that server and deploy on any platform. It can also acts as a social networking platform , you can share your docker image and whenever someone wants to start at given checkpoint the they can start from that point.&lt;/li&gt;&lt;li&gt;Docker is a company that manages all these things for you.&lt;/li&gt;&lt;/ol&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;div&gt;&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;Some best Docker Tutorials&lt;/h3&gt;&lt;ol&gt;&lt;li&gt;&lt;a data-mce-href=&quot;https://www.raywenderlich.com/library?q=docker&amp;amp;sort_order=relevance&quot; href=&quot;https://www.raywenderlich.com/library?q=docker&amp;amp;sort_order=relevance&quot; style=&quot;cursor: pointer;&quot;&gt;Docker - by raywenderlich&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a data-mce-href=&quot;http://tutorials.jenkov.com/docker/index.html&quot; href=&quot;http://tutorials.jenkov.com/docker/index.html&quot; style=&quot;cursor: pointer;&quot;&gt;Docker tutorials - by Jakob Jenkov&lt;/a&gt;&lt;/li&gt;&lt;li&gt;&lt;a data-mce-href=&quot;https://hackr.io/tutorials/learn-docker&quot; href=&quot;https://hackr.io/tutorials/learn-docker&quot; style=&quot;cursor: pointer;&quot;&gt;Docker tutorials - by hackr.io&lt;/a&gt;&lt;/li&gt;&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;So, that was all and I hope the gist of &lt;b&gt;Docker&lt;/b&gt; is clear to you now. Thanks for reading !!!&lt;/span&gt;&lt;/span&gt;&lt;div&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/2877018984261405802/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/09/what-is-docker-beginners-guide.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2877018984261405802'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2877018984261405802'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/09/what-is-docker-beginners-guide.html' title='What is Docker ? Explained in an Easy, Quick and Simple Way'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjQuYlj9pZjWUdNb4QLPPlVVJS8YUNsbvDWrskZF333N-DHtCVbXBB6W-MWg8F8nTHXIEmUXuwuzef3EBqEioioSLokgI4nTZ24dVbfQyrXKDvB0uR5dG93JBmpas-PcIcXDqhyEkqmDhVE/s72-w534-h300-c/docker-1600265965641-784.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-401743271509428103</id><published>2020-09-08T07:36:00.003-07:00</published><updated>2021-06-09T23:54:00.183-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Delete all even nodes of a Circular Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will discuss how to&amp;nbsp;&lt;b&gt;Delete all even nodes of a Circular Linked List&lt;/b&gt;. But if you are unfamiliar with the concepts of circular linked list then please refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-circular-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt;. Suppose we have a circular linked list like 1-&amp;gt;2-&amp;gt;3-&amp;gt;4 and 4 points back to 1, then after deleting even numbered nodes the list should look like 1-&amp;gt;3 and 3 pointing to 1. Here we have deleted the 2nd and 4th node. Now, let&#39;s discuss the procedure:&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Procedure&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a circular linked list. (&lt;b&gt;data structures types&lt;/b&gt;)&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now a circular linked list can have even nodes or odd nodes in total.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. So, we will divide our problem into 2 cases:&lt;/div&gt;
&lt;ul style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;li&gt;When the circular linked list has even number of nodes.&lt;/li&gt;
&lt;li&gt;When the circular linked list has odd number of nodes.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. When the list will have even number of nodes then we will delete even numbered node but in this case the last node will also be deleted so the second last node shall point to the first node now.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU7NYlLta53-Yg3M-Supea5FKUgK6_6HiyjQkI6CRfJ3yHBdNEON-zto2Qgc6OhjxGRv5y_S5AkmBIB7INyHlbj9U0uG9QmM4c8sIvezAz39zBfoxUJ5KSFoyTttQQPikgEDFiCXCptKUl/s1600/1+%25285%2529.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Delete all even nodes of a Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;319&quot; data-original-width=&quot;1081&quot; height=&quot;188&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU7NYlLta53-Yg3M-Supea5FKUgK6_6HiyjQkI6CRfJ3yHBdNEON-zto2Qgc6OhjxGRv5y_S5AkmBIB7INyHlbj9U0uG9QmM4c8sIvezAz39zBfoxUJ5KSFoyTttQQPikgEDFiCXCptKUl/s640/1+%25285%2529.webp&quot; title=&quot;Delete all even nodes of a Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5.&amp;nbsp;When the list will have odd number of nodes then the last node will also be odd numbered so no need to delete that node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMpyUJE-VgVrjDDQ0UzPjM-d7JiP6ctYiEri6shGUkzoYDq25GshyphenhyphenOlVJbddYRZqA91sKiXXNdflMKC0DJsMsf-IhkSawHhDZCV3cH6b90s1xY0_w8lp43L2qaD3CXofBiEu-fvyIMJSoX/s1600/2+%25284%2529.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Delete all even nodes of a Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;354&quot; data-original-width=&quot;1102&quot; height=&quot;204&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiMpyUJE-VgVrjDDQ0UzPjM-d7JiP6ctYiEri6shGUkzoYDq25GshyphenhyphenOlVJbddYRZqA91sKiXXNdflMKC0DJsMsf-IhkSawHhDZCV3cH6b90s1xY0_w8lp43L2qaD3CXofBiEu-fvyIMJSoX/s640/2+%25284%2529.webp&quot; title=&quot;Delete all even nodes of a Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
6. Now to know how to delete nodes go to the&amp;nbsp;&lt;strong&gt;Explanation of code&lt;/strong&gt;.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Code&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;75&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{

public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next=temp;

        }
    }

    void assignhead()
    {
        struct node* temp=head;
        while(temp-&gt;next!=NULL)
        {
            temp=temp-&gt;next;
        }
        temp-&gt;next=head;
    }


    void delete_even()
    {
        struct node* temp=head;
        int c=1;
        while(temp-&gt;next!=head)
        {
            c++;
            temp=temp-&gt;next;
        }
        temp=head;
        temp=temp-&gt;next;

        if(c%2==0)
        {
            struct node* temp1=head;
            int n=-1;
            while(temp-&gt;next!=head)
            {
                if(n==-1)
                {
                    temp1-&gt;next=temp-&gt;next;
                    temp1=temp1-&gt;next;
                }
                temp=temp-&gt;next;
                n=n*(-1);
            }
            temp1-&gt;next=head;
        }
        else if(c%2!=0)
        {
            struct node* temp1=head;
            int n=-1;
            while(temp-&gt;next!=head)
            {
                if(n==-1)
                {
                    temp1-&gt;next=temp-&gt;next;
                    temp1=temp1-&gt;next;
                }
                temp=temp-&gt;next;

                n=n*(-1);
            }
        }
        cout&lt;&lt;&quot;List after deleting even-numbered nodes: &quot;;
        disp();

    }


    void disp()
    {
        struct node* temp=head;
        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
        temp=temp-&gt;next;
        while(temp!=head)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(3);
    l.insert(5);
    l.insert(8);
    l.insert(1);
    l.insert(9);
    l.insert(0);
    l.insert(2);
    l.insert(5);
    l.assignhead();
    cout&lt;&lt;&quot;original list: &quot;;
    l.disp();
    l.delete_even();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Explanation of code&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;1. First we will write a simple code of circular linked list which will have 3 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the circular linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- assignhead() - to make a link between last and first node.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the circular linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the circular linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3.&amp;nbsp;Now we have created a function named&amp;nbsp;&lt;strong&gt;delete_even()&lt;/strong&gt;&amp;nbsp;to&amp;nbsp;&lt;b&gt;Delete all even nodes of a Circular Linked List&lt;/b&gt;.&lt;br /&gt;
4. First we have created a&amp;nbsp;&lt;strong&gt;temp&amp;nbsp;&lt;/strong&gt;variable pointing to the head. Then we have created a int variable &#39;&lt;strong&gt;c&#39;&lt;/strong&gt;&amp;nbsp;which is initialized to 1. This &#39;c&#39; will be used to store the number of nodes in the circular linked list.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; int c=1;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;5. Then we have written a while loop which iterate the whole list and increment c in each iteration. This will give us the number of nodes present in our list. (&lt;b&gt;data structures types&lt;/b&gt;)&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;while(temp-&amp;gt;next!=head)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
6. Now we have assigned temp to head again. And after that we have made temp point to the second node.&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;7. Now we have an if condition which will be executed if we have even number of nodes (i.e c%2==0).&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;if(c%2==0)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ---&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;/em&gt;8. Inside the if condition we have made another pointer to node variable&amp;nbsp;&lt;strong&gt;temp1&lt;/strong&gt;&amp;nbsp;which is initialized to&amp;nbsp;&lt;strong&gt;head&lt;/strong&gt;. Then we have declared&amp;nbsp;&lt;strong&gt;&#39;n&#39;&lt;/strong&gt;&amp;nbsp;which is initialized to -1.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp1=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; int n=-1;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;9. Then we have started a while loop which will iterate our circular linked list till be reach the last node.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;while(temp-&amp;gt;next!=head)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ---&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
10. Then we have used another if condition which checks if &#39;n&#39; is equal to -1 or not. Basic idea of using &#39;n&#39; is that &#39;n&#39; will be -1 whenever our&amp;nbsp;&lt;strong&gt;temp&amp;nbsp;&lt;/strong&gt;will point to an even numbered node. Since temp is initially pointing to the second node (refer to point 6) which is an even numbered node so this if condition will hold true and we will enter this if condition block.&amp;nbsp;&lt;strong&gt;temp1&lt;/strong&gt;&amp;nbsp;is pointing to the first node thus we have made&lt;strong&gt;&amp;nbsp;temp1&lt;/strong&gt;&amp;nbsp;to point to the next node of&amp;nbsp;&lt;strong&gt;temp&amp;nbsp;&lt;/strong&gt;and then we have incremented temp1. So, temp1 will now be at 3rd node.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;if(n==-1)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp1-&amp;gt;next=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp1=temp1-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;em data-mce-bogus=&quot;1&quot;&gt;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;11. Then we have incremented temp to next node (i.e it will also point to the 3rd node) and n will now become 1.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp=temp-&amp;gt;next;&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; n=n*(-1);&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;12. Now if you iterate again this while loop again then you will notice that&amp;nbsp;&lt;strong&gt;n&lt;/strong&gt;&amp;nbsp;is not -1 now so we won&#39;t enter the if block and we actually didn&#39;t wanted to enter the if block because we are at an odd numbered node (3rd node). I hope you can make sense of it now.&lt;br /&gt;
13. After this while loop gets over the we have written made&amp;nbsp;&lt;strong&gt;temp1&lt;/strong&gt;&amp;nbsp;to point to the first node because in this case we had even number of nodes so after deleting all even number of nodes the last node will also be even so&amp;nbsp;we will delete that too. But then the second last node shall point to the first node. In this case that second last node will be&amp;nbsp;&lt;strong&gt;temp1. (&lt;/strong&gt;&lt;b&gt;data structures types&lt;/b&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;temp1-&amp;gt;next=head;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;14. Next we have written another if condition which is for odd number of nodes. The complete logic will be same for odd number of nodes. Just one change will be there that the last node in this case will not be deleted because we have odd number of nodes so no need to write&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp1-&amp;gt;next=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;u&gt;Guys you can follow me here :&lt;/u&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/strong&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;em&gt;&lt;strong&gt;&lt;a href=&quot;https://github.com/mayank171&quot; target=&quot;_blank&quot;&gt;https://github.com/mayank171&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;em&gt;&lt;strong&gt;&lt;a href=&quot;https://www.linkedin.com/in/mayank-mewar-586090173/&quot; target=&quot;_blank&quot;&gt;https://www.linkedin.com/in/mayank-mewar-586090173/&lt;/a&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/401743271509428103/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/09/delete-all-even-nodes-of-circular.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/401743271509428103'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/401743271509428103'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/09/delete-all-even-nodes-of-circular.html' title='Delete all even nodes of a Circular Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgU7NYlLta53-Yg3M-Supea5FKUgK6_6HiyjQkI6CRfJ3yHBdNEON-zto2Qgc6OhjxGRv5y_S5AkmBIB7INyHlbj9U0uG9QmM4c8sIvezAz39zBfoxUJ5KSFoyTttQQPikgEDFiCXCptKUl/s72-c/1+%25285%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-8838863697998100887</id><published>2020-09-02T07:10:00.002-07:00</published><updated>2020-09-02T07:10:49.923-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Exchange First and Last nodes in Circular Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will be discussing the algorithm to&amp;nbsp;&lt;strong&gt;Exchange First and Last nodes in Circular Linked List .&amp;nbsp;&lt;/strong&gt;But, if you are unfamiliar with the concepts of circular linked list then refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-circular-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt;. Suppose we have circular linked list 1-&amp;gt;2-&amp;gt;3-&amp;gt;4 and 4 points to 1. Then after swapping the first and last node the circular linked list will look like 4-&amp;gt;2-&amp;gt;3-&amp;gt;1 and 1 points to 4. Now, let&#39;s discuss the procedure:&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Procedure&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a circular linked list.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now we will make a variable which will point to the first node.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. Then we will make another variable which will point to the last node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. At last, we will swap the data of these 2 variables.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVE4vED41bZ1EXj-UtBbY-bZX1V-oZKggyfqC-02uI5fI69-Qhd1PfZ_bmig0tFjFNAws-UUUIWwiNc5E2UhBey7EvYfrb6wwGiqRnnHNl0f8aoxS8zLqgg5p5vrlk1AZA6oNXQ0jgZfK7/s1600/1+%25284%2529.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Exchange First and Last nodes in Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;427&quot; data-original-width=&quot;1141&quot; height=&quot;238&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVE4vED41bZ1EXj-UtBbY-bZX1V-oZKggyfqC-02uI5fI69-Qhd1PfZ_bmig0tFjFNAws-UUUIWwiNc5E2UhBey7EvYfrb6wwGiqRnnHNl0f8aoxS8zLqgg5p5vrlk1AZA6oNXQ0jgZfK7/s640/1+%25284%2529.webp&quot; title=&quot;Exchange First and Last nodes in Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-weight: 400;&quot;&gt;
Code&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;form&gt;
&lt;textarea cols=&quot;75&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;70&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{

public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next=temp;

        }
    }

    void assignhead()
    {
        struct node* temp=head;
        while(temp-&gt;next!=NULL)
        {
            temp=temp-&gt;next;
        }
        temp-&gt;next=head;
    }


    void Swap()
    {
        struct node* temp=head;
        struct node* temp1=head;
        while(temp1-&gt;next!=head)
        {
            temp1=temp1-&gt;next;
        }
        swap(temp1-&gt;data,temp-&gt;data);
    }

    void disp()
    {
        struct node* temp=head;
        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
        temp=temp-&gt;next;
        while(temp!=head)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(3);
    l.insert(5);
    l.insert(8);
    l.insert(1);
    l.insert(-2);
    l.insert(7);
    l.insert(-3);
    l.assignhead();
    l.disp();
    l.Swap();
    l.disp();
    return 0;
}


&lt;/form&gt;
&lt;/textarea&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-weight: 400;&quot;&gt;
Explanation of code&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of circular linked list which will have 3 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the circular linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- assignhead() - to make a link between last and first node.&lt;/span&gt;&lt;br /&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the circular linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
2. Insert some values in the circular linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
3.&amp;nbsp;Now we have created a function named&amp;nbsp;&lt;strong&gt;Swap()&lt;/strong&gt;&amp;nbsp;to Exchange First and Last nodes in Circular Linked List.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
4. First we have created a pointer to node variable&amp;nbsp;&lt;strong&gt;temp&amp;nbsp;&lt;/strong&gt;which is initialized to&amp;nbsp;&lt;strong&gt;head.&amp;nbsp;&lt;/strong&gt;So, temp is pointing to the first node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;5. Next, we have created another pointer to node variable&amp;nbsp;&lt;strong&gt;temp1&lt;/strong&gt;&amp;nbsp;which is also initialized to head. But, later on we will use temp1 to point to the last node of list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp1=head;&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;6. Now we are using a while loop to traverse the list till end. Focus on the test condition of while loop. We have written that we will traverse the list until the next of the temp1 points to head&amp;nbsp;&lt;strong&gt;OR&amp;nbsp;&lt;/strong&gt;in other words we are traversing the list till the last node only. So, this condition will make temp1 point to the last node. Inside the while loop we are just moving to the next node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp1-&amp;gt;next!=head)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp1=temp1-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;7. At last we have used the in-built&amp;nbsp;&lt;strong&gt;&lt;em&gt;swap()&amp;nbsp;&lt;/em&gt;&lt;/strong&gt;to swap the data of temp and temp1.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/8838863697998100887/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/09/exchange-first-and-last-nodes-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/8838863697998100887'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/8838863697998100887'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/09/exchange-first-and-last-nodes-in.html' title='Exchange First and Last nodes in Circular Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVE4vED41bZ1EXj-UtBbY-bZX1V-oZKggyfqC-02uI5fI69-Qhd1PfZ_bmig0tFjFNAws-UUUIWwiNc5E2UhBey7EvYfrb6wwGiqRnnHNl0f8aoxS8zLqgg5p5vrlk1AZA6oNXQ0jgZfK7/s72-c/1+%25284%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-6318982779539955850</id><published>2020-08-27T07:34:00.001-07:00</published><updated>2020-08-27T07:34:52.759-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Delete a node at kth Position in Circular Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will discuss how to&amp;nbsp;&lt;strong&gt;delete a node at kth position in circular linked list&lt;/strong&gt;. But if you are unfamiliar with the concepts of circular linked list then please refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-circular-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; . Suppose you have a circular linked list 1-&amp;gt;2-&amp;gt;3-&amp;gt;4 and 4 pointing to 1. Let k=2 then we have to delete the second node. After deletion the list should be 1-&amp;gt;3-&amp;gt;4 and 4 pointing to 1. Now, let&#39;s discuss the procedure:&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Procedure&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a circular linked list.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Then we will traverse the list until we reach the node which is just previous to the kth node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. Now we will update the pointer of this previous node with the address of the node next to kth node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjY23_f75UmWlWeBUUKEp5Vezm-tDZ52LZ-po8hviADCs9JptjTKj5KbbidsNYToE4WadxXl4Eiu4m72veXgG8nzjsaUAl4ZoLaa_5MQUJ7fOBHZLv6U9TtZMaLThohOEfV5vOlse5kAf4t/s1600/1+%25283%2529.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Delete a node at kth Position in Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;376&quot; data-original-width=&quot;1275&quot; height=&quot;188&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjY23_f75UmWlWeBUUKEp5Vezm-tDZ52LZ-po8hviADCs9JptjTKj5KbbidsNYToE4WadxXl4Eiu4m72veXgG8nzjsaUAl4ZoLaa_5MQUJ7fOBHZLv6U9TtZMaLThohOEfV5vOlse5kAf4t/s640/1+%25283%2529.webp&quot; title=&quot;Delete a node at kth Position in Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. If the value of &#39;k&#39; is 1 then we have to delete the first node. While doing so we will traverse till last node and then we will update the pointer of this node with the address of the node next to 1st node. But we will also have to update the head pointer because if the first node is deleted then second node will now be the first node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkhXEs3DlgFtqqa3Cw1-dfCPPvOmzvIrEMRei9zvjsOvvykWpDhDlQoe3ikPjHac52nJRVwQ-4vdu76_QpGaKMDccvX3879XS8tGbN9wXeHNqBEWnHBDQogty0W4FbSK_wuhd4_GmBmpBT/s1600/2+%25283%2529.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Delete a node at kth Position in Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;346&quot; data-original-width=&quot;1255&quot; height=&quot;176&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjkhXEs3DlgFtqqa3Cw1-dfCPPvOmzvIrEMRei9zvjsOvvykWpDhDlQoe3ikPjHac52nJRVwQ-4vdu76_QpGaKMDccvX3879XS8tGbN9wXeHNqBEWnHBDQogty0W4FbSK_wuhd4_GmBmpBT/s640/2+%25283%2529.webp&quot; title=&quot;Delete a node at kth Position in Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;/h3&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Code&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;75&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{

public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next=temp;

        }
    }

    void assignhead()
    {
        struct node* temp=head;
        while(temp-&gt;next!=NULL)
        {
            temp=temp-&gt;next;
        }
        temp-&gt;next=head;
    }


    void Delete(int k)
    {
        if(k!=1)
        {
            struct node* temp=head;
            int c=1;
            while(c&lt;k-1)
            {
                temp=temp-&gt;next;
                c++;
            }
            temp-&gt;next=temp-&gt;next-&gt;next;
        }
        else
        {
            struct node* temp=head;
            while(temp-&gt;next!=head)
            {
                temp=temp-&gt;next;
            }
            head=temp-&gt;next-&gt;next;
            temp-&gt;next=temp-&gt;next-&gt;next;
        }
    }

    void disp()
    {
        struct node* temp=head;
        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
        temp=temp-&gt;next;
        while(temp!=head)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(3);
    l.insert(5);
    l.insert(8);
    l.insert(1);
    l.insert(-2);
    l.insert(7);
    l.insert(-3);
    l.insert(11);
    l.insert(0);
    l.insert(6);
    l.assignhead();
    l.disp();
    int k;
    cout&lt;&lt;&quot;enter position you want to delete: &quot;&lt;&lt;endl;
    cin&gt;&gt;k;
    l.Delete(k);
    l.disp();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;/h3&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Explanation of code&lt;/h3&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of circular linked list which will have 3 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the circular linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- assignhead() - to make a link between last and first node.&lt;/span&gt;&lt;br /&gt;
&lt;span data-keep-original-tag=&quot;false&quot; data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the circular linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the circular linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3.&amp;nbsp;Now we have created a function named&amp;nbsp;&lt;strong&gt;Delete()&lt;/strong&gt;&amp;nbsp;to delete a node at kth the circular linked list. It has a parameter &#39;k&#39; which tells us about the position of the node to be deleted.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. First we have checked if &#39;k&#39; is not equals to 1 because if we have divided out problem into 2 parts. If the node which is to be deleted is the first node or any other node.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5. If &#39;k&#39; is not equal to 1 then we have created a pointer to node variable&amp;nbsp;&lt;strong&gt;&#39;temp&#39;&lt;/strong&gt;&amp;nbsp;which is pointing to head. We have also created a variable&amp;nbsp;&lt;strong&gt;&#39;c&#39;&amp;nbsp;&lt;/strong&gt;which is a counter to traverse the list till the node which is just previous to kth node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;if(k!=1)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int c=1;&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;---&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;---&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
6. Then we have created a while loop which will iterate the list till we reach the node which is just previous to kth node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;while(c&amp;lt;k-1)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;c++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
7. Then we have updated the next pointer of temp with the next pointer of the kth node because the node just previous to kth node shall point to the node which is next to the kth node. (&lt;em&gt;&lt;strong&gt;kth node is not physically deleted from the list, we have just changed the pointers so that it shall not come in the output screen&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;).&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp-&amp;gt;next=temp-&amp;gt;next-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
8.&amp;nbsp; Now we have reached the else part which will be executed if k is equal to 1 or if we want to delete the first node.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
9.&amp;nbsp; We have again created a pointer to node variable&amp;nbsp;&lt;strong&gt;&#39;temp&#39;&lt;/strong&gt;&amp;nbsp;which is pointing to head.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;b&gt;&lt;i&gt;else&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;---&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;---&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
10. Then we have created a while loop which will iterate the list till we reach the last node whose next pointer is pointing to the first node again.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp-&amp;gt;next!=head)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;11. Then we have updated the head and assigned it the address of the node next to first node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;head=temp-&amp;gt;next-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
12. Then we have updated the next pointer of temp with the address of the node next to kth node . (&lt;em&gt;&lt;strong&gt;kth node is not physically deleted from the list, we have just changed the pointers so that it shall not come in the output screen&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;).&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp-&amp;gt;next=temp-&amp;gt;next-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/6318982779539955850/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/08/delete-node-at-kth-position-in-circular.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6318982779539955850'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6318982779539955850'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/08/delete-node-at-kth-position-in-circular.html' title='Delete a node at kth Position in Circular Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjY23_f75UmWlWeBUUKEp5Vezm-tDZ52LZ-po8hviADCs9JptjTKj5KbbidsNYToE4WadxXl4Eiu4m72veXgG8nzjsaUAl4ZoLaa_5MQUJ7fOBHZLv6U9TtZMaLThohOEfV5vOlse5kAf4t/s72-c/1+%25283%2529.webp" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-3734697291612518053</id><published>2020-08-21T07:13:00.002-07:00</published><updated>2021-06-09T23:54:48.537-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Sort a Circular Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will be discussing how to&amp;nbsp;&lt;strong&gt;Sort&lt;/strong&gt;&lt;strong&gt;&amp;nbsp;a Circular Linked List.&amp;nbsp;&lt;/strong&gt;But if you are unfamiliar with the concepts of circular linked list then please refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-circular-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt;. Suppose we have a circular linked list like 4-&amp;gt;3-&amp;gt;2-&amp;gt;1 and 1 is pointing again to 4 then the sorted list should be 1-&amp;gt;2-&amp;gt;3-&amp;gt;4 . (&lt;b style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;data structures types&lt;/b&gt;)&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Now let&#39;s discuss the procedure for sorting.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Procedure&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;1. First we will make a circular linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now we will be using bubble sort to sort the list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. We will be iterating from first node till the second last node. Then we making another iteration inside the first iteration itself which will traverse from the next node of the node at which the first iteration will be currently executing till the last node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Code&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;75&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{

public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next=temp;

        }
    }

    void assignhead()
    {
        struct node* temp=head;
        while(temp-&gt;next!=NULL)
        {
            temp=temp-&gt;next;
        }
        temp-&gt;next=head;
    }


    void sort()
    {
        struct node* temp=head;
        while(temp-&gt;next!=head)
        {
            struct node* temp1=new node();
            temp1=temp-&gt;next;
            while(temp1!=head)
            {
                if(temp1-&gt;data&lt;temp-&gt;data)
                {
                    swap(temp-&gt;data,temp1-&gt;data);
                }
                temp1=temp1-&gt;next;
            }
            temp=temp-&gt;next;
        }
    }

    void disp()
    {
        struct node* temp=head;
        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
        temp=temp-&gt;next;
        while(temp!=head)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(3);
    l.insert(5);
    l.insert(8);
    l.insert(1);
    l.insert(-2);
    l.insert(7);
    l.insert(-3);
    l.insert(11);
    l.insert(0);
    l.insert(6);
    l.assignhead();
    l.disp();
    l.sort();
    l.disp();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Explanation of code&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;1. First we will write a simple code of circular linked list which will have 3 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the circular linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- assignhead() - to make a link between last and first node.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the circular linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the circular linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3.&amp;nbsp;Now we have created a function named sort&lt;strong&gt;()&lt;/strong&gt;&amp;nbsp;to sort the circular linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. Then we have made a pointer to node&amp;nbsp;&lt;strong&gt;&quot;temp&quot;&amp;nbsp;&lt;/strong&gt;which is assigned to head.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;5. Then we have started a while loop which will iterate from&amp;nbsp;&lt;strong&gt;temp which is at head until it reaches&amp;nbsp;the second last node.&amp;nbsp;&lt;/strong&gt;The loop will stop when temp&#39;s next pointer will be pointing to head and only last node will point to the head so the loop will execute until second last node because&lt;strong&gt;&amp;nbsp;temp-&amp;gt;next will be equal to head for the last node.&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;while(temp-&amp;gt;next!=head)&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; {&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;....&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;&lt;/em&gt;6. Inside the while loop we have made another pointer to node called&amp;nbsp;&lt;strong&gt;&quot;temp1&quot;&amp;nbsp;&lt;/strong&gt;. Then we have made&amp;nbsp;&lt;strong&gt;temp1 to point to the next node of the temp&lt;/strong&gt;.&amp;nbsp;Each time when temp will be incremented then temp1 will always point to the next node of temp. (&lt;b style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;data structures types&lt;/b&gt;)&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp1=new node();&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; temp1=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;7. Then we have started the a while loop which will&amp;nbsp;&lt;strong&gt;iterate temp1 till the last node&lt;/strong&gt;.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp1!=head)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ...&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglw1HEdAhN_2OSMuiFVcCQRM50pdGJp9fN2reQPbK3lzfd6M7w746d-wmibKK4a89ZPPyMnff8i1eDlHrsKHzIo3c3mc3slyNrdcQxRjmLX3xcrTrqn9A5wYpbdTfG8HO0qw-VClDk7atW/s1600/1+%25282%2529.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Sort a Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;370&quot; data-original-width=&quot;913&quot; height=&quot;258&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglw1HEdAhN_2OSMuiFVcCQRM50pdGJp9fN2reQPbK3lzfd6M7w746d-wmibKK4a89ZPPyMnff8i1eDlHrsKHzIo3c3mc3slyNrdcQxRjmLX3xcrTrqn9A5wYpbdTfG8HO0qw-VClDk7atW/s640/1+%25282%2529.webp&quot; title=&quot;Sort a Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;8. Then we have checked the if condition if the&amp;nbsp;&lt;strong&gt;data of temp will be greater then temp1 the we will swap the data .&amp;nbsp;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;if(temp1-&amp;gt;data&amp;lt;temp-&amp;gt;data)&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp;{&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;swap(temp-&amp;gt;data,temp1-&amp;gt;data);&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp;}&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbsgHdEHKwzmFKip8MXcMJaNmeTh9dW7Kja_02-DfT3LeT2-B90DUf3lgmizhAH3Iva4VU9jPd-evVKtoZXtLdgDtc-Lc-K3JUhaX6-b6KNv3iyhWRq04G6ge-AOVwHjijGQDQKe6wnjKb/s1600/2+%25282%2529.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Sort a Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;405&quot; data-original-width=&quot;937&quot; height=&quot;276&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhbsgHdEHKwzmFKip8MXcMJaNmeTh9dW7Kja_02-DfT3LeT2-B90DUf3lgmizhAH3Iva4VU9jPd-evVKtoZXtLdgDtc-Lc-K3JUhaX6-b6KNv3iyhWRq04G6ge-AOVwHjijGQDQKe6wnjKb/s640/2+%25282%2529.webp&quot; title=&quot;Sort a Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqjQdogRgVNqK1-DXiTtWGfFmU-6H7aw1stOpmkGhyphenhyphen9CH6dlcJ1moRzMCheFWjQJ8yhoLKoNOFt3Erz3Bg_9snZogDp5uOBPBlQ_Ai7TH3lMNBxSzopPDdkB6iowsW0bLnBshj1tpFcmQW/s1600/3+%25281%2529.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Sort a Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;331&quot; data-original-width=&quot;958&quot; height=&quot;220&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjqjQdogRgVNqK1-DXiTtWGfFmU-6H7aw1stOpmkGhyphenhyphen9CH6dlcJ1moRzMCheFWjQJ8yhoLKoNOFt3Erz3Bg_9snZogDp5uOBPBlQ_Ai7TH3lMNBxSzopPDdkB6iowsW0bLnBshj1tpFcmQW/s640/3+%25281%2529.webp&quot; title=&quot;Sort a Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;9. Then we have incremented temp1.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp1=temp1-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
10. At last we have incremented temp.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;(&lt;/strong&gt;&lt;/em&gt;&lt;b style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;data structures types&lt;/b&gt;&lt;em&gt;&lt;strong&gt;)&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/3734697291612518053/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/08/sort-circular-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3734697291612518053'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3734697291612518053'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/08/sort-circular-linked-list.html' title='Sort a Circular Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglw1HEdAhN_2OSMuiFVcCQRM50pdGJp9fN2reQPbK3lzfd6M7w746d-wmibKK4a89ZPPyMnff8i1eDlHrsKHzIo3c3mc3slyNrdcQxRjmLX3xcrTrqn9A5wYpbdTfG8HO0qw-VClDk7atW/s72-c/1+%25282%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-3661514480492131317</id><published>2020-08-15T06:36:00.002-07:00</published><updated>2020-08-15T06:36:56.516-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Reverse a Circular Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will be discussing how to find reverse of a circular linked list. But, if are unfamiliar with the concepts of circular linked list then click on &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-circular-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; where I have explained everything about circular linked list from basic to advance. Suppose you have a circular linked list like 1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5 and 5 pointing to 1 again. then the reverse of this list will be 5-&amp;gt;4-&amp;gt;3-&amp;gt;2-&amp;gt;1 and then 1 pointing to 5 again. Let&#39;s see the procedure for reversing this kind of list:&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Procedure&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a circular linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now we will assign 3 pointers to the first 3 nodes of the list. Because, we have to reverse the links between 2 nodes and when we will reverse the link between 2 nodes then the node which is ahead will lose its link to the next node and we will not be able to go to the next node. SO that&#39;s why we need an extra pointer pointing ahead of current node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-nZUxdCEi2WrcUnXm66Oo1Af-4YBkroI-WFONu04BVEaQ90fSO6hYALVQOcYBpMG016G-FFLOjXX4fEa9laNBLrG-Yuv0NQ4ttu0Ky9W1mesbr_t6zDEuJ71zt7BTuSV7FTkNZN4wr53b/s1600/1+%25281%2529.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Reverse a Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;343&quot; data-original-width=&quot;1083&quot; height=&quot;202&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-nZUxdCEi2WrcUnXm66Oo1Af-4YBkroI-WFONu04BVEaQ90fSO6hYALVQOcYBpMG016G-FFLOjXX4fEa9laNBLrG-Yuv0NQ4ttu0Ky9W1mesbr_t6zDEuJ71zt7BTuSV7FTkNZN4wr53b/s640/1+%25281%2529.webp&quot; title=&quot;Reverse a Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Code&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;75&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;70&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{

public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next=temp;

        }
    }

    void assignhead()
    {
        struct node* temp=head;
        while(temp-&gt;next!=NULL)
        {
            temp=temp-&gt;next;
        }
        temp-&gt;next=head;
    }

    void reverse()
    {
        struct node* temp=head;
        struct node* prev=head;
        struct node* current=head;
        current=current-&gt;next;
        temp=temp-&gt;next-&gt;next;
        while(current!=head)
        {
            current-&gt;next=prev;
            prev=current;
            current=temp;
            temp=temp-&gt;next;
        }
        head=prev;
        current-&gt;next=head;
    }

    void disp()
    {
        struct node* temp=head;
        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
        temp=temp-&gt;next;
        while(temp!=head)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(3);
    l.insert(5);
    l.insert(8);
    l.insert(1);
    l.insert(-2);
    l.insert(7);
    l.insert(-3);
    l.insert(11);
    l.insert(0);
    l.insert(6);
    l.assignhead();
    l.disp();
    l.reverse();
    l.disp();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Explanation of Code&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of circular linked list which will have 3 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the circular linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- assignhead() - to make a link between last and first node.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the circular linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the doubly linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3.&amp;nbsp;Now we have created a function named&amp;nbsp;&lt;strong&gt;reverse()&lt;/strong&gt;&amp;nbsp;for reverse the circular linked list.&lt;br /&gt;
4. First we have created 3 pointer to node.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; struct node* prev=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; struct node* current=head;&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5. Then we have assigned&amp;nbsp;&lt;strong&gt;current&lt;/strong&gt;&amp;nbsp;to the second node and&amp;nbsp;&lt;strong&gt;temp&lt;/strong&gt;&amp;nbsp;to the third node of the list.&amp;nbsp;&lt;strong&gt;prev&lt;/strong&gt;&amp;nbsp;will be pointing to first node only.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
6. Now we have started a loop which will execute until we reach the head or iterate the list once.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;while(current!=head)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;......&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;7. Now we have assigned current-&amp;gt;next to prev which means we have assigned the next node of current to prev.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;current-&amp;gt;next=prev;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;8. Then we have moved&amp;nbsp;&lt;strong&gt;prev to current , current to temp and then we have incremented temp&lt;/strong&gt;.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;prev=current;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; current=temp;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQ9xr-jyaOCJTtMfunPHVAALhbvcf5ijn4ItkB-GdbA4MX5H18TbA_TOyR5fmPYLARwbPh_Qrq_IBVbXF1hFj50exgysLHEOCJHrScebM5cabTtyIKs4QON6ZKAT7tUpr1UwKPHi2YWutb/s1600/2+%25281%2529.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Reverse a Circular Linked List&quot; border=&quot;0&quot; data-original-height=&quot;456&quot; data-original-width=&quot;1110&quot; height=&quot;262&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgQ9xr-jyaOCJTtMfunPHVAALhbvcf5ijn4ItkB-GdbA4MX5H18TbA_TOyR5fmPYLARwbPh_Qrq_IBVbXF1hFj50exgysLHEOCJHrScebM5cabTtyIKs4QON6ZKAT7tUpr1UwKPHi2YWutb/s640/2+%25281%2529.webp&quot; title=&quot;Reverse a Circular Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;9. Then after the while loop we have assigned head to prev because head should now point to the last node after reversing .&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;head=prev;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
10. At last we have assigned current-&amp;gt;next to head because current will now be at the first node and we have assigned next of current to head because this node will now become the last node.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&lt;em&gt;&lt;strong&gt;current-&amp;gt;next=head;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/3661514480492131317/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/08/reverse-circular-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3661514480492131317'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3661514480492131317'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/08/reverse-circular-linked-list.html' title='Reverse a Circular Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-nZUxdCEi2WrcUnXm66Oo1Af-4YBkroI-WFONu04BVEaQ90fSO6hYALVQOcYBpMG016G-FFLOjXX4fEa9laNBLrG-Yuv0NQ4ttu0Ky9W1mesbr_t6zDEuJ71zt7BTuSV7FTkNZN4wr53b/s72-c/1+%25281%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-2345870497294850275</id><published>2020-08-11T07:17:00.002-07:00</published><updated>2020-09-26T05:41:03.817-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#technology"/><title type='text'>What is Devops ? Explained in an Easy, Quick and Simple Way</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
You Guys might have heard of Devops. Today we will be discussing&amp;nbsp;&lt;b&gt;What is Devops ? Explained in an Easy, Quick and Simple Way&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;b style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp;&lt;/b&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;and we&#39;ll see if it is the right choice for you or not.&amp;nbsp;Now if you search on Internet about what is devops then you will get thousands of results but i&#39;m pretty sure that none of them gave you the exact definition of devops.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Devops - Definition&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRJwmkObE8vj3scGnwpp0jhO9jMnpjxIwpUfTj_K3dV98KC7IzxZyWU5toNIKHDe98zRyrabneHB_eLivzatcFeqhXJsYkc_GZwspm-7afH_g9T7EvBQdAHVokulwBWFHnHWodLITV-eyw/s1600/2f23cbc2de50148af11408a92779c77f.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;What is Devops ? Explained for Beginners&quot; border=&quot;0&quot; data-original-height=&quot;282&quot; data-original-width=&quot;564&quot; height=&quot;320&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRJwmkObE8vj3scGnwpp0jhO9jMnpjxIwpUfTj_K3dV98KC7IzxZyWU5toNIKHDe98zRyrabneHB_eLivzatcFeqhXJsYkc_GZwspm-7afH_g9T7EvBQdAHVokulwBWFHnHWodLITV-eyw/s640/2f23cbc2de50148af11408a92779c77f.webp&quot; title=&quot;What is Devops ? Explained for Beginners&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;b&gt;Devops&lt;/b&gt; is not a tool or a programming language. It is the process of taking your product (website or an app) from its construction till the production state. DevOps is a method which helps the development team as well as the operational teams to do things in sync without panicking.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
The&amp;nbsp;&lt;strong&gt;Development team&lt;/strong&gt;&amp;nbsp;is responsible for writing code for application. The&amp;nbsp;&lt;strong&gt;Operational team&lt;/strong&gt;&amp;nbsp;is responsible for managing the server operations, security operation, backups.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
DevOps is a philosophy in which the&amp;nbsp; Development as well as the Operational team sits together and work in sync. Hence in this way the operational team can keep a check on development team and vica-versa. DevOps also focuses on the things to be automated so that the user does not have to do much and they can even while working with the application.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
DevOps includes many tools such as Jenkins, Docker, Github, Vagrant and a lot more. So DevOps is not about learning a particular tool because it is moreover a mindset. So, if you want yourself to be called DevOps Engineer then learning any of the tools won&#39;t help you much because it is more about understanding what is happening in every phase of Software Development Cycle.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Now there are position for a&amp;nbsp;&lt;strong&gt;DevOps manager&lt;/strong&gt;&amp;nbsp;in various companies. A DevOps manager is the person who has previous experience of merging the Development and Operational team and knows how to sync the 2 teams.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;i&gt;I hope now you have got the gist of &lt;b&gt;DevOps&lt;/b&gt;. Thanks for reading !!!&lt;/i&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/2345870497294850275/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/08/what-is-devops-explained-for-beginners.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2345870497294850275'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2345870497294850275'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/08/what-is-devops-explained-for-beginners.html' title='What is Devops ? Explained in an Easy, Quick and Simple Way'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgRJwmkObE8vj3scGnwpp0jhO9jMnpjxIwpUfTj_K3dV98KC7IzxZyWU5toNIKHDe98zRyrabneHB_eLivzatcFeqhXJsYkc_GZwspm-7afH_g9T7EvBQdAHVokulwBWFHnHWodLITV-eyw/s72-c/2f23cbc2de50148af11408a92779c77f.webp" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-4120087991235971854</id><published>2020-08-06T07:52:00.001-07:00</published><updated>2021-07-01T23:28:20.755-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#webdevelopment"/><title type='text'>How to Become A Full Stack Web Developer</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;b&gt;&lt;i&gt;What is Full Stack Web Development?&lt;/i&gt;&lt;/b&gt; &lt;b&gt;&lt;i&gt;How to become a Full Stack Web Developer?&lt;/i&gt;&lt;/b&gt; &lt;b&gt;&lt;i&gt;How many programming languages does one need to know to become a full-Stack Web Developer?&lt;/i&gt;&lt;/b&gt; These are the most common question among the students or programmers who want to start with web development. In this article, I will be discussing everything from what Full Stack Development means to complete roadmap to become a Full Stack Web Developer.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
What is Full Stack Development?&lt;/h2&gt;
&lt;div&gt;
It refers to the development of both front-end (client-side) as well as the back-end (server-side) portions of a web application.&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH-w_BZdAD2MLWf3kx8TLuevXuNmgjRUgRfH8f4LZcHqumOLoVmnoebfNG1Hk-1CQsP1bkhRRqeiBY1tAMBP3j_ED7_93YFsbZEfkeQPClV1ri_YxqhLoxMXz1OLAMmPeQoQx4aSejJo3b/s1600/project-2020-09-06_09-09_AM.jpg&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;How to Become A Full Stack Web Developer?&quot; border=&quot;0&quot; data-original-height=&quot;1275&quot; data-original-width=&quot;789&quot; height=&quot;640&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH-w_BZdAD2MLWf3kx8TLuevXuNmgjRUgRfH8f4LZcHqumOLoVmnoebfNG1Hk-1CQsP1bkhRRqeiBY1tAMBP3j_ED7_93YFsbZEfkeQPClV1ri_YxqhLoxMXz1OLAMmPeQoQx4aSejJo3b/s640/project-2020-09-06_09-09_AM.jpg&quot; title=&quot;How to Become A Full Stack Web Developer?&quot; width=&quot;396&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;Use Below Code to Embed this Info-graphic&lt;/b&gt;&lt;br /&gt;

&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;20&quot;&gt;&lt;p&gt;&lt;strong&gt;Please include attribution to https://geektech1717.blogspot.com/ with this graphic.&lt;/strong&gt;&lt;br /&gt;&lt;br /&gt;&lt;a href=&#39;https://geektech1717.blogspot.com/2020/08/how-to-become-full-stack-developer.html&#39;&gt;&lt;img src=&#39;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH-w_BZdAD2MLWf3kx8TLuevXuNmgjRUgRfH8f4LZcHqumOLoVmnoebfNG1Hk-1CQsP1bkhRRqeiBY1tAMBP3j_ED7_93YFsbZEfkeQPClV1ri_YxqhLoxMXz1OLAMmPeQoQx4aSejJo3b/s1600/project-2020-09-06_09-09_AM.jpg&#39; alt=&#39;How to Become A Full Stack Web Developer&#39; 540px border=&#39;0&#39; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;/textarea&gt;
&lt;/form&gt;


&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
How to Become A Full Stack Web Developer?&lt;/h2&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
First we need to understand&amp;nbsp;Front-end Development and Back-end&amp;nbsp;Development.&lt;/div&gt;
&lt;ul style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;li&gt;&lt;b&gt;Front-end Development&lt;/b&gt; means how your website is laid out to users on both web browsers and mobile devices. A front-end Developer must have knowledge of HTML, CSS, and Javascript. HTML forms the structure of the website. CSS is for making the website look good to the users. Javascript adds more features that you can use in your website like hover effects, lighters.&lt;/li&gt;
&lt;li&gt;&lt;b&gt;Back-end Development&lt;/b&gt; handles the functionality of your website. A back-end developer must have knowledge of scripting languages like PHP or Ruby or Python. A back-end developer must be able to write code to receive data from user and manipulate it. A backend developer also needs to know about databases because data received from users must be stored somewhere. So backend developer must have knowledge of query languages like MySQL or MongoDB.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Learning Front-end Development and Backend&amp;nbsp;Development is your first step to becoming a Full Stack Developer.&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Now a vast variety of websites use on the&amp;nbsp;&lt;b&gt;Linux operating system&lt;/b&gt;. So a Full Stack Web developer must know how to work with Linux OS. If you want to learn&lt;b&gt; Linux command&lt;/b&gt; the best platform will be &lt;a href=&quot;https://ubuntu.com/tutorials/command-line-for-beginners#1-overview&quot; target=&quot;_blank&quot;&gt;Ubuntu Linux Commands&lt;/a&gt;. This will be your second step to become a Full Stack Web Developer.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Now you need to learn about &lt;b&gt;Version Control Tools&lt;/b&gt; like GitHub, BeanStalk, or GitLab. If you want to know &lt;b&gt;what is git&lt;/b&gt; or &lt;b&gt;what is GitHub&lt;/b&gt; then you can refer to this &lt;a href=&quot;https://www.youtube.com/watch?v=OdbBmvfThJY&quot; target=&quot;_blank&quot;&gt;video&lt;/a&gt;&lt;b&gt;.&lt;/b&gt; This makes your entire workflow seamless. A Version Control System includes many tools which help you to record changes you made by keeping track of modifications made. This will be your final step&amp;nbsp;to become a Full Stack Web Developer.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Best resources to become Full Stack Developer&lt;/h2&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
To Learn HTML, CSS for free you can refer to these links:-&lt;/div&gt;
&lt;br /&gt;
&lt;ul style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px; text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://www.udemy.com/course/web-development-learn-by-doing-html5-css3-from-scratch-introductory/?LSNPUBID=JVFxdTr9V80&amp;amp;ranEAID=JVFxdTr9V80&amp;amp;ranMID=39197&amp;amp;ranSiteID=JVFxdTr9V80-tjaQE7HK.B7CBBm4rW5zNw&amp;amp;utm_medium=udemyads&amp;amp;utm_source=aff-campaign&quot; target=&quot;_blank&quot;&gt;Web Development By Doing: HTML / CSS from Scratch&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.udemy.com/course/how-i-landed-a-web-development-job-earned-5k-freelancing/?LSNPUBID=JVFxdTr9V80&amp;amp;ranEAID=JVFxdTr9V80&amp;amp;ranMID=39197&amp;amp;ranSiteID=JVFxdTr9V80-9aTjiqlS9JKQ.8t.d0zP2g&amp;amp;utm_medium=udemyads&amp;amp;utm_source=aff-campaign&quot; target=&quot;_blank&quot;&gt;Learn HTML And CSS: How to start your web development career&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.udemy.com/course/master-the-basics-of-html5-css3-beginner-web-development/?LSNPUBID=JVFxdTr9V80&amp;amp;ranEAID=JVFxdTr9V80&amp;amp;ranMID=39197&amp;amp;ranSiteID=JVFxdTr9V80-.sUoP06m0IhUMnUqswkkgQ&amp;amp;utm_medium=udemyads&amp;amp;utm_source=aff-campaign&quot; target=&quot;_blank&quot;&gt;Master the basics of HTML5 and CSS3&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;div&gt;&lt;span style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, trebuchet ms;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;To&lt;b&gt; learn javascript &lt;/b&gt;for Free you can refer to these links:-&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=PkZNo7MFNFg&quot; target=&quot;_blank&quot;&gt;Learn JavaScript: FreeCodeCamp&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;color: #888888;&quot;&gt;&lt;a href=&quot;https://channel9.msdn.com/Series/Javascript-Fundamentals-Development-for-Absolute-Beginners&quot; target=&quot;_blank&quot;&gt;Javascript Fundamental&lt;/a&gt;&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
To learn about Linux OS you can refer to these links:-&lt;br /&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://www.youtube.com/watch?v=wBp0Rb-ZJak&quot; target=&quot;_blank&quot;&gt;The complete Linux Course&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://training.linuxfoundation.org/resources/?_sft_content_type=free-course&quot; target=&quot;_blank&quot;&gt;The Linux Foundation Training&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
To learn Version Control Systems you can refer to these links:-&lt;/div&gt;
&lt;div&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;a href=&quot;https://www.udemy.com/course/git-started-with-github/?LSNPUBID=JVFxdTr9V80&amp;amp;ranEAID=JVFxdTr9V80&amp;amp;ranMID=39197&amp;amp;ranSiteID=JVFxdTr9V80-4ttZxcQuDJkwTrse27YP_g&amp;amp;utm_medium=udemyads&amp;amp;utm_source=aff-campaign&quot; target=&quot;_blank&quot;&gt;Learn GitHub&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;https://www.atlassian.com/git/tutorials/learn-git-with-bitbucket-cloud&quot; target=&quot;_blank&quot;&gt;Learn git with BitBucket&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/4120087991235971854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/08/how-to-become-full-stack-developer.html#comment-form' title='108 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/4120087991235971854'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/4120087991235971854'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/08/how-to-become-full-stack-developer.html' title='How to Become A Full Stack Web Developer'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgH-w_BZdAD2MLWf3kx8TLuevXuNmgjRUgRfH8f4LZcHqumOLoVmnoebfNG1Hk-1CQsP1bkhRRqeiBY1tAMBP3j_ED7_93YFsbZEfkeQPClV1ri_YxqhLoxMXz1OLAMmPeQoQx4aSejJo3b/s72-c/project-2020-09-06_09-09_AM.jpg" height="72" width="72"/><thr:total>108</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-5964378908336775697</id><published>2020-07-31T07:26:00.000-07:00</published><updated>2020-08-15T06:07:09.962-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Reverse a Doubly Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Today we will be discussing how to reverse a doubly linked list. But if you are new to doubly linked list then please refer to&lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-doubly-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt; this link&lt;/a&gt; where I have explained everything about doubly linked list from basic to advance.&lt;br /&gt;
Reversing a doubly linked list is a tricky problem which is asked in many interviews. In a doubly linked list every node has 2 pointers - one pointing to next node and the other one pointing to previous node. Many students forget the fact that doubly linked list is a special type of linked list which can be reversed much easier than a singly linked list.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;u&gt;Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
1. First we will make a linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
2. Then we just have to iterate the doubly linked list till the end but make sure to initialize the&amp;nbsp;&lt;strong&gt;previous pointer of the first node&lt;/strong&gt;&amp;nbsp;to NULL because after reversing the list the first node will become the last node and last node must point to NULL.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
3. When we reach the last node we assign&amp;nbsp;&lt;strong&gt;head pointer the address of last node&amp;nbsp;&lt;/strong&gt;.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_WskP9BanQkUifY96OsxpFI54qTAk_B9Qb5PJDYST5MtUHY7b-iAtLXT_vF8CYFwczMQeM2J4ANxYjVUaYz8BB-aFRPBTnI3vtqBv0nGfmDYF0SSiShn3kMeWqEGt9F-A5fZVGYNYFe4N/s1600/weewewew.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Reverse a Doubly Linked List&quot; border=&quot;0&quot; data-original-height=&quot;745&quot; data-original-width=&quot;1600&quot; height=&quot;298&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_WskP9BanQkUifY96OsxpFI54qTAk_B9Qb5PJDYST5MtUHY7b-iAtLXT_vF8CYFwczMQeM2J4ANxYjVUaYz8BB-aFRPBTnI3vtqBv0nGfmDYF0SSiShn3kMeWqEGt9F-A5fZVGYNYFe4N/s640/weewewew.webp&quot; title=&quot;Reverse a Doubly Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
4. Now you can iterate the list from head and&lt;strong&gt;&amp;nbsp;instead of using next pointer to iterate the list use previous pointer to iterate the list&lt;/strong&gt;&amp;nbsp;and you will get the list in reverse order.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;text-decoration: underline;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;text-decoration-line: underline;&amp;quot;}&quot;&gt;&lt;u&gt;Code&lt;/u&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;text-decoration: underline;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;text-decoration-line: underline;&amp;quot;}&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;form&gt;
&lt;textarea cols=&quot;75&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;70&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
    struct node* prev;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        temp-&gt;prev=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;
            temp-&gt;prev =temp1;
            temp-&gt;next=NULL;

        }
    }

    void reverse()
    {
        struct node* temp=head;
        temp-&gt;prev=NULL;
        while(temp-&gt;next!=NULL)
        {
            temp=temp-&gt;next;
        }
        head=temp;

        temp=head;
        cout&lt;&lt;&quot;reversed doubly linked list: &quot;;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;prev;
        }
        cout&lt;&lt;endl;
    }

    void disp()
    {
        struct node* temp=head;
        cout&lt;&lt;&quot;original linked list: &quot;;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(1);
    l.insert(2);
    l.insert(4);
    l.insert(5);
    l.insert(6);
    l.insert(8);
    l.insert(9);
    l.insert(18);
    l.disp();
    l.reverse();
    return 0;
}


&lt;/textarea&gt;&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;text-decoration: underline;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;text-decoration-line: underline;&amp;quot;}&quot;&gt;
&lt;/span&gt;&lt;/form&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;text-decoration: underline;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;text-decoration-line: underline;&amp;quot;}&quot;&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/h3&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Explanation of Code&lt;/u&gt;&lt;span data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h3&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of doubly linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the doubly linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the doubly linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the doubly linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3.&amp;nbsp;Now we have created a function named&amp;nbsp;&lt;strong&gt;reverse()&lt;/strong&gt;&amp;nbsp;for reverse the doubly linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. First we have created a pointer to node named&amp;nbsp;&lt;strong&gt;temp&lt;/strong&gt;&amp;nbsp;initialized to head of the list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5. Then we have&amp;nbsp;&lt;strong&gt;initialized the previous of temp to NULL&lt;/strong&gt;&amp;nbsp;because after reversing the list this node will become the last node of the list so its prev should be initialized to NULL.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp-&amp;gt;prev=NULL;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;6. Then have iterated the list till the last node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp-&amp;gt;next!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;7. Then we have&amp;nbsp;&lt;strong&gt;assigned head pointer the address of temp&amp;nbsp;&lt;/strong&gt;because after reversing the list the last node will be the first node and first node&#39;s address is contained in head pointer.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&lt;strong&gt;&lt;em&gt;head=temp;&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;8. Next we have&amp;nbsp;&lt;strong&gt;printed the data of list&lt;/strong&gt;&amp;nbsp;but in the loop&amp;nbsp;&lt;strong&gt;we have used prev pointer to access the next node&lt;/strong&gt;&amp;nbsp;because in a doubly list we have 2 pointers for every node so we can traverse it from both the ends.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;while(temp!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;temp-&amp;gt;data&amp;lt;&amp;lt;&quot; &quot;;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;prev;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/5964378908336775697/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/07/reverse-doubly-linked-list.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/5964378908336775697'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/5964378908336775697'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/07/reverse-doubly-linked-list.html' title='Reverse a Doubly Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg_WskP9BanQkUifY96OsxpFI54qTAk_B9Qb5PJDYST5MtUHY7b-iAtLXT_vF8CYFwczMQeM2J4ANxYjVUaYz8BB-aFRPBTnI3vtqBv0nGfmDYF0SSiShn3kMeWqEGt9F-A5fZVGYNYFe4N/s72-c/weewewew.webp" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-6719368789451338446</id><published>2020-07-25T06:26:00.002-07:00</published><updated>2020-07-25T06:26:43.983-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Find Pair with given sum in Doubly Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Today we will be discussing&amp;nbsp;how to&amp;nbsp;Find Pair of elements from doubly linked list with given sum. But if you are new to doubly linked list then please refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-doubly-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt;&amp;nbsp;where I have explained everything about doubly linked list. Suppose you have a doubly linked list like 1-2-3-4-5 and you have to find pair of elements which adds up to 5 then you should get the output as (1,4) and (2,3). Now let&#39;s discuss the procedure:&lt;br /&gt;
&lt;br data-mce-bogus=&quot;1&quot; /&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;u&gt;Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Then we will iterate the list by using 2 loops.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. Suppose the outer loop is run by a variable &#39;x&#39; and inner loop is run by a variable &#39;y&#39;.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. Thus &#39;x&#39; will start iterating from 1st node till 2nd last node of the list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5. And &#39;y&#39; will iterate from next node of &#39;x&#39; till the last node of the list.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH-D_9AYm4pSVvdQY62HDt3vEYBTNyPOSk_trZ3eqHN4QZsQTZcNxseRdAlEH6r1O3kj_DuxQWt1rmgyMKjOv66sD7oGyeZSm78Ir88ndQ5NE00XZc49JfOTgztedOoYuXrXWoDppFFjLV/s1600/sum.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Find Pair with given sum in Doubly Linked List&quot; border=&quot;0&quot; data-original-height=&quot;510&quot; data-original-width=&quot;1096&quot; height=&quot;296&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH-D_9AYm4pSVvdQY62HDt3vEYBTNyPOSk_trZ3eqHN4QZsQTZcNxseRdAlEH6r1O3kj_DuxQWt1rmgyMKjOv66sD7oGyeZSm78Ir88ndQ5NE00XZc49JfOTgztedOoYuXrXWoDppFFjLV/s640/sum.webp&quot; title=&quot;Find Pair with given sum in Doubly Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
6. In this way each pair of node will be compared and we can easily find the pair of nodes with given sum.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;u&gt;Code&lt;/u&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;75&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;70&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;

        }
    }


    void sum(int s)
    {
        struct node* temp=head;
        while(temp-&gt;next!=NULL)
        {
            struct node* temp1=temp-&gt;next;
            while(temp1!=NULL)
            {
                if(temp-&gt;data + temp1-&gt;data ==s)
                {
                    cout&lt;&lt;&quot;(&quot;&lt;&lt;temp-&gt;data&lt;&lt;&quot;,&quot;&lt;&lt;temp1-&gt;data&lt;&lt;&quot;)&quot;&lt;&lt;endl;
                }
                temp1=temp1-&gt;next;
            }
            temp=temp-&gt;next;
        }

    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(5);
    l.insert(3);
    l.insert(-4);
    l.insert(7);
    l.insert(-6);
    l.insert(0);
    l.insert(9);
    l.insert(18);
    l.disp();

    int s;
    cout&lt;&lt;&quot;Enter sum that you want to find in the doubly linked list: &quot;;
    cin&gt;&gt;s;
    l.sum(s);
    return 0;
}




&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Explanation of Code&lt;/u&gt;&lt;span data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h3&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of doubly linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the doubly linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the doubly linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the doubly linked list.&lt;br /&gt;
3. Now we have created a function named&amp;nbsp;&lt;strong&gt;sum()&lt;/strong&gt;&amp;nbsp;for find pair of nodes which adds up to given sum. It takes an argument &#39;s&#39; which contain desired sum we have to find.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. Then we have created a pointer to node variable &#39;temp&#39; which is initialized to head.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5. Then we have used a while loop to iterate temp from 1st node till 2nd last node of the list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp-&amp;gt;next!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ----------&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;6. Now we have created another pointer to node which is temp1 and initialized it with next node of temp.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;struct node* temp1=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;7. Next we have iterated temp1 till the end of the list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp1!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; {&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;-----------&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;8. Inside temp1 &#39;s while loop we have placed a if condition to check if the sum of data at temp and data at temp1 is equal to the sum &#39;s&#39; passed in the function.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;if(temp-&amp;gt;data + temp1-&amp;gt;data ==s)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; ----------&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;9. If the sum of temp&#39;s data and temp1 &#39;s data is equal to s the we have printed the pair of nodes inside if condition.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;cout&amp;lt;&amp;lt;&quot;(&quot;&amp;lt;&amp;lt;temp-&amp;gt;data&amp;lt;&amp;lt;&quot;,&quot;&amp;lt;&amp;lt;temp1-&amp;gt;data&amp;lt;&amp;lt;&quot;)&quot;&amp;lt;&amp;lt;endl;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;10. Then we have incremented temp1.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;temp1=temp1-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
11. Outside this loop we have incremented temp.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/6719368789451338446/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/07/find-pair-with-given-sum-in-doubly.html#comment-form' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6719368789451338446'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6719368789451338446'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/07/find-pair-with-given-sum-in-doubly.html' title='Find Pair with given sum in Doubly Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjH-D_9AYm4pSVvdQY62HDt3vEYBTNyPOSk_trZ3eqHN4QZsQTZcNxseRdAlEH6r1O3kj_DuxQWt1rmgyMKjOv66sD7oGyeZSm78Ir88ndQ5NE00XZc49JfOTgztedOoYuXrXWoDppFFjLV/s72-c/sum.webp" height="72" width="72"/><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-6168805807424649347</id><published>2020-07-21T07:09:00.002-07:00</published><updated>2021-06-03T23:32:06.359-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>How to find Middle Element of Doubly Linked List?</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will be discussing&amp;nbsp;how to find middle element of doubly linked list. But if you are new to doubly linked list then please refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2020/01/what-is-doubly-linked-list-in-data.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; where I have explained everything about linked lists. Suppose you have a doubly linked list like 1-2-3-4-5 then the middle of this list will be 3. So, let&#39;s discuss the procedure to solve this problem.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br data-mce-bogus=&quot;1&quot; /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Then we will count the number of nodes in the doubly linked list.&lt;/div&gt;
&lt;ul style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;li&gt;A list which will have even number of nodes will have 2 middle nodes at position &#39;n/2&#39; and &#39;n/2+1&#39; (n is number of nodes in a list).&lt;/li&gt;
&lt;li&gt;A list which will have odd number of nodes will have 1 middle element only at position (n+1)/2&amp;nbsp;(n is number of nodes in a list).&lt;/li&gt;
&lt;/ul&gt;
&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkgRCZlXZRV6HAHUDt5bSQYNTfuVrNWcVvf6jqiBYk_F0wvgkeQJcRg4JIu-Xca7zWgasraQLlPY9psaJtHn4HQ_YJ0vMJg7hgY_hFF6NHg79xEw-P8FKzdzDAjOqW2oSw2mRLv7syDdMM/s1600/middle.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;How to find Middle Element of Doubly Linked List?&quot; border=&quot;0&quot; data-original-height=&quot;697&quot; data-original-width=&quot;1569&quot; height=&quot;284&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkgRCZlXZRV6HAHUDt5bSQYNTfuVrNWcVvf6jqiBYk_F0wvgkeQJcRg4JIu-Xca7zWgasraQLlPY9psaJtHn4HQ_YJ0vMJg7hgY_hFF6NHg79xEw-P8FKzdzDAjOqW2oSw2mRLv7syDdMM/s640/middle.webp&quot; title=&quot;How to find Middle Element of Doubly Linked List?&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
&lt;div&gt;
&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;/ul&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. Then depending on even or odd number of nodes we will iterate the list till the positions mentioned above and print the values.&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node{

  int data;
  struct node* next;
  struct node* prev;

};
struct node* head=NULL;

class doubly
{
    public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        if(head==NULL)
        {
            head=temp;
            temp-&gt;prev=NULL;
            temp-&gt;next=NULL;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp-&gt;prev=temp1;
            temp1-&gt;next=temp;
            temp-&gt;next=NULL;
        }
    }

    void middle()
    {
        struct node* temp=head;
        int c=0;
        while(temp!=NULL)
        {
            c++;
            temp=temp-&gt;next;
        }

        temp=head;
        int p=1;
        if(c%2==0)
        {
            int mid=c/2;
            while(temp!=NULL)
            {
                if(p==mid)
                    break;
                p++;
                temp=temp-&gt;next;
            }
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;&lt;&lt;temp-&gt;next-&gt;data&lt;&lt;endl;
        }
        else
        {
            int mid=(c+1)/2;
            while(temp!=NULL)
            {
                if(p==mid)
                    break;
                p++;
                temp=temp-&gt;next;
            }
            cout&lt;&lt;temp-&gt;data&lt;&lt;endl;
        }
    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
    }
};

int main()
{
    doubly l;
    l.insert(3);
    l.insert(5);
    l.insert(1);
    l.insert(4);
    l.insert(8);
    l.insert(7);
    l.insert(0);
    l.insert(2);
    l.disp();
    cout&lt;&lt;endl;
    l.middle();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br data-mce-bogus=&quot;1&quot; /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;font-weight: normal;&quot;&gt;&lt;u&gt;Explanation of Code (line by line)&lt;/u&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the doubly linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the doubly linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the doubly linked list.&lt;br /&gt;
3. Now we have created a function named&amp;nbsp;&lt;strong&gt;middle()&amp;nbsp;&lt;/strong&gt;which will print the middle element of doubly linked list.&lt;br /&gt;
4. First we have created a &#39;temp&#39; which is pointer to node. Then we have created some other variables like &#39;c=0&#39;, &#39;mid&#39;, &#39;p=1&#39;.&lt;br /&gt;
5. Then we have started a while loop to count number of nodes in the list. The number of nodes are stored in variable &#39;c&#39;.&lt;br /&gt;
6. Then we have initialized temp to head.&lt;br /&gt;
7. Then there is an if condition which checks if c%2==0 or not i.e number of nodes in the list are even or not.&lt;br /&gt;
8. If the number of nodes in the list are even then we have stored the middle value c/2 in a variable mid.&lt;br /&gt;
9. Then we have started a while loop which will iterate till &#39;mid&#39; in the list. If &#39;p&#39; is equal to &#39;mid&#39; then we will break out of the loop because we have reached the middle of the list. Otherwise we will increment p and temp.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;while(temp!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(p==mid)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; p++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
10. Then we have printed the middle values. First we have printed&amp;nbsp;&lt;strong&gt;temp-&amp;gt;data&lt;/strong&gt;&amp;nbsp;which will display the first middle node the we have printed the second middle value by using&amp;nbsp;&lt;strong&gt;temp-&amp;gt;next-&amp;gt;data&lt;/strong&gt;&amp;nbsp;which will point to the data part of the next of the first middle value.&lt;br /&gt;
11. If the number of nodes in the list are not even the they will be odd. So the next if condition where we have checked if c%2!=0 is for odd number of nodes.&lt;br /&gt;
12.&amp;nbsp;&amp;nbsp;If the number of nodes in the list are odd then we have stored the middle value (c+1)/2 in a variable mid.&lt;br /&gt;
13. Then we have started a while loop which will iterate till &#39;mid&#39; in the list. If &#39;p&#39; is equal to &#39;mid&#39; then we will break out of the loop because we have reached the middle of the list. Otherwise we will increment p and temp.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(p==mid)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;p++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
14. Then we have simply printed the value at mid by using temp-&amp;gt;data which is the middle element.&lt;br /&gt;
&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;b&gt;&lt;u&gt;You can follow me here if you are interested :&amp;nbsp;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://github.com/mayank171&quot; target=&quot;_blank&quot;&gt;https://github.com/mayank171&lt;/a&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;&lt;br /&gt;&lt;/b&gt;&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;&lt;u&gt;&lt;a href=&quot;https://www.linkedin.com/in/mayank-mewar-586090173/&quot; target=&quot;_blank&quot;&gt;https://www.linkedin.com/in/mayank-mewar-586090173/&lt;/a&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/6168805807424649347/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/07/how-to-find-middle-element-of-doubly.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6168805807424649347'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6168805807424649347'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/07/how-to-find-middle-element-of-doubly.html' title='How to find Middle Element of Doubly Linked List?'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgkgRCZlXZRV6HAHUDt5bSQYNTfuVrNWcVvf6jqiBYk_F0wvgkeQJcRg4JIu-Xca7zWgasraQLlPY9psaJtHn4HQ_YJ0vMJg7hgY_hFF6NHg79xEw-P8FKzdzDAjOqW2oSw2mRLv7syDdMM/s72-c/middle.webp" height="72" width="72"/><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-8527899367187616998</id><published>2020-07-16T05:42:00.000-07:00</published><updated>2020-07-19T05:49:38.820-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Middle element of a Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will be discussing the algorithm to print middle element of a linked list. If you are new to linked list data structure then please refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; where I have explained everything about linked list. Suppose you have a list like 1-&amp;gt;4-&amp;gt;2-&amp;gt;8-&amp;gt;5 then the middle element of this list will be 2. SO we will write a program which will do so but first let&#39;s discuss the procedure.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a linked list.&lt;br /&gt;
2. Then we will count the number of nodes in this list.&lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;A list which will have even number of nodes will have 2 middle nodes at position &#39;n/2&#39; and &#39;n/2+1&#39; (n is number of nodes in a list).&lt;/li&gt;
&lt;li&gt;A list which will have odd number of nodes will have 1 middle element only at position (n+1)/2&amp;nbsp;(n is number of nodes in a list).&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEju_2YNOL7KJM6cdYiuaD8YJkz2Jr4qChiAGPYYCHBDfZQwfjSuer51DU_eazwtcg7s52RONTTZHM__hgwe3eu9mFIMFmikOtkZX1DvPS8GOLTJgHUVwwwL8fLFBbGf-UrGEmix2ZW8o_w2/s1600/today+%25281%2529.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;middle element of a linked list&quot; border=&quot;0&quot; data-original-height=&quot;595&quot; data-original-width=&quot;1075&quot; height=&quot;354&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEju_2YNOL7KJM6cdYiuaD8YJkz2Jr4qChiAGPYYCHBDfZQwfjSuer51DU_eazwtcg7s52RONTTZHM__hgwe3eu9mFIMFmikOtkZX1DvPS8GOLTJgHUVwwwL8fLFBbGf-UrGEmix2ZW8o_w2/s640/today+%25281%2529.webp&quot; title=&quot;middle element of a linked list&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
3. Then depending on even or odd number of nodes we will iterate the list till the positions mentioned above and print the values.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br data-mce-bogus=&quot;1&quot; /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;u&gt;Code&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;

        }
    }


    void middle()
    {
        struct node* temp=head;
        int c=0;
        int mid1;
        int p=1;
        while(temp!=NULL)
        {
            temp=temp-&gt;next;
            c++;
        }
        temp=head;
        if(c%2==0)
        {
            mid1=c/2;
            while(temp!=NULL)
            {
                if(p==mid1)
                    break;
                p++;
                temp=temp-&gt;next;
            }
            cout&lt;&lt;&quot;Middle values are &quot;&lt;&lt;temp-&gt;data&lt;&lt;&quot;and&quot;&lt;&lt;temp-&gt;next-&gt;data&lt;&lt;endl;
        }
        else if(c%2!=0)
        {
            mid1=(c+1)/2;
            while(temp!=NULL)
            {
                if(p==mid1)
                    break;
                p++;
                temp=temp-&gt;next;
            }
            cout&lt;&lt;&quot;Middle values are &quot;&lt;&lt;temp-&gt;data&lt;&lt;endl;
        }
    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(2);
    l.insert(5);
    l.insert(21);
    l.insert(46);
    l.insert(48);
    l.insert(54);
    l.insert(10);
    l.insert(6);
    l.insert(18);
    l.insert(-4);


    l.disp();
    l.middle();
    return 0;
}




&lt;/textarea&gt;
&lt;/form&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;u&gt;Explanation of Code&lt;/u&gt;&lt;span style=&quot;font-weight: 400;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;font-weight: 400;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Insert some values in the linked list.&lt;br /&gt;
3. Now we have created a function named&amp;nbsp;&lt;strong&gt;middle()&amp;nbsp;&lt;/strong&gt;which will print the middle element of a linked list.&lt;br /&gt;
4. First we have created a &#39;temp&#39; which is pointer to node. Then we have created some other variables like &#39;c=0&#39;, &#39;mid1&#39;, &#39;p=1&#39;.&lt;br /&gt;
5. Then we have started a while loop to count number of nodes in the list. The number of nodes are stored in variable &#39;c&#39;.&lt;br /&gt;
6. Then we have initialized temp to head.&lt;br /&gt;
7. Then there is an if condition which checks if c%2==0 or not i.e number of nodes in the list are even or not.&lt;br /&gt;
8. If the number of nodes in the list are even then we have stored the middle value c/2 in a variable mid1.&lt;br /&gt;
9. Then we have started a while loop which will iterate till &#39;mid1&#39; in the list. If &#39;p&#39; is equal to &#39;mid1&#39; then we will break out of the loop because we have reached the middle of the list. Otherwise we will increment p and temp.&lt;br /&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp;while(temp!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if(p==mid1)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; p++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
10. Then we have printed the middle values. First we have printed&amp;nbsp;&lt;strong&gt;temp-&amp;gt;data&lt;/strong&gt;&amp;nbsp;which will display the first middle node the we have printed the second middle value by using&amp;nbsp;&lt;strong&gt;temp-&amp;gt;next-&amp;gt;data&lt;/strong&gt;&amp;nbsp;which will point to the data part of the next of the first middle value.&lt;br /&gt;
11. If the number of nodes in the list are not even the they will be odd. So the next if condition where we have checked if c%2!=0 is for odd number of nodes.&lt;br /&gt;
12.&amp;nbsp;&amp;nbsp;If the number of nodes in the list are odd then we have stored the middle value (c+1)/2 in a variable mid1.&lt;br /&gt;
13. Then we have started a while loop which will iterate till &#39;mid1&#39; in the list. If &#39;p&#39; is equal to &#39;mid1&#39; then we will break out of the loop because we have reached the middle of the list. Otherwise we will increment p and temp.&lt;br /&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;while(temp!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(p==mid1)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;p++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong&gt;&lt;/strong&gt;14. Then we have simply printed the value at mid1 by using temp-&amp;gt;data which is the middle element.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/8527899367187616998/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/07/middle-element-of-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/8527899367187616998'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/8527899367187616998'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/07/middle-element-of-linked-list.html' title='Middle element of a Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEju_2YNOL7KJM6cdYiuaD8YJkz2Jr4qChiAGPYYCHBDfZQwfjSuer51DU_eazwtcg7s52RONTTZHM__hgwe3eu9mFIMFmikOtkZX1DvPS8GOLTJgHUVwwwL8fLFBbGf-UrGEmix2ZW8o_w2/s72-c/today+%25281%2529.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-6344058755755714464</id><published>2020-07-11T06:17:00.002-07:00</published><updated>2021-07-05T07:07:12.239-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Sorting of Linked List in Increasing Order</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;span face=&quot;sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;&quot; style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;Today we will be discussing how to&amp;nbsp;sorting of linked list in increasing order. But if you are unfamiliar with linked list then refer to &lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; where I have explained you about linked list from basic to advance. Suppose we have a linked list 4-&amp;gt;2-&amp;gt;5-&amp;gt;8-&amp;gt;1 then after sorting it should look like 1-&amp;gt;2-&amp;gt;4-&amp;gt;5-&amp;gt;8. So, let&#39;s discuss the procedure to solve the problem.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;
&lt;u&gt;
Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;1. First we will make a linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now we have many sorting algorithms like bubble sort, insertion sort, selection sort etc. We will be using bubble sort for sorting our list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. Now I hope you guys know how bubble sort works in arrays. If you do not know then refer to &lt;a href=&quot;https://www.geeksforgeeks.org/bubble-sort/&quot; target=&quot;_blank&quot;&gt;this link&lt;/a&gt; .&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. We will use bubble to sort linked list in the similar way we use it to sort an array.&lt;/div&gt;&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/div&gt;&lt;div style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #800180;&quot;&gt;SEE ALSO :&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;span style=&quot;color: #888888;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;span color=&quot;var(--post-title-color)&quot; face=&quot;&amp;quot;Work Sans&amp;quot;, Arial, sans-serif&quot; style=&quot;background-color: white; font-size: small;&quot;&gt;&lt;a href=&quot;https://geektech1717.blogspot.com/2019/06/20-easy-intermediate-advanced-python.html&quot; target=&quot;_blank&quot;&gt;&lt;i&gt;20+ Difficulty Wise Python Project Ideas for Students in 2021&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;
&lt;u&gt;
Code&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;

        }
    }


    void sort()
    {
        struct node* temp=head;

        while(temp-&gt;next!=NULL)
        {
            struct node* temp1=temp-&gt;next;
            while(temp1!=NULL)
            {
                if(temp-&gt;data &gt; temp1-&gt;data)
                {
                    swap(temp-&gt;data,temp1-&gt;data);
                }
                temp1=temp1-&gt;next;
            }
            temp=temp-&gt;next;
        }
    }


    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(2);
    l.insert(5);
    l.insert(21);
    l.insert(46);
    l.insert(48);
    l.insert(54);
    l.insert(10);
    l.insert(6);
    l.insert(18);
    l.insert(-84);
    l.insert(90);
    l.insert(42);
    l.insert(81);
    l.insert(34);
    l.insert(-101);
    l.insert(6);
    l.insert(48);
    l.insert(54);
    l.insert(5);
    l.insert(-6);
    l.insert(8);
    l.insert(-54);
    l.insert(10);

    l.disp();
    l.sort();
    l.disp();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, arial, verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;
&lt;u&gt;
Explanation of Code&lt;/u&gt;&lt;span style=&quot;font-weight: 400;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
2. Insert some values in the linked list.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
3. Now we have created a function named&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;sort()&lt;/strong&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&amp;nbsp;for&amp;nbsp;sorting of linked list in increasing order.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
4. Then we have created&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&#39;temp&#39;&lt;/strong&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&amp;nbsp;which is a pointer to node. It is initialized to head .&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
5.&amp;nbsp; Now we have started a while loop for iterating the list. Remember we are iterating from first node till the second last node.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;&amp;nbsp;while(temp-&amp;gt;next!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
6. Then we have created a new&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&#39;temp1&#39;&amp;nbsp;&lt;/strong&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;which is another pointer to node. It is initialized to temp-&amp;gt;next which means it will always point to the next node of&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;strong style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;temp&lt;/strong&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&amp;nbsp;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;struct node* temp1=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
7. Now we are starting another loop from temp1 till the last node.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;while(temp1!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;br /&gt;&lt;b style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;span style=&quot;color: #800180;&quot;&gt;SEE ALSO :&lt;/span&gt;&lt;/b&gt;&lt;b style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&amp;nbsp;&lt;/b&gt;&lt;/em&gt;&lt;span color=&quot;var(--post-title-color)&quot; face=&quot;&amp;quot;Work Sans&amp;quot;, Arial, sans-serif&quot; style=&quot;background-color: white;&quot;&gt;&lt;span&gt;&lt;i&gt;&lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/the-ultimate-guide-to-best-sorting.html&quot; target=&quot;_blank&quot;&gt;The Ultimate Guide To Best Sorting Algorithms With Programs&lt;/a&gt;&lt;/i&gt;&lt;/span&gt;&lt;/span&gt;&lt;em&gt;&lt;b style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/b&gt;&lt;/em&gt;&lt;em&gt;&lt;br /&gt;&lt;/em&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWYuHfPhdz8KB8CeurK4jBETkC5_t-V4GpW1QpZRLqpv7mtpIHQ0TF9LTeiDCKTYDhoak9rxXtx84tlHqr_OX_ctOcCGyOjNPCueItBkHyw1TsnWLAoRB-UZBw6bAeqgrxJ9fFAcoHgAIu/s1600/today1.webp&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Sorting of Linked List in Increasing Order&quot; border=&quot;0&quot; data-original-height=&quot;596&quot; data-original-width=&quot;1600&quot; height=&quot;236&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWYuHfPhdz8KB8CeurK4jBETkC5_t-V4GpW1QpZRLqpv7mtpIHQ0TF9LTeiDCKTYDhoak9rxXtx84tlHqr_OX_ctOcCGyOjNPCueItBkHyw1TsnWLAoRB-UZBw6bAeqgrxJ9fFAcoHgAIu/s640/today1.webp&quot; title=&quot;Sorting of Linked List in Increasing Order&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;
&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;
&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;strong&gt;&lt;/strong&gt;8. Then we are comparing the data of 2 nodes adjacent to each other. If data of previous node is greater than the data of node ahead of it the&amp;nbsp; we will swap the 2 nodes.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;if(temp-&amp;gt;data &amp;gt; temp1-&amp;gt;data)&lt;br /&gt;&amp;nbsp; &amp;nbsp;{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; swap(temp-&amp;gt;data,temp1-&amp;gt;data);&lt;br /&gt;&amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;9. Then we have incremented temp1 so that it can point to a new node.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;temp1=temp1-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;10. This loop will run until a node comes to its original position in a sorted list.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
11. Then outside this loop we have incremented temp.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;&lt;div&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot; style=&quot;color: #888888;&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;br /&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;span face=&quot;sans-serif, Arial, Verdana, trebuchet ms&quot;&gt;&lt;span style=&quot;font-size: 14px;&quot;&gt;&lt;b&gt;&lt;i&gt;&lt;span style=&quot;color: #800180;&quot;&gt;See Also :&lt;/span&gt;&lt;span style=&quot;color: #888888;&quot;&gt;&amp;nbsp;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;/span&gt;&lt;span color=&quot;var(--post-title-color)&quot; face=&quot;&amp;quot;Work Sans&amp;quot;, Arial, sans-serif&quot; style=&quot;background-color: white;&quot;&gt;&lt;span&gt;&lt;a href=&quot;https://geektech1717.blogspot.com/2019/10/interesting-facts-about-c-programming.html&quot; target=&quot;_blank&quot;&gt;&lt;i&gt;&lt;b&gt;Interesting facts about C Programming&lt;/b&gt;&lt;/i&gt;&lt;/a&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;&lt;div&gt;&lt;em style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;b style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;If you guys want me to sort linked list using some other sorting algorithm then please comment below.&lt;/b&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/6344058755755714464/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/07/sorting-of-linked-list-in-increasing.html#comment-form' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6344058755755714464'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6344058755755714464'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/07/sorting-of-linked-list-in-increasing.html' title='Sorting of Linked List in Increasing Order'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiWYuHfPhdz8KB8CeurK4jBETkC5_t-V4GpW1QpZRLqpv7mtpIHQ0TF9LTeiDCKTYDhoak9rxXtx84tlHqr_OX_ctOcCGyOjNPCueItBkHyw1TsnWLAoRB-UZBw6bAeqgrxJ9fFAcoHgAIu/s72-c/today1.webp" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-281707841882726188</id><published>2020-07-08T07:23:00.001-07:00</published><updated>2020-07-15T05:12:02.034-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Swap a node with its previous node in a Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will discuss how to&amp;nbsp;swap a node with its previous node in a linked list. But, if you are new to linked lists then please refer to&lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt; this link&lt;/a&gt; where I have explained everything about linked lists. So, let&#39;s start shall we?&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we w 1. First we will make a linked list.&lt;br /&gt;
2. Then user will enter the position&amp;nbsp;&lt;strong&gt;&#39;p&#39;&lt;/strong&gt;&amp;nbsp;of the node which is to be swapped with its previous node. ill make a linked list.&lt;br /&gt;
2. Then user will enter the position&amp;nbsp;&lt;strong&gt;&#39;p&#39;&lt;/strong&gt;&amp;nbsp;of the node which is to be swapped with its previous node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. Then we will iterate the list till that position&amp;nbsp;&lt;strong&gt;&#39;p-1&#39;&lt;/strong&gt;&amp;nbsp;and then we will swap the data of the previous node and the node at position&amp;nbsp;&lt;strong&gt;&#39;p&#39;&lt;/strong&gt;.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;u&gt;Code&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;form&gt;
&lt;textarea cols=&quot;70&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;

        }
    }


    void swap(int x)
    {
        struct node* temp=head;
        int c=1,t;
        if(x&lt;=1)
            cout&lt;&lt;&quot;exception&quot;;
        else
        {
            while(c&lt;x-1)
            {
                temp=temp-&gt;next;
                c++;
            }
            t=temp-&gt;data;
            temp-&gt;data=temp-&gt;next-&gt;data;
            temp-&gt;next-&gt;data=t;
            disp();
        }
    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(2);
    l.insert(5);
    l.insert(21);
    l.insert(46);
    l.insert(48);
    l.insert(54);
    l.insert(10);

    l.disp();
    int n;
    cout&lt;&lt;&quot;enter position of node: &quot;;
    cin&gt;&gt;n;
    l.swap(n);
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;u&gt;Explanation of code(line by line)&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
2. Insert some values in the linked list.&lt;br /&gt;
3. Now we have created a function named&amp;nbsp;&lt;strong&gt;swap()&lt;/strong&gt;&amp;nbsp;to&amp;nbsp;swap a node with previous node in a linked list. It will take an argument&amp;nbsp;&lt;strong&gt;&#39;x&#39;&lt;/strong&gt;&amp;nbsp;which denotes the position of the node which is to be swapped with its previous node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
4. Then we have created variables:&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;ul&gt;
&lt;li&gt;temp - Which is a pointer to node. It is initialized to head.&amp;nbsp;&amp;nbsp;&lt;/li&gt;
&lt;li&gt;c - It is a variable which will be used in iterating the list. It is initialized to 1.&lt;/li&gt;
&lt;li&gt;t - It is another variable which will be helping us in swapping data.&lt;/li&gt;
&lt;/ul&gt;
&amp;nbsp;5. Then there&#39;s a if condition. If x &amp;lt;=1 then we have printed the message &quot;exception&quot; because if x&amp;lt;=1 then there will be no previous node to x.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;&amp;nbsp; if(x&amp;lt;=1)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; cout&amp;lt;&amp;lt;&quot;exception&quot;;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;6. Otherwise we will enter the else condition.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
7. Then we have iterated the list till the node which contain the address of the previous node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;strong&gt;&lt;em&gt;&amp;nbsp;while(c&amp;lt;x-1)&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; {&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c++;&lt;/em&gt;&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;em&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; }&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;8. Then we have placed the data of the previous node in variable&lt;strong&gt;&amp;nbsp;&#39;t&#39;&lt;/strong&gt;.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;t=temp-&amp;gt;data;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;9. Then we have placed the data of the node ahead of previous node in previous node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp-&amp;gt;data=temp-&amp;gt;next-&amp;gt;data;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;10. Then we have placed the data in&lt;strong&gt;&amp;nbsp;&#39;t&#39;&lt;/strong&gt;&amp;nbsp;in the node ahead of previous node.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp-&amp;gt;next-&amp;gt;data=t;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXYEkIKmauuhr7wUMTmW2pUh_YV9L-62hpU_vRKavR94aSO9ut7mJuhRUG3gAuu4j6VFUianQM77P5y6QbwYm8lkbJN3qVE51vsQqXP30a5MsH4DWl_u05FvtWYMrIk5HYTaQHCF0bFRaG/s1600/today.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Swap a node with its previous node in a Linked List&quot; border=&quot;0&quot; data-original-height=&quot;747&quot; data-original-width=&quot;1420&quot; height=&quot;336&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXYEkIKmauuhr7wUMTmW2pUh_YV9L-62hpU_vRKavR94aSO9ut7mJuhRUG3gAuu4j6VFUianQM77P5y6QbwYm8lkbJN3qVE51vsQqXP30a5MsH4DWl_u05FvtWYMrIk5HYTaQHCF0bFRaG/s640/today.webp&quot; title=&quot;Swap a node with its previous node in a Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;em&gt;&lt;strong&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;em&gt;&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;
&lt;strong&gt;&lt;em&gt;&lt;/em&gt;&lt;/strong&gt;&lt;em&gt;&lt;/em&gt;11. Then we have called the disp() which will display the list after swapping.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/281707841882726188/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/07/swap-node-with-its-previous-node-in.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/281707841882726188'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/281707841882726188'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/07/swap-node-with-its-previous-node-in.html' title='Swap a node with its previous node in a Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXYEkIKmauuhr7wUMTmW2pUh_YV9L-62hpU_vRKavR94aSO9ut7mJuhRUG3gAuu4j6VFUianQM77P5y6QbwYm8lkbJN3qVE51vsQqXP30a5MsH4DWl_u05FvtWYMrIk5HYTaQHCF0bFRaG/s72-c/today.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-5317128371352623963</id><published>2020-07-03T07:24:00.000-07:00</published><updated>2020-07-15T05:15:30.847-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>nth node from end of Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will discuss how to find nth node from end of linked list. But, if you don&#39;t know what linked lists are then please refer to&lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt; this post&lt;/a&gt; where I have explained everything about linked list. Let me explain you about the problem. Suppose we have a linked list 1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5 and we are asked to find 2th node from end, then the answer 2nd node from end would be 4. Now let&#39;s understand how to solve&amp;nbsp;nth node from end of linked list problem.&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a linked list.&lt;br /&gt;
2. Then we will count number of nodes in the linked list.&lt;br /&gt;
3. Then we the following formulae to find the position of nth node from beginning.&lt;br /&gt;
Suppose the list is 1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5 and we have to find 2nd node from end. Then we can also say that we have to find the 4th node from beginning because 2nd node from end and 4th node from the beginning are the same. Now there&#39;s a formula you can use to find position of a node from beginning when its position is given from the end:-&lt;br /&gt;
&lt;div data-mce-style=&quot;text-align: center;&quot; style=&quot;text-align: center;&quot;&gt;
&amp;nbsp;&lt;em&gt;&lt;strong&gt;position of node from beginning = number of nodes - position of node from end +1&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. Now that we have got the&amp;nbsp;&lt;em&gt;&lt;strong&gt;position of node from beginning&amp;nbsp;&lt;/strong&gt;&lt;/em&gt;&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;so we can simply iterate till that position and print the output.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Code&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;80&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;75&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;
        }
    }

    void nthnodefromend(int x)
    {
         struct node* temp=head;
         int c=0;
         while(temp!=NULL)
         {
             temp=temp-&gt;next;
             c++;
         }
         c=c-x;
         temp=head;
         int t=1;
         while(t&lt;=c)
         {
             temp=temp-&gt;next;
             t++;
         }
         cout&lt;&lt;x&lt;&lt;&quot;th node from end is &quot;&lt;&lt;temp-&gt;data&lt;&lt;endl;

    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(2);
    l.insert(5);
    l.insert(21);
    l.insert(46);
    l.insert(48);
    l.insert(54);
    l.insert(10);

    l.disp();
    int n;
    cout&lt;&lt;&quot;enter position of node from end to be searched: &quot;;
    cin&gt;&gt;n;
    l.nthnodefromend(n);
    return 0;
}


&lt;/textarea&gt;&lt;u&gt;
&lt;/u&gt;&lt;/form&gt;
&lt;u&gt;
&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Explanation of code(line by line)&lt;/u&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;
2. Insert some values in the linked list.&lt;br /&gt;
3. Now we have created a function named&amp;nbsp;&lt;strong&gt;nthnodefromend()&lt;/strong&gt;&amp;nbsp;to find nth node from end of a linked list. This function takes an argument &#39;x&#39; which denotes the position of node we have to find from the end.&lt;br /&gt;
4. First we have created a pointer to node temp which is initialized with head and a variable &#39;c&#39; which is initialized to 0. &#39;c&#39; will store the number of nodes in the list.&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; struct node* temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; int c=0;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;5. Then we have started a while loop to count number of nodes in the list.&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; while(temp!=NULL)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; c++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;/em&gt;6. Then we have used the formula to find position of a node from beginning when the position from end is given:&lt;br /&gt;
&lt;div data-mce-style=&quot;text-align: center;&quot; style=&quot;text-align: center;&quot;&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;em&gt;&lt;strong&gt;position of node from beginning = number of nodes - position of node from end +1&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: center;&quot; style=&quot;text-align: center;&quot;&gt;
&lt;em&gt;&lt;strong&gt;c=c-x;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot;&gt;
&lt;em&gt;&lt;/em&gt;7. We have not added 1 to the above statement because a linked list starts its traversal from the first node itself.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot;&gt;
8. Then we have initialized temp to head again and we have made another variable named t initialized to 1 which will be a counter variable which will iterate list till desired position.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;temp=head;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;int t=1;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot;&gt;
&lt;em&gt;&lt;/em&gt;9. Then we have started a while loop which will iterate till&amp;nbsp;&lt;strong&gt;the position of node from beginning.&amp;nbsp;&lt;/strong&gt;That means list will iterate from t=1 to c as we have stored the position of node from beginning in c itself.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot;&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; while(t&amp;lt;=c)&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;{&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; temp=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; t++;&lt;/strong&gt;&lt;/em&gt;&lt;br /&gt;
&lt;em&gt;&lt;strong&gt;&amp;nbsp; &amp;nbsp;}&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;10.At last we have printed the data in the node which was xth from then end of the list.&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/5317128371352623963/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/07/nth-node-from-end-of-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/5317128371352623963'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/5317128371352623963'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/07/nth-node-from-end-of-linked-list.html' title='nth node from end of Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-9129961569909315957</id><published>2020-06-29T06:01:00.000-07:00</published><updated>2020-07-16T20:07:03.140-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Clockwise Rotation of a Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we are going to discuss the algorithm for clockwise rotation of a linked list, but if you are new to linked lists then please refer to&lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt; this link&lt;/a&gt; where I have explained everything about linked list. Let me explain you what is clockwise rotation of a linked list- Suppose we have a linked list 2-&amp;gt;5-&amp;gt;2-&amp;gt;6-&amp;gt;4-&amp;gt;3. Then user enters an integer K=3. Now the linked list should rotate clockwise K times clockwise and we will get the linked list as 6-&amp;gt;4-&amp;gt;3-&amp;gt;2-&amp;gt;5-&amp;gt;2.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;
Procedure&lt;/u&gt;&lt;/h3&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a linked list.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now we will traverse the linked list. We will traverse the list till K times.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. In each traversal we will remove the last node from linked list and insert it in the beginning of the list.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi600soNaCu2SaBWWC2HsPBGVCSn6aATcNVFVkafx09WoMi0gnuX-lk8KNvAIFLq9Qa3lcNdn7Q_Dn-Q9rHs6eTWVeEs3GuesaV1atEblMy5yEsgT2nOtBz-udQ0eqxYNKNeW9BR4ZeAhx3/s1600/rotate.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Clockwise Rotation of a Linked List&quot; border=&quot;0&quot; data-original-height=&quot;385&quot; data-original-width=&quot;1054&quot; height=&quot;232&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi600soNaCu2SaBWWC2HsPBGVCSn6aATcNVFVkafx09WoMi0gnuX-lk8KNvAIFLq9Qa3lcNdn7Q_Dn-Q9rHs6eTWVeEs3GuesaV1atEblMy5yEsgT2nOtBz-udQ0eqxYNKNeW9BR4ZeAhx3/s640/rotate.webp&quot; title=&quot;Clockwise Rotation of a Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;Code&lt;/u&gt;&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;80&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;
        }
    }


    void rotate(int k)
    {

        int c=0;
        while(c&lt;k)
        {
            struct node* temp=head;
            struct node* x=NULL;
            while(temp-&gt;next-&gt;next!=NULL)
            {
                   temp=temp-&gt;next;
            }
            x=temp-&gt;next;
            temp-&gt;next=NULL;
            x-&gt;next=head;
            head=x;
            c++;
            disp();
        }


    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(2);
    l.insert(5);
    l.insert(2);
    l.insert(6);
    l.insert(4);
    l.insert(3);
    l.disp();
    int n;
    cout&lt;&lt;&quot;enter number of times you want to rotate the list: &quot;;
    cin&gt;&gt;n;
    l.rotate(n);
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;u&gt;
Explanation of code(line by line)&lt;/u&gt;&lt;/h3&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;2. Insert some values in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;3. Now we have created a function named&amp;nbsp;&lt;strong&gt;rotate()&lt;/strong&gt;&amp;nbsp;to clockwise rotation of a linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;4. In the rotate() first we have created a variable&amp;nbsp;&lt;strong&gt;&#39;c&#39;&lt;/strong&gt;&amp;nbsp;which is initialized to 0. It will be variable which will keep track of the number of times we have to traverse the list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;5. Then we have created &#39;&lt;strong&gt;temp&#39;&lt;/strong&gt;&amp;nbsp; which is a pointer to node variable initialized to head.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;6. Then we have created&amp;nbsp;&lt;strong&gt;&#39;x&#39;&lt;/strong&gt;&amp;nbsp;which is another pointer to node variable which can store addresses of nodes only just like the head node.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-original-attrs=&quot;{&amp;quot;data-mce-style&amp;quot;:&amp;quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&amp;quot;,&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot;&gt;7. Next we have started another while loop which will traverse from start till the second last node of the list.&lt;/span&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;while(temp-&amp;gt;next-&amp;gt;next!=NULL)&lt;br /&gt;{&lt;br /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;temp=temp-&amp;gt;next;&lt;br /&gt;}&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
8. Next we have stored the address of the last node of linked list in x with the help of temp-&amp;gt;next (temp was the second last node from above while loop so its next will store the address of last node).&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;x=temp-&amp;gt;next;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;9. Then we have assigned&amp;nbsp;&lt;em&gt;&lt;strong&gt;temp-&amp;gt;next = NULL&lt;/strong&gt;&lt;/em&gt;&amp;nbsp;because now temp-&amp;gt;next will be the last node and last node points to NULL.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
10. Then in x-&amp;gt;next we have assigned value of head. x-&amp;gt;next means the last node of linked list which was removed, will now point to the first node.&amp;nbsp;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;strong&gt;x-&amp;gt;next=head;&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;em&gt;&lt;/em&gt;11. Then in head we have stored address of x because now first node will be the last node which was removed and the address of last node was in x.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;em&gt;head=x;&lt;/em&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong&gt;&lt;/strong&gt;12. Then c is incremented because first rotation is complete and next we will perform second rotation.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
13. Then we have called&amp;nbsp;&lt;strong&gt;disp()&amp;nbsp;&lt;/strong&gt;so that user can see the rotated linked list in each iteration.&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-original-attrs=&quot;{&amp;quot;style&amp;quot;:&amp;quot;&amp;quot;}&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/9129961569909315957/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/06/clockwise-rotation-of-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/9129961569909315957'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/9129961569909315957'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/06/clockwise-rotation-of-linked-list.html' title='Clockwise Rotation of a Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi600soNaCu2SaBWWC2HsPBGVCSn6aATcNVFVkafx09WoMi0gnuX-lk8KNvAIFLq9Qa3lcNdn7Q_Dn-Q9rHs6eTWVeEs3GuesaV1atEblMy5yEsgT2nOtBz-udQ0eqxYNKNeW9BR4ZeAhx3/s72-c/rotate.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-2074430563786198294</id><published>2020-06-25T07:44:00.003-07:00</published><updated>2020-07-15T20:10:52.335-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Reverse a Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today we will discuss how to &lt;b&gt;reverse a linked list&lt;/b&gt;. Proceed if you have basic knowledge of linked list otherwise you can read&lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt; this post&lt;/a&gt; on where I have explained you about linked list. Let me explain you what a reversed linked list is. Suppose we have a linked list 1-&amp;gt;2-&amp;gt;3-&amp;gt;4-&amp;gt;5-&amp;gt;NULL then the reverse of this list will be 5-&amp;gt;4-&amp;gt;3-&amp;gt;2-&amp;gt;1-&amp;gt;NULL. So, let&#39;s discuss how we can &lt;b&gt;reverse a linked list&lt;/b&gt;.&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Procedure&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now we will traverse the linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. While traversing when we will reach the first node we will make it point NULL.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. Then at each node we will make that node point to the previous node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5. When we will reach the last node then we will update head and initialize it with the address of last node.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGvwLHogfjbmqqeMIucJFxmBAVecZmOOZnpKcfS6WXKNhgW8LJlSALK7F_9HL11dT5JS02_Z8MSg8wA3eIBlMFK2tQWj_FB287z4KULv1CSZUZ7LX3m-hd5NePcyZyqH9_38ZUaqMkeOig/s1600/reverse1.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Reverse a Linked List&quot; border=&quot;0&quot; data-original-height=&quot;307&quot; data-original-width=&quot;1600&quot; height=&quot;122&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGvwLHogfjbmqqeMIucJFxmBAVecZmOOZnpKcfS6WXKNhgW8LJlSALK7F_9HL11dT5JS02_Z8MSg8wA3eIBlMFK2tQWj_FB287z4KULv1CSZUZ7LX3m-hd5NePcyZyqH9_38ZUaqMkeOig/s640/reverse1.webp&quot; title=&quot;Reverse a Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Code&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;form&gt;
&lt;textarea cols=&quot;80&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;
        }
    }

    void reverse()
    {
        struct node* current=head;
        struct node* prev=head;
        struct node* forward=head;
        forward=current-&gt;next;
        current-&gt;next=NULL;
        while(current!=NULL &amp;&amp; forward!=NULL)
        {
            prev=current;
            current=forward;
            forward=forward-&gt;next;
            current-&gt;next=prev;
        }
        head=current;
    }


    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(1);
    l.insert(2);
    l.insert(3);
    l.insert(4);
    l.insert(5);
    l.insert(6);

    l.insert(9);
    l.disp();
    l.reverse();
    l.disp();
    return 0;
}


&lt;/textarea&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;/span&gt;&lt;/form&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;/h3&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Explanation of Code( line by line)&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;2. Insert some values in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;3. Now we have created a function named&amp;nbsp;&lt;strong&gt;reverse()&lt;/strong&gt;&amp;nbsp;to reverse the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;4. In the reverse() function we have made 3 pointer to node variables:&lt;/span&gt;&lt;br /&gt;
&lt;ul style=&quot;text-align: left;&quot;&gt;
&lt;li&gt;&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;prev : Which will point to the node previous to current node.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;current : Which will point to the current node.&lt;/span&gt;&lt;/li&gt;
&lt;li&gt;&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;forward : Which will point to the node ahead of current node.&lt;/span&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;5. Initially prev, current and forward are initialized with head.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;6. Then forward is initialized with the next node after current.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;forward=current-&amp;gt;next;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;7. Then&amp;nbsp;&lt;strong&gt;current-&amp;gt;next is initialized to NULL&lt;/strong&gt;&amp;nbsp;because this current is the first node and after reversing the list it will become last node so it shall point to NULL.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;8. Then we have started a loop to traverse the list till&lt;strong&gt;&amp;nbsp;current!=NULL and forward!=NULL&lt;/strong&gt;.&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;9.In the loop we have assigned&amp;nbsp;&lt;strong&gt;prev with the address of current&lt;/strong&gt;&amp;nbsp;and&amp;nbsp;&lt;strong&gt;current with the address of forward&lt;/strong&gt;&amp;nbsp;and&amp;nbsp;&lt;strong&gt;forward with forward-&amp;gt;next&lt;/strong&gt;&amp;nbsp;so that all 3 pointer to node can increment.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; prev=current;&lt;br /&gt;&amp;nbsp; &amp;nbsp; current=forward;&amp;nbsp;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;10. Then we have assigned the current-&amp;gt;next with the address the prev because in order to reverse the linked list each node shall point to its previous node.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;current-&amp;gt;next=prev;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;11. After while loop is over we have assigned head the address of current node because when the loop will be over current will be at last node and the last node will become the first node after reversing and first node is always pointed by head node.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/2074430563786198294/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/06/reverse-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2074430563786198294'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/2074430563786198294'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/06/reverse-linked-list.html' title='Reverse a Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiGvwLHogfjbmqqeMIucJFxmBAVecZmOOZnpKcfS6WXKNhgW8LJlSALK7F_9HL11dT5JS02_Z8MSg8wA3eIBlMFK2tQWj_FB287z4KULv1CSZUZ7LX3m-hd5NePcyZyqH9_38ZUaqMkeOig/s72-c/reverse1.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-3357459933724500467</id><published>2020-06-21T05:57:00.003-07:00</published><updated>2020-07-18T07:30:18.148-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Search an element in Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;Today we are going to discuss the algorithm to&lt;b&gt; search an element in linked list&lt;/b&gt; data structure.&amp;nbsp;&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;But if you do not know what a linked list is then press on the &lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt;link&lt;/a&gt;. Searching is a very basic problem.&lt;/span&gt;&lt;span style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;&amp;nbsp;So let&#39;s see how to do&lt;b&gt; search an element in linked list&lt;/b&gt;.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;span style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Procedure&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;1. First we will make a linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;2. Now we will traverse the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;3. For each node we will compare its data with the value to be searched.&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-size: 14px;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Code&lt;/span&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;form&gt;
&lt;textarea cols=&quot;80&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;
        }
    }

    void search(int x)
    {
         int flag=0,c=0;
         struct node* temp=head;
         while(temp!=NULL)
         {
             if(temp-&gt;data==x)
             {
                flag=1;
                break;
             }
             temp=temp-&gt;next;
             c++;
         }
         if(flag==1)
         {
             cout&lt;&lt;&quot;value found at &quot;&lt;&lt;c+1&lt;&lt;endl;
         }
         else
         {
             cout&lt;&lt;&quot;value not found &quot;;
         }

    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(2);
    l.insert(5);
    l.insert(2);
    l.insert(6);
    l.insert(4);
    l.insert(5);
    l.insert(1);
    l.insert(3);
    l.insert(2);
    l.insert(6);
    l.insert(5);
    l.insert(2);
    l.insert(9);
    l.insert(7);
    l.insert(6);
    l.insert(2);
    l.insert(3);
    l.insert(3);
    l.insert(14);
    l.insert(2);
    l.disp();
    int n;
    cout&lt;&lt;&quot;enter value to be searched: &quot;;
    cin&gt;&gt;n;
    l.search(n);
    return 0;
}


&lt;/textarea&gt;&lt;span style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/form&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-size: 14px;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;span style=&quot;font-family: inherit;&quot;&gt;Explanation of code(line by line)&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: inherit; font-size: 14px;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;2. Insert some values in the linked list.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;3. We have made a function named search() in which we will write the algorithm for searching a value. Search() also takes an argument which is the value to be searched in the linked list.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;4. We have made 3 variables.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- flag - which will indicate we have found the value to be searched or not. Initial value is 0.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- c - which will count the position of node. Initial value is 0&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- temp - which is pointer to node. Initially pointing to head.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;5. Next we have started the while loop which will iterate over the linked list.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;6. Inside the while loop we have placed an&amp;nbsp;&lt;strong&gt;if condition&lt;/strong&gt;&amp;nbsp;which checks if the data in current node is equal to the value to be searched or not.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;7. If the data is equal to value to be searched then we make as&amp;nbsp;&lt;strong&gt;flag=1&amp;nbsp;&lt;/strong&gt;and&amp;nbsp;&lt;strong&gt;break&amp;nbsp;&lt;/strong&gt;out of the loop.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;8. Otherwise we will increment to next node and increment&amp;nbsp;&lt;strong&gt;c&lt;/strong&gt;&amp;nbsp;as well.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;9. After the while loop is over then we will check if flag is 1 or 0.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; - If flag is 1 then we have found the value. So we display a message along with&amp;nbsp;&lt;strong&gt;c+1&amp;nbsp;&lt;/strong&gt;which gives the&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;position of that value in the linked list.&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&amp;nbsp; &amp;nbsp; - If flag is 0 then that means we have not found the value in the linked list.&amp;nbsp;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;h3 style=&quot;background-color: white; color: #555555; font-weight: 400;&quot;&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;Output&lt;/span&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6AHiM200UsWMhSF5KYnNwhfOslxMDOSSK8ljBCBd8I03ZM_XHCne8fjDpgYEwXVZsjZZic3UKdIlkDvu2xfgdCIqNGhITzKIGBoPYa_Z27JwSSfWMj_ONESP_pEbQ4ISh9MH1qdhRYLm9/s1600/search.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Search an element in Linked List&quot; border=&quot;0&quot; data-original-height=&quot;409&quot; data-original-width=&quot;838&quot; height=&quot;312&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6AHiM200UsWMhSF5KYnNwhfOslxMDOSSK8ljBCBd8I03ZM_XHCne8fjDpgYEwXVZsjZZic3UKdIlkDvu2xfgdCIqNGhITzKIGBoPYa_Z27JwSSfWMj_ONESP_pEbQ4ISh9MH1qdhRYLm9/s640/search.webp&quot; title=&quot;Search an element in Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span data-mce-style=&quot;color: #888888; font-family: sans-serif, arial, verdana, &#39;trebuchet ms&#39;;&quot; style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;font-family: inherit;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/3357459933724500467/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/06/search-element-in-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3357459933724500467'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/3357459933724500467'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/06/search-element-in-linked-list.html' title='Search an element in Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6AHiM200UsWMhSF5KYnNwhfOslxMDOSSK8ljBCBd8I03ZM_XHCne8fjDpgYEwXVZsjZZic3UKdIlkDvu2xfgdCIqNGhITzKIGBoPYa_Z27JwSSfWMj_ONESP_pEbQ4ISh9MH1qdhRYLm9/s72-c/search.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-8004294095189346042</id><published>2020-06-17T06:18:00.002-07:00</published><updated>2020-07-15T20:09:15.104-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Remove Duplicates in Linked List</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;Today we will discuss how to remove duplicates in linked list but before we start the discussion make sure you know about&lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt; linked list&lt;/a&gt;. Removing duplicates from a linked list is a very famous programming problem asked in many programming interviews. So, let&#39;s start, shall we?&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;
&lt;br /&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Procedure&lt;/h3&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;1. First we will make a linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;2. Now we will start traversing the list in such a way that the data in each node is compared with the data of every other node.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;3. While traversing the list if we encounter a duplicate then we will assign that duplicate with some &lt;b&gt;special value&lt;/b&gt; so that we can remember that it is a duplicate.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;4. Then we will delete all the nodes with the special value.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Code&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 14px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;form&gt;
&lt;textarea cols=&quot;80&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;85&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;

class linked
{
public:
    void insert(int x)
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next =temp;
        }
    }

    void duplicate()
    {
        struct node* temp=head;
        int count=0;
        while(temp!=NULL)
        {
            count++;
            temp=temp-&gt;next;
        }
        int p=0;
        temp=head;
        while(temp-&gt;next!=NULL &amp;&amp; p&lt;count)
        {
            struct node* temp1=temp-&gt;next;
            while(temp1!=NULL)
            {
                if(temp-&gt;data!=-1)
                {
                    if(temp-&gt;data==temp1-&gt;data)
                    {
                         temp1-&gt;data=-1;
                    }

                }
                temp1=temp1-&gt;next;
            }
            temp=temp-&gt;next;
            p++;
        }

        temp=head;
        struct node* temp1=head;
        while(temp-&gt;next!=NULL)
        {
            if(temp-&gt;next-&gt;data==-1)
            {
                temp1=temp;
                while(temp1-&gt;next!=NULL)
                {
                    if(temp1-&gt;next-&gt;data!=-1)
                        break;
                    temp1=temp1-&gt;next;
                }
                temp-&gt;next=temp1-&gt;next;
                if(temp-&gt;next==NULL)
                    break;
            }
            temp=temp-&gt;next;
        }

    }

    void disp()
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};

int main()
{
    linked l;
    l.insert(2);
    l.insert(5);
    l.insert(2);
    l.insert(6);
    l.insert(4);
    l.insert(5);
    l.insert(1);
    l.insert(3);
    l.insert(2);
    l.insert(6);
    l.insert(5);
    l.insert(2);
    l.insert(9);
    l.insert(7);
    l.insert(6);
    l.insert(2);
    l.insert(3);
    l.insert(3);
    l.insert(14);
    l.insert(2);
    l.disp();
    l.duplicate();
    l.disp();
    return 0;
}


&lt;/textarea&gt;&lt;span style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 14px;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/form&gt;
&lt;span style=&quot;color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;;&quot;&gt;&lt;span style=&quot;background-color: white; font-size: 14px;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Explanation of code(line by line)&lt;/h3&gt;
&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;1. First we will write a simple code of linked list which will have 2 functions:&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- insert() - which will insert elements in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;- disp() - which will display the linked list.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;2. Insert some values in the linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;3. Now we will make a function named &lt;b&gt;duplicate()&lt;/b&gt; which will delete duplicates from a linked list.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;4. We will make a pointer to node named temp which will point to head.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;5. Then we will count the number of nodes.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;6. Now we will start traversing the list such that every node is compared with every other node.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;7. We will use temp to traverse from beginning till second last node and temp1(another pointer to node) to traverse from second node till last node. temp will traverse in outer loop and temp1 will traverse in inner loop.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEZNtGyJPoDqipwJKl2Z5xdgv2FwwtxG0LgwhG21jl4E6xiGiM6HJ6WAU0zm6hUFgrD4mEWziDUbBR0-iiJUAIMUmgNp55tZmEDENTOyXMTa5N8E_UC6TyowbTwpBZ-dk9hl4r30-60qzu/s1600/dupli1.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Remove Duplicates in Linked List&quot; border=&quot;0&quot; data-original-height=&quot;289&quot; data-original-width=&quot;1528&quot; height=&quot;120&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEZNtGyJPoDqipwJKl2Z5xdgv2FwwtxG0LgwhG21jl4E6xiGiM6HJ6WAU0zm6hUFgrD4mEWziDUbBR0-iiJUAIMUmgNp55tZmEDENTOyXMTa5N8E_UC6TyowbTwpBZ-dk9hl4r30-60qzu/s640/dupli1.webp&quot; title=&quot;Remove Duplicates in Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div&gt;
&lt;br style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot; /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;8. In this way every temp node will be compared with every temp1 node .&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;9. To traverse temp from beginning till second last node we will use a variable named &#39;p&#39; which will traverse the list till second last node.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;10. Then we have declared temp1 and initialized it with the second node and after that we have started traversing temp1 till end.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;11. Now if we encounter a duplicate then we will assign that node with value &lt;b&gt;-1(special value)&lt;/b&gt;. Then there is an if statement which will check if the data of a node is not -1 because there will be chances that a node with -1 value is encountered. So, this if statement will take care of that.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;12. If the data of node is not -1 then we come in the inner if statement which checks if data of temp = data of temp1. If they are equal then we assign temp1 with -1 value.&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;13. Then we have incremented temp1, temp and p.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;14. Then we have initialized temp with head. In the next line we have made a pointer to node temp1 initialized to head.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;15. Then we have started iterating over the linked list using temp.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;16. The first if condition checks if the data of next node is -1 or not.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;17. If the data of next node is -1 then that means it it a duplicate and we have to delete it.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;18. To delete we have used temp1 and iterated it over the linked list from the node where temp is currently present.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;19. We iterate temp1 till the data of next node comes out to be -1, because there may be a situation in which multiple -1&#39;s are present consecutively. In that case we have to delete all those nodes.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;i&gt;&lt;b&gt;while(temp1-&amp;gt;next!=NULL)&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;{&lt;/span&gt;&lt;br style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if(temp1-&amp;gt;next-&amp;gt;data!=-1)&lt;/span&gt;&lt;br style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; break;&lt;/span&gt;&lt;br style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;temp1=temp1-&amp;gt;next;&lt;/span&gt;&lt;br style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;}&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIVdgG5mgsbRxNnuUgy7iG7VRYCi5QkZWIQyRl0GCwjZ6mpQIzTAc0QMEAc-NUL_435e4FoYPC-eziOVtV7lfzpv7-8pbtGmFjxhKAkgFoKr7j27TjM-H7vfSJF3UIm5qbSkxLevtqTDoH/s1600/dupli2.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Remove Duplicates in Linked List&quot; border=&quot;0&quot; data-original-height=&quot;459&quot; data-original-width=&quot;1470&quot; height=&quot;198&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjIVdgG5mgsbRxNnuUgy7iG7VRYCi5QkZWIQyRl0GCwjZ6mpQIzTAc0QMEAc-NUL_435e4FoYPC-eziOVtV7lfzpv7-8pbtGmFjxhKAkgFoKr7j27TjM-H7vfSJF3UIm5qbSkxLevtqTDoH/s640/dupli2.webp&quot; title=&quot;Remove Duplicates in Linked List&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;20. Inside the loop for temp1 we have another if condition which checks if data of next node not -1 then we have to break out of the loop otherwise we will increment temp1.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;21. After coming out of the loop of temp1 we have assigned&lt;/span&gt;&lt;strong style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp;temp-&amp;gt;next = temp1-&amp;gt;next&lt;/strong&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;&amp;nbsp;which means temp&#39;s next will have the address of the next node which has data not equal to -1.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;22. The next if statement is for the situation if the last node contains a duplicate then we break out of the loop.&lt;/span&gt;&lt;/div&gt;
&lt;div&gt;
&lt;b&gt;&lt;i&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;if(temp-&amp;gt;next==NULL)&lt;/span&gt;&lt;br style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot; /&gt;&lt;span style=&quot;background-color: white; color: #888888; font-family: sans-serif , &amp;quot;arial&amp;quot; , &amp;quot;verdana&amp;quot; , &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;break;&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
(try the code without including the above 2 statement and observe the output).&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
23. Then temp is incremented.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/8004294095189346042/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/06/remove-duplicates-in-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/8004294095189346042'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/8004294095189346042'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/06/remove-duplicates-in-linked-list.html' title='Remove Duplicates in Linked List'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiEZNtGyJPoDqipwJKl2Z5xdgv2FwwtxG0LgwhG21jl4E6xiGiM6HJ6WAU0zm6hUFgrD4mEWziDUbBR0-iiJUAIMUmgNp55tZmEDENTOyXMTa5N8E_UC6TyowbTwpBZ-dk9hl4r30-60qzu/s72-c/dupli1.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-6629488618582851127</id><published>2020-06-13T06:35:00.001-07:00</published><updated>2020-07-18T07:34:07.873-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Swap alternate elements of Linked List pairwise</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Today, we will discuss how to swap alternate elements of a linked list pairwise. But before starting make sure you have basic knowledge of&lt;a href=&quot;https://geektech1717.blogspot.com/2019/12/what-are-linked-lists-in-data-structures.html&quot; target=&quot;_blank&quot;&gt; linked lists&lt;/a&gt;. So, let&#39;s start, shall we?&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;strong&gt;&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;Procedure&lt;/span&gt;&lt;/strong&gt;&lt;/h3&gt;
&lt;div&gt;
&lt;strong&gt;&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-bogus=&quot;1&quot; data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. First we will make a linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Now we will start traversing the linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. We will use a pointer to node which will consider 2 nodes at a time and then perform a swap operation on them.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. Then we will increment pointer to node 2 times so that next pair can be swapped.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;b&gt;&lt;i&gt;Don&#39;t worry if you can&#39;t understand the procedure thoroughly. Keep reading till end and you will be able to solve this problem on your own.&lt;/i&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;strong&gt;Algorithm&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;First we will write a simple code of linked list which will have 2 functions:&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. insert() - which will insert elements in the linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. disp() - which will display the linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Insert some values in the linked list.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Then make a function swap() which will swap alternate elements of linked list pairwise.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
In the swap() function first we will make a pointer to node named temp which will initially points to the head node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Then we will make a variable &#39;p&#39; which we will use in swapping.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Now we will iterate the linked list such that the current node and the pointer to next node&amp;nbsp;is not&amp;nbsp;NULL which means&amp;nbsp;2 nodes exists&amp;nbsp;and we will swap them. Otherwise if there exists only 1 node in the end then we will leave it as it is.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7cP2i8dToX41fmU-TM_eBHl8k9b0ijUK6wxyMZz6u3m6jPWaI-ylwxGcZarMYoMJwEbvcW5ExBF6Bu7mP0psiso_rlriB12BxPgs-Ekf9inzMYr23UZGb-UlqJUjT_ACHtZFaA9IggZen/s1600/link3.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Swap alternate elements of Linked List pairwise&quot; border=&quot;0&quot; data-original-height=&quot;379&quot; data-original-width=&quot;1297&quot; height=&quot;186&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7cP2i8dToX41fmU-TM_eBHl8k9b0ijUK6wxyMZz6u3m6jPWaI-ylwxGcZarMYoMJwEbvcW5ExBF6Bu7mP0psiso_rlriB12BxPgs-Ekf9inzMYr23UZGb-UlqJUjT_ACHtZFaA9IggZen/s640/link3.webp&quot; title=&quot;Swap alternate elements of Linked List pairwise&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Next we will perform a swap operation between current and next node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
p=temp-&amp;gt;data;&lt;br /&gt;
temp-&amp;gt;data=temp-&amp;gt;next-&amp;gt;data;&lt;br /&gt;
temp-&amp;gt;next-&amp;gt;data=p;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. In the above swapping code we have stored the data of current node in a p.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Then in the data of current node we have placed the data of next node. &#39;temp-&amp;gt;next-&amp;gt;data&#39; will point to the data portion of next node.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. Then in the data of next node we have placed the value of p.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgg78AnK9QnWHirPJBvERx32ArYciBoeZEt8PJMIcYn7k54hC6R7Gj_xOSVXLhBw_9AJymgoVBa5-wOxFdBn09NWvqD0I2VBlgKK2PLclcu0SJtX4bia-Q3dFnuXKD_fwE_Yx1w7l1pWGb/s1600/link1.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Swap alternate elements of Linked List pairwise&quot; border=&quot;0&quot; data-original-height=&quot;472&quot; data-original-width=&quot;1489&quot; height=&quot;201&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhgg78AnK9QnWHirPJBvERx32ArYciBoeZEt8PJMIcYn7k54hC6R7Gj_xOSVXLhBw_9AJymgoVBa5-wOxFdBn09NWvqD0I2VBlgKK2PLclcu0SJtX4bia-Q3dFnuXKD_fwE_Yx1w7l1pWGb/s640/link1.webp&quot; title=&quot;Swap alternate elements of Linked List pairwise&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
In this way we have swapped the values of 2 alternate nodes pairwise.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
At last we will double increment temp so that it will point to next pair of nodes (temp=temp-&amp;gt;next-&amp;gt;next).&amp;nbsp;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhylFe8x4uPrFeR3Gan5gdMzJSqZhzabQ92xBjOeStfXQwhr5uZV9DPOweNWqACx092Uhs60GqnX3_W21LjQ1iXMIM2nTziJ9YPKsJZTTTtkZHI1PpOa71H29Yy5Qn9QW7J3FfBHcQs9_CG/s1600/link2.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Swap alternate elements of Linked List pairwise&quot; border=&quot;0&quot; data-original-height=&quot;329&quot; data-original-width=&quot;1414&quot; height=&quot;148&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhylFe8x4uPrFeR3Gan5gdMzJSqZhzabQ92xBjOeStfXQwhr5uZV9DPOweNWqACx092Uhs60GqnX3_W21LjQ1iXMIM2nTziJ9YPKsJZTTTtkZHI1PpOa71H29Yy5Qn9QW7J3FfBHcQs9_CG/s640/link2.webp&quot; title=&quot;Swap alternate elements of Linked List pairwise&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;b&gt;&lt;u&gt;Implementation in C++&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;form&gt;
&lt;textarea cols=&quot;80&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;73&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL;
class linked
{
public:
    void insert(int x)          //insert node in a linked list
    {
        struct node* temp=new node();
        temp-&gt;data=x;
        temp-&gt;next=NULL;
        if(head==NULL)
        {
            head=temp;
        }
        else
        {
            struct node* temp1=head;
            while(temp1-&gt;next!=NULL)
            {
                temp1=temp1-&gt;next;
            }
            temp1-&gt;next=temp;
        }
    }

    void swap()   //swap alternate elements pairwise
    {
        struct node* temp=head;
        int p;
        while(temp!=NULL &amp;&amp; temp-&gt;next!=NULL)
        {
             p=temp-&gt;data;
             temp-&gt;data=temp-&gt;next-&gt;data;
             temp-&gt;next-&gt;data=p;
             temp=temp-&gt;next-&gt;next;
        }
    }

    void disp()    //display the linked list
    {
        struct node* temp=head;
        while(temp!=NULL)
        {
            cout&lt;&lt;temp-&gt;data&lt;&lt;&quot; &quot;;
            temp=temp-&gt;next;
        }
        cout&lt;&lt;endl;
    }
};
int main()
{
    linked l;
    l.insert(2);
    l.insert(4);
    l.insert(-3);
    l.insert(-6);
    l.insert(8);
    l.insert(9);
    l.disp();
    l.swap();
    l.disp();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;b&gt;&lt;u&gt;OUTPUT:&lt;/u&gt;&lt;/b&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-toBRtAQAacG3UQSyaSLGBq3TaQRXtlZP-TNBoU01lnkdlKGXZlgMTuLbb81QyyXgGiJE-cr4TpPdTkLQ9InST4NXgkVbh3kRYR99Sgvm0Nn0Os9Mf_TeLLqQTPt3L27cFaa13KJuQoiP/s1600/link4.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Swap alternate elements of Linked List pairwise&quot; border=&quot;0&quot; data-original-height=&quot;390&quot; data-original-width=&quot;757&quot; height=&quot;328&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg-toBRtAQAacG3UQSyaSLGBq3TaQRXtlZP-TNBoU01lnkdlKGXZlgMTuLbb81QyyXgGiJE-cr4TpPdTkLQ9InST4NXgkVbh3kRYR99Sgvm0Nn0Os9Mf_TeLLqQTPt3L27cFaa13KJuQoiP/s640/link4.webp&quot; title=&quot;Swap alternate elements of Linked List pairwise&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/6629488618582851127/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/06/swap-alternate-elements-of-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6629488618582851127'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/6629488618582851127'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/06/swap-alternate-elements-of-linked-list.html' title='Swap alternate elements of Linked List pairwise'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg7cP2i8dToX41fmU-TM_eBHl8k9b0ijUK6wxyMZz6u3m6jPWaI-ylwxGcZarMYoMJwEbvcW5ExBF6Bu7mP0psiso_rlriB12BxPgs-Ekf9inzMYr23UZGb-UlqJUjT_ACHtZFaA9IggZen/s72-c/link3.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-4141716589201606701</id><published>2020-06-10T06:58:00.000-07:00</published><updated>2020-07-16T20:16:21.515-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#datastructures"/><title type='text'>Delete middle element of a linked list</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
Today we will discuss how to delete middle element of a linked list. But before moving to the solution you should be familiar with the concept of linked list.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&amp;nbsp;&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;strong&gt;&lt;u&gt;Procedure&lt;/u&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;br /&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;/span&gt;First we will make a linked list.&lt;br /&gt;
Then we will count the number of nodes in a linked list.&lt;br /&gt;
Now we will have 2 cases -&lt;br /&gt;
1.) when the number of nodes are even then we will have 2 middle elements (n/2 and n/2+1).&lt;br /&gt;
2.) when the number of nodes are odd then we will have 1 middle element ((n+1)/2).&lt;br /&gt;
Then we will write the underlying code to delete middle element by analyzing if the number of nodes are even or odd.&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=&quot;color: #555555; font-weight: 400;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;strong&gt;&lt;u&gt;Algorithm&lt;/u&gt;&lt;/strong&gt;&lt;/span&gt;&lt;/h3&gt;
&lt;br /&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;/span&gt;We will first write a simple code which will have 2 functions:&lt;br /&gt;
&lt;br /&gt;
1. insert() - which will insert elements to linked list.&lt;br /&gt;
2. show() - to display the linked list.&lt;br /&gt;
&lt;br /&gt;
Now we will make a function deletemiddle() which will delete the middle element.&lt;br /&gt;
In the deletemiddle() we will make&amp;nbsp;1 node&amp;nbsp;temp1 which will initially point to head of the linked list.&lt;br /&gt;
Then we will count the number of nodes in the linked list by traversing the list with temp1 and storing the count values in a count variable.&lt;br /&gt;
Now again we will initialize temp1 to head.&lt;br /&gt;
Now we will check count%2 == 0 (even) or count%2!=0 (odd).&lt;br /&gt;
&lt;br /&gt;
If count%2==0 or number of nodes are even then:&lt;br /&gt;
1. We will find mid by count/2.&lt;br /&gt;
2. We will make a variable p of int type which will tell us if we have reached the desired node or not.&lt;br /&gt;
3. Now we will traverse the list. We will increment p with each iteration and also check if p+1 == mid or not.&lt;br /&gt;
4. If p+1 == mid that means the next node is then first middle node and the next node is the second middle node.&lt;br /&gt;
5. So we will use (temp1-&amp;gt;next=temp1-&amp;gt;next-&amp;gt;next-&amp;gt;next) which means to put the address of the node after the second middle element in the current node.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghxCwrvif-ifqyDnfRznHWSaJ9SlFaqjEN9J95IHGoJf2v8udJX1hvasYxFPHlAzeFKwxZrsKh5-DTRZRW0k19Qx_NO9URmUAnK-dD4skqBuSlwEp1efIR4auGO6f7J87HgdQs7kuqBbSI/s1600/link12.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Delete middle element of a linked list&quot; border=&quot;0&quot; data-original-height=&quot;327&quot; data-original-width=&quot;1336&quot; height=&quot;156&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghxCwrvif-ifqyDnfRznHWSaJ9SlFaqjEN9J95IHGoJf2v8udJX1hvasYxFPHlAzeFKwxZrsKh5-DTRZRW0k19Qx_NO9URmUAnK-dD4skqBuSlwEp1efIR4auGO6f7J87HgdQs7kuqBbSI/s640/link12.webp&quot; title=&quot;Delete middle element of a linked list&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
If count%2!=0 or number of nodes are odd then:&lt;br /&gt;
1. We will find mid by count/2.&lt;br /&gt;
2. We will make a variable p of int type which will tell us if we have reached the desired node or not.&lt;br /&gt;
3. Now we will traverse the list. We will increment p with each iteration and also check if p+1 == mid or not.&lt;br /&gt;
4. If p+1 == mid that means the next node is then middle node .&lt;br /&gt;
5.&amp;nbsp;5. So we will use (temp1-&amp;gt;next=temp1-&amp;gt;next-&amp;gt;next) which means to put the address of the node after the middle element in the current node.&lt;br /&gt;
&lt;br /&gt;
&lt;br data-mce-bogus=&quot;1&quot; style=&quot;background-color: white; color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot; /&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjftDrZBnnxMyRyPJJEWacsmNrqzkUurJ7nYxOG94PIBNjqVjgMsq85QKnC1J_Jbg8NYTpaQWmRs6yDdkIa4rf2D_Za1BBCsBJiQQfj6Ii0lqymE2krcXS_WrK_8Hp-wdilVVGOclWjGgX4/s1600/linkodd.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;Delete middle element of a linked list&quot; border=&quot;0&quot; data-original-height=&quot;340&quot; data-original-width=&quot;1120&quot; height=&quot;194&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjftDrZBnnxMyRyPJJEWacsmNrqzkUurJ7nYxOG94PIBNjqVjgMsq85QKnC1J_Jbg8NYTpaQWmRs6yDdkIa4rf2D_Za1BBCsBJiQQfj6Ii0lqymE2krcXS_WrK_8Hp-wdilVVGOclWjGgX4/s640/linkodd.webp&quot; title=&quot;Delete middle element of a linked list&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;&lt;u&gt;Implementation in C++&lt;/u&gt;&lt;/b&gt;&lt;br /&gt;
&lt;b&gt;&lt;u&gt;&lt;br /&gt;&lt;/u&gt;&lt;/b&gt;
&lt;br /&gt;
&lt;form&gt;
&lt;textarea cols=&quot;80&quot; onclick=&quot;select()&quot; readonly=&quot;&quot; rows=&quot;80&quot;&gt;#include&lt;iostream&gt;
using namespace std;
struct node
{
    int data;
    struct node* next;
};
struct node* head=NULL ;

void insert(int x)   //function it insert elements
{

    struct node* temp=new node();
    temp-&gt;data=x;
    temp-&gt;next=NULL;
    if(head==NULL)
    {
        head=temp;
    }
    else
    {
        struct node* temp1=head;
        while(temp1-&gt;next!=NULL)
        {
            temp1=temp1-&gt;next;
        }
        temp1-&gt;next=temp;
        temp-&gt;next=NULL;
    }
}

void deletemiddle()   //function to delete middle element
{
    struct node *temp1=head;
    int count=0;
    while(temp1!=NULL)    //count number of nodes in linked list
    {
        count++;
        temp1=temp1-&gt;next;
    }
    temp1=head;
    if(count%2==0)      //if number of nodes are even
    {
        int mid1=count/2;     //find mid of linked list
        int p=0;              //it will tell if we have reached mid or not
        while(temp1!=NULL)
        {
            p++;
            if(p+1==mid1)     // it means next node is the mid of linked list
            {
                temp1-&gt;next=temp1-&gt;next-&gt;next-&gt;next;
            }
            temp1=temp1-&gt;next;
        }
    }
    else if(count%2!=0)    //if number of nodes are odd
    {
        int mid=(count+1)/2;    //find mid of linked list
        int p=0;                //it will tell if we have reached mid or not
        while(temp1!=NULL)
        {
            p++;
            if(p+1==mid)        // it means next node is the mid of linked list
            {
                temp1-&gt;next=temp1-&gt;next-&gt;next;
            }
            temp1=temp1-&gt;next;
        }
    }
}

void show()          //function to display linked list
{
    struct node* temp=head;
    while(temp!=NULL)
    {
        cout&lt;&lt;temp-&gt;data&lt;&lt;&quot;  &quot;;
        temp=temp-&gt;next;
    }
    cout&lt;&lt;endl;
}

int main()
{
    insert(2);
    insert(4);
    insert(-3);
    insert(7);
    insert(9);
    insert(8);
    insert(-13);
    cout&lt;&lt;&quot;original linked list: &quot;;
    show();
    cout&lt;&lt;endl;
    cout&lt;&lt;&quot;after deleting middle element: &quot;;
    deletemiddle();
    show();
    return 0;
}


&lt;/textarea&gt;
&lt;/form&gt;
If you have any doubts then please ask in the comment section below.&lt;br /&gt;
&lt;br /&gt;
&lt;strong data-mce-bogus=&quot;1&quot;&gt;&lt;/strong&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/4141716589201606701/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/06/delete-middle-element-of-linked-list.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/4141716589201606701'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/4141716589201606701'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/06/delete-middle-element-of-linked-list.html' title='Delete middle element of a linked list'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEghxCwrvif-ifqyDnfRznHWSaJ9SlFaqjEN9J95IHGoJf2v8udJX1hvasYxFPHlAzeFKwxZrsKh5-DTRZRW0k19Qx_NO9URmUAnK-dD4skqBuSlwEp1efIR4auGO6f7J87HgdQs7kuqBbSI/s72-c/link12.webp" height="72" width="72"/><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-1254145323936187668.post-4655157097320399806</id><published>2020-06-04T06:40:00.002-07:00</published><updated>2020-06-04T06:40:29.425-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="#operatingsystem"/><title type='text'>C LOOK disk scheduling algorithm - operating system tutorial</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;
&lt;br /&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
If you are new to disk scheduling then please read about &lt;a href=&quot;https://geektech1717.blogspot.com/2020/05/disk-scheduling-operating-system.html&quot; target=&quot;_blank&quot;&gt;basics of&amp;nbsp;disk scheduling&lt;/a&gt;&amp;nbsp;first and then read this post on C LOOK disk scheduling algorithm.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
C LOOK disk scheduling algorithm is the last disk scheduling algorithm. It is a combination of &lt;a href=&quot;https://geektech1717.blogspot.com/2020/05/c-scan-disk-scheduling-operating-system.html&quot; target=&quot;_blank&quot;&gt;C-SCAN disk scheduling algorithm&lt;/a&gt; and &lt;a href=&quot;https://geektech1717.blogspot.com/2020/06/look-disk-scheduling-algorithm.html&quot; target=&quot;_blank&quot;&gt;LOOK disk scheduling algorithm&lt;/a&gt;. So, let&#39;s start the discussion on&amp;nbsp;C-LOOK disk scheduling algorithm along with its advantages and let&#39;s find out if there are some disadvantages of it or not.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;C-LOOK disk scheduling algorithm&lt;/span&gt;&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
C-LOOK disk scheduling algorithm starts fulfilling the requests in one direction till it reaches the last track in that direction and then it reverts back to the other end without fulfilling any request and then again revert back and fulfill the requests and so on. It is a combination of both&amp;nbsp;C-SCAN disk scheduling algorithm and LOOK disk scheduling algorithm.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
Let&#39;s understand the concept with the help of an example:&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: center;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px; text-align: center;&quot;&gt;
98, 183, 41, 122, 14, 124, 65, 67&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&amp;nbsp;Fulfill the requests according to C-LOOK disk scheduling algorithm. The read/write head is at 53 initially.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;span data-mce-style=&quot;text-decoration: underline;&quot; style=&quot;text-decoration-line: underline;&quot;&gt;soln&lt;/span&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;separator&quot; style=&quot;clear: both; text-align: center;&quot;&gt;
&lt;a href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhju8RcocZrAHkt4DgkPrcyNpzp-rB3uQDj8GyF_nyKm1qLYA02TjDWFYKMrkUXa6yg2BN_eVsiNxFThOGhK1w3-J2aH-zfvbibovC-3ec4honE_cvr0kWUiR6JHpNSklAtXpXFr3V7jPDb/s1600/cllok.webp&quot; imageanchor=&quot;1&quot; style=&quot;margin-left: 1em; margin-right: 1em;&quot;&gt;&lt;img alt=&quot;C LOOK disk scheduling algorithm - operating system tutorial&quot; border=&quot;0&quot; data-original-height=&quot;471&quot; data-original-width=&quot;1510&quot; height=&quot;198&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhju8RcocZrAHkt4DgkPrcyNpzp-rB3uQDj8GyF_nyKm1qLYA02TjDWFYKMrkUXa6yg2BN_eVsiNxFThOGhK1w3-J2aH-zfvbibovC-3ec4honE_cvr0kWUiR6JHpNSklAtXpXFr3V7jPDb/s640/cllok.webp&quot; title=&quot;C LOOK disk scheduling algorithm - operating system tutorial&quot; width=&quot;640&quot; /&gt;&lt;/a&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. Initially the read/write head is at 53.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. Then the read/write head will move in one direction either left or right (in my case it moves right).&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
3. The first request it fulfills will be 65.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
4. Then it&amp;nbsp;fulfills request 67.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
5. Then it fulfills request 98.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
6. Then it fulfills request 122.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
7. Then it fulfills request 124.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
8. Then it fulfills request 183.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
9. Now it will not go to 199 just like LOOK algorithm and it will revert back from 183 and move till end without fulfilling any request just like C-SCAN algorithm.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
10. Now it will fulfill the request 14.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
11. Then it fulfills the request 41.&lt;/div&gt;
&lt;div data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 data-mce-style=&quot;text-align: left;&quot; style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Advantages&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. C-LOOK disk scheduling algorithm gives a more uniform and better waiting time as compared to LOOK algorithm.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. It is more efficient as compared to C-SCAN.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h3 style=&quot;color: #555555; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-weight: 400;&quot;&gt;
Disadvantages&lt;/h3&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
1. Extra code or effort to build this algorithm.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
2. It should not be used in case there is very high load.&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style=&quot;color: #888888; font-family: sans-serif, Arial, Verdana, &amp;quot;trebuchet ms&amp;quot;; font-size: 14px;&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='https://geektech1717.blogspot.com/feeds/4655157097320399806/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='https://geektech1717.blogspot.com/2020/06/c-look-disk-scheduling-algorithm.html#comment-form' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/4655157097320399806'/><link rel='self' type='application/atom+xml' href='https://www.blogger.com/feeds/1254145323936187668/posts/default/4655157097320399806'/><link rel='alternate' type='text/html' href='https://geektech1717.blogspot.com/2020/06/c-look-disk-scheduling-algorithm.html' title='C LOOK disk scheduling algorithm - operating system tutorial'/><author><name>mayank mewar</name><uri>http://www.blogger.com/profile/13424782535110876204</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='31' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguBnxoQytmcd1fU0PdH2FAeWZuF1UjnkNkg7e01CtwPz-Jk2YJ72ns3lTuHm4kyRRkO50sBHQJGAi5qUHtl8DyUssKZVPfAwnG4Epce2_YK9dk9qbPp3kDSU2cRq2Mdcs/s220/th.jpg'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhju8RcocZrAHkt4DgkPrcyNpzp-rB3uQDj8GyF_nyKm1qLYA02TjDWFYKMrkUXa6yg2BN_eVsiNxFThOGhK1w3-J2aH-zfvbibovC-3ec4honE_cvr0kWUiR6JHpNSklAtXpXFr3V7jPDb/s72-c/cllok.webp" height="72" width="72"/><thr:total>0</thr:total></entry></feed>