【问题标题】:How to create some methods(ex: insertAtIndex()) for custom linked list?如何为自定义链表创建一些方法(例如:insertAtIndex())?
【发布时间】:2018-12-01 11:24:44
【问题描述】:

我是链表概念的新手,第一次构建这个自定义链表时遇到了很多麻烦。

我有两个班级:CellPhoneCellList

CellPhone 中,我有 4 个属性:serialNum(long)、brand(String)、year(int) 和 price(double)。

CellList,我有:

  • 一个名为CellNode的内部类,它有两个属性:phone(CellPhone)和next(CellNode)
  • 还有两个属性head(CellNode)和size(int)

这是来自我的 CellList 类:

private CellNode head; // point first node in this list object
private int size; // current size of the list(how many nodes in the list)

public CellList() {
    head = null;
    size = 0;
}
public CellList(CellList c) { // is this a correct deep copying?
    head = new CellNode(c.head);
    size = c.getSize();
}

public int getSize() {
    return size;
}

public void addToStart(CellPhone c) {
    head = new CellNode(c, null); //head.getPhone() = c, head.getNextNode() = null.
    size++;
}

我什至不确定addToStart 方法是否正确完成,现在我需要添加insertAt(/deleteFrom)Index(CellPhone c, int index) 之类的方法。我已经做到了:

public void insertAtIndex(CellPhone c, int index) { //index is invalid when it's not 0<index<size-1
    if(index<0 || index>size-1) {
        throw new NoSuchElementException("index is invalid! System terminated.");
    }

但我无法完全理解 Node 的工作原理,所以我被卡住了。


这里是完整的代码:

import java.util.NoSuchElementException;

public class CellList {

    class CellNode {
        private CellPhone phone;
        private CellNode next;
        public CellNode() {
            phone = null;
            next = null;
        }
        public CellNode(CellPhone c, CellNode n) {
            phone = c;
            next = n;
        }
        public CellNode(CellNode c) {
            this(c.getPhone(), c.getNextNode());
        }

        public CellNode clone() {
            CellNode c = new CellNode(phone, next);
            return c;
        }

        public CellPhone getPhone() {
            return phone;
        }
        public CellNode getNextNode() {
            return next;
        }

        public void setPhone(CellPhone c) {
            phone = c;
        }
        public void setNextNode(CellNode n) {
            next = n;
        }
    }

    private CellNode head; // point first node in this list object
    private int size; // current size of the list(how many nodes in list)

    public CellList() {
        head = null;
        size = 0;
    }
    public CellList(CellList c) {
        head = new CellNode(c.head);
        size = c.getSize();
    }

    public int getSize() {
        return size;
    }

    public void addToStart(CellPhone c) {
        head = new CellNode(c, null); //head.getPhone() = c, head.getNextNode() = null.
        size++;
    }

    public void insertAtIndex(CellPhone c, int index) { //index is invalid when it's not 0<index<size-1
        if(index<0 || index>size-1) {
            throw new NoSuchElementException("index is invalid! System terminated.");
        }

    }

    public void showContents() {
        while(head.getNextNode() != null) {
            System.out.println(head.getPhone()+"---->");
            head = head.getNextNode();
        }
    }

}

【问题讨论】:

    标签: java linked-list


    【解决方案1】:

    如果你想在索引 x 处插入一个节点,你必须这样做, 转到索引 x-1 处的节点,将节点 x-1 的下一个值存储在临时变量中,将要插入的节点放入 x-1 节点的下一个属性中,并将值放入临时变量中要插入的节点的 next 属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      • 2015-06-04
      • 1970-01-01
      • 2012-06-26
      • 2014-06-08
      相关资源
      最近更新 更多