Implement Queue using Stacks | Queue in Data Structure & Algorithms | Interview Question Hello World

Sdílet
Vložit
  • čas přidán 7. 09. 2024
  • This is the video under the series of DATA STRUCTURE & ALGORITHM in a STACK & QUEUE Playlist. Now we are going to Learn to solve Questions on Queue in Data Structure from Leetcode Implement Queue using Stacks.
    Join My Telegram channel for more Updates: telegram.me/he...
    complete DSA preparation: github.com/Pri...
    ---------------------------------------------------------------------------------------
    ► Implement Queue using Stacks: leetcode.com/p...
    ► code In this video: github.com/Pri...
    ----------------------------------------------------------------------------------------
    *Follow me *
    LinkedIn► / iamprince
    Facebook► / helloworldofficials
    Instagram► / helloworldbyprince
    Twitter► / prince_king_
    Telegram► telegram.me/he...
    ----------------------------------------------------------------------------------------
    ►Our Playlists on:-
    ► Tree: • Tree Data Structure & ...
    ► Stack & Queue: • Stack & Queue Data Str...
    ► Hashing: • Hashing Data Structure...
    ► Graph: • Graph Data Structure &...
    ► Matrix: • Matrix (Multidimension...
    ► STL: • Standard Template Libr...
    ► Leetcode: • LeetCode Solutions And...
    ►Competitive Programming: • Full course in Competi...
    ►C++ Full Course : • C++ full Course in HINDI
    ►Algorithms: • L-01 || Prefix Sum Arr...
    ►Data Structure: • Data Structures with C...
    ------------------------------------------------------------------------
    🌟 Please leave a LIKE ❤️ and SUBSCRIBE for more AMAZING content! 🌟
    ✨ Tags ✨
    leetcode problems
    leetcode problems java
    leetcode problems python
    leetcode problems that got me tired
    leetcode problems c++
    leetcode problems and solutions python
    leetcode problems playlist
    leetcode problems and solutions java
    leetcode problems in Hindi
    leetcode problems javascript
    leetcode problems and solutions
    leetcode problems of the day
    leetcode problems for beginners
    leetcode problems easy
    leetcode problems js
    Introduction to the queue data structure
    Queue playlist Hello world
    Queue practice problems
    Queue practice problems gfg
    leetcode Queue questions
    leetcode Queue queue
    Queue hello world
    Types of Queue in Data structure & algorithms
    playlist Queue Hindi
    question asked in Google
    off-campus placement
    Practice Queue data structure
    Queue in a data structure in Hindi
    Queue Full playlist for Beginners
    algorithms
    graph
    data structure
    sorting algorithms
    algorithm analysis
    gate computer science preparation
    programming languages
    #Queue #Leetcode #programming

Komentáře • 49

  • @code-nk
    @code-nk Před 11 dny

    class MyQueue {
    stackinput,output;
    // when doing peek or pop we have to fill the elements in a different container (so we will use two stacks for this) as in stack the element that should be on front in queue are at the last
    void transfer() {
    if (output.empty()) {
    while (!input.empty()) {
    output.push(input.top());
    input.pop();
    }
    }
    }
    public:
    MyQueue() {}

    void push(int x) {
    input.push(x);
    }

    int pop() {
    transfer();
    int front = output.top();
    output.pop();
    return front;

    }

    int peek() {
    transfer();
    return output.top();
    }

    bool empty() {
    return input.empty()&& output.empty();
    }
    };

  • @pritishpattnaik4674
    @pritishpattnaik4674 Před 2 lety +1

    Beautifully explained Prince bhai , endsem chal raha tha toh consistency thoda break ho gaya mera
    My code :
    class MyQueue {
    public:

    stack input, output;

    MyQueue() {

    }

    void push(int x) {
    input.push(x);
    }

    int pop() {
    int ans = peek();
    output.pop();
    return ans;
    }

    int peek() {
    if (output.empty() == true){
    while (!input.empty()){
    output.push(input.top());
    input.pop();
    }
    }
    return output.top();
    }

    bool empty() {
    if (input.empty() == true and output.empty() == true){
    return true;
    }
    return false;
    }
    };

  • @iftekhar_ansari
    @iftekhar_ansari Před 2 lety +1

    there is catch
    input.push(10);
    input.push(20);
    input.push(30);
    input.push(40);
    if i you call pop() method then it should return 10 but in your code it will return 40; if you never call peek() method before calling pop() method

    • @HelloWorldbyprince
      @HelloWorldbyprince  Před rokem

      waooo

    • @arijitianbiplab2604
      @arijitianbiplab2604 Před rokem

      @@HelloWorldbyprince wow kya hota hai master ji, push k time output mein dal dijiye .

    • @arijitianbiplab2604
      @arijitianbiplab2604 Před rokem

      class MyQueue {
      public:
      stack s1,s2;
      MyQueue() {

      }

      void push(int x) {
      s2.push(x);
      }

      int pop() {
      if(s1.empty())
      {
      while(!s2.empty())
      {
      s1.push(s2.top());
      s2.pop();
      }
      }
      int v=s1.top();
      s1.pop();
      return v;
      }

      int peek() {
      if(s1.empty())
      {
      while(!s2.empty())
      {
      s1.push(s2.top());
      s2.pop();
      }
      }
      return s1.top();
      }

      bool empty() {
      return s1.empty()&&s2.empty();

      }
      };

    • @arijitianbiplab2604
      @arijitianbiplab2604 Před rokem

      shayad yeh a66a hai thoda

  • @showsshorts4696
    @showsshorts4696 Před rokem +1

    sir 318 virtual contest ke questions pe video baniyye . it will be really helpful

  • @devsharma181
    @devsharma181 Před 10 měsíci

    1st method implemented successfully bro

  • @govind_suthar05
    @govind_suthar05 Před 7 měsíci

    Done in one go.

  • @thenextlevel4248
    @thenextlevel4248 Před 10 měsíci

    ye pop vale operation me apne peek () function ko call kiya hai kya niche vala

  • @masumali8356
    @masumali8356 Před rokem

    Great video......Thank you.

  • @ujjwalgupta9032
    @ujjwalgupta9032 Před rokem

    Great video 👍

  • @memefulland7140
    @memefulland7140 Před 2 lety +1

    o(n) vala coded myself

  • @sandeepdhiman8084
    @sandeepdhiman8084 Před měsícem

    aaya. same thaught

  • @Anuj-vk1vm
    @Anuj-vk1vm Před 2 lety

    very nice!!

  • @sourav6321
    @sourav6321 Před 2 lety

    Soch liya bhaiya pehle wala method

  • @ashvanikharwar1500
    @ashvanikharwar1500 Před 2 lety

    i will try it

  • @kumar_sanjeevdutta1069
    @kumar_sanjeevdutta1069 Před 11 měsíci

    Sir, I tried

  • @sus_tha_coder
    @sus_tha_coder Před 2 lety

    nice video!

  • @himanshutiwari169
    @himanshutiwari169 Před 7 měsíci

    i think this solution but fell lazy to implement

  • @raufurrahmankhan1284
    @raufurrahmankhan1284 Před rokem

    Bhai ye jo last me solution btaya iski complexity bhi toh O(N) aaegi?

    • @raufurrahmankhan1284
      @raufurrahmankhan1284 Před rokem

      Kyuki peek me jab dusra queue empty hoga toh poora first queue khali krke dusre me daalna padh rha

  • @sarangtamrakar8723
    @sarangtamrakar8723 Před 2 lety +1

    Bhai Video to theek h , but leetcode ka logo kahi or se inspired lag raha h

  • @raufurrahmankhan1284
    @raufurrahmankhan1284 Před rokem

    Prince bhai complexity iski bhi O(N) hogai kya?

  • @manikchadha6271
    @manikchadha6271 Před 2 lety +1

    Nhi socha maine...Meri galti I should have invested more time

  • @shaantyagi2187
    @shaantyagi2187 Před 2 lety

    mera bhi run kar gya ...answer sahi aa gya but koi test case fail ho gya tha.

  • @asurstark
    @asurstark Před rokem

    Showing TLE , code not working class MyQueue {
    public:
    stack input, output;
    MyQueue() {

    }

    void push(int x)
    {
    input.push(x);
    }

    int pop()
    {
    int val =output.top();
    output.pop();
    return val;
    }

    int peek()
    {
    if(output.empty())
    {
    while(input.empty()==false)
    output.push(input.top());
    input.pop();
    }
    return output.top();
    }

    bool empty()
    {
    return input.empty() && output.empty();
    }
    };
    /**
    * Your MyQueue object will be instantiated and called as such:
    * MyQueue* obj = new MyQueue();
    * obj->push(x);
    * int param_2 = obj->pop();
    * int param_3 = obj->peek();
    * bool param_4 = obj->empty();
    */

  • @kushalshukla444
    @kushalshukla444 Před 11 měsíci

    aagya

  • @oqant0424
    @oqant0424 Před rokem

    maine second solution soch liya tha.....but implementation me problem ho rahi thi