Linked List in C/C++ - Delete a node at nth position

preview_player
Показать описание
See complete series on linked list here:
In this lesson, we have written C code to delete node at nth position in a linked list.

Additional Resources:
Series on pointers:

Lesson on Dynamic memory allocation:
Рекомендации по теме
Комментарии
Автор

7 years down still no better teacher than you. Thank you so much.

asmereg
Автор

For those who are not getting this thing here is the full code of c:

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* head;
void Insert(int x)
{
struct Node* temp=(struct Node*)malloc(sizeof(struct Node));
temp->data = x;
temp->next = head;
head = temp;
}
void Delete(int n)
{
struct Node* temp1 = head;
if(n==1){
head = temp1->next;
free(temp1);
return;
}
int i;
for(i=0;i<n-2;i++)
{
temp1 = temp1->next;
}
struct Node* temp2 = temp1->next;
temp1->next = temp2->next;
free(temp2);
}
void Print()
{
struct Node* temp = head;
printf("List is:");
while(temp != NULL)
{
printf(" %d", temp->data);
temp= temp->next;
}
printf("\n");
}
int main(){
head = NULL; //empty list
Insert(4);
Insert(2);
Insert(3);
Insert(5);
Print();
int n;
printf("Enter a position\n");
scanf("%d", &n);
Delete(n);
Print();
}

sahilkaif
Автор

Thanks a lot ! Yes, There will be more in data structures series on all of these topics. Please stay tuned.

mycodeschool
Автор

Seriously you are the best tutor I have ever come across. I just love the way you explain your topic with such clarity and simplicity. Thank you so much!!

nirajabcd
Автор

You my friend are officially my favorite YouTuber.. MUCH THANKS!

James-cqdp
Автор

10 years after posting and this is still so very helpful. Thank you!

alizzyb
Автор

I am glad that these tutorials helped you Musab. :)

mycodeschool
Автор

A better way for beginners to understand would be to use the loop as:
for (i=1;i<n-1;i++)

Sachin
Автор

While I am learning this course, I just paused my video to Thank You for this Amazing series on Data Structures
and...
Thanks A Lot

lakhdeepsingh
Автор

I couldn't figure out this for so You saved my university homework :D

BigB
Автор

I LOVE YOU! I spent days trying to figure this out by reading my book, and you explained it in minutes :D

ugotsmurfd
Автор

when you are 2013 i was played in school and now i learned a lot from your channel thank you

emanmohsen
Автор

Thank you so much! I have been stuck on nodes for a while now!

bridger
Автор

Dude...you are a programming champion and an excellent teacher!....thanks a lot!!!..Its really sad that such excellent and informative videos never become viral and stupid and senseless videos earn millions and billions of views..neways..u r doing a really good Job :)

BustinJieber
Автор

God bless you for this gold you've helped and are helping me with.!

XGRAVITYXxx
Автор

zabardast explanation bhai!!!
superb work

vishaltandale
Автор

Beautiful clarity. Makes me regret avoiding C all these years.

BStack
Автор

I am in love with you and data structures

saik
Автор

//THE CODE FOR C++
#include <iostream>

using namespace std;

//Linked List: Delete a node at nth position

struct Node {
    int data;
    Node* next;
};

Node* head; //Global

void Insert(int data) { //Insert an integer at the end of list
    Node* temp1 = new Node;
    temp1->data = data;
    temp1->next = head;
    head = temp1;
}

void Print() { //Print all elements in the list
    Node* temp1 = head;
    while(temp1 != NULL) {
        cout << " " << temp1->data;
        temp1 = temp1->next;
    }
    cout << endl;
}

void Delete(int n) { //Delete node at position n
    Node* temp1 = head;
    if(n == 1) {
        head = temp1->next; //head now points to second node
        delete temp1;
        return;
    }
    for(int i = 0; i < n - 2; i++)
        temp1 = temp1->next; //temp1 points to (n - 1)th Node
    Node* temp2 = temp1->next; //nth Node
    temp1->next = temp2->next; // (n + 1)th Node
    delete temp2;

}


int main() {

    head = NULL; //Empty list
    Insert(2);
    Insert(4);
    Insert(6);
    Insert(5); //List: 2, 4, 6, 5
    Print();
    int n;
    cout << "Enter a position" << endl;
    cin >> n;
    Delete(n);
    Print();




    return 0;
}

plemyk
Автор

Thanks for spending some of your time to do this fantastic video. Cheers

rayinoz
visit shbcf.ru