简体   繁体   English

对链接列表进行排序?

[英]Sorting this linked lists?

I'm trying to sort a polynomial linked list where the degree is contained in the node. 我正在尝试对节点中包含度的多项式链接列表进行排序。 For example, the poly.term.degrees of the polynomial 5x^2 + 5x + 5 would be 2,1,0. 例如,多项式5x ^ 2 + 5x + 5的多项式度为2,1,0。 Unfortunately I have a method that adds polynomials but returns them in the opposite fashion (5 + 5x + 5x^2) which won't get me credit on this assignment I'm trying to complete, so I need to make a sort method for it. 不幸的是,我有一个添加多项式但以相反的方式(5 + 5x + 5x ^ 2)返回多项式的方法,这对我要完成的作业没有让我功劳,因此我需要为它。 I tried a few times but I can't seem to get the hang of it - passing the head of a linked list through this method returns only the head I passed through and deletes the rest of the nodes. 我尝试了几次,但似乎无法理解它-通过此方法传递链表的头仅返回我经过的头,并删除其余节点。 Can anybody help me out? 有人可以帮我吗?

Alternatively I could use an addToRear method while adding the polynomials instead of an addToFront, but I can't seem to get one working... I posted both my in-progress sort method and in-progress addToRear below, any input would be appreciated! 或者,我可以在添加多项式而不是addToFront的同时使用addToRear方法,但是我似乎无法正常工作...我在下面同时发布了正在进行的排序方法和正在进行的addToRear,任何输入都将不胜感激!

private void sort(Node head)                                        // CURRENTLY BROKEN!!!!!!!!!!
{ 
    Node temp; 
    Node curr = head; 
    while(curr.next != null)
    { 
    if(curr.term.degree < curr.next.term.degree) //either degree is smaller or greater 
        {//swap 
            temp = curr; //save first element
            curr = curr.next; //set first element to second
            temp.next = curr.next; //set next of first to third
            curr.next = temp; //set second element to the first that we saved before
        }
    curr = curr.next; //move to next element
    }
}


private void addToBack(float coeff, int deg, Node head)
{
    if(head==null)
    {
        // System.out.println("List empty, creating new node");
        Node n = new Node(coeff,deg,head);
        head = n;
        System.out.println(head.term.coeff);
    }
    else
    {
        Node n = new Node(coeff,deg,head);
        Node temp = head;
        while(temp.next!=null)
        {
            System.out.println("a");
            temp.next=temp;
        }
        head = n;
    }
}

I'll try to point you on some of your errors. 我会尝试指出一些错误。

  1. Sorting that you try to do (some modification of bubble sort) should have two nested loops. 您尝试执行的排序(对冒泡排序的某些修改)应该具有两个嵌套循环。 Outer loop will be while (changes) , inner loop iterates the elements. 外循环将是while (changes) ,内循环将迭代元素。 When you swap two elements, make changes = true . 交换两个元素时,请进行changes = true

  2. When swapping elements don't try to swap nodes, swap their values ( term ). 当交换元素不尝试交换节点时,请交换其值( term )。 It's as easy as that: 就这么简单:

    Term temp = curr.term; curr.term = curr.next.term; curr.next.term = temp;

  3. In addToBack you have two errors, first, you seem to expect that head will change outside of method after you assign it ( head = n; ), but this is not true, you change only local copy. 在addToBack中,您有两个错误,首先,您似乎期望head在分配方法后会在方法之外更改( head = n; ),但这不是真的,您只更改了本地副本。 Second, in the else branch, instead of head = n; 第二,在else分支中,而不是head = n; there should be temp.next = n; 应该有temp.next = n;

Instead of writing separate method addToBack you may consider constructing your list in place, like that: 除了编写单独的方法addToBack您还可以考虑像这样构造列表:

 head = elementSource.next();
 tail = head;
 while (elementSource.hasNext()) {
   tail.next = elementSource.next();
   tail = tail.next;
 }

Note that elements are added to the tail of the list. 请注意,元素已添加到列表的末尾。 Here I used elementSource instead your actual source of elements just for illustration reasons. 在这里,出于说明原因,我使用elementSource代替了您实际的元素来源。

 if(curr.term.degree < curr.next.term.degree) //either degree is smaller or greater 
    {//swap problem should be here while swapping
        node temp;
        temp=curr;
        curr=curr.next;//curr changes here
        temp.next=curr; //maintaining the link
    }
curr = curr.next; //move to next element

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM