Implementing a Queue Using Two Stacks - Data Structures

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ม.ค. 2025

ความคิดเห็น • 40

  • @athego
    @athego 5 หลายเดือนก่อน +1

    Great explanation, you basically have an input and an output Stack!

    • @williamsinitiativesltd
      @williamsinitiativesltd 5 หลายเดือนก่อน

      That's kind of fun when you think of it that way!

  • @KSanofficial
    @KSanofficial 3 ปีที่แล้ว +3

    Very well explained. Helped me a lot!

  • @urrpurr
    @urrpurr 5 ปีที่แล้ว +7

    Quick and thorough explanation, also very good English!

  • @tetsuorulin9009
    @tetsuorulin9009 2 ปีที่แล้ว +1

    thanks G. explained in 5 minutes very well

  • @turtleyoda7703
    @turtleyoda7703 3 ปีที่แล้ว +2

    Quick and clear! Great explanation, thanks

  • @Gauravsharma-gd7fm
    @Gauravsharma-gd7fm 2 ปีที่แล้ว +1

    The best video I came across , helped me a lot..

  • @lazyelectron8376
    @lazyelectron8376 5 ปีที่แล้ว +10

    Thank you very much ! You helped me with my homework. I gave you a thumbs up !

    • @asiandramas1477
      @asiandramas1477 5 ปีที่แล้ว

      Hey do u know the program for this algorithm? My sis wanted it. It would be great help if ull send me the program

    • @lazyelectron8376
      @lazyelectron8376 5 ปีที่แล้ว +3

      ​@@asiandramas1477
      package twostackqueue;
      import java.util.EmptyStackException;
      import java.util.List;
      import java.util.Stack;
      /**
      * @author LazyElectron
      * @since 03/25/2019
      */
      public class TwoStackQueues {
      /**
      * MyQueueI interface to be implemented.
      * @param is the element in the Queue
      */
      interface MyQueueI {
      public E dequeue();
      public void enqueue(E element);
      public boolean isEmpty();
      public E peek();
      }
      /**
      * A queue that is implemented using two stacks.
      * @param is the element in the Queue.
      */
      public static class TwoStackQueue implements MyQueueI {

      private Stack s1;
      private Stack s2;
      /**
      * Default Constructor
      */
      public TwoStackQueue() {
      s1 = new Stack();
      s2 = new Stack();
      }

      /**
      * Constructor which accepts a List as a parameter and builds a Queue with it.
      * @param L is the list from which we build the Queue.
      */
      public TwoStackQueue(List L) {
      s1 = new Stack();
      s2 = new Stack();

      for (Object o : L)
      s1.push((E) o);

      while (!s1.isEmpty())
      s2.push(s1.pop());
      }

      /**
      * Method to remove an item from the queue.
      * @return item that has been removed.
      */
      @Override
      public E dequeue(){
      if (isEmpty())
      throw new EmptyStackException();

      if (s2.isEmpty())
      while (!s1.isEmpty())
      s2.push(s1.pop());

      return s2.pop();
      }
      /**
      * Method to add an item to the queue.
      * @param element is the element to be added.
      */
      @Override
      public void enqueue(E element) {
      s1.push(element);
      }
      /**
      * Method which checks whether the queue is empty.
      * @return true if empty, false if not.
      */
      @Override
      public boolean isEmpty() {
      return s1.isEmpty() && s2.isEmpty();
      }
      /**
      * Method which checks and returns the front of the queue.
      * @return front element in the queue.
      */
      @Override
      public E peek() {
      if (isEmpty())
      throw new EmptyStackException();

      if (s2.isEmpty())
      while (!s1.isEmpty())
      s2.push(s1.pop());

      return s2.peek();
      }
      /**
      * String representation of the TwoStackQueue
      * @return String representation of the TSQ
      */
      @Override
      public String toString() {
      return "TwoStackQueue{" + "s1=" + s1 + ", s2=" + s2 + '}';
      }


      }
      }

    • @asiandramas1477
      @asiandramas1477 5 ปีที่แล้ว

      Thank you so much. What if we want to implement it in c language ? Is it different?

    • @lazyelectron8376
      @lazyelectron8376 5 ปีที่แล้ว +1

      @@asiandramas1477 You're welcome. Sorry, but I can't help you with the C programming language . But you can simply search Two Stack Queue on Google and there will be a link to a website called Geeksforgeeks where they have the same thing written in C.
      Good luck !!

    • @asiandramas1477
      @asiandramas1477 5 ปีที่แล้ว

      Thanks

  • @mitchell3088
    @mitchell3088 2 ปีที่แล้ว +1

    Very clear!!! Thank you

  • @joaquinnader
    @joaquinnader 3 ปีที่แล้ว +1

    Loved this, thanks.

  • @phantomfires2496
    @phantomfires2496 3 ปีที่แล้ว +1

    Amazing explanation.

  • @scum3112
    @scum3112 4 ปีที่แล้ว +1

    You're a life saver! Thanks!

  • @lakhveer0362
    @lakhveer0362 4 ปีที่แล้ว +1

    Super sir😍😍😍thank you so much.. Love you 😍

  • @CodeProps
    @CodeProps 4 ปีที่แล้ว +2

    Thanks dude! this helped a lot!

  • @melissam6037
    @melissam6037 5 ปีที่แล้ว +1

    Fast and easy to u deter and explanation, thanks!

  • @NoodleGrand
    @NoodleGrand 4 ปีที่แล้ว +1

    Well explained

  • @HarshVerma-oz5uo
    @HarshVerma-oz5uo 3 ปีที่แล้ว +1

    Very nice video beautiful explanation of problem and the solution, I hope you will make more videos on DS and Algo. many people struggle with these topics.

  • @sukritkapil2562
    @sukritkapil2562 4 ปีที่แล้ว +1

    thanks, it helps buddy!

  • @vivekgautam9766
    @vivekgautam9766 3 ปีที่แล้ว +1

    Crystal Clear

  • @joeklinck
    @joeklinck หลายเดือนก่อน

    In dequeue you forgot an important step. Either you need to transfer everything back from s2 to s1 or you can have a separate variable that is always pointing to the stack that you intend to be your queue. like this
    Stack myQueue = s1
    perform dequeue
    myQueue = s2
    After that you will use s1 as the stack you will transfer to when performing a dequeue or front and then myQueue will be pointing towards s1

  • @alittlecoding
    @alittlecoding 2 ปีที่แล้ว

    Helpful

  • @kewtomrao
    @kewtomrao 4 ปีที่แล้ว

    wouldnt the pop() on s2 give 'z' since pop() removes the last elemnt of the array but first element of the stack?

  • @nidhaleddinechenni6585
    @nidhaleddinechenni6585 4 ปีที่แล้ว +1

    thank you

  • @cjchen3906
    @cjchen3906 5 ปีที่แล้ว +2

    This is not correct. 5:17 if you call size() after pop(), you are supposed to get 3 {y,z,b} instead of 1 {b}.

  • @jayquelin
    @jayquelin 7 ปีที่แล้ว +3

    Why would one ever do this in real life? It seems to me this approach supports O(n) for dequeue....but a linked list or circular array could do dequeue in O(1) could it not?

    • @Jt-yt8xi
      @Jt-yt8xi 5 ปีที่แล้ว +3

      Late answer but for anyone else wondering why you would use this approach. Reversing the contents of an array is in fact an O(n) operation but the overall dequeue cost is still amortized O(1). Imagine having a large number of items in both the let and right stack. IF you dequeue all the elements, first it will remove all of the elements from the left stack, then reverse copy the right stack only one, and then continue removing elements off the left stack. So there are a few benefits here. Compared to array-based implementation, we were able to transform dequeue into an amortized O(1) operation. Two stack implementation is fully dynamic and doesn't have the fixed size restriction the ring-buffer implementation does. Better then Linked List in terms of spacial locality. This is because array elements are next to each other in memory blocks. So a large number of elements will be loaded in a cache on first access. Linked list aren't in contiguous blocks of memory which could lead to more cache misses and increase access time.

  • @sushmithacs3907
    @sushmithacs3907 7 ปีที่แล้ว +2

    where do we apply this??

    • @srsqtee
      @srsqtee 7 ปีที่แล้ว +3

      It's a common interview question.

  • @ManaGameZone
    @ManaGameZone 6 ปีที่แล้ว +1

    Thanks

  • @Supercool7042
    @Supercool7042 3 ปีที่แล้ว

    nice tutorial but voice is very low

  • @VarsneyaSrinivas
    @VarsneyaSrinivas 4 ปีที่แล้ว

    That accent though.... BTW nice video thanks!