java - Finding uppercase in linkedlist and returning new linkedlist with found elements? -
i have write method search linkedlist(listnode, cointaining 1 char per listnode), finding uppercase chars, copy them new listnode , returning new listnode. code far, fails junit testing (provided prof.)
this list node:
public class listnode { public char element; public listnode next; }
and method ive wrote, dont seem work:
public static listnode copyuppercase(listnode head) { listnode newlistnode = mkempty(); if(head == null){ throw new listsexception("lists: null passed copyuppercase"); }else{ char[] sss = tostring(head).tochararray(); for(int = 0; < sss.length ; i++ ) if(character.isuppercase(sss[i])){ newlistnode.element = sss[i]; } newlistnode = newlistnode.next; } return newlistnode; }
what worng code? why fail?
expanding on @enterbios's answer (+1 him), try following:
public static listnode touppercase(listnode head) { if (head == null) throw new listsexception("lists: null passed copyuppercase"); listnode newhead = null; listnode current = null; char[] sss = tostring(head).tochararray(); (int i=0; i<sss.length; i++) { if (character.isuppercase(sss[i])) { if (current == null) { current = mkempty(); newhead = current; } else { current.next = mkempty(); current = current.next; } current.element = sss[i]; } } return newhead; }
Comments
Post a Comment