数据结构 - 链表

news/2024/7/3 5:53:54

链表

数据结构 - 链表。

// 链表实现
// ES5
var linkedList = 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;
      }
      // while循环执行完后,current已经是链表的最后一项了
      current.next = node;
    }
    length++;
  };

  // 链表某个位置添加元素
  this.insert = function(position, element) {
    // 越界
    if (position > -1 && position < length) {
      var node = new Node(element);
      var current = head;
      // 在链表头添加
      if (position == 0) {
        head = node;
        head.next = current;
      // 在其他位置添加
      } else {
        var index = 0;
        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) {
      var current = head;
      if (position == 0) {
        head = current.next;
      } else {
        var index = 0;
        var previous = null;
        while(index < position) {
          previous = current;
          current = current.next;
          index++;
        }
        // while循环执行完后,index == position
        previous.next = current.next;
      }
      length--;
      return current;
    }
    return null;
  };

  // 获取元素在链表中的位置
  this.indexOf = function(element) {
    var index = 0;
    var current = head;
    while (current) {
      if (current.element == element) {
        return index;
      }
      current = current.next;
      index++;
    }
    return -1;
  };

  // removeAt(position) 删除某个位置的元素
  // indexOf(element)   获取某个元素的位置
  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;
  };
};

var l = new linkedList();
l.append(1);
l.append(2);
l.append(3);

l.insert(1, 10);

http://www.niftyadmin.cn/n/4253829.html

相关文章

数据结构 - 集合

集合数据结构 - 集合&#xff0c;不重复。 // 集合实现 // ES5 var MySet function() {var items {};// 检查元素是否存在this.has function(value) {return items.hasOwnProperty(value);};// 添加元素this.add function(value) {// 集合不重复 - 先检查元素是否存在if (!…

libuv定时器的使用

2019独角兽企业重金招聘Python工程师标准>>> 定时器有这么以下几个函数: int uv_timer_init(uv_loop_t *, uv_timer_t *handle); int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat); int uv_timer_stop(uv_timer_t *hand…

手工杀掉传奇终结者变种

EXERT.EXE是病毒文件 病毒发作现象及危害&#xff1a; 该病毒是一个可以在WIN9X/NT/2000/XP等操作系统上运行的盗号木马。病毒会强行终止多种杀毒软件的进程&#xff0c;使其不能正常运行。它会频繁检查游戏客户端的窗口&#xff0c;如果窗口存在&#xff0c;就会取得当前鼠标…

STL源代码剖析 容器 stl_vector.h

本文为senlie原创。转载请保留此地址&#xff1a;http://blog.csdn.net/zhengsenlie vector ---------------------------------------------------------------------- 描写叙述&#xff1a; 1.迭代器 vector 维护的是一个连续线性空间。它的迭代器是普通指针。 能满足 Random…

数据结构 - 字典

字典数据结构 - 字典&#xff0c;可重复。 // 字典实现 // ES5 var Dictionary function() {var items {};// 检查键是否存在this.has function(key) {// return items.hasOwnProperty(key);return key in items;};// 添加元素this.set function(key, value) {items[key] …

手工删除update.exe病毒

1.update.exe在启动项里启动,文件位置在把C:/Program Files/Common Files/UPDATE下,包括把update.dat和update.exe,删除 2.在其他系统文件夹下C:/WINDOWS和C:/WINDOWS/SYSTEM32下查找可疑文件&#xff0c;可疑文件是任意生成的...例如up.dll、UPDATE.exe、spted.dll等 ,删除 …

python排序出现的问题以及解决方案

对某个文件夹中的文件重命名的时候&#xff0c;发现有些文件丢失&#xff0c;代码如下&#xff1a; #codinggbk # Findthe every dir, if 01.rm exist in it, then rename it. #!/usr/bin/python Utilitiesof file & directories. import os import re import strin…

直线栅格化(基于 Bresenham 算法)

直线栅格化在计算机图形处理中非常常用&#xff0c;看了看网上的介绍比较常用的就是Bresenham 算法&#xff0c;搜索了下网上的算法&#xff0c;试了试&#xff0c;有的有问题&#xff0c;自己写了一个&#xff0c;测试通过。 #include <stdio.h> #include <stdlib.h&…