<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: siva sankar</title>
    <description>The latest articles on DEV Community by siva sankar (@siva089).</description>
    <link>https://dev.to/siva089</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F338866%2F6f6cc571-05c4-41d2-9334-bf827fad0b67.jpeg</url>
      <title>DEV Community: siva sankar</title>
      <link>https://dev.to/siva089</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/siva089"/>
    <language>en</language>
    <item>
      <title>Stacks</title>
      <dc:creator>siva sankar</dc:creator>
      <pubDate>Tue, 21 Dec 2021 03:38:33 +0000</pubDate>
      <link>https://dev.to/siva089/stacks-4772</link>
      <guid>https://dev.to/siva089/stacks-4772</guid>
      <description>&lt;h2&gt;
  
  
  What is a stack?
&lt;/h2&gt;

&lt;p&gt;A LIFO data structure&lt;/p&gt;

&lt;p&gt;LIFO (Last in first out)&lt;/p&gt;

&lt;p&gt;The last element added to the stack will be the first element removed from the stack.&lt;/p&gt;

&lt;p&gt;Think about it like a pile of books. You can only add books on top, and you can only remove the book on top.&lt;/p&gt;

&lt;p&gt;we are going to create stack that has only two methods&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Push() : Method to add data to the stack&lt;/li&gt;
&lt;li&gt;Pop()  : Method to remove data from the stack&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;we can do this in different ways, In this article we are going to implement with JavaScript es6 classes.&lt;/p&gt;

&lt;h2&gt;
  
  
  JavaScript Stack Implementation
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
    constructor(val) {
        this.val = val
        this.next = null
    }
}

class Stack {

    constructor() {

        this.first = null;
        this.last = null;
        this.size = 0

    }

    push(val) {
        let newNode = new Node(val)
        if (!this.first) {
            this.first = newNode;
            this.last = newNode;
        }
        else {
            let temp = this.first;
            this.first = newNode;
            this.first.next = temp;
        }
        return ++this.size;
    }

    pop() {
        if (!this.first) return null;

        let temp = this.first;
        if (this.size === 1) {
            this.last = null;
        }
        this.first = this.first.next
        this.size--;
        return temp.value
    }
}


const stack = new Stack()

stack.push(1)
stack.push(2)
stack.push(3)
stack.pop()
stack.pop()
stack.pop()
stack.pop()

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Where stacks are used
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Managing function invocations&lt;/li&gt;
&lt;li&gt;Undo/Redo&lt;/li&gt;
&lt;li&gt;Routing (History Object)&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  BIG O of Stacks
&lt;/h2&gt;

&lt;p&gt;Insertion - O(1)&lt;br&gt;
  Removal - O(1)&lt;br&gt;
  Searching - O(n)&lt;br&gt;
  Access - O(n)      &lt;/p&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
    <item>
      <title>Singly Linked List</title>
      <dc:creator>siva sankar</dc:creator>
      <pubDate>Mon, 20 Dec 2021 07:43:44 +0000</pubDate>
      <link>https://dev.to/siva089/singly-linked-list-pi</link>
      <guid>https://dev.to/siva089/singly-linked-list-pi</guid>
      <description>&lt;p&gt;&lt;strong&gt;What is a Singly Linked list?&lt;/strong&gt;&lt;br&gt;
A Singly linked list is a collection of nodes and each node has a value and a pointer to another node or null. The value can be any valid data type. You can see this illustrated in the diagram below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ilToPcK_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4v7a0t42o48f67qehmkt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ilToPcK_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4v7a0t42o48f67qehmkt.png" alt="Singly linked list" width="591" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The entry node in linked list is called Head and the last node is called Tail.&lt;/p&gt;

&lt;p&gt;In Javascript a linked list can be implemented like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Node {
    constructor(val) {
        this.val = val;
        this.next=null

    }
}

class SinglyLinkedList{
  constructor() {
        this.head = null;
        this.tail = null;
        this.length=0
    }


//This method used to push a value to end of the linked list
push(val){
   let newNode = new Node(val);
        if (!this.head) {
            this.head = newNode;
            this.tail=newNode;
        }
        else {
            this.tail.next = newNode;
            this.tail = newNode;
        }

        this.length++;
        return this;
}


//this method is used to remove an element from end of linked list.
 pop() {
        if(!this.head)return undefined

        let current = this.head;
        let previous = current;
        while (current.next) {
            previous = current;
            current = current.next

        }
        this.tail = previous
        previous.next = null;
        this.length = this.length - 1;
        if (this.length == 0) {
            this.tail = null
            this.head=null
        }
return this
    }


//this method is used to add a value to beginning of a list.
   unshift(val) {
        let newNode = new Node(val)
        if (!this.head) {
            this.head = newNode
            this.tail=newNode
        }
        else {
            let currentHead = this.head;
            newNode.next = currentHead;
            this.head=newNode
        }

        this.length++
        return this
    }



//this method is used to remove a value from beginning of a list
 shift() {

        if (!this.head) return undefined;
        if (this.length === 1) {
            this.head = null;
            this.tail = null

        }
        else {
            this.head = this.head.next;
        }

        this.length--;
return this;
    }


//this method is used to get a value from a particular position in list.

 get(position) {
        if (position &amp;lt; 0 ||position&amp;gt;=this.length) {
         return undefined
        }
        let index = 0;
        let currentHead = this.head;
        while (position!=index) {
            currentHead = currentHead.next;
            index++;
        }

return currentHead
    }


//this method is used to replace a value from a particular position in list.

   set(value, position) {

        let getValue = this.get(position)
        if (!getValue) return false;
        getValue.val=value
        return this
    }



//this method is user to insert an element in the list at a particular position.

insert(position, val) {
        if (position &amp;lt; 0 || position &amp;gt; this.length) {
            return undefined;
        }
        if (position === 0) {
            return this.unshift(val)
        }
        else if (position === this.length) {
          return  this.push(val)
        }
        let newNode = new Node(val)
        let previous = this.get(position - 1)
        if (!previous) return false;
        let nextEl = previous.next;
        newNode.next = nextEl;
        previous.next=newNode
        this.length++;

return this;
    }


//this method is used to remove a value from a particular position

remove(position) {

        if (position &amp;lt; 0 || position &amp;gt; this.length) {
            return undefined
        }

        if (position === 0) {
            return this.shift(val)
        }
        else if (position === this.length-1) {
            return this.pop(val)
        }
        let getEl = this.get(position-1)
        getEl.next = getEl.next.next;

        this.length--;
return this
    }

//this method is used to get the size of the list

size(){
return this.length
}

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Advantages of singley linked list
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Insertions and Deletions can be done easily&lt;/li&gt;
&lt;li&gt;It does not need movement of elements for insertion and deletion unlike arrays.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Disadvantages of singley linked list
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Search operations are slow in linked lists. Unlike arrays, random access of data elements is not allowed. Nodes are accessed sequentially starting from the first node.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  BIG O Notation
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Access O(n)&lt;/li&gt;
&lt;li&gt;Search O(n)&lt;/li&gt;
&lt;li&gt;Insertion O(1)&lt;/li&gt;
&lt;li&gt;Deletion O(1)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>javascript</category>
      <category>algorithms</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
