I need to a function called count. To count the number of occurrences of item in
ID: 3546080 • Letter: I
Question
I need to a function called count. To count the number of occurrences of item in the bag. For example if the number 5 appears in the set of numbers twice it will output number five appears twice. Then I need main to test all the functions in the class named bag.
// Class implementing a linked list.
class Bag
{
private Node first; //dummy header node
// Initializes the list to empty creating a dummy header node.
public Bag()
{
first = new Node();
}
// Returns true if the list is empty, false otherwise
public boolean isEmpty()
{
return (first.getNext() == null);
}
// Clears all elements from the list
public void clear()
{
first.setNext(null);
}
// Returns the number of item in the list
public int getLength()
{
int length = 0;
Node current = first.getNext();
while (current != null)
{
length++;
current = current.getNext();
}
return length;
}
// Prints the list elements.
public String toString()
{
String list = "";
Node current = first.getNext();
while (current != null)
{
list += current.getInfo() + " ";
current = current.getNext();
}
return list;
}
// Adds the element x to the beginning of the list.
public void add(int x)
{
Node p = new Node();
p.setInfo(x);
p.setNext(first.getNext());
first.setNext(p);
}
// Deletes an item x from the list. Only the first
// occurrence of the item in the list will be removed.
public void remove(int x)
{
Node old = first.getNext();
Node p = first;
//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x)
found = true;
else
{
p = old;
old = p.getNext();
}
}
//if x is in the list, remove it.
if (found)
p.setNext(old.getNext());
}
// Returns the element at a given location in the list
public int get(int location)
{
int item = -1;
int length = getLength();
if (location <1 || location > length)
System.out.println(" Error: Attempted get location out of range.");
else
{
int n = 1;
Node current = first.getNext();
while (n < location)
{
n++;
current = current.getNext();
}
item = current.getInfo();
}
return item;
}
/************************************************************************
Students to complete the following two methods for the LinkedList class
***********************************************************************/
// Replaces the info in the list at location with item
public void replace(int location, int item)
{
Node currentone = new Node();
Node previousone = new Node();
Node next = new Node();
int length = getLength();
if (location <1 || location > length) //error handling
System.out.println(" Error: Attempted get location out of range.");
else
{
previousone = first;
for(int i=0; i < location-1; i++)
{
previousone = previousone.getNext();
}
currentone = previousone.getNext();
next = currentone.getNext();
Node newNode = new Node();
newNode.setInfo(item);
newNode.setNext(next);
previousone.setNext(newNode);
}
}
public int count(int item)
{
}
// Inner class Node.
private class Node
{
private int info; //element stored in this node
private Node next; //link to next node
// Initializes this node setting info to 0 and next to null
public Node()
{
info = 0;
next = null;
}
// Sets the value for this node
public void setInfo(int i)
{
info = i;
}
// Sets the link to the next node
public void setNext(Node lnk)
{
next = lnk;
}
// Returns the value in this node
public int getInfo()
{
return info;
}
// Returns the link to the next node
public Node getNext()
{
return next;
}
}
}
class LinkedList
{
private Node first; //dummy header node
// Initializes the list to empty creating a dummy header node.
public LinkedList()
{
first = new Node();
}
// Returns true if the list is empty, false otherwise
public boolean isEmpty()
{
return (first.getNext() == null);
}
// Clears all elements from the list
public void clear()
{
first.setNext(null);
}
// Returns the number of item in the list
public int getLength()
{
int length = 0;
Node current = first.getNext();
while (current != null)
{
length++;
current = current.getNext();
}
return length;
}
// Prints the list elements.
public String toString()
{
String list = "";
Node current = first.getNext();
while (current != null)
{
list += current.getInfo() + " ";
current = current.getNext();
}
return list;
}
// Adds the element x to the beginning of the list.
public void add(int x)
{
Node p = new Node();
p.setInfo(x);
p.setNext(first.getNext());
first.setNext(p);
}
// Deletes an item x from the list. Only the first
// occurrence of the item in the list will be removed.
public void remove(int x)
{
Node old = first.getNext();
Node p = first;
//Finding the reference to the node before the one to be deleted
boolean found = false;
while (old != null && !found)
{
if (old.getInfo() == x)
found = true;
else
{
p = old;
old = p.getNext();
}
}
//if x is in the list, remove it.
if (found)
p.setNext(old.getNext());
}
// Returns the element at a given location in the list
public int get(int location)
{
int item = -1;
int length = getLength();
if (location <1 || location > length)
System.out.println(" Error: Attempted get location out of range.");
else
{
int n = 1;
Node current = first.getNext();
while (n < location)
{
n++;
current = current.getNext();
}
item = current.getInfo();
}
return item;
}
/************************************************************************
Students to complete the following two methods for the LinkedList class
***********************************************************************/
// Adds item to the end of the list
public void addEnd(int item)
{
Node currentone = new Node(); //create a new node
Node newnode = new Node(); //create a new node
newnode.setInfo(item); //load the data
currentone = first;
if (currentone != null) //error handling
{
while(currentone.getNext() !=null) //points at blank space
{
currentone = currentone.getNext();
}
currentone.setNext(newnode);
}
else
{
first.setNext(newnode);
}
}
// Replaces the info in the list at location with item
public void replace(int location, int item)
{
Node currentone = new Node();
Node previousone = new Node();
Node next = new Node();
int length = getLength();
if (location <1 || location > length) //error handling
System.out.println(" Error: Attempted get location out of range.");
else
{
previousone = first;
for(int i=0; i < location-1; i++)
{
previousone = previousone.getNext();
}
currentone = previousone.getNext();
next = currentone.getNext();
Node newNode = new Node();
newNode.setInfo(item);
newNode.setNext(next);
previousone.setNext(newNode);
}
}
// Inner class Node.
private class Node
{
private int info; //element stored in this node
private Node next; //link to next node
// Initializes this node setting info to 0 and next to null
public Node()
{
info = 0;
next = null;
}
// Sets the value for this node
public void setInfo(int i)
{
info = i;
}
// Sets the link to the next node
public void setNext(Node lnk)
{
next = lnk;
}
// Returns the value in this node
public int getInfo()
{
return info;
}
// Returns the link to the next node
public Node getNext()
{
return next;
}
}
}
public class Lab2B
{
public static void main(String args[])
{
LinkedList intList = new LinkedList();
int count =0;
int number=113;
while(count < 30)
{
int c = 0;
for(int i =2; i<number; i++)
{
if(number%i==0)
{
c=1;
break;
}
}
if (c==0)
{
intList.add(number);
//System.out.print(number + " ");
count++;
}
number--;
}
System.out.print(intList.toString());
}
}
/**************************************************************************
**System Output Below:
******************************************************************************/
Explanation / Answer
// This is your count method
public int count(int item)
{
int count = 0;
Node current = first.getNext();
while (current != null)
{
if (current.getInfo()==item)
{
count++;
}
current = current.getNext();
}
return count;
}
// Put this in main
// Counts the # of occurrences of item in the bag
System.out.println(" Count the number of occurrences:");
for(int i = 0; i <= 10; i++)
{
System.out.println("Item " + i + " count = " + intBag.count(i));
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.