javaScriptはチェーンの自動ソートを実現します.

2113 ワード

余計なことを言わないで、直接コードを入れてください.




    
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>



    <script>
        function LinkList() {
            var Node = function (element) {
                this.element = element;
                this.next = null;
            };
  
            var head = null;  //   
            var prev = null;  //       
            var length = 0;

            this.append = function (element) {
 
                let node = new Node(element),
                    current;

                if (head == null) {
                    head = node;
                } else {
                    current = head;
                    prev = this;     //  this    
                    
                    while (current != null && current.element <= node.element) {  //                   
                        prev = current;
                        current = current.next;
                    }
                    node.next = current;
                    prev.next = node;  
                    head = (head == this.next) ? head : this.next;  //              ,                  
                }
            };
            this.traversal = function () {
                let current = head;
                while (current) {
                    console.log(current.element);
                    current = current.next;
                }
            }
        }

        let list = new LinkList();
        list.append(9);
        list.append(7);
        list.append(8);
        list.append(10);
        list.append(11);
        list.append(88);
        list.append(100);
        list.append(16);
        list.append(13);
        list.append(14);
        list.append(15);
        list.append(10);
        list.append(11);
        list.append(99);
        list.append(16);
        list.traversal();
    </script>



</code></pre> 
 </div> 
</div>
                            </div>
                        </div>