Tech Unleashed

Wohoo!! We're on YouTube

Wednesday, 4 October 2023

C++ : Lecture 18: Insertion in Doubly Circular Linked List : Mastering Linked List Made Simple !

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


Insertion in Doubly Circular Linked List code [C++] :


#include<iostream>

using namespace std;

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

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

int length(Node* head, Node* tail){
    Node* temp = head;
    int cnt = 1;
    while(temp != tail){
        temp = temp->next;
        cnt++;
    }
    return cnt;
}

void insert(Node* &head, Node* &tail, int data, int pos){
    // Step 1: creation of newNode
    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
    // Sub-1 : insert at beg
    if(pos == 1){
        newNode->next = head;
        newNode->prev = tail;
        head->prev = newNode;
        tail->next = newNode;
        head = newNode;
        return;
    }

    // Sub-2 : insert at end
    int len = length(head,tail);
    if(pos == len+1){
        newNode->next = head;
        newNode->prev = tail;
        head->prev = newNode;
        tail->next = newNode;
        tail = newNode;
        return;
    }

    // Sub-3 : insert at any pos
    Node* p1 = NULL;
    Node* p2 = head;
    int cnt = 1;
    while(cnt < pos){
        p1 = p2;
        p2 = p2->next;
        cnt++;
    }
    newNode->next = p2;
    newNode->prev = p1;
    p1->next = newNode;
    p2->prev = newNode;
}

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

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

    insert(head,tail,5,10);
    insert(head,tail,4,1);
    insert(head,tail,6,3);
    insert(head,tail,7,4);
    insert(head,tail,10,3);
    insert(head,tail,20,2);


    print(head);


    return 0;
}

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

- Concept

- Insertion in doubly circular linked list

- Steps in insertion

- Cases and subcases

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

4:53 Operations

5:35 Insertion

9:20 Insertion at beginning

15:40 Insertion at end

24:30 Code

31:48 Insertion at any position

37:41 Code

39:25 Output

40:14 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