2.6 Deletion of a node from Linked List (from beginning, end, specified position) | DSA Tutorials
ฝัง
- เผยแพร่เมื่อ 5 ก.ย. 2024
- In this video we have written a C program for Deletion operation in Linked List. We have discussed how to delete a node form singly linked list (from beginning, from end, from given position) with examples. and with Code
DSA Full Course: https: • Data Structures and Al...
******************************************
See Complete Playlists:
C Programming Course: • Programming in C
C++ Programming: • C++ Complete Course
Python Full Course: • Python - Basic to Advance
Printing Pattern in C: • Printing Pattern Progr...
DAA Course: • Design and Analysis of...
Placement Series: • Placements Series
Dynamic Programming: • Dynamic Programming
Operating Systems: // • Operating Systems
DBMS: • DBMS (Database Managem...
************************************************
Connect & Contact Me:
Facebook: / jennys-lectures-csit-n...
Quora: www.quora.com/...
Instagram: / jayantikhatrilamba
#datastructures
#linkedlistindatastructure
#algorithms
#dsa
CORRECTION: 1) In delfrombeg() function we will write one more condition to check whether there is only one node in the list :
if(head->next==0)
{ head=0;
free(temp);
}
2) in delfrompos() function check for corner case (if position is 1 then call delfrombeg() function):
if(pos==1)
{ delfrombeg();
}
Also check for invalid position by finding the length of the list.(This case I have discussed in deletion from circular linked list video)
yes,that is great approach
Correction 1): not required.
head=head->next;
Will work
int i = 1;
while (i < pos - 1) {
temp = temp->next;
i++;
}
nextNode = temp->next;
if (pos == 1) {
head = nextNode;
free(temp);
} else {
temp->next = nextNode->next;
free(nextNode);
}
According to me, the only modification required is the one which I've written with if (pos == 0) and it's else part. Otherwise, the current code is good to go. I am assuming here the user has written a separate code for delete at the beginning.
MA'AM No.1) mein free(head) hoga na ?
Clear and neat explaination mam......3 months of class solved in one day😅😅😅🤩🤩🤩🤩
Ummm
Ye link list itni tough kyu h 😮
The singly Linked list was discussed by my college professors and tuition teacher at least 4 or 5 times still there was confusion. But now it is so clear within one day. Thank you ma'am 🙇
True 😢
it took me 2years to clear my doubts about linked list.thank you so much ma'am.
Thankyou Ma'am So much . ❤❤❤
I am taking stress for DSA subject 😢.
But after attending your lecture it seems like DIL GARDEN GARDEN HO GYA.
Full respect ma'am.
गुरु ब्रह्मा गुरु विष्णु गुरु देवो महेश्वरा:
गुरु साक्षात परम ब्रम्ह तस्मे श्री गुरवे नमः
What a explanation madam everyone without any basics will also understand your lectures .Thank you mam 🙏🙏
Thanks mam, you are the best teacher in the world for programming.
I think you are great teacher thanks alot. You thought far much better than my lecturer in class.
//code for single linked list
#include
#include
void creation();
void display();
void insertion();
void deletion();
void search();
int length();
struct node
{
int data;
struct node *next;
};
struct node *temp,*newnode,*head,*nextnode;
int main()
{
int ch;
while(1)
{
scanf("%d",&ch);
switch(ch)
{
case 1:creation();
break;
case 2:display();
break;
case 3:insertion();
break;
case 4:deletion();
break;
case 5:search();
break;
case 7:exit(0);
break;
default:printf("Invalid Choice");
}
}
}
void creation()
{
int x=1;
head=NULL;
while(x)
{
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==NULL)
temp=head=newnode;
else
{
temp->next=newnode;
temp=newnode;
}
printf("Enter 0 to stop or 1 to continue
");
scanf("%d",&x);
}
}
void display()
{
temp=head;
while(temp!=NULL)
{
printf("%d ",temp->data);
temp=temp->next;
}
}
void insertion()
{
int pos,i=1,x;
x=length();
temp=head;
printf("Enter position ");
scanf("%d",&pos);
if(pos>x)
printf("Invalid Positon");
else
{
while(inext;
i++;
}
newnode=(struct node *)malloc(sizeof(struct node));
printf("Enter Data: ");
scanf("%d",&newnode->data);
newnode->next=temp->next;
temp->next=newnode;
}
}
void deletion()
{
int pos,i=1,y;
y=length();
temp=head;
printf("Enter Position ");
scanf("%d",&pos);
if(pos>y)
printf("Invalid Position");
else
{
while(inext;
i++;
}
nextnode=temp->next;
temp->next=nextnode->next;
free(nextnode);
}
}
void search()
{
int key,flag=0;
temp=head;
printf("Enter key: ");
scanf("%d",&key);
while(temp!=NULL)
{
if(temp->data==key)
{
flag=1;
}
temp=temp->next;
}
if(flag==1)
printf("Key is found");
else
printf("Key is not found");
}
int length()
{
int count=0;
temp=head;
while(temp!=NULL)
{
count++;
temp=temp->next;
}
return count;
}
Thank you brother
Thank you 😊
Hey how can I copy this?
@@lifeahead5208for phone -> take screen shoot of comments //2->go to goggle and serach your screen shot then select text then u can copy that
ai
You're the best for all those are beginners within the world of DSA. .....I tried to understand Linked list many times specially create () ,struct node *...but always remain confused ,your explaination made me understand with ease. ..I hated Linked list but now I got it all...Thanking you a lot miss jenny: ))....keep up the great work ,I love all your video lectures more than any offline lecturer ..Thanks again ;
:))
I agree that you feel proud to born in India. But we also feel that we are proud of that- excellent educator like you born In our country. Thank you, madam, you are great.
I first listen to the concept and try on my own to code. And yeah it works! 😄 Such a good explanation Didi.
Also consider my request making a playlist dedicated to 'Linux Kernel internals'. Not just concepts, but programs also. I see there's no solid work created for Kernel internals in TH-cam.
Thank you!
One of the best way to explain these all
damn, You are good :) I was struggling through the link list many many times. thank you for the clearing. you are gorgeous and brainy :) regards from Sri Lanka!
i attended class and did not understand about linked list then i have seen your videos and now m really good at it. thank you
No one will beat Jenny's mam explanation in the world 🎉🎉🎉❤❤ wow 👏 great explanation
Mam you are my god you saved me whole night I'm holding my head while studying in textbook now in the morning I saw your video and understood so well
I m regretful why I didn't came here before ....best way to explain, thnku ma'am 🙌
Tomorrow i have exam of data structure and i am just only watching your videos..i am perfectly learn and study your video that my 15+ experienced sir is not good explained as you..thank you so much😀
Before data structure was very difficult but at least now data structure is like and very interesting..thanks again!
Thank you ma'am for your excellent explanation. I'm following your all videos on DS and It is really helping me to clear my concept from scratch. Thank you again 😊
I always wait for ur video u are my favourite
Best way of explanation. I ❤️ the way. I have seen many teachers's video but no one can teach like this.
0:00 Delete from beginning
6:18 Delete from end
16:15 Delete at position
You are doing such a great work.I was confusing at every step but your explanation made me everything clear.Thankyou for your existence
Tomorrow is my lab external exam i don't even know what is inked list by watching your video iam able write a code on different operations on linked list thank you man thank you so much !!
thank you so much for understanding in easier way. our teacher tells in a way that she only doesnt understand .
bro kaha placement hua
@@cat-codes1on1 how do u know that I got placed ??
@@sushanthraju3215 bro 2 yrs ago u commented on this video...so i guessed ki placement ho hi gya hoga...btw konsi company me hua
Crystal clear explanation mam 💝💝 thanks a lot ...hats off to your hardwork ..👏👏✨💝😍
mam you are of of the best teacher , the way you explain , no one can
thank you so much.
Unable to stop commenting this video excellent explaining keep it up thanks for the upload😍😍
Mam, The effort you put makes your class wonderful.. will you please say whether this code is correct for deleting a node from a particular position.
while(inext;
i++;
}
prevnode->next=temp->next;
free(temp);
i have bought a course from codingblocks but still i learn from your channel.
Firstly because aap bohot acha padhte hain.
Second of all man laga rehta hai.
JazakAllah bhut Acha smjahati hai
You are one of the best teacher mam!
I request u to upload java coding also
Mam....hatsoff to u mam🤗🤗🤗🤗it's better to spend a week with ur lectures rather than attending college lectures....neat and clear explanation...great effect mam
Mam really ur lecture is awesome...
Thanku so🤗🤗 much to provide such a costly lecture at free....
I added all this DSA lecture in my college curriculum.. Task..
Simplicity is elegance
~thank you ma'am
very helpfull , only your explanation make complex form easy. thank you
Your teaching is very helpfuul for me.Your teaching is very nice ma'am.
Mam, when I saw data structure, I was think8ng why I took MCA course, but thanks a lot to save me.😌😌😌😌
As usual your contents are superb. #JustRandom Love the way you say "Nowww"
Not me watching ur vedios instead of studying😂 thank u so much it cleared all my doubts, by the way my exams start after 3 days .. 😅
You are so great finaally i cleared my concept in linked list
I have a problem to understand linked lists , suddenly search in you tube and I watched your only one video and I become fan of your methology. Thanks for serving us.
Outstanding mam, best wishes from me.
FARHAN...
Thanks, you are teaching in very nice way.☺️
Here is the complete code: #include
#include
#include
//
struct node {
int data;
struct node *next;
};
struct node *head = NULL, *newnode, *temp;
int length = 0;
void display() {
printf("
Values of this Singly Linked List: ");
temp = head;
while (temp != NULL) {
printf(" %d\t", temp->data);
temp = temp->next;
}
}
void create(){
int choice1 = 1;
while(choice1){
newnode = (struct node*)malloc(sizeof(struct node));
printf(" Enter data: ");
scanf("%d",&newnode->data);
newnode->next=0;
if(head==0){
head = temp = newnode;
}
else{
temp->next=newnode;
temp=newnode;
}
length++;
printf("Do you want to continue(0,1)?");
scanf(" %d",&choice1);
}
}
void Delete_From_Beg(){
if (head == 0){ // if the list is empty
printf("List is Empty");
}
else if(head->next == 0){ // if only one node
temp = head;
head = 0;
free(temp);
}
else{
temp = head;
head = head->next;
free(temp);
}
printf("
Updated singly linked list: ");
display(); // calling display() to show the updated linked list
}
void Delete_From_End(){
struct node* prevnode;
if (head == NULL) { // if the list is empty
printf("List is Empty");
return;
}
else if (head->next == NULL) { // if only one node
temp = head;
head = NULL;
free(temp);
}
else {
temp = head;
while (temp->next != NULL) {
prevnode = temp;
temp = temp->next;
}
prevnode->next = NULL;
free(temp);
}
printf("
Updated singly linked list: ");
display();
}
void Delete_From_Position(){
struct node* nextnode;
int pos;
int i = 1;
printf("Enter Position: ");
scanf("%d",&pos);
if(pos > length || pos < 1) {
printf("Invalid Position");
} else if(pos == 1){
Delete_From_Beg();
} else {
// Special case for deleting second node
if (pos == 2) {
nextnode = head->next;
free(head);
head = nextnode;
} else {
temp = head;
while (i < pos-1) {
temp = temp->next;
i++;
}
nextnode = temp->next;
temp->next = nextnode->next;
free(nextnode);
}
}
printf("
Updated singly linked list: ");
display();
}
int main(){
create();
int choice = 1;
display();
printf("
Press 1 to delete node a from the beginning, 2 to delete a node from the end, and 3 to delete a node from a given position: ");
scanf(" %d",&choice);
switch (choice) {
case 1:
Delete_From_Beg();
break;
case 2:
Delete_From_End();
break;
case 3:
Delete_From_Position();
break;
default:
printf("
Error! The input is not valid");
}
getch();
return 0;
}
Brilliantly explained. I just wonder how does the free() method work, rest everything explained in the video is super clear! Thank you so much!!
great explanation mam.......cutest teacher ever
Agreed
mam your videos helped me a lot in learning concepts of DSA .
// creating a linked list and deleting nodes from it full implementation
// All rights reserved to Just Curious 😊
struct node {
int data;
struct node *next;
};
struct node *head = NULL,*new_node,*last_node,*prev_node,*temp; // last node is to keep track of the last node so we can link it to a new node
void create (){
new_node = malloc(sizeof(struct node));
printf("Enter the data for the node : ");
scanf("%d",&new_node->data);
new_node->next = NULL;
if (head == NULL){
head = last_node = new_node;
}
else {
last_node->next = new_node;
last_node = new_node;
}
}
void delete_from_beginning(){
temp = head;
if(head != NULL) {
head = head->next;
free(temp);
}
}
void delete_from_end(){
temp = head;
prev_node = NULL;
if(head != NULL){
while(temp->next!=NULL){
prev_node = temp;
temp = temp->next;
}
}
if(prev_node != NULL){
prev_node->next = NULL;
}
free(temp);
}
void delete_from_position(){
int pos;
printf("Please enter the position : ");
scanf("%d",&pos);
if(pos == 1) {
delete_from_beginning();
return;
}
int counter = 0;
temp = head;
while(temp != NULL){
++counter;
temp= temp->next;
}
if(pos counter){
printf("Error we can't delete this node !!
");
return;
}
temp = head;
prev_node = NULL;
int i = 1;
while(inext;
++i;
}
prev_node->next = temp->next;
free(temp);
}
void display(){
temp = head;
while(temp != NULL){
printf("%d\t",temp->data);
temp = temp->next;
}
}
int main () {
int size;
printf("Enter the size of the linked list to start with : ");
scanf("%d",&size);
for(int i = 0 ; i < size ; i++){
create();
}
display();
printf("
");
int choice = true ;
while(choice && head) {
printf("Choose 1 of these operations to be done to the linked list :
" \
"press 1 : to delete from the beginning.
" \
"press 2 : to delete from the end.
" \
"press 3 : to delete from a specific position.
" \
"press 0 : to exit.
");
scanf("%d",&choice);
switch(choice){
case 1 : delete_from_beginning(), display() , printf("
");
break;
case 2 : delete_from_end(), display() , printf("
");
break;
case 3 : delete_from_position(), display() , printf("
");
break;
default : printf("Good buy!!
");
}
}
return 0;
}
Display light came in 5minutes as the video was playing well addressed thank you ma'am ❤
From ur video I have learnt how to make a code by myself🔥.
Thanks madam. This is the best explanation I have ever hear. Thanks a lot...........
Tq so much mam because of u I am learning data structure easily and interestingly hatts off to you mam
Clear explanation madam .... thank you so much 👌
Very helpful video,thank you ma'am.
In your explanation use telugu also. Yours explanation is very super
I also shocked when u said "happy independence day" because I am watching it on 15 Aug.
Ma'am your explanation is very very to me, thanks a lot
Best teacher ever love from Bangladesh.
good video , you teaching very well.
Greetings from Nigeria, ma'am.
I implemented the delete_start/delete_end function and they both work fine separately but anytime I run them together, I get an infinite loop.
Thanks!...This video has helped me...
Best Teacher 🌟
Extra-ordinary explanation mam
Very useful for exams
Your teachings are great mam ! I guess we can also do it with just, head and temp, the del from last one by changing a lil bit fron the function created if we assume that we haven't deleted the first node ... 🙂
Thank you ma'am for your support
Its Very clear to understand.....❤❤❤
This is a very clear explanation, when ever I need a recall , I refer this content, great work mam
Mam ur way of explanation is awesome and also please upload the video how to search and display in linked list once I am thanking u from my bottom of my heart
In delete from any position
temp=head;i=1;
then condition is
while(i
Wow nice explanation for c programming
Can u give explanation for c++?
And i must say one best beautiful youtuber
while deleting from specific position we are using (pos-1),but while inserting a node at specific position u have used (pos).why is that?
Please try to enable playback speed option for your video.
It will be very helpful.
Clarity -> You ❤️
I would like to say you are the best ........
Thank You Ma'am, very clear explanation !!!
Thanks a lot mam......😊
well explained......
Please make a video for Fibonacci search with examples, algorithm as soon as possible .
Ohh Yes
Yeah, it will be very helpful for me also.
Thanks a lot mam , your explanations are awesome.
*very useful and informative to me
Make more videos to teach us easily
Waise samjhate achhi tarah se hai usme koi dikkat nahi
Very well explained hats off to you
explanation and logic implementation excellent mam
today is also 15 august and i am watching this lecture
We can use for loop for deletion from end and also calculate length of linked list . And then loop it till n-1 and we can empty it
i love ur class
Mam you so Buetifull ❤❤❤❤🤩
What amazing lecture❤️
Thanks a lot mam, It really helps.
Hey deletion from given position can also be done in different way
Same like deleting at the end.....
If we have given delete a given node from list inplce of position than.
We can traverse it. Than when the data is equal to given data.
We can store Temp of next on a new pointer say nxt.
And we can save the previous pointer on pre. Than
We can simply say pre of next = nxt of next 👍👍👍👍
Just we required a extra pointer here i.e 3 rd pointer
TQ u mam...make many videos ..it is well understand
I love u r teaching
i have a doubt that
for deleting at specific location
can we write this
is this correct
{
temp=head;
while(inext;
i++;
}
prev->next=temp->next;
free(temp);
}
I easily understand ..... Thank you for explaining.... Keep going 👍👍💯
Now I better understand the linked list
So cute lecturing methods tnx lot
Amazing mam..👏👏
Very helpfull great work Mam
Thank you ma'am for this lecture