a) Write a method called countWord. It takes in a String returns the number of t
ID: 3859560 • Letter: A
Question
a) Write a method called countWord. It takes in a String returns the number of times the String is contained in the LinkedList. public int countWord (String word)
b) Write a method called lastIndexOf. It takes in a String and returns the index of the last occurrence in the list (assume the first element (front) is at index 0). It returns -1 if the item is not in the list. public int lastIndexOf (String word)
c) Write a method called remove which takes in a String as a parameter that removes every occurrence in the LinkedList (note, this is a void method). public void remove (String word)
please write both methods also the output of the demo class thank u
Explanation / Answer
/*
* Java Program to Implement Singly Linked List
*/
/* Class linkedList */
public class linkedList
{
/* Class Node */
class Node
{
protected String data;
protected Node link;
/* Constructor */
public Node()
{
link = null;
data = null;
}
/* Constructor */
public Node(String d,Node n)
{
data = d;
link = n;
}
/* Function to set link to next Node */
public void setLink(Node n)
{
link = n;
}
/* Function to set data to current Node */
public void setData(String d)
{
data = d;
}
/* Function to get link to next node */
public Node getLink()
{
return link;
}
/* Function to get data from current Node */
public String getData()
{
return data;
}
}
protected Node start;
protected Node end ;
public int size ;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/* Function to check if list is empty */
public boolean isEmpty()
{
return start == null;
}
/* Function to get size of list */
public int getSize()
{
return size;
}
/* Function to insert an element at begining */
public void insertAtStart(String val)
{
Node nptr = new Node(val, null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
nptr.setLink(start);
start = nptr;
}
}
/* Function to insert an element at end */
public void insertAtEnd(String val)
{
Node nptr = new Node(val,null);
size++ ;
if(start == null)
{
start = nptr;
end = start;
}
else
{
end.setLink(nptr);
end = nptr;
}
}
/* Function to insert an element at position */
public void insertAtPos(String val , int pos)
{
Node nptr = new Node(val, null);
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink() ;
ptr.setLink(nptr);
nptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size++ ;
}
/* Function to delete an element at position */
public void deleteAtPos(int pos)
{
if (pos == 1)
{
start = start.getLink();
size--;
return ;
}
if (pos == size)
{
Node s = start;
Node t = start;
while (s != end)
{
t = s;
s = s.getLink();
}
end = t;
end.setLink(null);
size --;
return;
}
Node ptr = start;
pos = pos - 1 ;
for (int i = 1; i < size - 1; i++)
{
if (i == pos)
{
Node tmp = ptr.getLink();
tmp = tmp.getLink();
ptr.setLink(tmp);
break;
}
ptr = ptr.getLink();
}
size-- ;
}
/* Function to display elements */
public void display()
{
if (size == 0)
{
System.out.print("empty ");
return;
}
if (start.getLink() == null)
{
System.out.println(start.getData() );
return;
}
Node ptr = start;
System.out.println(start.getData());
ptr = start.getLink();
while (ptr.getLink() != null)
{
System.out.println(ptr.getData());
ptr = ptr.getLink();
}
System.out.print(ptr.getData()+ " ");
}
public int countWord (String word)
{
int count=0;
if (size == 0)
{
return count;
}
if (start.getLink() == null)
{
if(word.equals(start.data))
{
count++;
}
return count;
}
Node ptr = start;
ptr = start.getLink();
while (ptr.getLink() != null)
{
if(word.equals(ptr.data))
{
count++;
}
ptr = ptr.getLink();
}
return count;
}
public int lastIndexOf (String word)
{
int wordIndex=-1;
int currentIndex=0;
if (size == 0)
{
return wordIndex;
}
if (start.getLink() == null)
{
if(word.equals(start.data))
{
wordIndex=currentIndex;
}
return wordIndex;
}
Node ptr = start;
while (ptr.getLink() != null)
{
currentIndex++;
if(word.equals(ptr.data))
{
wordIndex=currentIndex;
}
ptr = ptr.getLink();
}
return wordIndex;
}
public void remove (String word)
{
if (size == 0)
{
return ;
}
else if (start.getLink() == null)
{
if(word.equals(start.data))
{
start = start.getLink();
size--;
}
return ;
}
else {
// The string, if it occurs at all, is somewhere beyond the
// first element of the list. Search the list.
Node runner; // A node for traversing the list.
Node previous; // Always points to the node preceding runner.
runner = start.getLink(); // Start by looking at the SECOND list node.
previous = start;
while ( runner != null ) {
// Move previous and runner along the list until runner
// falls off the end or hits a list element that is
// greater than or equal to deleteItem. When this
// loop ends, runner indicates the position where
// deleteItem must be, if it is in the list.
if(runner.getData().equals(word))
{
previous.setLink(runner.getLink());
}
else
{
previous = runner;
}
runner = runner.getLink();
}
}
}
}
-----------------------------------
public class Demo
{
public static void main(String args[])
{
linkedList l=new linkedList();
l.insertAtEnd("a");
l.insertAtEnd("b");
l.insertAtEnd("b");
l.insertAtEnd("b");
l.insertAtEnd("d");
System.out.println(l.countWord("b"));
System.out.println(l.lastIndexOf("b"));
l.remove("b");
System.out.println(" content:");
l.display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.