判断单链表相等,运行不出来,请问有什么问题?

判断单链表相等,运行不出来,请问有什么问题?,第1张

判断单链表相等,运行不出来,请问有什么问题?
package model;
import java.util.ArrayList;
import java.util.List;

public  class SinglyList{
    public Node head;
    public SinglyList(){
        this.head=new Node();
    }
    public SinglyList(T[] str) {
        this();
        Node rear = this.head;
        for (int i = 0; i < str.length; i++) {
            if (str[i] != null) {
                rear.next = new Node(rear, null);
                rear = rear.next;
            }
        }

   }
//    public SinglyList(SinglyList singlyList){
//        containsAll(singlyList);
//    }

    public boolean equal(Object obj) {
        if (this == obj) return true;
        if(obj instanceof SinglyList) {
            SinglyList that = (SinglyList) obj;
            Node tNode = this.head.next;
            Node vNode = that.head.next;
            while (tNode != null) {
                if (tNode.data!=vNode.data) {
                    return false;
                }
                tNode = tNode.next;
                vNode = vNode.next;
            }
        }else{
            return false;
        }
        return true;
    }
//
//    public boolean containsAll(SinglyList list){
//
//        if(list==null){
//            throw new NullPointerException("list为空!");
//        }
//        Node lNode= list.head.next;
//        boolean flag=false;
//        while(lNode!=null){
//            flag=false;
//            for(Node tNode=this.head.next;tNode!=null;tNode = tNode.next){
//                if((lNode.data).equals(tNode.data)){
//                    flag = true;
//                }
//            }
//            if(flag==false){
//                return flag;
//            }
//            lNode= lNode.next;
//        }
//        return flag;
//    }
    public static void main(String[] args) {
        String[] str= {"a","a","aa","b","b","ba","ab","bb","a","b","aa"};
        String[] str1={"a","c","d","e","f"};
        String[] str2={"a","a","aa","b","b","ba","ab","bb","a","b","aa"};
        SinglyList singlyList =new SinglyList(str);
        SinglyList singlyList1 =new SinglyList(str1);
        SinglyList singlyList2 =new SinglyList(str2);
        System.out.println(singlyList.equal(singlyList1));
        System.out.println(singlyList.equal(singlyList2));
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5638253.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存