Mastering Linked List Made Simple: Your Easy Guide to Data Structures !
Insertion at 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 NodeNode* newNode = new Node(data);// Step 2: Handle empty list caseif(head == NULL){head = newNode;tail = newNode;return;}// Step 3: Handle non-empty list casenewNode->next = head;head->prev = newNode;head = newNode;}void insertAtEnd(Node* &head, Node* &tail, int data){// Step 1: Create a new NodeNode* newNode = new Node(data);// Step 2: Handle empty list caseif(head == NULL){head = newNode;tail = newNode;return;}// Step 3: Non empty list handletail->next = newNode;newNode->prev = tail;tail = newNode;}void insertAtPos(Node* &head, Node* &tail, int data, int pos){// Step 1Node* newNode = new Node(data);// Step 2:if(head == NULL){head = newNode;tail = newNode;return;}// Step 3// Subcase 1if(pos == 1){insertAtBeg(head,tail,data);return;}// Subcase 2int len = length(head);if(pos == len+1){insertAtEnd(head,tail,data);return;}// Subcase 3Node* back = NULL;Node* front = head;int cnt = 1;while(cnt<pos){back = front;front = front->next;cnt++;}newNode->next = front;newNode->prev = back;back->next = newNode;front->prev = 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);insertAtEnd(head,tail,6);insertAtEnd(head,tail,7);insertAtPos(head,tail,1,1);insertAtPos(head,tail,8,6);insertAtPos(head,tail,2,2);insertAtPos(head,tail,3,3);print(head,tail);}
Hey, thanks for watching our video about Linked list ! In this video we’ll walk you through:
- Concept
- Insertion at any position
- Cases in insertion
- Subcases in insertion
- 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:43 Concept
3:28 Approach
7:05 Subcase-1: insert at beginning
8:07 Subcase-2: insert at end
9:49 Subcase-3: any random position
26:55 Code
31:05 Output
33:43 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