public class StringNode { private char ch; private StringNode next; public Strin
ID: 3630701 • Letter: P
Question
public class StringNode {
private char ch;
private StringNode next;
public StringNode ( char c, StringNode n)
{
ch = c;
next = n;
}
Write int indexof( StringNode str, char ch) method which returns the position of the first occurance of character ch in the given linked- list string.If there is none,returns -1.
Write int length( StringNode str) method determines the number of character in the linked list string to which str refers.
Write void print(StringNode str) writes the specified linked-list string to System.out
Explanation / Answer
public int indexOf(StringNode str, char ch) { int indexCount = 0; while (str.next != null) { if(ch == str.ch) { return indexCount; } indexCount++; str = str.next; } return -1; } public int length(StringNode str) { int length = 0; while(str.next != null) { length++; } return length; } public void print(StringNode str) { while(str.next != null) { System.out.print(str); str = str.next; } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.