public class CharNode {
private char _data;
private CharNode _next;
public CharNode(char dat, CharNode n) {
_data = dat;
_next = n;
}
public char getData() {
return _data;
}
public CharNode getNext() {
return _next;
}
public void setData(char d) {
_data = d;
}
public void setNext(CharNode node) {
_next = node;
}
}
public class CharList {
private CharNode _head;
public CharList( ) {
_head = null;
}
public CharList (CharNode node) {
_head = node;
}
public int subList(IntList list)
{
int count = 0;
IntNode listNode = _head;
IntNode otherListNode = list._head; ;
while (listNode != null)
{
if (otherListNode == null)
otherListNode = list._head;
if (listNode.getValue() == otherListNode.getValue())
{
listNode = listNode.getNext();
otherListNode = otherListNode.getNext();
if (otherListNode == null)
count++;
}
else
{
listNode = listNode.getNext();
otherListNode = list._head;
}
}
return count;
}
}
I need to write function public int subList (CharList list)
that gets list
and returns how many times this list exist.
For example if my list is a b c d a b g e
and the list received as parameter is a b
it will return 2.
The method should be as efficient as possible.
Currently my problem is that I don't know how to loop over the two lists at the same time in order to compare the values