简体   繁体   中英

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. 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. 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!

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. When you swap two elements, make changes = true .

  2. When swapping elements don't try to swap nodes, swap their values ( 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. Second, in the else branch, instead of head = n; there should be temp.next = n;

Instead of writing separate method addToBack you may consider constructing your list in place, like that:

 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.

 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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