zoukankan      html  css  js  c++  java
  • javascript数据结构-链表

    gihtub博客地址

    链表 是一种物理存储单元上非连续、非顺序的存储结构,它既可以表示线性结构,也可以用于表示非线性结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结点可以在运行时动态生成。每个结点包括两个部分:一个是存储数据元素的数据域,另一个是存储下一个结点地址的指针域。

    function LinkedList() {
    	function Node(element) {
    		this.element = element;
    		this.next = null;
    	}
    	var length = 0;
    	var head = null;
    	this.append = function(element) {
    		var node = new Node(element);
    		if (!head) {
    			head = node;
    		} else {
    			var current = head;
    			while (current.next) {
    				current = current.next;
    			}
    			current.next = node;
    		}
    		length++;
    	};
    	this.insert = function(element, position) {
    		if(!position || !element){
    			return false;
    		}
    		var current = head,
    			previous = null,
    			index = 0;
    		var node = new Node(element);
    		if(position >= 0 && position <= length-1){
    			if(position == 0){
    				node.next = current;
    				head = node;
    			}else{
    				while(index++ < position){
    					previous = current;
    					current = current.next;
    				}
    				node.next = current;
    				previous.next = node;
    			}
    			length++;
    		}
    	};
    	this.removeAt = function(position) {
    		var current = head,
    			previous = null,
    			index = 0;
    		if(position >= 0 && position <= length-1){
    			if(position == 0){
    				head = current.next;
    			}else{
    				while(index++ < position){
    					previous = current;
    					current = current.next;
    				}
    				previous.next = current.next;
    			}
    			length--;
    			return current.element;
    		}
    	};
    	this.remove = function(element) {
    		var index = this.indexOf(element);
    		return this.removeAt(index);
    	};
    	this.indexOf = function(element) {
    		var current = head,
    			index = 0;
    		while(current){
    			if(element == current.element){
    				return index;
    			}
    			index++;
    			current = current.next;
    		}
    		return -1;
    	};
    	this.isEmpty = function() {
    		return length == 0;
    	};
    	this.size = function() {
    		return length;
    	};
    	this.getHead = function() {
    		return head.element;
    	};
    	this.toString = function() {
    		var current = head;
    		var s = "";
    		while(current){
    			s += (","+current.element);
    			current = current.next;
    		}
    		return s.substr(1);
    	};
    	this.print = function() {
    		console.log(this.toString())
    	};
    }
    

      

  • 相关阅读:
    HDU 4725 The Shortest Path in Nya Graph(优先队列+dijkstra)
    POJ 3216 Repairing Company(二分图匹配)
    POJ 3414 Pots(bfs打印路径)
    POJ 3278 Catch That Cow(bfs)
    poj 3009 curling2.0 (dfs)
    用“道”的思想解决费用流问题---取/不取皆是取 (有下界->有上界) / ACdreamoj 1171
    hdu2448 / 费用流 / harbin赛区c题
    大数类模板(+-*/%等等)
    hdu4619 / 最大独立集
    hdu4888 多校B 最大流以及最大流唯一判断+输出方案
  • 原文地址:https://www.cnblogs.com/donglegend/p/6043443.html
Copyright © 2011-2022 走看看