Home > front end >  Swaping elements in likendlist for a few times java
Swaping elements in likendlist for a few times java

Time:04-29

I am trying to swap elements but the cycle is infinite. The problem is in method change() or find().
I am just learning JAVA, so its hard to me to understand where is the mistake.

import java.util.*;

public class CustomLinkedList {
    class Node{
        int element;
        Node next;
        
        Node(int element){ 
            this.element = element;
        }   
    }
    private Node first;
    private Node last;
    private int size;
    private Node temp;
    
    
    public void add(int element) {
        Node newNode = new Node(element);
        if(first == null) {
            first = last = newNode;
        } else {
            last.next = newNode;
            last = newNode;
        }
        size  ;
    }
    
    public void output() {
        Node current = first;
        if(current == null) {
            System.out.println("YOUR LIST IS EMPTY!!! ADD SOME ELEMENTS!!!");
        }
        else {
            while(current != null) {
                System.out.print("[" current.element  "]"   " ");
                current = current.next;
            }
            System.out.println();
        }
    }
    public Node find(int index) {
        Node current = first;
        for(int i = 0; i < index; i  ) {
            current = current.next;
        }
        return current;
    }
    public void change(int index, int k) {
        
        int count = 0;
        
        while(count < k) {
            Objects.checkIndex(index, size);
            
            Node our = find(index);
            temp = our.next;
            our.next = our;
            our = temp;
            
            our = null;

            count  ;
            index  ;
        }
    }
}

Maybee, someone know how to do this.

CodePudding user response:

When you swap two elements in a linked list, you actually need to modify up to three elements. For example, look at the following sub-list:

... -> a -> b -> c -> d -> ...

If you swap c and b, you get:

... -> a -> c -> b -> d -> ...

Now, the following has changed:

  • The next of a is now c (it was b).
  • The next of b is now d (it was c).
  • The next of c is now b (it was d).

Three nodes changed. Your code in the change() method only modifies two nodes, it forgets about node a.

Update

Possible implementation of a swap method:

/**
 * Swaps the elements at the i-th and the (i-1)-th indices.
 */
public void swap(int index) {
    Objects.checkIndex(index, size - 1);
    if (index == 0) {
        // Before swap: first -> x -> y
        Node oldFirst = first;
        Node x = first.next;
        Node y = x.next;

        // After swap: x -> first -> y
        x.next = first;
        first.next = y;
        first = x;

        // Update `last` if last two elements swapped
        if (size == 2) {
            last = oldFirst;
        }
    } else {
        // Before swap: a -> b -> c -> d
        Node a = find(index - 1);
        Node b = a.next;
        Node c = b.next;
        Node d = c.next;

        // After swap: a -> c -> b -> d
        a.next = c;
        b.next = d;
        c.next = b;

        // Update `last` if last two elements swapped
        if (index == size - 2) {
            last = b;
        }
    }
}

CodePudding user response:

//node at index starts pointing to itself
our.next = our;
//pointer to node at index now points to next of node at index
our = temp;
//starts pointing to null
our = null;
  •  Tags:  
  • java
  • Related