Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
576 views
in Technique[技术] by (71.8m points)

database - Swap two adjacent elements in a list by adjusting only the links (and not the data) using a singly-linked list

This program swaps the adjacent elements of a singly linked list without swapping the data. When I run the program the lists aren't printing out. I am not sure if there's something wrong with the swap function or just the print function itself.

class SingleSwap {
    int data;
    SingleSwap next;
    SingleSwap(int num) {
        data = num;
        next = null;
    }
}

class Single_LinkedList {
    SingleSwap head; //create head of list

    public void swap(int m, int n) {
        SingleSwap currentX = head;
        while (currentX != null && currentX.data != m) { //checks to see if head is empty and that
            currentX = currentX.next;                    //m is not already equal to the entry
        }                                                //if not, data is stored in .next node

        SingleSwap currentY = head;
        while (currentY != null && currentY.data != n){ //checks the same information for n
            currentY = currentY.next;
        }

        assert currentX != null;
        SingleSwap temp = currentX.next; //swap the x and y pointers
        assert currentY != null;
        currentX.next = currentY.next;
        currentY.next = temp;
    }

    public void push_single(int data) {
        SingleSwap Node = new SingleSwap(data); //new node for data
        Node.next = head; //make new node's next as head
        head = Node; //update the head of the list
    }

    public void print(){
        SingleSwap Node = head;
        while (Node != null) { //while the list is not empty, iterate over the list and print values
            System.out.print(Node.data+" ");
            Node = Node.next;
        }
    }

    public static void main(String[] args) {
        Single_LinkedList list = new Single_LinkedList();
        list.push_single(0);
        list.push_single(1);
        list.push_single(3);
        list.push_single(4);
        list.push_single(5);

        System.out.print("
Before swap: ");
        list.print();
        list.swap(3, 4);
        System.out.print("
After swapping: ");
        list.print();
    }
}
question from:https://stackoverflow.com/questions/65861967/swap-two-adjacent-elements-in-a-list-by-adjusting-only-the-links-and-not-the-da

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I just looked through you code a bit and it all has to do with your swap function. Take a deeper look at this section of the code and debug it to see if you are getting expected values.

 assert currentX != null;
    SingleSwap temp = currentX.next; //swap the x and y pointers
    assert currentY != null;
    currentX.next = currentY.next;
    currentY.next = temp;

I am doing something similar to this also through school. Code look clean, just try to figure that pot out and you should be golden. I will look at it some more when I get in from work.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...