如何实现SetImpl.java的add和member方法?
各位。任何人都可以帮助我如何开始这个问题。我不是很清楚。非常感谢 问题是: 实现SetImpl.java的add和member方法。请注意,强烈建议您在添加过程中不允许重复-这将使其他方法的实现更具挑战性 以下是关于SetImpl.java的java编码:如何实现SetImpl.java的add和member方法?,java,Java,各位。任何人都可以帮助我如何开始这个问题。我不是很清楚。非常感谢 问题是: 实现SetImpl.java的add和member方法。请注意,强烈建议您在添加过程中不允许重复-这将使其他方法的实现更具挑战性 以下是关于SetImpl.java的java编码: import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class SetImpl<T> implements S
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){
}
}
import java.util.List;
导入java.util.ArrayList;
导入java.util.Iterator;
公共类SetImpl实现Set{
//链表节点的容器类
私有类节点{
公共T值;
公共节点下一步;
}
private Node root;//以空集开头
//不需要构造函数
//通过检查成员身份向集合中添加新元素..如果不是
//然后添加到列表的前面
公共无效添加(T val){
}
//从列表中删除元素-可以是多个副本。
公共作废删除(T val){
}
//名单的成员资格测试
公共布尔成员(T val){
返回false;
}
//转换为列表
公众名单收费表(){
ArrayList res;
返回res;
}
//简单集并集吗
公共无效联合(集合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;
}
}
私有节点root=null;
私有节点head=null;
私有节点tail=null;
公共无效添加(T val){
if(head==null){
头=尾=新节点();
head.val=val;
root.next=tail;
尾=头;
}否则{
tail.next=新节点();
tail=tail.next;
tail.val=val;
}
}
好的,这里只是一些提示,告诉你应该如何使用逻辑:
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){
}
}
public类SetImpl实现Set{
//链表节点的容器类
/////据说“链表”-->首先提示谷歌
私有类节点{
公共T值;
公共节点下一步;
}
private Node root;//以空集开头
/////这就是我的“根”,意思是我会有后代
//不需要构造函数
//通过检查成员身份向集合中添加新元素..如果不是
//然后添加到列表的前面
//基本上这里什么都说了。
//1-检查会员资格
//2-如果是真的,什么都不做(正如人们所说的,这是一个集合,如果你不知道为什么我说谷歌集合
//如果false(这意味着我要添加的val不在我的集合中),那么我可以将其添加到集合中
公共无效添加(T val){
}
//从列表中删除元素-可以是多个副本。
公共作废删除(T val){
}
//名单的成员资格测试
//这是递归调用。如何检查?在哪里存储值?
//如果您当前的Node.val属性==该值,或者其他节点的值为member,则为true
公共布尔成员(T val){
返回false;
}
//转换为列表
公众名单收费表(){
ArrayList res;
返回res;
}
//简单集并集吗
公共无效联合(集合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;
}
}
私有节点root=null;
私有节点head=null;
私有节点tail=null;
公共无效添加(T val){
if(head==null){
头=尾=新节点();
head.val=val;
root.next=tail;
尾=头;
}否则{
tail.next=新节点();
tail=tail.next;
tail.val=val;
}
}
我猜你不需要在这样的练习中添加字段。这对你来说已经是半生不熟了。看看吧。我很懒,所以我提供了谷歌的第一个“链接列表”结果。:)
玩得开心这里有一个很好的建议:开始实施它,一旦你遇到困难,我们会帮助你。这看起来像是一个可疑的家庭作业/单科练习问题。你应该知道,现在很多机构都有软件来帮助他们发现人们何时过度使用互联网寻求帮助。也不要对他太苛刻,他要求帮助,而不是开始解决问题。先让我们看看你试过什么。扰流板提示:
//以空集合开始
和//向集合添加新元素…
这是我的家庭作业。为什么我发布这个问题来寻求帮助,因为我真的不知道从哪里开始这个问题。我是java新手。我只需要一些提示。@hjxlpp就像彼得说的,先试试,让我们看看你做了什么。那我们就可以帮忙了。