Christ1m 0 Newbie Poster

First write the method

publi static lp append(lp head1, lp head2)

It takes two lists and puts them together -- first the elements of head1, then the elements of head2. If head1 is empty, the answer is head2. Otherwise, use a recursive call to append head1.rest to head2. Now use "new lp" to add the first element of head1 to the result of the recursive call. That is your answer. Now write

public static lp reverse(lp head)

If the input is empty, return null. Otherwise, use a recursive call to reverse the list head.rest, then use the append function to add head.first to the end of the reversed list. The second argument of append is a list -- not a number. But you can make a list of length one, containing only the number head.first. Then you can use append to add this list to the end of another list. The user should see:

input positive integers: 5 3 8 2 -1
reverse: 2 8 3 5

Here is my code.. Im having problems with the main method. How do I print out the result of the recursive functions with the main method.?
Right Now im trying to make the main method ask for inputs to create a list in reverse order .When a negative integer is put, then it stops the loop and prints out the result.
For example
input integer: 1
input integer: 2
input integer: 3
input integer: -1
result: 3 2 1

import java.util.*;
import java.io.*;
public class lp
{
public int first;
public lp rest;

public lp(int first, lp rest)
{
this.rest = rest;
this.first = first;
}

public static lp append(lp head1, lp head2)
{
if(head1==null){
return head2;
}
else{
return new lp(head1.first,append(head1.rest,head2))...
}
}

public static lp reverse(lp head)
{
if(head==null)
{
return null;
}
if(head.rest == null)
{
return new lp(head.first,reverse(head.rest));

}
else
{
return append(head.rest,reverse(head.rest));
}
}


public static void main(String[] args)
{
// initialize values
int index;
int[] A = new int[1000];

// Initializes the Scanner
Scanner sc = new Scanner(System.in);

// Print out the value
System.out.print("input positive integers:");
int num = sc.nextInt();

// Creates additional scanner acoording to the for loop
for (index =0; num >= 0 && index<1000; index++)
{
A[index] = num;
System.out.print("input integer:");
num = sc.nextInt();
}
// Prints out the sum when user inputs a negative number.
//System.out.println("reverse" + lp.append(head.first,reverse(head.rest))...


}// end of main
}// end of class