C++ Recursion

Use recursion and the structure Node, to create a linked list. Create the recursive routine of insertRecur that takes a call-by-reference Node pointer and the value. Put the structure Node and the insertRecur in the header file called rlinked.h and the insertRecur in the implementation file called rlinked.cpp. Have the third file have the main that will input numbers and call the insertRecur routine. End on a negative number. Then use a for loop to print out the linked list.

.It is not printing LinkLIST. How can I Fix it?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
  #include <iostream>
 using namespace std;

#ifndef RLINKED_H_
 #define RLINKED_H_

class Node{
    public:
        int value;
        Node *next;
        Node(int n){
            value = n;
        }
 };

class rlinked{
    public:
        Node *head;
        rlinked();
        void insertRecur(Node *h, int v);
        void print();
 };

#endif


rlinked.cpp

rlinked::rlinked(){
    head = NULL;
 }
 void rlinked::insertRecur(Node *h, int v){
    Node *temp = new Node(v);
    temp->next = h;
    h = temp;
 }
 void rlinked::print(){
    Node *temp = head;
    cout << "LINKLIST IS : ";
    while (temp != NULL){
        cout << temp->value<< " ";
        temp = temp->next;
    }
    cout << endl;
 }


main.cpp

 int main(){
    rlinked* rl = new rlinked();
    int n;
    while (true){
        cout << "Enter a value ";
        cin >> n;
        if (n < 0) break;
        rl->insertRecur(rl->head,n);      
    }
    rl->print();
    return 0;
 }
Last edited on
Topic archived. No new replies allowed.