<?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: dumtochukwu</title>
    <description>The latest articles on DEV Community by dumtochukwu (@narthcodes).</description>
    <link>https://dev.to/narthcodes</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%2F178174%2Fd5f6caa3-ca99-4fe9-9226-284326549c1e.jpg</url>
      <title>DEV Community: dumtochukwu</title>
      <link>https://dev.to/narthcodes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/narthcodes"/>
    <language>en</language>
    <item>
      <title>Dequeue data structure simplified</title>
      <dc:creator>dumtochukwu</dc:creator>
      <pubDate>Tue, 27 Apr 2021 12:03:37 +0000</pubDate>
      <link>https://dev.to/narthcodes/deque-data-structure-simplified-g35</link>
      <guid>https://dev.to/narthcodes/deque-data-structure-simplified-g35</guid>
      <description>&lt;p&gt;Hello friends, in today's article, I'd be writing about my understanding of dequeue data structure, which would be helpful to us in one way or the other.&lt;/p&gt;

&lt;p&gt;Recently, I have been reading and practising Data structure and algorithm in javascript, and one of the data structure, I came across that got me into making more research on it is the dequeue data structure. &lt;/p&gt;

&lt;p&gt;So now let's get to know what this structure is, &lt;strong&gt;&lt;em&gt;Dequeue is a data structure that allows both insertion and deletion of an element from both ends of a list.&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M3MJDcox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rb3q3qhomsjdk6qhlhbw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M3MJDcox--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rb3q3qhomsjdk6qhlhbw.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The diagram above shows us clearly what a dequeue structure looks like. Dequeue can also be seen as the combination of Queues (FIFO = first in first out) and Stack (LIFO = last in first out) operations. Stacks and Queues are one ended operations, unlike the dequeue which is a double-ended operation because of what it does.&lt;/p&gt;

&lt;p&gt;In the Dequeue system, we have the &lt;strong&gt;rear&lt;/strong&gt; (i.e the end of a list) and &lt;strong&gt;front&lt;/strong&gt; (which is the beginning). Elements can either be added from the front or from the rear and also can be removed vice-versa.&lt;/p&gt;

&lt;p&gt;Now I'd drop a code snippet to show you how the dequeue data structure can be implemented in Javascript,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function Deque() {
  this.addFront = addFront
  this.removeFront = removeFront
  this.addBack = addBack
  this.removeBack = removeBack
  this.arr = []
  this.size =size
}

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

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; function size(){
    return this.arr.length // returns the length of the list
 }

 function addFront(el) {
   return this.arr.unshift(el) // Adds an element to the beginning of the list
 }

 function addBack(el){
   return this.arr.push(el) // Adds an element to the rear(end) of the list
 }

  function removeFront(el){
    return this.arr.shift(el) // Removes element from the beginning of the list
  }

  function removeBack(el){
    return this.arr.pop(el) // Removes element from the end of the list
  }


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

&lt;/div&gt;



&lt;p&gt;The above code snippet creates a dequeue constructor and also chains different methods which we'd use to execute our operations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let test = new Deque();
test.addFront('apple');
test.addFront('melon');
test.addBack('ragna')
test.arr // ['ragna', 'apple', 'melon']
test.size() // 3
test.removeBack() // pops out 'ragna'
test.arr // ['apple', 'melon']
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we know how this operation work using Javascript.&lt;br&gt;
Let's try to implement a palindrome checker using the dequeue system&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function palindrome(str){
   let myQueue = new Deque();

   for(let i = 0; i &amp;lt; str.length; i++){
      myQueue.addBack(str[i])
   }

   let isEqual = false;

   while(myQueue.size() &amp;gt; 1){

     let front = myQueue.removeFront();
     let back = myQueue.removeBack();
     if(front === back){
       isEqual = true;

     }
   }
return isEqual
 }

console.log(palindrome('racecar')) // true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And this is how we implement a palindrome checker using a dequeue data structure in javascript. I hope you were able to learn one or two from this article. Kindly leave a reply or a like if you learnt something new thanks 😉.&lt;/p&gt;

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