Results 1 to 3 of 3
- 01-01-2015, 11:56 PM #1
Member
- Join Date
- Apr 2014
- Posts
- 6
- Rep Power
- 0
Creating a reverse List<Integer> using recursion - HELP!
Can someone help me?
I created this code, seems like it should be working properly and the code is right, but the output is not what it should be.
The action gets a List<Integer> and should make it reversed.
As example: 1 2 3 4 5 ---> 5 4 3 2 1
The Nodes in the new list (lst2) should be in a reversed position.
The code does compile with no errors.
Note: I do have Node<T> and List<T> classes
Java Code:public class NodeReverse { public static void reverse (List<Integer> lst, Node<Integer> node, int counter, List<Integer> lst2, Node<Integer> pos) // Should make lst = lst2 while lst2 is the opposite to lst { node = node.getNext(); counter++; if(node.getNext()!=null) reverse(lst, node, counter, lst2, pos); pos = lst2.insert(pos,node.getInfo()); if(counter==0) lst = lst2; } public static final int Nfor=5, Nnode=10; public static void main(String[] args) { Node<Integer> node = new Node<Integer>(Nnode); Node<Integer> temp = node; for(int i=0 ; i<Nfor ; i++) // Setting a new Node<Integer> chain { Node<Integer> next = new Node<Integer>((Nnode+i+1)); temp.setNext(next); temp=temp.getNext(); } List<Integer> list = new List<Integer>(); Node<Integer> pos = list.getFirst(); for(int i=0 ; i<=Nfor ; i++) // Inserts the Node<Integer> chain into the List<Integer> 'list' and presents it {' int info = node.getInfo(); pos = list.insert(pos,info); System.out.print(info+" "); // Prints: "10 11 12 13 14 15" node = node.getNext(); } System.out.println(); List<Integer> list2 = new List<Integer>(); // Creates an empty List<Integer> Node<Integer> rNode = list.getFirst(); int counter=0; reverse(list, rNode, counter, list2, null); // Should make the List<Integer> 'list' equals to List<Integer> 'list2' pos = list.getFirst(); for(int i=0 ; i<=Nfor ; i++) // Presents the new-old List<Integer> 'list' { int info = pos.getInfo(); System.out.print(info+" "); // Prints the same: "10 11 12 13 14 15" pos = pos.getNext(); } } }
10 11 12 13 14 15
10 11 12 13 14 15
The output I need:
10 11 12 13 14 15
15 14 13 12 11 10
I think the problem is the part where I typed: lst = lst2;
HELP!Last edited by Ben Bublil; 01-02-2015 at 01:56 PM.
- 01-02-2015, 01:05 AM #2
Re: Creating a reverse List<Integer> using recursion - HELP!
The posted code doesn't compile without errors.
Please fix the errors and make sure all class definitions are provided.
Also add some comments describing what the statements in the code are trying to do.If you don't understand my response, don't ignore it, ask a question.
- 01-02-2015, 03:28 AM #3
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Creating a reverse List<Integer> using recursion - HELP!
There must be more to this than just what you stated in the title. Why do you have a class Node? And since you can't instantiate an interface that code could not possibly be correct.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Creating Palindromes Using Recursion
By Ben Bublil in forum New To JavaReplies: 4Last Post: 10-20-2014, 08:05 PM -
Recursion adding the digits of an integer
By romero4742 in forum New To JavaReplies: 2Last Post: 06-06-2012, 01:59 PM -
Doubly linked list, reverse()
By hamed in forum New To JavaReplies: 4Last Post: 10-29-2010, 09:53 AM -
Help with syntax to reverse a positive integer
By sonny in forum New To JavaReplies: 14Last Post: 02-27-2010, 06:19 AM -
Linked List integer list
By igniteflow in forum Advanced JavaReplies: 1Last Post: 12-10-2008, 08:53 PM
Bookmarks