1) A LinkedList class has a Node head as one of it’s instance variables. As usus
ID: 3727002 • Letter: 1
Question
1) A LinkedList class has a Node head as one of it’s instance variables. As ususal an empty list has head == null. Implement a method public void insertFirst(int data) that inserts a value at the front of the linked list.
2) Write a method int countDifferentChars(String s, String t) that counts the number of corresponding characters of s and t that don't match (are different). For example countDifferentChars("abcxa", "abcdax") would return 2 and countDifferentChars("xabc", "abcx") would return 4. Notice that if one string is longer than another, all the additional characters count as non-matches.
Explanation / Answer
Q.1)
public void insertFirst (int data)
{
Node nptr = new Node(data, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
Q.2)
public class count {
public int countDifferentChars(String s, String t)
{int f=0,i,l,m;
m=Math.max(s.length(), t.length());
l=Math.min(s.length(), t.length());
for(i=0;i<l;i++)
{
if(s.charAt(i)!=t.charAt(i))
f++;
}
return (f+(m-l));
}
public static void main(String[] args) {
// TODO Auto-generated method stub
count ob =new count();
System.out.println(ob.countDifferentChars("abcxa", "abcdax"));
System.out.println(ob.countDifferentChars("xabc", "abcx"));
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.