Tech Unleashed

Wohoo!! We're on YouTube

Saturday, 30 September 2023

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

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


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


#include<iostream>

using namespace std;

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

    Node(int val){
        this->data = val;
        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 new Node
    Node* newNode = new Node(data);

    // Step 2: Empty list handle
    if(head == NULL){
        head = newNode;
        tail = newNode;
        return;
    }

    // Step 3: Non-empty list handle
    // Sub-1
    if(pos == 1){
        newNode->next = head;
        tail->next = newNode;
        head =newNode;
        return;
    }

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

    // Sub-3
    Node* back = NULL;
    Node* front = head;
    int cnt = 1;
    while(cnt<pos){
        back = front;
        front = front->next;
        cnt++;
    }
    newNode->next = front;
    back->next = 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,4,7);
    insert(head,tail,2,1);
    insert(head,tail,8,3);
    insert(head,tail,6,3);
    print(head);

    return 0;
}

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

- Concepts

- Operations

- Insertion in circular linked list

- Steps in insertion

- Length of circular linked list

- 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 of circular linked list

5:13 Operations

5:34 Insertion

9:05 Steps in insertion

27:25 Length of circular linked list

30:50 Code

39:10 Output

42:05 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