【问题标题】:Java LinkedList inserting Object at indexJava LinkedList 在索引处插入对象
【发布时间】:2018-09-22 23:40:57
【问题描述】:

我在理解如何将对象放置在链接中时遇到了一些麻烦。 在这种情况下,如果特定索引处已经有一个对象,它将不会替换它(即用于另一种方法)。我想我无法理解如何访问特定索引、从该索引中检索数据,然后将数据放在那里并连接节点,或者告诉用户那里已经有一个对象。

这是我的代码:

public class CourseList {

    private Coursenode head;
    int currentSize;

    public void insertAtIndex(Course c, int index) {

        Coursenode insert =new Coursenode(c,head); 
        Coursenode temp = new Coursenode();

        if (index > currentSize - 1 || index < 0) {
            throw (new IndexOutOfBoundsException());
        }


        for(int x = 0; x < index; x++) {
            if (insert.getNext()!= null) {
                temp = insert;
                insert.setNext(insert);
                insert.setData(temp.getData());
            }
            if (insert.getNext() == null && x == index) {
                insert.setNext(insert.getNext());
            }
            if (insert.getNext() != null && x == index) {
                System.out.println("There is already a Course at that Index");
            }

        }
    }
}

这是内部类 Coursenode:

public class Coursenode {

    private Course data;
    private Coursenode next;

    public Coursenode() {
        this.data = null;
        this.next = null;
    }

    public Coursenode(Course course, Coursenode next) {
        this.data=course;
        this.next= next;
    }

    public Coursenode(Coursenode x) {
        this.data = x.getData();
        this.next = x.getNext();
    }

    public Course getData() {
        return data;
    }

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

    public Coursenode getNext() {
        return next;
    }

    public void setNext(Coursenode next) {
        this.next = next;
    }

    //Clone method
    public void clone(Coursenode new_cn){ 
         new_cn = new Coursenode (this.getData(),this.getNext());
    }
}

任何想法都将不胜感激,我怀疑我迷失在节点之间的头部引用中,但我无法完全弄清楚如何解决问题。

【问题讨论】:

    标签: java methods linked-list inner-classes singly-linked-list


    【解决方案1】:

    有三种方法(假设为正的index 值)在链表中的索引处进行插入:

    1. 在头部 (index == 0)
    2. 尾巴后(index &gt;= currentSize)
    3. 在中间(在被占用的索引处)(index &gt; 0 &amp;&amp; index &lt; currentSize)

    可能有一种倾向认为在尾部插入是另一种情况,但稍后我们会看到尾部插入与中间插入相同,因为尾部会向前滑动。

    如果插入在头部,则需要将插入节点的next设置为旧的head,然后将head设置为插入节点:

    private void insertAtHead(Course course) {
        Coursenode insertedNode = new Coursenode(c, head);
        head = insertedNode;
    }
    

    如果插入发生在尾部之后,处理这种情况的常用方法是抛出某种异常,例如IndexOutOfBoundsException

    throw new IndexOutOfBoundsException("Cannot insert course after the tail of the course list");
    

    如果插入发生在一个被占用的索引处,则必须将现有节点(以及现有节点之后的所有节点)向前推。这意味着插入节点的next必须设置为当前占用索引的节点,并且当前索引处的节点之前的节点的next必须设置为插入的节点。本质上,插入的节点被融合到列表中。为此,必须遍历列表,直到找到被占用的节点:

    private void insertAtOccupied(Course course, int index) {
        Coursenode previous = null;
        Coursenode current = head;
    
        for (int i = 1; i <= index; i++) {
            // Track the previous and current nodes
            //   previous = node at i - 1
            //   current = node at i
            previous = current;
            current = current.next;
        }
    
        Coursenode insertedNode = new Coursenode(c, current.next);
        previous.next = insertedNode;
    }
    

    将这些案例放在一起,我们可以创建以下逻辑:

    public void insertAt(Course course, int index) {
    
        if (index == 0) {
            insertAtHead(course);
        }
        else if (index >= currentSize) {
            throw new IndexOutOfBoundsException("Cannot insert course after the tail of the course list");
        }
        else if (index > 0 && index < currentSize) {
            insertAtOccupied(course, index);
        }
    }
    

    【讨论】:

    • 我很确定在insertAtOccupied 中对Coursenode 的调用应该是current 而不是current.next,否则该操作是替换,而不是插入。
    【解决方案2】:

    首先在链表中如果

    索引

    那么在链表中已经有一个对象。如果您在 node.next 中有一个空节点,那么您如何知道您已到达链表的末尾。

    public class CourseList {
    
        private Coursenode head;
        int currentSize;
    
        public void insertAtIndex(Course c, int index) {
    
            Coursenode insert =new Coursenode(c,head); 
            Coursenode temp = new Coursenode();
    
            if (index > currentSize - 1 || index < 0) {
                throw (new IndexOutOfBoundsException());
            }
    
            //tempnode = head;
            for(int x=1; x< index;x++) {
             //tempnode = tempnode.next;
            }
            //nodeatindex = tempnode;
            //you can get details of the node
    } 
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2022-12-05
      • 1970-01-01
      • 1970-01-01
      • 2011-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多