-4

每个人。任何人都可以帮助我如何开始这个问题。我不是很清楚。非常感谢。

问题是: 实现SetImpl.java的addmember方法。请注意,强烈建议您在添加期间不允许重复 - 这会使其他方法的实施更具挑战性。

以下是关于SetImpl.java的 java 编码:

import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

public class SetImpl<T> implements Set<T>{


    // container class for linked list nodes
    private class Node<T>{
        public T val;
        public Node<T> next;
    }

    private Node<T> root; // empty set to begin with

    // no need for constructor


    // add new element to the set by checking for membership.. if not
    // then add to the front of the list
    public void add(T val){
    }

    // delete element from the list - may be multiple copies.
    public void delete(T val){

    }


    // membership test of list
    public boolean member(T val){

        return false;
    }

    // converts to a list
    public List<T> toList(){
        ArrayList<T> res;
        return res;
    }

    // does simple set union   
    public void union(Set<T> s){

    }

}

任何人都可以给我一些关于这个问题的提示吗?非常感谢!

第一次尝试

private Node < T > root = null;
private Node < T > head = null;
private Node < T > tail = null;
public void add(T val) {
    if (head == null) {
        head = tail = new Node < T > ();
        head.val = val;
        root.next = tail;
        tail = head;
    } else {
        tail.next = new Node < T > ();
        tail = tail.next;
        tail.val = val;
    }
}
4

1 回答 1

0

好的,这里只是提示你应该如何进入你的逻辑:

public class SetImpl<T> implements Set<T>{


    // container class for linked list nodes
    ///// It is said "Linked list" --> first hint google that
    private class Node<T>{
        public T val;
        public Node<T> next;
    }

    private Node<T> root; // empty set to begin with
    ///// So that's my "root" which means I will have descendants

    // no need for constructor


    // add new element to the set by checking for membership.. if not
    // then add to the front of the list
    // Basically here everything is said. 
    // 1- check membership
    // 2- if true do nothing (as it has been said, it's a Set, if you don't know why I say that google Set Collections
    //if false (which means the val I want to add is not in my set) then I can add it to the Set
    public void add(T val){
    }

    // delete element from the list - may be multiple copies.
    public void delete(T val){

    }


    // membership test of list
    // that's recursive calls. How do you check that? where do you store your values?
    // It's true if your current Node.val attribute == the value OR if the rest of the Nodes has the value as member. Here you really need to know about Linked List
    public boolean member(T val){

        return false;
    }

    // converts to a list
    public List<T> toList(){
        ArrayList<T> res;
        return res;
    }

    // does simple set union   
    public void union(Set<T> s){

    }

}

您的实施

private Node < T > root = null;
private Node < T > head = null;
private Node < T > tail = null;
public void add(T val) {
    if (head == null) {
        head = tail = new Node < T > ();
        head.val = val;
        root.next = tail;
        tail = head;
    } else {
        tail.next = new Node < T > ();
        tail = tail.next;
        tail.val = val;
    }
}

我的猜测是您不需要在此类练习中添加字段。它已经为你准备好了一半。看看链表是如何工作的。我很懒所以我提供了谷歌的第一个“链接列表”结果。:) 玩得开心

于 2013-07-01T16:05:04.760 回答