zoukankan      html  css  js  c++  java
  • 【数据结构 JavaScript版】- web前端开发精品课程【红点工场】 --javascript-- 链表实现

    <!DOCTYPE html>
    <html>
    
    <head>
        <title></title>
    </head>
    
    <body>
        <script>
        //【数据结构 JavaScript版】- web前端开发精品课程【红点工场】 --javascript-- 链表实现
    
        // 对象存储的方式
    
    
        var linkSheet = function() {
            var head = null;
            var length = 0;
            var Node = function(element) {
                this.element = element;
                this.next = null;
            }
    
            // 链表尾部添加元素
            this.append = function(element) {
                var node = new Node(element)
                if (head == null) {
                    head = node;
                } else {
                    var current = head;
                    while (current.next) {
                        current = current.next;
                    }
                }
                length++;
            }
    
            //链表插入元素
            this.insert = function(position, element) {
                if (position >= -1 && position < length) {
                    var node = new Node(element);
                    if (position == 0) {
                        var current = head;
                        head = node;
                        head.next = current;
                    } else {
                        var index = 0;
                        var current = head;
                        var previous = null;
                        while (index < position) {
                            previous = current;
                            current = current.next;
                            index++;
                        }
                        previous.next = node;
                        node.next = current;
                    }
                    length++;
                }
            }
    
    
            // 移除下标元素
            this.removeAt = function(position){
            	if(position>-1&&position<length){
            		if(position==0){
            			var current = head;
            			head = current.next;
            		}
            		else{
            			var current = head;
            			var previous = null;
            			var index = 0;
            			while(index<position){
            				previous = current;
            				current = current.next;
            				index++;
            			}
            			previous.next= current.next;
            		}
            		length--;
            		return current;
            	}
            	return null;
            }
    
            // 获取元素下标
            this.indexOf = function(element){
            	var current = head;
            	var index = 0;
            	while(current){
            		if(current.element==element){
            			return index;
            		}
            		current= current.next;
            		index++;
            		return index;
            	}
            	return -1;
            }
    
           // 移除元素
           this.remove = function(element){
               return this.removeAt(this.indexOf(element));
           }
    
           // 判断为空
           this.isEmpty = function(){
           	return length==0;
           }
    
           // 获取长度
           this.size = function(){
           	return length;
           }
    
    
           // 返回头部
    
           this.getHead = function(){
           	return head;
           }
    
    
        }
        </script>
    </body>
    
    </html>
    

      

  • 相关阅读:
    composer 安装laravel
    面向对象
    dedecms常用标签
    Illegal modifier for parameter *** , only final is permitted”
    import static和import的区别
    <<Effective Java>> 第四十三条
    java valueOf
    linux的shell编程
    DFA和NFA的区别
    发现一个正则网
  • 原文地址:https://www.cnblogs.com/SunlikeLWL/p/10124318.html
Copyright © 2011-2022 走看看