Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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; } }