Tech Unleashed

Wohoo!! We're on YouTube

Monday, 18 December 2023

C++ : Lecture 25: Queue - Linked List Implementation : Mastering Queue Made Simple !

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


Queue - Linked List Implementation code [C++] :


// Shyam Sunder Kanth
// Insta : still_23.6_8
// ?Queue Linked List implementation

#include<iostream>
using namespace std;

class Node{
    public:
    int data;
    Node* next;

    Node(int data){
        this->data = data;
        this->next = NULL;
    }
};

void enque(Node* &head, Node* &tail, int data){
    // Step 1: Creation of new node
    Node* newNode = new Node(data);

    // Step 2: Handle empty list case
    if(head == NULL){
        head = newNode;
        tail = newNode;
        return;
    }

    // Step 3: Handle non-empty list case
    tail->next = newNode;
    tail = newNode;
}

void deque(Node* &head, Node* &tail){
    if(head==NULL){
        cout<<"Queue underflow.";
        return;
    }
    if(head->next == NULL){
        head = tail = NULL;
        return;
    }
    Node* temp = head;
    head = head->next;
    temp->next = NULL;
}

int front(Node* head){
    if(head == NULL){
        cout<<"Queue is empty.";
        return -1;
    }
    return head->data;
}

int back(Node* head, Node* tail){
    if(head==NULL){
        cout<<"Queue is empty";
        return -1;
    }
    return tail->data;
}

int length(Node* head, Node* tail){
    if(head==NULL){
        cout<<"Queue is empty.";
        return -1;
    }
    Node* temp = head;
    int cnt = 0;
    while(temp!=NULL){
        temp=temp->next;
        cnt++;
    }
    return cnt;
}

void print(Node* head){
    if(head == NULL){
        cout<<"Queue is empty.";
        return;
    }
    Node* temp = head;
    while(temp!=NULL){
        cout<<temp->data<<" ";
        temp = temp->next;
    }
}

bool isEmpty(Node* head){
    if(head == NULL){
        return true;
    }
    return false;
}

int main()
{
    Node* head = NULL;
    Node* tail = NULL;

    enque(head,tail,4);
    enque(head,tail,7);
    enque(head,tail,9);

    deque(head,tail);
    deque(head,tail);

    print(head);

    cout<<endl;

    cout<<"Front element is : "<<front(head)<<endl;
    cout<<"Last element is : "<<back(head,tail)<<endl;
    cout<<"Length of queue is : "<<length(head,tail)<<endl;
    cout<<isEmpty(head)<<endl;

    return 0;
}

Check out this in-depth tutorial on implementing a queue using a linked list in C++. Learn how to create a linked list, enqueue and dequeue elements, and handle edge cases. Whether you're a beginner or an experienced programmer, this video will guide you through the process step by step.

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

Hey, thanks for watching our video about Queue Linked List ! In this video we’ll walk you through:

- Concept

- Operations

- Code


All codes are available at my GitHub account, check them out here:

https://github.com/shyamkanth/Placements


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


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


Find us at: https://shyamkanth.github.io/


Timestamps:

0:00 Intro

0:11 Concept

4:45 Operations

10:39 Code

28:00 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