交换方法无法交换两个相邻节点(单个链接列表)

时间:2020-07-05 17:45:15

标签: java algorithm data-structures logic

以下是我的控制台日志输出...用户输入要添加的整数,以及他们希望切换的元素的索引:

输入要添加的整数:4
输入另一个要添加的整数:5
输入另一个要添加的整数:6
输入另一个要添加的整数:7
输入另一个要添加的整数:8
双向链表的节点:
4 5 6 7 8
输入您要交换的元素的索引:2
双向链表的节点:
4 5 6 7 8

...但是,最后一行当然应该是45768

package ass1Q2A;

public class SLList {

    public SLNode head = null;
    public SLNode tail = null;

    public void add(int a) { //adds new element to our single-linked list.
        SLNode newNode = new SLNode(a);

        if (head == null) {
            head = newNode;
            tail = newNode;
        } else {
            tail.next = newNode;
            tail = newNode;
        }
    }

    //THIS SLSWAP METHOD IS WHERE I THINK THE ERROR IS:

    public void SLswap(int i) throws illegalOperation { //i is index of node to change. method swaps specified node of index "i" with following node.
        if(i>=(size()-1))
            throw new illegalOperation("must swap one of the nodes, OR there does not exist node after tail");

        if (size() < 2)
            throw new illegalOperation("cant swap if theres less than two elements");

        else if (size() == 2) {
            SLNode placeholder = tail;
            tail = head;
            head = placeholder;
        } // end else if

        else { //LOGICAL ERROR MUST EXIST IN THIS ELSE STATEMENT
            SLNode iterationNode = head;
            for (int j = 0; j<i; i++) {//order of pointing: iterationNode => iterationNode.next => nodeB => iterationNode.next.next.NEXT
                if (j==(i-1)) { //GOAL: swap iterationNode.next with nodeB(iteraitonNode.next.next)
                    SLNode nodeB = iterationNode.next.next;
                    iterationNode.next.next = iterationNode.next.next.next;
                    nodeB.next = iterationNode.next;
                    iterationNode.next = nodeB;
                    break;
                }
                iterationNode = iterationNode.next; //iterate to next node
            } // end for-loop
        }

    }

    public int size() {//returns number of nodes in SingleLinkedList
        SLNode u = head;
        int size = 0;
        while (u != null) {
            size++;
            u = u.next;
        }
        return size;
    }
    
     public void display() {  //prints values of all nodes in SLL, starting from head 
            SLNode current = head;  
            if(head == null) {  
                System.out.println("List is empty");  
                return;  
            }  
            System.out.println("Nodes of doubly linked list: ");  
            while(current != null) {  
                //Prints each node by incrementing the pointer.  
      
                System.out.print(current.data + " ");  
                current = current.next;  
            }  
        }
    
    
     
     
     public static void main(String[] args) {  
         
          //EVERYTHING WORKS BUT SWAP METHOD... LOGICAL ERROR?
            SLList myList = new SLList();  
            //Add nodes to the list  
            int toAdd1 = IOHelper.getInt("enter integer to add:");
            int toAdd2 = IOHelper.getInt("enter another integer to add:");
            int toAdd3 = IOHelper.getInt("enter another integer to add:");
            int toAdd4 = IOHelper.getInt("enter another integer to add:");
            int toAdd5 = IOHelper.getInt("enter another integer to add:"); //completed prompting user for input data for SinglyLinkedList
            myList.add(toAdd1);  
            myList.add(toAdd2);  
            myList.add(toAdd3);  
            myList.add(toAdd4);
            myList.add(toAdd5); //completed adding user-inputted data to SinglyLinkedList
            myList.display(); //Displays the nodes present in the SLList before any swapping takes place
            
            int indexToSwap = IOHelper.getInt("enter the index of the element that you would like to swap:"); //prompt user for index he/she wishes to swap
            try {  
                try {
                    
            myList.SLswap(indexToSwap);  //call SLSwap with user-inputted index
                
                }catch(NullPointerException i) {};
            }catch(illegalOperation e) {};
            
            myList.display(); //display list after the switch
        } 
}

public class SLNode {
    int data; //data that a node holds
    SLNode next; // next node in the SSList that this given node will point to
    
    public SLNode(int data) {
        this.data = data;
        this.next = null;
    }
}

0 个答案:

没有答案
相关问题