如何修改链表中的追加(append)和插入(insert)操作?

huangapple 未分类评论49阅读模式
英文:

How do i Modify the append and insert operations in a linked list?

问题

以下是翻译后的内容:

class Node {
    Node next;
    int num;

    public Node(int val) {
        num = val;
        next = null;
    }
}

public class LinkedList {
    Node head;

    public LinkedList(int val) {
        head = new Node(val);
    }

    public void append(int val) {
        Node tmpNode = new Node(val);
        tmpNode.num = val;
        tmpNode.next = head;
        head = tmpNode;
    }

    public void insert(int val) {
        Node currentNode = head;
        Node nextNode = head.next;

        if (currentNode.num < val) {
            Node tmpNode = head;
            head = new Node(val);
            head.next = tmpNode;
            return;
        }
        if (nextNode != null && nextNode.num < val) {
            currentNode.next = new Node(val);
            currentNode.next.next = nextNode;
            return;
        }
        while (nextNode != null && nextNode.num < val) {
            currentNode = nextNode;
            nextNode = nextNode.next;
        }
        currentNode.next = new Node(val);
        currentNode.next.next = nextNode;
    }

    public void delete(int val) {
        Node prevNode = null;
        Node currNode = head;

        if (head.num == val) {
            head = head.next;
            return;
        }
        while (currNode != null && currNode.num != val) {
            prevNode = currNode;
            currNode = currNode.next;
        }
        if (currNode == null) {
            System.out.println("A node with that value does not exist.");
        } else {
            prevNode.next = currNode.next;
        }
    }

    public void print() {
        Node tmpNode = head;

        while (tmpNode != null) {
            System.out.print(tmpNode.num + " -> ");
            tmpNode = tmpNode.next;
        }

        System.out.print("null" + "\n");
    }

    public static void main(String[] args) {
        LinkedList myList = new LinkedList(2);
        myList.append(5);
        myList.insert(10);
        myList.insert(4);
        myList.print();
    }
}

请注意,我只是按照你的要求翻译了代码部分,没有包含其他的内容。如果你有任何问题或需要进一步的帮助,请随时提问。

英文:

Hello i have an Assignment to modify a java program :

1- Makeing the Append operation be always in the front of the List :

LinkedList myList = new LinkedList(5);

myList.append(7);
myList.print();

it should be like this :

7 -> 5 -> null

instead of :

5 -> 7 -> null

2- Insert operation will be always as follows: any new added element using insert operation
should be after the current maximum element in the List

LinkedList myList = new LinkedList(2);

myList.append(5); 
myList.insert(1);
myList.insert(4);
myList.print();

it should be :

5 -> 4 -> 1 -> 2 -> null

instead of :

5 -> 4 -> 2 -> 1 -> null

here is the code

class Node {

    Node next;
    int num ;
    public Node(int val) {

        num = val;
        next = null;
    }
}

public class LinkedList {

    Node head;

    public LinkedList(int val) {

        head = new Node(val);
    }

    public void append(int val) {
        Node tmpNode = new Node(val);
        tmpNode.num = val;
        tmpNode.next = head;
        head  = tmpNode;
    } 

    public void insert(int val) {
        Node currentNode = head;
        Node nextNode = head.next;

        if (currentNode.num &lt; val) {

            Node tmpNode = head;
            head = new Node(val);
            head.next = tmpNode;
            return;
        }
        if (nextNode != null &amp;&amp; nextNode.num &lt; val) {

            currentNode.next = new Node(val);
            currentNode.next.next = nextNode;
            return;
        }    
        while (nextNode != null &amp;&amp; nextNode.num &lt; val) {

            currentNode = nextNode;
            nextNode = nextNode.next;
        }
        currentNode.next = new Node(val);
        currentNode.next.next = nextNode;

    }

    public void delete(int val) {

        Node prevNode = null;

        Node currNode = head;

        if (head.num == val) {

            head = head.next;
            return;
        }
        while (currNode != null &amp;&amp; currNode.num != val) {

            prevNode = currNode;

            currNode = currNode.next;
        }
        if (currNode == null) {

            System.out.println(&quot;A node with that value does not exist.&quot;);
        } else {

            prevNode.next = currNode.next;
        }
    }

    public void print() {

        Node tmpNode = head;

        while (tmpNode != null) {

            System.out.print(tmpNode.num + &quot; -&gt; &quot;);

            tmpNode = tmpNode.next;
        }

        System.out.print(&quot;null&quot; + &quot;\n&quot;);
    }


    public static void main(String[] args) {

        LinkedList myList = new LinkedList(2);

        myList.append(5); 
        myList.insert(10);
        myList.insert(4);
        myList.print();
    }
} 

I have tried to do it but i am not sure i did it right

Thanks for your time !

huangapple
  • 本文由 发表于 2020年4月11日 00:46:59
  • 转载请务必保留本文链接:https://java.coder-hub.com/61144813.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定