简体   繁体   中英

Java linkedlist - How to copy data?

I'm trying to code a function that moves 'n' amount of nodes in a doubly linkedlist from the front and adds them to the back:

public class PetList {
    protected class LinkNode {
        protected LinkNode next;
        protected LinkNode prev;
        protected Pet animal; // note that my data within a node is an object

        public LinkNode() {
            next = prev = null;
        }
    }
    protected LinkNode first;
    protected LinkNode last;

    public PetList() {
        first = last = null;
    }
//...
//... other functions

public void Rotate(int n) {

}

Now I know how to insert a node to the rear, for example:

public void insertRear(Pet giraffe) {
    LinkNode temp = new LinkNode();
    temp.animal = new Pet(giraffe);
    if(first == null) {
        first = last = temp;
        return;
    }
    temp.prev = last;
    last.next = temp;
    last = temp;
}

But in my Rotate function:

public void Rotate(int n)

I don't have an object as the parameter. That is, how can I create a temp LinkNode and copy the data in the object of the first LinkNode, then move my temp Node to the back, the delete my first node? Or is there another better way of doing this? Thanks for any help.

It should be pretty simple. Here is how you move one node from the head to the tail:

public void moveHeadToTail() {
    if (first == null || last == null) {
        throw new IllegalStateException("can't do that on empty list");
    }
    // Detach the first node
    LinkNode temp = first;
    first = temp.next;
    first.previous = null;
    temp.next = null;

    // Put the tmp node at the end
    last.next = temp;
    temp.prev = last;
    last = temp;
}

Then, you can split that into better functions: popHead and pushTail :

public LinkNode popHead() {
    if (first == null) {
        throw new IllegalStateException("can't do that on empty list");
    }
    LinkNode temp = first;
    first = temp.next;
    first.previous = null;
    temp.next = null;
    return temp;
}

public void pushTail(LinkNode node) {
    if (last == null) {
        throw new IllegalStateException("can't do that on empty list");
    }
    last.next = node;
    node.prev = last;
    node.next = null;
    last = node;
}

Which then makes the rotation easier:

public void moveHeadToTail() {
    pushTail(popHead());
}

Then the modification to get that to work for n should be fairly trivial.

I would chop off a piece of length n nodes from the start of the list, keeping track of both the first and the last node in the chopped off list. Set first to the node following the chopped off sublist to make it the new first node. Then I would append the sublist to the end by providing links both ways between the old last node and the first node in the sublist. Finally set last to the end of the sublist, and I'd be done.

Draw your nodes as boxes on paper and the links as arrows to other boxes. Wipe out and draw to visualize the steps. Then it should all be clearer.

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