Tech Unleashed

Wohoo!! We're on YouTube

Wednesday, 20 September 2023

C++ : Lecture 5: Insertion at Any Position in Singly Linked List : Mastering Linked List Made Simple !

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


Insertion at any position in Singly Linked List Code [C++] :


#include<iostream>

using namespace std;

//Implementation of Node
class Node{
    public:
    int data;
    Node* next;
    Node(int data){
        this->data = data;
        this->next = NULL;
    }
};

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

void insertAtBeginning(Node* &head,Node* &tail, int data){
    // Creation of new Node
    Node* newNode = new Node(data);
    // CHeck if the list is empty
    if(head == NULL){
        head = newNode;
        tail = newNode;
        return;
    }
    newNode->next = head;
    head = newNode;
}

void insertionAtEnd(Node* &head, Node* &tail, int data){
    Node* newNode = new Node(data);
    if(head == NULL){
        head = newNode;
        tail = newNode;
    }else{
        tail->next = newNode;
        tail = newNode;
    }
}

void insertAtPosition(Node* &head, Node* &tail, int data, int pos){
    // Step 1
    Node* newNode = new Node(data);
    // Step 2
    if(head == NULL){
        head = newNode;
        tail = newNode;
        return;
    }
    // Step 3
    // Case 1 : Insert at beginning
    if(pos == 1){
        insertAtBeginning(head, tail, data);
        return;
    }
    // Case 2 : insert at end
    int len = length(head);
    if(pos == len+1){
        insertionAtEnd(head, tail, data);
        return;
    }
    // Case 3: any random position
    Node* previous = NULL;
    Node* front = head;
    int cnt = 1;
    while(cnt<pos){
        previous = front;
        front = front->next;
        cnt++;
    }
    newNode->next = front;
    previous->next = newNode;
}

void print(Node* head, Node* tail){
    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;
    insertAtBeginning(head,tail,10);
    insertAtBeginning(head,tail,20);
    insertionAtEnd(head,tail,30);
    insertAtPosition(head,tail,5,1);
    insertAtPosition(head,tail,40,5);
    insertAtPosition(head,tail,25,4);
    
    print(head,tail);
    return 0;
}

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

- Concept

- Steps for insertion

- Cases and subcases

- Length of linked list


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:40 Insertion at any position concept

2:17 Approach

4:00 Step wise implementation

5:18 Subcase 1: Insertion at beginning

6:20 Subcase 2: Insertion at end

8:32 Length of linked list

13:04 Subcase 3: Any random position

24:49 Code

30:40 Code output

32:56 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