Tech Unleashed

Wohoo!! We're on YouTube

Tuesday, 26 December 2023

C++ : Lecture 26: Stack Implementation Using Queue : Mastering Stack Made Simple !

 Mastering Stack Made Simple: Your Easy Guide to Data Structures !


Stack implementation using queue code [C++] :


// Shyam Sunder Kanth
// Insta : still_23.6_8
// ? Stack implementation using Queue

#include<iostream>
#include<queue>

using namespace std;

void push(queue<int> &q1, queue<int> &q2, int data){
    // Step 1: Insert element in q2
    q2.push(data);

    // Step 2: Transfer all elements from q1 to q2
    while(!q1.empty()){
        int ele = q1.front();
        q1.pop();
        q2.push(ele);
    }
    
    // Step 3: Swap both queues
    swap(q1,q2);
}

void pop(queue<int> &q1){
    q1.pop();
}

int top(queue<int> q1){
    return q1.front();
}

int main()
{
    queue<int> q1;
    queue<int> q2;

    push(q1,q2,1);
    push(q1,q2,2);
    push(q1,q2,3);
    push(q1,q2,4);


    pop(q1);
    
    cout<<top(q1);


    return 0;
}

In this comprehensive tutorial, I guide you through the process of implementing a stack data structure using a queue in a step-by-step manner. You will learn the fundamental principles behind both stacks and queues, and how to leverage the FIFO (First In, First Out) property of a queue to emulate the LIFO (Last In, First Out) behavior of a stack. By the end of this tutorial, you will have a solid understanding of how to effectively use queues to create a stack, and how this can be applied in real-world scenarios.


Key Topics Covered:

- Concept

- Implementation

- Code


All codes are available at my GitHub account, check them out here: https://github.com/shyamkanth/Placements


Check out other video from DS Revealed Playlist: https://www.youtube.com/playlist?list=PLNXqJgOsTCZOB60T9HDhMf_o8RXbNWgqS


Check out other video from C++ Confusion Buster Playlist: https://youtube.com/playlist?list=PLNXqJgOsTCZNf9SWlh60UJzkryy3nQ6vZ&si=SEq97hVG_AvtjG75


Check out other video from Honest Talk Playlist: https://youtube.com/playlist?list=PLNXqJgOsTCZNNw8_5f9biPcbdZRuFOHO-&si=wWNpaaj93ID2x90W


Check out our channel here: https://www.youtube.com/@DevelopersByte


Timestamps:

0:00 Intro

01:15 Concept

05:15 Implementation

18:20 Code

26:35 Outro


About our channel:

Our channel is about Revealing the secrets of Data Structure. We cover lots of cool stuff such as Codes, Concepts and Implementations.

Check out our channel here: https://www.youtube.com/@DevelopersByte

Don’t forget to subscribe!


Follow me on social media:

Get updates or reach out to Get updates on our Social Media Profiles!

GitHub: https://github.com/shyamkanth/

Instagram: https://instagram.com/still_23.6_8

Instagram personal: https://instagram.com/itz_sammmii

No comments:

Post a Comment