Tech Unleashed

Wohoo!! We're on YouTube

Friday, 22 September 2023

C++ : Lecture 10: Insertion in Doubly Linked List (Insertion at Beg) : Mastering Linked List Made Simple !

 Mastering Linked List Made Simple: Your Easy Guide to Data Structures !


Insertion at beginning in doubly linked list code [C++] :


#include<iostream>

using namespace std;

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

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

void insertAtBeg(Node* &head, Node* &tail, int data){
    // Step 1: Create a 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
    newNode->next = head;
    head->prev = newNode;
    head = newNode;
}

void print(Node* head, Node* tail){
    if(head == NULL){
        cout<<"List is empty."<<endl;
        return;
    }
    Node* temp = head;
    while(temp!=NULL){
        cout<<temp->data<<" ";
        temp=temp->next;
    }
    cout<<endl;
    cout<<"Head: "<<head->data<<endl;
    cout<<"Tail: "<<tail->data<<endl;
}



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

    insertAtBeg(head,tail,5);
    insertAtBeg(head,tail,4);

    print(head,tail);

    
}

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

- Concept

- Cases and subcases in insertion

- Insertion at beginning

- Dry run

- 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:54 Concept

6:43 Operations

10:24 Insertion at beginning

12:36 Approach

21:20 Code

28:25 Output

30:26 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