Ive been having a problem for the past few days..as ive got an assignment set making a list with methods such as reverse,append and length.
Im having issues with a method im trying to create which is insert, inserting into a sorted list.
Main class
class week8_easy
{ //method i am having problems with to work recursively.
static seblist <String> insert (String x, seblist <String> m) {
// Initiate following variables so we do not get a non-destructive method
seblist <String> a=m;
seblist <String> b=null;
//insert and sort checking first characters of string using index 0
String s=x;
if(s.charAt(0)> a.head().charAt(0)){
//b = new sebcons(m.head().toString()+","+s,a.tail());
}
else {
//b = new sebcons(s+","+a.head().toString(),a.tail());
}
int aa=0;
for(int i=1;i<a.length()-1;i++){
if(x.charAt(0)>m.tail().elementAt(i).toString().charAt(0)){
insert(b.head().toString(),B)/>;
}
}
return b;
}
// static seblist <String> sort (seblist <String> m){}
public static void main (String []args)
{
seblist <String> m= new sebcons("hello",new sebcons("goodbye",new sebnil()));
seblist <String> n= new sebcons("hesdfllo",new sebcons("gooasdfye",new sebnil()));
System.out.println("reverse of " +m+" is");
System.out.println(m.reverse());
System.out.println(m.length());
System.out.println(insert("pple",m));
}
}
Heres the following classes if needed.
public class sebcons <T> extends seblist <T>
{
public T h;
seblist <T> t;
public sebcons(T o1, seblist m1)
{h=o1;t=m1;}
public boolean isnil()
{return false;}
public String commasBetween()
{ if (t.isnil()) return h.toString();
else return h.toString() +","+ t.commasBetween();}
public int length()
{ return 1+t.length();}
public T head()
{
return h;
}
public seblist <T> tail()
{
return t;
}
public T elementAt(int n)
{
if (n==0) return h;
else return t.elementAt(n-1);
}
public seblist <T> append(seblist <T> m)
{
return new sebcons(h,t.append(m));
}
public seblist <T> reverse()
{
return t.reverse().append(new sebcons(h,new sebnil()));
}
}
public abstract class seblist <T>
{
public abstract boolean isnil();
public String toString()
{
return "[" + commasBetween() + "]";
}
public abstract int length();
public abstract T head();
public abstract seblist <T> tail();
public abstract T elementAt(int n);
public abstract seblist <T> append(seblist <T> m);
public abstract seblist <T> reverse();
public abstract String commasBetween();
}
public class sebnil <T> extends seblist <T>
{
public boolean isnil()
{return true;
}
public String commasBetween()
{ return "";}
public int length()
{ return 0;}
public T head()
{
throw new IllegalArgumentException("Can't do head of empty list");
}
public seblist <T> tail()
{
throw new IllegalArgumentException("Can't do tail of empty list");
}
public T elementAt(int n)
{
throw new IllegalArgumentException("Empty list has no elements");
}
public seblist <T> append(seblist <T> m)
{
return m;
}
public seblist <T> reverse()
{
return new sebnil();
}
}
Any help would be appreciated. thanks.

New Topic/Question
Reply
MultiQuote









|