Print elements of a linked list in forward and reverse order using recursion

preview_player
Показать описание
See complete series on linked list here:

See series on data structures here:

In this lesson, we will write two recursive functions "Print" and "ReversePrint" that will print the elements of a singly linked list in forward and reverse order respectively.

Feel free to drop your questions, feedback and suggestions in comments section.

You may also like us on facebook:
Рекомендации по теме
Комментарии
Автор

I just started to appreciate the art of using recursion to reverse something after watching this video! Genius! Thanks for your explanation!

leepat
Автор

i login my youtube account just to say you the best... i haved for long trouble to understand the link list data structure until you uploaded your terrific videos actually i lost few days at work just to follow your all courses :) thank you

johncage
Автор

How can you be so good? No one explains things this well. Without your tutorials, I would have never understood data structures. Can you please make one on heap also?

umajha
Автор

Hi Vikas,
We are using Microsoft Visual Studio Express edition.

mycodeschool
Автор

I would like to say that, hopefully one day in the near future, I will get a job because of the things I learned in your videos. I am still going through your videos so I am eager to learn more. I want to thank you for all you have done. How easily you explain these concepts shows how intelligent you are! Thank you!

Xarlos
Автор

Became fan of yourself, the way you are explaining is AWESOME. No words man,
You are great.
Thank you for sharing your knowledge here.

ravitejabolli
Автор

All those who are not understanding how the printf function is called, this is what i understood
we are making making recursive calls to rp(node *p), actually printf statement will be called only after the recursive function is executed(completed) but till the last rp(null) we never reached there because we always have other function before printf, now in rp(null) we return from the function RP(NULL) not RP(250) .
therefore returning from rp(null) means we have executed rp(250)(becaues rp(null) was called from rp(250)) and therefore now printf(p->data) will be executed.
now after printf(p-data) means rp(150) executed((becaues rp(250) was called from rp(150)), rp(150) execution means we can now call the printf(p->data) and so on...

I hope this helps

changumangu
Автор

your videos are my all time recommended videos to any one wants to understand coding in C, I really appreciate the quality and the effort of your videos.

mohamedmostafak
Автор

I really had some issues understanding the lessons at first and had to take the help of books along with your lessons but now this lesson was one piece of a gem <3 Thank you arigato

saswatapatra
Автор

Thanks! Your explanation of memory addresses and pointers is excellent. I was able to learn from your example and finish my school project. I also now understand what is happening behind the scenes with my code. Excellent work.

DennisSkillman
Автор

Thanks, most clear explanation I've seen so far.

salvadorinio
Автор

This is the best example to understand call stacks and how recursion work

wassimboussebha
Автор

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

using namespace std;

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


/* void Print(Node* ptrHead) { //Prints the output
    if(ptrHead == NULL)
        return; //Exit condition
    cout << " " << ptrHead->data; //First print the value in the Node
    Print(ptrHead->next); //Recursive call
    //printf("%d ", p->data); ->For C

}*/

void ReversePrint(Node* ptrHead) { //Reverse the output
    if(ptrHead == NULL)
        return; //Exit condition
    ReversePrint(ptrHead->next); //Recursive call
    //printf("%d ", p->data); ->For C
    cout << " " << ptrHead->data; //First print the value in the Node


}


Node* Insert(Node* head, int data) {
    Node* temp = new Node;
    temp->data = data;
    temp->next = NULL;
    if(head == NULL)
        head = temp;
    else {
        Node* temp1 = head;
        while(temp1->next != NULL)
            temp1 = temp1->next;
            temp1->next = temp;
    }
    return head;


}

using namespace std;

int main() {

    Node* head = NULL; //Local variable
    head = Insert(head, 2);
    head = Insert(head, 4);
    head = Insert(head, 6);
    head = Insert(head, 5);
    //Print(head);
    ReversePrint(head);


    return 0;
}

plemyk
Автор

Reverse print using recursion explanation is mindblowing!!!!

nirmaljeffrey
Автор

best videos ever seen....please make videos on networking, algorithms, database, computer organistion and operating systems

vibinvenu
Автор

Very clean and beautiful way of explaining the recursion :)

gayathrisarmishtaamara
Автор

No one can teach like you...., thank you so much sir.

surjyaroy
Автор

Very clear and detailed explanation. Job well done!

Nick-lfen
Автор

i see that you created these videos almost a year and a half back. i hope i am not asking for a lot, but it would be amazing if you could do some tutorials on java also. actually java is alot like c++ and yet nothing like it. personally, i like java even more, because i had the pleasure of learning it from an amazing teacher. the way he explained things, it seemed like even 2+2 if difficult than java. and that is exactly how anybody would feel after watching you videos too. i understand creating these videos is alot more difficult than just sit around and watch them. but your efforts have paid so much to so many. even if the tutorials on java never show up, i should thank you for what you have provided us so far. wishing you the best in all the future endeavors.    

mansitiwari
Автор

YOUR VIDEOS ARE WONDERFUL, Thank you very much, you are helping alot by explaining these things, Keeep it up .

rakanshadid