Possible Solution:
Compile two files in the same folder:
javac list.java
javac homework5.java
Then run: java homework5
1/ list.java
class list
{
public int first;
public list rest;
public list(int x, list y)
{
first = x;
rest = y;
}
}
{
public int first;
public list rest;
public list(int x, list y)
{
first = x;
rest = y;
}
}
2/ homework5.java
public class homework5
{
public static void main(String[] args)
{
boolean ans;
int N;
list L = new list(1, new list(2, new list(3, new list(4, new list(5, null)))));
N = 4;
ans = occur(L, N);
if (ans == true)
System.out.println(N + " is in the list");
else
System.out.println(N + " is not in the list");
N = 9;
ans = occur(L, N);
if (ans == true)
System.out.println(N + " is in the list");
else
System.out.println(N + " is not in the list");
}
static boolean occur(list L, int N)
{
if (L == null)
return false;
else
{
if (L.first == N)
return true;
else
return occur(L.rest, N);
}
}
}