Mastering Linked List Made Simple: Your Easy Guide to Data Structures !
Deletion from any position in Singly Linked List Code [C++] :
#include<iostream>using namespace std;class Node{public:int data;Node* next;Node(int val){this->data = val;this->next = NULL;}};int length(Node* head){Node* temp = head;int cnt = 1;while(temp->next != NULL){cnt++;temp = temp->next;}return cnt;}void delFromBeg(Node* &head, Node* &tail){// Step 1: Handle Empty list caseif(head == NULL){cout<<"List is empty"<<endl;return;}// Step 2: Handle Non empty list caseNode* temp = head;head = head->next;temp->next = NULL;}void delFromEnd(Node* &head, Node* &tail){// Step 1: EMpty listif(head == NULL){cout<<"List is empty"<<endl;return;}// Step 2: Non-empty listNode* temp = head;while(temp->next != tail){temp = temp->next;}tail = temp;temp->next = NULL;}void delFromPos(Node* &head, Node* &tail, int pos){// Case 1: handle empty list caseif(head == NULL){cout<<"List is empty"<<endl;return;}// Case 2: Non-empty list handle karo// Subcase 1: Deletion from beginning caseif(pos == 1){delFromBeg(head,tail);return;}// Subcase 2: Deletion from end caseint len = length(head);if(pos == len){delFromEnd(head,tail);return;}// Subcase 3: Any random positionNode* curr = head;Node* prev = NULL;int cnt = 1;while(cnt<pos){prev = curr;curr = curr->next;cnt++;}prev->next = curr->next;curr->next = NULL;}void insertAtBeginning(Node* &head,Node* &tail, int data){// Creation of new NodeNode* newNode = new Node(data);// CHeck if the list is emptyif(head == NULL){head = newNode;tail = newNode;return;}newNode->next = head;head = 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);insertAtBeginning(head,tail,30);insertAtBeginning(head,tail,40);insertAtBeginning(head,tail,50);insertAtBeginning(head,tail,60);delFromPos(head,tail,1);delFromPos(head,tail,5);delFromPos(head,tail,3);cout<<"After Deletion"<<endl;print(head,tail);return 0;}
Hey, thanks for watching our video about Linked list ! In this video we’ll walk you through:
- Concepts
- Cases and Subcases in deletion
- Implementation
- 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:25 Concept
1:46 Approach
1:56 Handling empty list case
3:06 Handling non empty list case
4:01 Subcase 1: Deletion from beginning
4:50 Subcase 2: Deletion from end
7:58 Subcase 3: Any other position case
24:20 Code
29:25 Output
32:02 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