Advertisement
CHU2

Recursion Linked List

Mar 9th, 2023
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | Source Code | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct node {
  6.     int data;
  7.     node* next;
  8. };
  9.  
  10. void recursion(node*);
  11.  
  12. int main() {
  13.     node* head = new node;
  14.     node* ichi = new node;
  15.     node* ni = new node;
  16.     node* san = new node;
  17.     node* shi = new node;
  18.     node* go = new node;
  19.  
  20.     head->data = 10;        //example given linked list para akon la
  21.     ichi->data = 20;        //10, 20, 30, 40, 50
  22.     ni->data = 30;
  23.     san->data = 40;
  24.     shi->data = 50;
  25.  
  26.     head->next = ichi;
  27.     ichi->next = ni;
  28.     ni->next = san;
  29.     san->next = shi;
  30.     shi->next = go;
  31.     go->next = NULL;
  32.  
  33.     recursion(head);
  34.  
  35.     return 0;
  36. }
  37.  
  38. void recursion(node* root) {        //an test b example code kanina na kailangan ig debug and explain
  39.     if (root == NULL) {
  40.         return;
  41.     }
  42.  
  43.     recursion(root->next);      //recursion anay means looping anay bago output data
  44.     cout << root->data << ' ';      //output then return looping here
  45. }       //output should be in reverse which is 50, 40, 30, 20, 10
  46.         //don't forget to take the NULL before output the numbers
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement