Results 1 to 3 of 3
  1. #1
    Ben Bublil is offline Member
    Join Date
    Apr 2014
    Posts
    6
    Rep Power
    0

    Exclamation 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();
        }
      }
    }
    The output:
    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default 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.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default 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,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Creating Palindromes Using Recursion
    By Ben Bublil in forum New To Java
    Replies: 4
    Last Post: 10-20-2014, 08:05 PM
  2. Recursion adding the digits of an integer
    By romero4742 in forum New To Java
    Replies: 2
    Last Post: 06-06-2012, 01:59 PM
  3. Doubly linked list, reverse()
    By hamed in forum New To Java
    Replies: 4
    Last Post: 10-29-2010, 09:53 AM
  4. Help with syntax to reverse a positive integer
    By sonny in forum New To Java
    Replies: 14
    Last Post: 02-27-2010, 06:19 AM
  5. Linked List integer list
    By igniteflow in forum Advanced Java
    Replies: 1
    Last Post: 12-10-2008, 08:53 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •