如何按顺序在链表中插入节点

时间:2014-01-26 06:12:04

标签: java node.js insert linked-list

今天是我在课堂上学习计算机编程的第三年。我很高兴地说,在这几个月里,我已经取得了好成绩。如果没有本网站的帮助,我不会通过查看其他问题并调整类似的答案来帮助我完成作业和诊断测试。

现在我有另一个问题,我觉得它很重要。今天我已经接受了另一位教授(两周内到期)的任务,涉及员工,双链表和测试,这次我们被允许使用一些快捷方式,但因为大多数方法都是已经写好了,我们被假定使用那些,因为所有这些都是不完整的,我们应该完成它们。

我遇到了关于方法 addInOrder 的问题。该算法旨在获取一个对象并将其作为另一个节点插入其中。虽然我已经重写了它,以便它可以在最后添加节点(如果对象比其他对象大,或者如果列表本身为空),我无法如何添加某些条件以便它可以添加到列表的中间/开头

我已经调试了它,我发现它与我在addInOrder方法中记下的内容有关,因为我已经测试了用于测试的其他方法,它们确实没有错误。

如何重写方法以包含这些条件?

操作系统:Windows 7 Workbench:Eclipse

该类只有2个节点:head,tail。它还使用一个名为Employee的导入类(我已经完成没有错误)。

public void addInOrder(Employee employee) {
    Node Previous = null;
    Node position = head;
    while(position != null){
        if(position.getEmployee().compareTo(employee) > 0){
            this.setHead(new Node(employee,position.getPrevious(), position));
            return;
        }
        Previous = position;
        position = position.getNext();
    }
    if(isEmpty() == true || head == null || Previous.getNext() == null)
        this.add(employee);


}

编辑:这实际上是AddInOrder方法。

    public void testAddInOrder() {
    EmployeeList list = new EmployeeList();
    list.addInOrder(new Employee("d", 12.5));
    list.addInOrder(new Employee("e", 13.5));
    list.addInOrder(new Employee("a", 12.5));
    list.addInOrder(new Employee("g", 13.5));
    list.addInOrder(new Employee("c", 13));
    list.addInOrder(new Employee("a", 12.5));
    list.addInOrder(new Employee("b", 13.5));
    list.addInOrder(new Employee("d", 12.8));
    list.addInOrder(new Employee("b", 13));
    //A12.5 B13 B13 B13.5 C13 D12.5 D12.8 E13.5 G13.5
    assertEquals("a",list.get(0).getName());
    assertEquals("a",list.get(1).getName());
    assertEquals("b",list.get(2).getName());
    assertEquals("b",list.get(3).getName());
    assertEquals("c",list.get(4).getName());
    assertEquals("d",list.get(5).getName());
    assertEquals("d",list.get(6).getName());
    assertEquals("e",list.get(7).getName());
    assertEquals("g",list.get(8).getName());
    assertEquals(12.5, list.get(0).getSalaryRate(), 0.01);
    assertEquals(12.5, list.get(1).getSalaryRate(), 0.01);
    assertEquals(13, list.get(2).getSalaryRate(), 0.01);
    assertEquals(13.5, list.get(3).getSalaryRate(), 0.01);
    assertEquals(13, list.get(4).getSalaryRate(), 0.01);
    assertEquals(12.5, list.get(5).getSalaryRate(), 0.01);
    assertEquals(12.8, list.get(6).getSalaryRate(), 0.01);
    assertEquals(13.5, list.get(7).getSalaryRate(), 0.01);
    assertEquals(13.5, list.get(8).getSalaryRate(), 0.01);
}

这是陪伴它的考验。

3 个答案:

答案 0 :(得分:0)

以下是如何考虑写这个:假设你有一个按顺序排列的列表。如果您知道这是真的,您可以通过推进列表轻松添加项目,直到要插入的项目大于上一项目并且小于或等于下一项目。那时,只需将前一个指向to_insert并将to_insert指向下一个并且你就是好的。如果您这样做,您的列表将保持有序。 要处理初始插入,您可以在循环外部首先采用该情况:如果要插入的项目在第一个项目之前排序,请将其插入第一个项目之前并返回。

您还知道没有项目的列表是有序的,因此您始终可以从排序顺序的列表开始。所以这应该不难 - 只需编写我上面描述的方法。 由于这是一项家庭作业,我不会对你的代码发表评论,但如果你提出具体的问题,我可能会提供一些提示。

答案 1 :(得分:0)

我能想到的方式---->在插入add语句中,遍历列表并找到最佳位置。像

这样的东西
  1. while (tempptr->next != NULL && tempptr->data >= nodelist->data )

  2. if true----> insert in that position

答案 2 :(得分:0)

public void addInOrder(Employee employee){

if (head==null){//if your list is empty just insert it in the beginning
head=new Node(employee);
return;
}//end if(head==null)


if (head.getNext()==null)//if we have one node only{
if(position.getEmployee().compareTo(employee) > 0){//if it should be replaced than put it in the beginning
Node node=new Node(employee);
node.setNext(head);
head=node;
node.getNext().setPrevious(node);
return;
    }
else{
//add it to the end,i am assuming you know how to do that,if not than comment on my answer.
return;
       }
}
else{
Node position=head;
while(position!=null){
if(position.getEmployee().compareTo(employee) > 0){
Node node=new Node(employee);           //you should add the node before position
node.setNext(position);                 //so set the node's next to position
node.setPrevious(position.getPrevious());//set the node's previous to position's previous
position.getPrevious.setNext(node);      //set the next of position's previous to node
return;//and exit
           }
position=position.getNext();//else continue looping through the list
      }
//if we looped through the whole array and we haven't inserted the node so we didn't              exit the method,than add it to the end.
 Node n=new Node(employee);

 position.setNext(new Node(n);
 n.setPrevious(position);
   }


}
相关问题