Tech Unleashed

Wohoo!! We're on YouTube

Wednesday, 27 September 2023

C++ : Lecture 15: Deletion from Any Position in Doubly Linked List : Mastering Linked List Made Simple !

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


Deletion from Any Position 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;
    }
};

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

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 delFromBeg(Node* &head, Node* &tail){
    // Step 1:Empty list case
    if(head == NULL){
        cout<<"List is empty";
        return;
    }

    // Step 2: Handle non empty list case
    head = head->next;
    head->prev->next = NULL;
    head->prev = NULL;
}

void delFromEnd(Node* &head, Node* &tail){
    // Step 1: Empty list
    if(head == NULL){
        cout<<"List is empty";
        return;
    }

    // Step 2: Non empty list
    tail = tail->prev;
    tail->next->prev = NULL;
    tail->next = NULL;
}

void delFromPos(Node* &head, Node* &tail, int pos){
    // Step 1: Handle empty list case
    if(head == NULL){
        cout<<"List is empty";
        return;
    }

    // Step 2: Handle non-empty list case
    // case 1: Deletion at beginning
    if(pos == 1){
        delFromBeg(head,tail);
        return;
    }

    // Case 2: Deletion from end
    int len = length(head);
    if(pos == len){
        delFromEnd(head,tail);
        return;
    }

    // Case 3: Any random position
    Node* back = NULL;
    Node* curr = head;
    int cnt = 1;
    while(cnt<pos){
        back = curr;
        curr = curr->next;
        cnt++;
    }
    back->next = curr->next;
    curr->next->prev = back;
    curr->prev = NULL;
    curr->next = NULL;
}

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,8);
    insertAtBeg(head,tail,7);
    insertAtBeg(head,tail,6);
    insertAtBeg(head,tail,5);
    insertAtBeg(head,tail,4);
    insertAtBeg(head,tail,3);
    insertAtBeg(head,tail,2);
    insertAtBeg(head,tail,1);

    delFromPos(head,tail,1);
    delFromPos(head,tail,7);
    delFromPos(head,tail,3);
    delFromPos(head,tail,4);

    print(head,tail);
    return 0;

}

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

- Concept

- Cases and subcases in deletion

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

2:27 Approach

3:03 Handling empty list case

3:42 Handling non-empty list case

5:25 Subcase 1: Deletion from beginning

6:20 Subcase 2: Deletion from end

8:25 Subcase 3: Deletion from any random position

22:50 Code

28:53 Output

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