Recursion through links list in reverse

Hey everyone,
So I have the recursion part down pat! Thanks to everyone who has helped me out over the last few weeks. I have another challenge.
I have to create a linked list without created a new node.
now the way I was going to do it was as follows:

1
2
3
4
5
6
7
8
9
10
11
void main()
{
arr1[10]{3,4,6,7,8,5,3,1,7,5};
arr2[4]{3,7,5,2};
	initlist(list1);
	system("pause");
	for (i = 4; i >= 0; i--) create(list1, a1[i]);
	initlist(list2);
	for (i = 4; i >= 0; i--) create(list2, a2[i]);
	
}
Last edited on
You do show an incomplete program, so we have no idea what it does.

I have to create a linked list without created a new node.
Recursion ... in reverse

Please, explain.
keskiverto...
the part that I was showing was the part about adding the arr of numbers to the linked lists. I essentially was making sure that I was doing it right.


I am printing out a linked list in reverse using recursion. However, I am not allowed to create a new node in order to create the list.
I am printing out a linked list in reverse using recursion.

That is a simple, clear task.

I am not allowed to create a new node in order to create the list.

What?
First, printing a list has nothing to do with creation of a list. They are entirely separate operations.

Second, if a list is a linked set of nodes, and you are not allowed to have nodes, then you cannot have any list either. Either you have not understood the assignment correctly or you fail to explain it to us properly.


Are you asking whether the code that you show is correct?
It is not.
Line 1: main() must return int, not void.
Line3: What is the type of "arr1"?
Line4: What is the type of "arr2"?
Line5: We have no idea what the "initlist" does, nor what the "list1" is.
Line6: There is no excuse to have that.
Line7: We have no idea what the "create" does, nor what the "a1" is.
We hav no idea what the "list2", "a2", "list3" and "list4" are.
We are not allowed to introduce new nodes.
Let me show you each thing here:

Create:
1
2
3
4
5
6
7
8
9
10
11
void create(node *&list,int n)
{
	
	node *temp;
	temp = new node;
	temp->value = n;
	temp->next = list;
	list = temp;


}


initlist
1
2
3
4
5
6
void initlist(node *&list)
{
	list = new node;
	list->value = -1;
	list->next = NULL;
}


As for list3,list4 please ignore them. I got rid of those two options.

The type of arr1 and arr2 are arrays of numbers. I am using them to create a new list so arr1 will populate list1 and arr2 will populate list2.
Last edited on
Your program does not use arr1 and arr2. It does use a1 and a2, whatever they are. To us you tell that arr1 is numbers but in the program you don't say a thing. Besides, "numbers" is not a valid typename (unless you have defined such type).

Why aren't the list1 and list2 declared within the program?

Why are there two functions?
Why does the last node of the list have value -1?

Your initlist() and create() clearly introduce "new nodes". Are they allowed?
If they are allowed, what is not allowed?


But lets forget what can and can not be introduced.
Write a recursive function that prints a list in reverse order.
Nothing more, nothing less.



For extra points:
Why the class node doesn't have constructors?

How do you deallocate the dynamically allocated nodes at the end of the program?
Your right.

I have been changing and editing and moving around and I got a whole new everything going here. As for the reverse function this is my reverse function that I wrote:

1
2
3
4
5
6
7
node *reverse(node*print)
{
      if(print==NULL)
        return 0;
       reverse(print->next);  
      cout <<""<<print->data;
}




As for the new nodes. I'm not to sure what is asked of me. I don't see how it's possible to print out a linked list without first creating said linked list. It just doesn't seem possible to me.
So I went ahead and created a new linked list.
oh just so you know I am not allowed to use head or first or last or anything like that. So I created a different name for head which in this case is du
The issue I am having now is that when I run the program it crashes immediately.

So this is my new code:

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
struct node
{
	int data;
	 node *next;
};
node *reverse(node *ptr,ofstream &outf)
{
	if (ptr == NULL)
		return 0;
	reverse(ptr->next,outf);
	outf << "" << ptr->data;
}
node *in(node *du, int data)
{
	node *temp = new node;
	temp->data = data;
	temp->next = NULL;
	if (in == NULL)
		du = temp;
	else{
		node *temp1 = du;
		while (temp1->next != NULL)
			temp1 = temp1->next;
		temp1->next = temp;
	}
	return du;

}
int main()
{
	ofstream outf("class4.ot");
	node *du= NULL;
	du = in(du, 2);
	du = in(du, 4);
	du = in(du, 5);
	du = in(du, 10);
	reverse(du,outf);
	return 0;

}
Last edited on
Line 18 has logical error.


Why does reverse() return a pointer?

Why don't you deallocate properly at the end of the program?

Does your compiler give warnings?


A rose by any other name is still a rose. Renaming head to du is cosmetic.
Topic archived. No new replies allowed.