Use recursion and the structure Node, to create a linked list. Create the recurs
ID: 3672213 • Letter: U
Question
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. Use C++
we have this but idont know why it is not printing linklist. please check if it prints in your sid if it is not what is wrong?
#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;
}
Explanation / Answer
Hi the code working fine and prints the output perfect, In case you want to work with other replacement of code try this sample code and check for the output prints?
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.