【问题标题】:How to fix Linked List implementation problem?如何解决链表实现问题?
【发布时间】:2022-01-20 01:52:30
【问题描述】:

我认为正确插入值也可以在控制台屏幕上查看,但是搜索方法无法以奇怪的方式工作

我的所有代码都缺少什么?它非常清晰和基本的结构

在输出部分“43”通常包括结构,它需要显示为真,但可能它不起作用我不知道原因。 输出: 43 43 33 43 13 3 检查:假

 NodeList head;

    public void insert(int insertKey) {
        NodeList myhead = new NodeList();
        if (head == null) {
            myhead.setData(insertKey);
        } else {
            myhead.setData(insertKey);
            myhead.setLink(head);
           
        }
            head = myhead;
    }
    public boolean search(int key) {

        boolean check = false;
        NodeList current = head;
        while (current != null) {
            if (current.getData() == key) {
                check = true;
                break;
            }
            current = current.getLink();

        }
        return check;
    }

public void display() {
        if (head == null) {
        } else {
            while (head != null) {
                System.out.print(head.getData() + " ");
                head = head.getLink();
            }
            System.out.println();
        }
    }



package ds_project;

public class NodeList {

    private NodeList Link;
    private int data;

    public NodeList(int data) {
        this.Link = null;
        this.data = data;
    }

    public NodeList() {
    }


    public NodeList getLink() {
        return Link;
    }

    public void setLink(NodeList Link) {
        this.Link = Link;
    }

    public int getData() {
        return this.data;
    }

    public void setData(int data) {
        this.data = data;
    }

result screen
  SLL Linkedlist = new SLL();
        Linkedlist.insert(3);
        Linkedlist.insert(13);
        Linkedlist.insert(43);
        Linkedlist.insert(33);
        Linkedlist.insert(43);
        Linkedlist.insert(43);
        Linkedlist.display();
        boolean check=Linkedlist.search(43);
        System.out.println("check: " + check);}

输出: 43 43 33 43 13 3 检查:假

【问题讨论】:

    标签: java data-structures linked-list


    【解决方案1】:

    显示方式错误,head会变成tail节点。使用临时节点:

        public void display() {
            NodeList tmp = head;
            if (tmp != null) {
                while (tmp != null) {
                    System.out.print(tmp.getData() + " ");
                    tmp = tmp.getLink();
                }
                System.out.println();
            }
        }
    

    编辑:我的测试代码:

    public class NodeList {
    
        private NodeList Link;
        private int data;
    
        public NodeList(int data) {
            this.Link = null;
            this.data = data;
        }
    
        public NodeList() {
        }
    
    
        public NodeList getLink() {
            return Link;
        }
    
        public void setLink(NodeList Link) {
            this.Link = Link;
        }
    
        public int getData() {
            return this.data;
        }
    
        public void setData(int data) {
            this.data = data;
        }
    
        NodeList head;
    
        public void insert(int insertKey) {
            NodeList myhead = new NodeList();
            if (head == null) {
                myhead.setData(insertKey);
            } else {
                myhead.setData(insertKey);
                myhead.setLink(head);
    
            }
            head = myhead;
        }
        public boolean search(int key) {
    
            boolean check = false;
            NodeList current = head;
            while (current != null) {
                if (current.getData() == key) {
                    check = true;
                    break;
                }
                current = current.getLink();
    
            }
            return check;
        }
        public void display() {
            NodeList tmp = head;
            if (tmp != null) {
                while (tmp != null) {
                    System.out.print(tmp.getData() + " ");
                    tmp = tmp.getLink();
                }
                System.out.println();
            }
        }
    
        public static void main(String[] args) {
            NodeList Linkedlist = new NodeList();
            Linkedlist.insert(3);
            Linkedlist.insert(13);
            Linkedlist.insert(43);
            Linkedlist.insert(33);
            Linkedlist.insert(43);
            Linkedlist.insert(43);
            Linkedlist.display();
            boolean check=Linkedlist.search(43);
            System.out.println("check: " + check);
        }
    
    }
    

    【讨论】:

    • 我自己没有区别,我运行我再次得到相同的结果。
    • check 还是 false?在我的电脑上,我测试了检查输出是否为真..
    • 我很困惑我会在我的电脑上关机后尝试。所以你能分享输出作为链接吗?我想知道它是怎么做到的?
    • 我确认检查是真的。
    • @ChengThao 这意味着我写的代码是真的,所以你能分享一下执行的代码吗?
    猜你喜欢
    • 2021-12-11
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 2019-03-29
    • 2013-05-23
    • 1970-01-01
    • 2014-07-01
    • 1970-01-01
    相关资源
    最近更新 更多