0

我的 int count(int i) 函数有问题:该函数应该计算一个数字出现在列表中的次数。例如,有一个数字数组 3 4 4 1 3 2 1 这就是它应该显示的内容:

Item 0  count = 0
Item 1  count = 2
Item 2  count = 1
Item 3  count = 2
Item 4  count = 2
Item 5  count = 0

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

//public int count(int item) {
  // int count = 0;

   //for(int i = 0; i < length; i++) {
    //   if(bag[i] == item) {
    //     count++;
      // }
   //}

  // return count;
//}


// 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 implementing a linked list.
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("\nError: 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 currentPos = new Node();
    Node newPos = new Node(); //create a new node
    newPos.setInfo(item); //load the data
    currentPos = first;

        while(currentPos.getNext() !=null)
        {
            currentPos = currentPos.getNext();
        }
        currentPos.setNext(newPos);
}

// Replaces the info in the list at location with item
public void replace(int location, int item)
{
    Node currentPos = new Node();
    Node prevPos = new Node();
    Node nextPos = new Node();
    int length = getLength();

    if (location <1 || location > length)
        System.out.println("\nError: Attempted get location out of range.");
    else
    {
        prevPos = first;
        for(int i=0; i < location-1; i++)
        {
            prevPos = prevPos.getNext();
        }
        currentPos = prevPos.getNext();
        nextPos = currentPos.getNext();
        Node newPos = new Node();
        newPos.setInfo(item);
        newPos.setNext(nextPos);
        prevPos.setNext(newPos);    
    }
}

// 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 Lab2B2
{
public static void main(String args[])
{
    Bag intBag = new Bag();

    for (int i =0; i < 10; i++)
    {
        int info = (int)(Math.random()*10);
        intBag.add(info);
    }

    // Before List
    System.out.print("List creation before: " + intBag);

    // Counts the # of occurrences of item in the bag

    //System.out.println("\nCount the number of occurrences:");
    //for(int i = 0; i <= 10; i++) 
    //{
    //System.out.println("Item " + i + " count = " + list.count(i));
    //}

    // Returns the number of items in the bag
    System.out.print("\nLength of List: " + intBag.getLength());

    // Adds an item to the bag
    intBag.add(9);
    System.out.print("\nList creation - Add(9):  " + intBag);

    // Removes an item from the bag, all occurrences of item in the bag
    intBag.remove(8);
    System.out.print("\nList creation - Remove(8):  " + intBag);

    // Removes all of the items from the bag
    intBag.clear();
    System.out.print("\nList creation - Clear():  " + intBag);

    // Determines whether the bag is empty

}
}
4

2 回答 2

2

您正在管理一个 LinkedList。您需要做的是在节点上运行并检查它们中的任何一个是否等于您正在检查的项目。在这种情况下,您增加计数。该代码充满了如何遍历列表的示例。getLength() 方法是一个很好的开始,因为它有遍历所有元素的代码。稍加修改,你就得到你想要的。

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;
} 
于 2013-11-09T22:39:49.780 回答
1

int[] bag由于没有int[] bag声明为类成员,您不应该也将 an作为参数传递吗?

public int count(int item, int[] bag) { 
    int count = 0;

    for(int i = 0; i < length; i++) {
        if(bag[i] == item) {
            count++;
        }
    }
    return count;
}
于 2013-11-09T22:29:52.443 回答