<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>DSA on Nitesh Raya&#39;s Blogs</title>
        <link>/categories/dsa/</link>
        <description>Recent content in DSA on Nitesh Raya&#39;s Blogs</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en-us</language>
        <copyright>nitesh</copyright>
        <lastBuildDate>Wed, 22 Jan 2025 00:00:00 +0000</lastBuildDate><atom:link href="/categories/dsa/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>The ordered list implemenatation</title>
        <link>/post/the-ordered-list-implemenatation/</link>
        <pubDate>Wed, 22 Jan 2025 00:00:00 +0000</pubDate>
        
        <guid>/post/the-ordered-list-implemenatation/</guid>
        <description>&lt;img src="https://miro.medium.com/v2/resize:fit:1024/1*wA75yhv1kptx0bW_c9ooAQ.jpeg" alt="Featured image of post The ordered list implemenatation" /&gt;&lt;p&gt;The ordered list is the collection of items where each items maintains it&amp;rsquo;s position unlike Unordered list. The ordering of the item is typically ascending and descending and we assume that items are ordered with meaningful comparison operation.&lt;/p&gt;
&lt;h2 id=&#34;operations-of-ordered-list&#34;&gt;Operations of Ordered List
&lt;/h2&gt;&lt;ol&gt;
&lt;li&gt;&lt;strong&gt;OrderedList()&lt;/strong&gt;: Creates new ordered list that is empty. Returns empty list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;add(item)&lt;/strong&gt; : adds a new item to the list maintaining the order. Returns nothing.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;remove(item)&lt;/strong&gt;: removes the item from the list , assuming the item is present in the list. Returns boolean(True or False)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;search(item)&lt;/strong&gt;: Searches for the item in the list. Returns boolean(True of False).&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;is_empty()&lt;/strong&gt;: Test to see whether the list is empty. Returns boolean.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;size()&lt;/strong&gt;: Returns the number of the item in the list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;index(item)&lt;/strong&gt;: Returns the position of the item in the list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pop()&lt;/strong&gt;: Removes and returns the last item in the list.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;pop(pos)&lt;/strong&gt;: Removes and returns the position at the position &lt;code&gt;pos&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;&lt;img src=&#34;../../images/Pasted%20image%2020250121190525.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;Image Description&#34;
	
	
&gt;&lt;/p&gt;
&lt;h2 id=&#34;how-to-search-in-the-list&#34;&gt;How to search in the list?
&lt;/h2&gt;&lt;p&gt;Since the list is ordered it is pretty straight-forward to search through the list.
There are two things we need to consider, &lt;code&gt;item&lt;/code&gt; the data or the value that we are searching for and &lt;code&gt;current.data&lt;/code&gt; the data or the value of the current node.&lt;/p&gt;
</description>
        </item>
        <item>
        <title>The Unordered List (Singly Linked List) Implementation</title>
        <link>/post/the-unordered-list-singly-linked-list-implementation/</link>
        <pubDate>Mon, 30 Dec 2024 00:00:00 +0000</pubDate>
        
        <guid>/post/the-unordered-list-singly-linked-list-implementation/</guid>
        <description>&lt;img src="https://miro.medium.com/v2/resize:fit:1024/1*wA75yhv1kptx0bW_c9ooAQ.jpeg" alt="Featured image of post The Unordered List (Singly Linked List) Implementation" /&gt;&lt;h2 id=&#34;understanding-unordered-list&#34;&gt;Understanding Unordered List
&lt;/h2&gt;&lt;p&gt;The simplest way to describe about unordered list is it is a container of items with no ordering, meaning it can be anywhere on the container. This list contains a collection of individual nodes that has pointer to another node. It is important to know about &lt;strong&gt;Node&lt;/strong&gt; before learning unordered list.&lt;/p&gt;
&lt;div style=&#34;display: flex; align-items: center; background-color: #0f172a; border-radius: 8px; padding: 15px; border-left: 4px solid #3b82f6; color: #cbd5e1;&#34;&gt;
    &lt;div style=&#34;color: #3b82f6; font-size: 24px; margin-right: 12px; line-height: 1;&#34;&gt;&amp;#9432;&lt;/div&gt;
    &lt;div style=&#34;flex: 1; font-size: 16px;&#34;&gt;
        &lt;strong&gt;Node:&lt;/strong&gt; A node is a container which has two fields [data, next]. Data contains the actual value on the node and next contains the pointer to the next node if there is any otherwise it contains &lt;strong&gt;null&lt;/strong&gt;. 
    &lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;To understand it more clearly here is the figure that explains the entire analogy of Singly unordered list aka. &lt;strong&gt;Singly Linked List&lt;/strong&gt;.
&lt;img src=&#34;../../images/Pasted%20image%2020241230224311.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;Image Description&#34;
	
	
&gt;&lt;/p&gt;
&lt;p&gt;We have list of items that starts with 5 also known as head of the list, which then points to the other data of the list.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Things to point&lt;/strong&gt;&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Each item on the list is a node not an individual data, where node contains both data and the pointer to another node known as next.&lt;/li&gt;
&lt;li&gt;The only reference of the list is set by the head which points to the latest node inserted.&lt;/li&gt;
&lt;li&gt;The list which is created only knows the head of the whole linked list so there is no way to get the value by index.&lt;/li&gt;
&lt;/ol&gt;
&lt;h2 id=&#34;how-to-add-a-node-to-the-list&#34;&gt;How to add a node to the list?
&lt;/h2&gt;&lt;p&gt;There are many ways to add data to the list, we can add either way , to the front or to the end of the list. The order of the list is not important as this is unordered. Here i will show you how to add to the front of the list i.e the head of the list.
Since we already know the head of the list as the only reference to the list is the head. We do the following to add the item to the list.&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Create a node with data, and the pointer to null. &lt;img src=&#34;../../images/Pasted%20image%2020241230225902.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;Image Description&#34;
	
	
&gt;&lt;/li&gt;
&lt;li&gt;We need to store this on some variable because we need to point it&amp;rsquo;s next to the current head of the list.&lt;img src=&#34;../../images/Pasted%20image%2020241230230435.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;Image Description&#34;
	
	
&gt;&lt;/li&gt;
&lt;li&gt;Then we point the next of the temp to the current head and then assign the head to the temp.&lt;img src=&#34;../../images/Pasted%20image%2020241230230951.png&#34;
	
	
	
	loading=&#34;lazy&#34;
	
		alt=&#34;Image Description&#34;
	
	
&gt;&lt;/li&gt;
&lt;/ol&gt;
&lt;h3 id=&#34;python-code-to-implement-the-list&#34;&gt;Python code to implement the list
&lt;/h3&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;add&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;temp&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;Node&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;temp&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;set_next&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;temp&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;&lt;div style=&#34;display: flex; align-items: center; background-color: #0f172a; border-radius: 8px; padding: 15px; border-left: 4px solid #3b82f6; color: #cbd5e1;&#34;&gt;
    &lt;div style=&#34;color: #3b82f6; font-size: 24px; margin-right: 12px; line-height: 1;&#34;&gt;&amp;#9432;&lt;/div&gt;
    &lt;div style=&#34;flex: 1; font-size: 16px;&#34;&gt;
        &lt;strong&gt;Time Complexity: &lt;/strong&gt; Since we only have to work with the first pointer the time complexity is O(1). 
    &lt;/div&gt;
&lt;/div&gt;
&lt;h2 id=&#34;getting-the-size-of-the-list&#34;&gt;Getting the size of the List
&lt;/h2&gt;&lt;p&gt;How do we get the size of the list? Can you try and think of a solution to get the size?
The most obvious thing that comes to mind is &lt;strong&gt;going through all the nodes and counting them right&lt;/strong&gt;?
YES! that&amp;rsquo;s the solution.
How do we implement that?
1. First thing is we need something to store the count of the list.
2. Then we start from the head (because we only have head as the reference of the list)
3. We then go step by step and increase the count of the variable until we hit the null pointer of the Node.
4. When we hit the null we stop and return the count of the list.&lt;/p&gt;
&lt;h3 id=&#34;python-code-to-implement-the-list-1&#34;&gt;Python code to implement the list
&lt;/h3&gt;&lt;div class=&#34;highlight&#34;&gt;&lt;div class=&#34;chroma&#34;&gt;
&lt;table class=&#34;lntable&#34;&gt;&lt;tr&gt;&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code&gt;&lt;span class=&#34;lnt&#34;&gt;1
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;2
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;3
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;4
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;5
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;6
&lt;/span&gt;&lt;span class=&#34;lnt&#34;&gt;7
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;
&lt;td class=&#34;lntd&#34;&gt;
&lt;pre tabindex=&#34;0&#34; class=&#34;chroma&#34;&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;&lt;span class=&#34;k&#34;&gt;def&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;size&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;):&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;n&#34;&gt;current&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;head&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;current&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;!=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;None&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+=&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt; 
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;            &lt;span class=&#34;n&#34;&gt;current&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;current&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;get_next&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;span class=&#34;line&#34;&gt;&lt;span class=&#34;cl&#34;&gt;        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;count&lt;/span&gt;
&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
&lt;/div&gt;
&lt;/div&gt;</description>
        </item>
        
    </channel>
</rss>
