qq_53489991 2022-03-15 16:04 采纳率: 100%
浏览 55
已结题

JAVA实现链表查找函数错误

删减的函数都没有带static,只有查找的函数报了静态的错误。
find函数加上static又会报Node无法引用,需要去掉static的错误。

package LinkedList;

public class Linked {

private class Node{
    private T t;
    private Node next;
    public Node(T t,Node next){
        this.t = t;
        this.next = next;
    }
    public Node(T t){
        this(t,null);
    }
}
private Node head;            //头结点
private int size;            //链表元素个数
private int n;              //寻找元素个数
//构造函数
public Linked(){
    this.head = null;
    this.size = 0;
    this.n = 0;
}

//获取链表元素的个数
public int getSize(){
    return this.size;
}
//判断链表是否为空
public boolean isEmpty(){
    return this.size == 0;
}
//链表头部添加元素
public void addFirst(T t){
    Node node = new Node(t);    //节点对象
    node.next = this.head;
    this.head = node;
    this.size++;
}
//向链表尾部插入元素
public void addLast(T t){
    this.add(t, this.size);
}
//向链表中间插入元素
public void add(T t,int index){
    if (index <0 || index >size){
        throw new IllegalArgumentException("index is error");
    }
    if (index == 0){
        this.addFirst(t);
        return;
    }
    Node preNode = this.head;
    //找到要插入节点的前一个节点
    for(int i = 0; i < index-1; i++){
        preNode = preNode.next;
    }
    Node node = new Node(t);
    //要插入的节点的下一个节点指向preNode节点的下一个节点
    node.next = preNode.next;
    //preNode的下一个节点指向要插入节点node
    preNode.next = node;
    this.size++;
}

//删除链表元素
public void remove(T t){
    if(head == null){
        System.out.println("无元素可删除");
        return;
    }
    //要删除的元素与头结点的元素相同
    while(head != null && head.t.equals(t)){
        head = head.next;
        this.size--;
    }

    //对头结点的下一个结点进行判别
    Node cur = this.head;
    while(cur != null && cur.next != null){
        if(cur.next.t.equals(t)){
            this.size--;
            cur.next = cur.next.next;
        }
        else cur = cur.next;
    }

}

//找匹配某元素的个数
public void find(T t){
    Node cur = this.head;
    while(cur != null){
        if(cur.t.equals(t)){
            this.n++;
            cur = cur.next;
        }
        else cur = cur.next;
    }
    System.out.println("该元素在队列中个数为:" + this.n);
}

//删除链表第一个元素
public T removeFirst(){
    if(this.head == null){
        System.out.println("无元素可删除");
        return null;
    }
    Node delNode = this.head;
    this.head = this.head.next;
    delNode.next =null;
    this.size--;
    return delNode.t;
}

//删除链表的最后一个元素
public T removeLast(){
    if(this.head == null){
        System.out.println("无元素可删除");
        return null;
    }
    //只有一个元素
    if(this.getSize() == 1){
        return this.removeFirst();
    }
    Node cur = this.head;    //记录当前结点
    Node pre = this.head;    //记录要删除结点的前一个结点
    while(cur.next != null){
        pre = cur;
        cur = cur.next;
    }
    pre.next = cur.next;
    this.size--;
    return cur.t;
}

public String toString() {
    StringBuilder sb = new StringBuilder();
    Node cur = this.head;
    while(cur != null){
        sb.append(cur.t).append("->");
        cur = cur.next;
    }
    sb.append("NULL");
    return sb.toString();
}

public static void main(String[] args) {
    Linked linked = new Linked();
    for(int i = 0; i < 10; i++){
        linked.addFirst(i);
    }
    System.out.println("原始列表为:");
    System.out.println(linked);
    Linked.find(1);
    linked.addLast(10);
    System.out.println("在链表末尾插入10:");
    System.out.println(linked);
    linked.addFirst(10);
    System.out.println("在链表头结点插入10:");
    linked.add(10, 5);
    System.out.println(linked);
    System.out.println("在链表删除10:");
    linked.remove(10);
    System.out.println(linked);
    System.out.println("删除第一个元素:"+linked.removeFirst());
    System.out.println(linked);
    System.out.println("删除最后一个元素:"+linked.removeLast());
    System.out.println(linked);

}

}

  • 写回答

4条回答 默认 最新

  • CSDN专家-sinJack 2022-03-15 17:22
    关注

    调用find方法的地方呢,需要创建对象调用。
    代码都贴出来吧,帮你调试一下

    public class Linked<T> {
        private class Node{
            private T t;
            private Node next;
            public Node(T t,Node next){
                this.t = t;
                this.next = next;
            }
            public Node(T t){
                this(t,null);
            }
        }
        private Node head;            //头结点
        private int size;            //链表元素个数
        private int n;              //寻找元素个数
        //构造函数
        public Linked(){
            this.head = null;
            this.size = 0;
            this.n = 0;
        }
    
        //获取链表元素的个数
        public int getSize(){
            return this.size;
        }
        //判断链表是否为空
        public boolean isEmpty(){
            return this.size == 0;
        }
        //链表头部添加元素
        public void addFirst(T t){
            Node node = new Node(t);    //节点对象
            node.next = this.head;
            this.head = node;
            this.size++;
        }
        //向链表尾部插入元素
        public void addLast(T t){
            this.add(t, this.size);
        }
        //向链表中间插入元素
        public void add(T t,int index){
            if (index <0 || index >size){
                throw new IllegalArgumentException("index is error");
            }
            if (index == 0){
                this.addFirst(t);
                return;
            }
            Node preNode = this.head;
            //找到要插入节点的前一个节点
            for(int i = 0; i < index-1; i++){
                preNode = preNode.next;
            }
            Node node = new Node(t);
            //要插入的节点的下一个节点指向preNode节点的下一个节点
            node.next = preNode.next;
            //preNode的下一个节点指向要插入节点node
            preNode.next = node;
            this.size++;
        }
    
        //删除链表元素
        public void remove(T t){
            if(head == null){
                System.out.println("无元素可删除");
                return;
            }
            //要删除的元素与头结点的元素相同
            while(head != null && head.t.equals(t)){
                head = head.next;
                this.size--;
            }
    
            //对头结点的下一个结点进行判别
            Node cur = this.head;
            while(cur != null && cur.next != null){
                if(cur.next.t.equals(t)){
                    this.size--;
                    cur.next = cur.next.next;
                }
                else cur = cur.next;
            }
    
        }
    
        //找匹配某元素的个数
        public void find(T t){
            Node cur = this.head;
            while(cur != null){
                if(cur.t.equals(t)){
                    this.n++;
                    cur = cur.next;
                }
                else cur = cur.next;
            }
            System.out.println("该元素在队列中个数为:" + this.n);
        }
    
        //删除链表第一个元素
        public T removeFirst(){
            if(this.head == null){
                System.out.println("无元素可删除");
                return null;
            }
            Node delNode = this.head;
            this.head = this.head.next;
            delNode.next =null;
            this.size--;
            return delNode.t;
        }
    
        //删除链表的最后一个元素
        public T removeLast(){
            if(this.head == null){
                System.out.println("无元素可删除");
                return null;
            }
            //只有一个元素
            if(this.getSize() == 1){
                return this.removeFirst();
            }
            Node cur = this.head;    //记录当前结点
            Node pre = this.head;    //记录要删除结点的前一个结点
            while(cur.next != null){
                pre = cur;
                cur = cur.next;
            }
            pre.next = cur.next;
            this.size--;
            return cur.t;
        }
    
        public String toString() {
            StringBuilder sb = new StringBuilder();
            Node cur = this.head;
            while(cur != null){
                sb.append(cur.t).append("->");
                cur = cur.next;
            }
            sb.append("NULL");
            return sb.toString();
        }
    
        public static void main(String[] args) {
            Linked linked = new Linked();
            for(int i = 0; i < 10; i++){
                linked.addFirst(i);
            }
            System.out.println("原始列表为:");
            System.out.println(linked);
            linked.find(1);
            linked.addLast(10);
            System.out.println("在链表末尾插入10:");
            System.out.println(linked);
            linked.addFirst(10);
            System.out.println("在链表头结点插入10:");
            linked.add(10, 5);
            System.out.println(linked);
            System.out.println("在链表删除10:");
            linked.remove(10);
            System.out.println(linked);
            System.out.println("删除第一个元素:"+linked.removeFirst());
            System.out.println(linked);
            System.out.println("删除最后一个元素:"+linked.removeLast());
            System.out.println(linked);
    
        }
    }
    

    img

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(3条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月20日
  • 已采纳回答 3月15日
  • 修改了问题 3月15日
  • 修改了问题 3月15日
  • 展开全部

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题