<?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: Buddhini Abayaratna</title>
    <description>The latest articles on DEV Community by Buddhini Abayaratna (@espurrr).</description>
    <link>https://dev.to/espurrr</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%2F133630%2Ffa7aa1b9-3298-4dc9-82a9-39ea8afeaa9e.jpg</url>
      <title>DEV Community: Buddhini Abayaratna</title>
      <link>https://dev.to/espurrr</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/espurrr"/>
    <language>en</language>
    <item>
      <title>Queue using stacks &amp; stack using queues</title>
      <dc:creator>Buddhini Abayaratna</dc:creator>
      <pubDate>Thu, 22 Aug 2019 18:40:59 +0000</pubDate>
      <link>https://dev.to/espurrr/queue-using-stacks-stack-using-queues-ab0</link>
      <guid>https://dev.to/espurrr/queue-using-stacks-stack-using-queues-ab0</guid>
      <description>&lt;h1&gt;
  
  
  Stack and Queue: What ❔
&lt;/h1&gt;

&lt;p&gt;Both are non-primitive, linear data structures. The main difference among two is that the stack uses LIFO (Last-in First-out) access policy whereas the queue uses FIFO (First-in First-out) access policy.&lt;/p&gt;

&lt;p&gt;To quickly demonstrate the two data structures:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fsocv0kv20ymmfiwsrdid.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fsocv0kv20ymmfiwsrdid.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this post, we're not gonna dig deep into the implementation of the two data structures. Instead, we will talk about a very common interview question for computer science / software engineering graduates.&lt;/p&gt;

&lt;h2&gt;
  
  
  Q : How to implement a queue using stacks and a stack using queues?😮
&lt;/h2&gt;

&lt;p&gt;Worry not, it's peanuts!🙃&lt;br&gt;
As you know, for both data structures there're two main operations we consider about. &lt;em&gt;INSERTION&lt;/em&gt; and &lt;em&gt;DELETION&lt;/em&gt; &lt;br&gt;
In other terms, for stack it should be &lt;em&gt;push&lt;/em&gt; and &lt;em&gt;pop&lt;/em&gt; while for queue it's &lt;em&gt;enqueue&lt;/em&gt; and &lt;em&gt;dequeue&lt;/em&gt;.&lt;br&gt;
Let's go through one at a time..&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Queue using stacks (actually &lt;em&gt;two&lt;/em&gt;)&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  ENQUEUE
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fer70jjsesvaf3ri24ikg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fer70jjsesvaf3ri24ikg.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
So we have a queue which has correctly enqueued 1 , 2 elements and now we wish to add 3 into it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 1 :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fimi0y32tkndb0a9eg5ag.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fimi0y32tkndb0a9eg5ag.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
pop elements from stack 1 and push into stack 2 until stack 1 is empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 2 :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ft9dsfzzsptbid85q9ht2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Ft9dsfzzsptbid85q9ht2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Now, push the element to be enqueued into stack 1&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 3 :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6bkk2chny8ck7g8olcmj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6bkk2chny8ck7g8olcmj.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
And then, pop elements from stack 2 and push them back to stack 1 until stack 2 is empty.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 4 :&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnuauuv68sumlia9ex7fh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fnuauuv68sumlia9ex7fh.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
There you go!&lt;/p&gt;

&lt;h4&gt;
  
  
  DEQUEUE
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;STEP 1 :&lt;/strong&gt;&lt;br&gt;
First, we need to check whether stack 1 is empty (which in our case is &lt;em&gt;not&lt;/em&gt;). If so, an error message should be displayed 'Underflow'. Because there's no element to throw away.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 2:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F3p6qxt2037mzre49mabm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F3p6qxt2037mzre49mabm.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
pop an element from stack 1 and return it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note to consider:&lt;/em&gt; we made the enqueue algorithm expensive here. Which means, we did the hard work for enqueue and dequeue was just popping up the top element. You can try it yourself making the dequeue algorithm costly.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Stack using queues (again &lt;em&gt;two&lt;/em&gt;)&lt;/strong&gt;
&lt;/h3&gt;

&lt;h4&gt;
  
  
  PUSH
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fcopwplt9rs4jnbr23jp0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fcopwplt9rs4jnbr23jp0.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
So we have a stack which has correctly pushed 1 , 2 elements and now we have to push 3 into it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 1:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F11vtho0r14ef9mlfm8to.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F11vtho0r14ef9mlfm8to.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Enqueue the element to be pushed into queue 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 2:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1dpbtdg4b63t3agjjxt6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F1dpbtdg4b63t3agjjxt6.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
One by one, dequeue elements from queue 1 and enqueue them into queue 2.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 3:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fslwmdasmhirzuzkcwtcl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fslwmdasmhirzuzkcwtcl.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
Now, simply swap the name of the two queues.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 4:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn0j9335ydz0gb6sr0mnf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fn0j9335ydz0gb6sr0mnf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
That's it!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Another Approach:&lt;/em&gt; Check out &lt;a class="mentioned-user" href="https://dev.to/mvthanoshan"&gt;@mvthanoshan&lt;/a&gt;'s take on this.. &lt;br&gt;
 &lt;a href="https://dev.to/mvthanoshan/comment/19hpc"&gt;https://dev.to/mvthanoshan/comment/19hpc&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  POP
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;STEP 1:&lt;/strong&gt;&lt;br&gt;
Check whether both queues are empty. If so, it should alert 'Underflow'.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;STEP 2:&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkvvnq4baefuozndrd7s4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Fkvvnq4baefuozndrd7s4.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
dequeue an element from queue 1 and return it.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note to consider:&lt;/em&gt; here as well we made the insertion method, pushing expensive. You may try the vice versa for your practice.&lt;/p&gt;

&lt;p&gt;Tada!🎉&lt;br&gt;
And you are one step closer to impress your interviewer.&lt;/p&gt;

</description>
      <category>queue</category>
      <category>stack</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
