数据结构与算法(三):链表

1.概述

链表和数组一样,可以用于存储一系列的元素,但是链表和数组的实现机制完全不同。

先来说说数组:
要存储多个元素,数组可能是常用的数据结构,但它也有许多缺点,如:

  • 需要申请一段连续存储的内存空间,在大多数语言中还需要预先限定大小,所以当数组不能满足容量需求时还需要扩容。
  • 在数组开头或中间位置插入数据成本很高,需要进行大量元素的位移。

所以要存储多个元素,还有一个选择就是链表

相对于数组,它有以下优点:

  1. 内存空间不必是连续的,可以充分利用计算机的内存,实现灵活的内存动态管理。
  2. 不需要创建时就给定大小,可以动态添加删除,甚至无限延申下去
  3. 链表在插入和删除数据时,时间复杂度可以达到O(1),相对数组效率高很多

当然相对数组,它也有缺点:

  1. 每次访问,它只能从头节点开始访问
  2. 不能通过下标直接访问,需要从头一个个访问

2.单向链表

在链表中,常见的有单向链表,顾名思义,它是单向的,如下:
在这里插入图片描述
可见,头节点指向第一个节点,每个节点含有data、next属性,data内保存了节点的数据,next保存了指向的下一个节点的地址。

封装单链表

  <script>
    function linkedList() {
      function Node(data) {
        this.data = data;
        this.next = null;
      }
      this.head = null;
      this.length = 0;

      // 1.在链表末尾添加节点
      linkedList.prototype.appendlist = function (data) {
        let newNode = new Node(data);
        if (this.length === 0) {
          this.head = newNode
          console.log(this.head);
        }
        else {
          let current = this.head;
          while (current.next) {
            current = current.next;
          }
          current.next = newNode;
        }
        this.length += 1;
      }

      // 2.toString
      linkedList.prototype.toSting = function () {
        let str = '';
        let current = this.head;
        while (current) {
          str = str + current.data + ' ';
          current = current.next;
        }
        return str;
      }
      // 3.在某个位置插入节点
      linkedList.prototype.insert = function (position, data) {
        if (position < 0 || position > this.length + 1) return false;
        else {
          let newNode = new Node(data);
          let num = 1;
          let current = this.head;
          while (current) {
            if (position === 1) {
              newNode.next = this.head;
              this.head = newNode;
            }
            else if (position - 1 === num) {
              let temp = current.next;
              current.next = newNode;
              newNode.next = temp;
            }
            num += 1;
            current = current.next;
          }
          this.length += 1;
        }

      }
      // 4.更换某个位置的节点
      linkedList.prototype.update = function (position, data) {
        let newNode = new Node(data)
        let num = 1;
        let current = this.head;
        if (position < 0 || position > this.length + 1) return false;
        while (current) {
          if (num === position - 1) {
            let temp = current.next;
            let tempnext = temp.next;
            current.next = newNode;
            newNode.next = tempnext;
          }
          current = current.next;
          num += 1;
        }
      }
      // 5.查看某个位置的节点
      linkedList.prototype.get = function (position) {
        if (position < 0 || position > this.length + 1) return false;
        let num = 1;
        let current = this.head;
        while (current) {
          if (num === position)
            return current
          current = current.next;
          num += 1;
        }
      }
      // 6.是否包含某个节点,若包含返回是第几个,不包含返回-1
      linkedList.prototype.indexOf = function (data) {
        let num = 1;
        let current = this.head;
        let ishave = false;
        while (current) {
          if (current.data === data) {
            ishave = true;
            return num;
          }
          current = current.next;
          num += 1;
        }
        if (!ishave) return -1;
      }

      // 7.根据位置删除
      linkedList.prototype.removeAt = function (position) {
        if (position < 0 || position > this.length + 1) return false;
        let num = 1;
        let current = this.head;
        while (current) {
          if (num === position - 1) {
            let temp = current.next.next;
            current.next = temp;
          }
          current = current.next;
          num += 1;
        }
        this.length -= 1;
      }
      // 8.根据数据删除
      linkedList.prototype.remove = function (data) {
        let num = 1;
        let current = this.head;
        let before = null;
        while (current) {
          if (current.data === data) {
            let temp = current.next;
            if (num === 1) {
              this.head = temp;
            }
            else {
              before.next = temp
            }

          }

          if (current === this.head) {
            before = this.head
          }
          else {
            before = before.next;
          }
          current = current.next;
          num = num + 1;
        }
        this.length -= 1;
      }

      // 9.是否为空
      linkedList.prototype.isEmpty = function () {
        return this.length === 0;
      }
      // 10.返回长度
      linkedList.prototype.size = function () {
        return this.length;
      }
    }
  </script>

3.双向链表

除了单向链表,常用的还有双向链表,顾名思义,它是双向的,如下:
在这里插入图片描述
可见,头节点指向第一个节点,每个节点含有data、prev、next属性,data内保存了节点的数据,pre 保存了指向的上一个节点的地址,next保存了指向的下一个节点的地址。

封装双向链表

 <script>
    function DoublyLinkedList() {
      // 内部类
      function Node(data) {
        this.data = data;
        this.prev = null;
        this.next = null;
      }
      this.head = null;
      this.tail = null;
      this.length = 0;
      // 添加节点
      DoublyLinkedList.prototype.appendList = function (data) {
        let NewNode = new Node(data);

        if (this.length === 0) {
          this.head = NewNode;
          this.tail = NewNode;
        }
        else {
          let current = this.head;
          while (current.next) {
            current = current.next
          }
          current.next = NewNode
          current.next.prev = current
          this.tail = NewNode
        }

        this.length += 1;
      }
      // 1.toString
      DoublyLinkedList.prototype.toString = function () {
        return this.forwarString();
      }
      // 2.forwardString 正向遍历
      DoublyLinkedList.prototype.forwarString = function () {
        let current = this.head;
        let str = '';
        while (current) {
          str = str + current.data + ' ';
          current = current.next
        }
        return str;
      }
      // 3.backwordString 反向遍历
      DoublyLinkedList.prototype.backwordString = function () {
        let current = this.tail;
        let str = '';
        while (current) {
          str = str + current.data + ' ';
          current = current.prev
        }
        return str;
      }

      // 4.insert插入
      DoublyLinkedList.prototype.insert = function (position, data) {
        let newNode = new Node(data);
        // 1.范围限制
        if (position < 0 || position > this.length) return false;
        let current = this.index;
        if (position === 0) {
          this.head.prev = newNode;
          this.head = newNode
          this.tail = newNode
        }
        else if (position === this.length) {
          this.tail.next = newNode;
          let temp = this.tail;
          this.tail = newNode
          this.tail.prev = temp;
        }
        else {
          let index = 0;
          let current = this.head;
          while (index++ < position) {
            current = current.next;
          }
          current.prev.next = newNode
          newNode.prev = current.prev
          current.prev = newNode;
          newNode.next = current
        }
        this.length += 1;
        return true;
      }
      // 5.get根据位置查数据
      DoublyLinkedList.prototype.getNode = function (position) {
        if (position < 0 || position >= this.length) return false;

        if (position <= (this.lenth / 2 - 1)) {
          let current = this.head;
          let index = 0;
          while (index++ < position) {
            current = current.next;
          }
          return current
        }
        else {
          let current = this.tail;
          let index = this.length - 1;
          while (index-- > position) {
            current = current.prev
          }
          return current
        }

      }
      // 6.indexof查找元素
      DoublyLinkedList.prototype.indexOf = function (data) {
        let current = this.head;
        let index = 0;
        while (current) {
          if (current.data === data)
            return index
          current = current.next;
          index += 1;
        }
        return -1;
      }
      // 7.update更新数据
      DoublyLinkedList.prototype.update = function (position, data) {
        if (position < 0 || position >= this.length) return false;
        let index = 0;
        let current = this.head;
        while (index++ < position) {
          current = current.next;
        }
        current.data = data;
        return true;
      }
      // 8.removeAt根据位置删除
      DoublyLinkedList.prototype.removeAt = function (position) {
        if (position < 0 || position >= this.length) return false;
        let current = this.head;
        if (this.length === 1) {
          this.head = null;
          this.tail = null;
        }
        else {
          if (position === 0) {
            this.head.next.prev = null;
            this.head = this.head.next;
          }
          else if (position === this.length - 1) {
            this.tail.prev.next = null;
            this.tail = this.tail.prev;
          }
          else {
            current = this.getNode(position);
            current.prev.next = current.next;
            current.next.prev = current.prev;
            current.prev = null;
            current.next = null;
          }
        }
        this.length -= 1;
        return current.data
      }

      // 9.remove根据元素名删除
      DoublyLinkedList.prototype.remove = function (data) {
        let position = this.indexOf(data)
        console.log(position);
        this.removeAt(position)
      }
    }
  </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
ava实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),可运行高分资源 Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现的毕业设计&&课程设计(包含运行文档+数据库+前后端代码),Java实现
C语言是一种广泛使用的编程语言,它具有高效、灵活、可移植性强等特点,被广泛应用于操作系统、嵌入式系统、数据库、编译器等领域的开发。C语言的基本语法包括变量、数据类型、运算符、控制结构(如if语句、循环语句等)、函数、指针等。下面详细介绍C语言的基本概念和语法。 1. 变量和数据类型 在C语言中,变量用于存储数据,数据类型用于定义变量的类型和范围。C语言支持多种数据类型,包括基本数据类型(如int、float、char等)和复合数据类型(如结构体、联合等)。 2. 运算符 C语言中常用的运算符包括算术运算符(如+、、、/等)、关系运算符(如==、!=、、=、<、<=等)、逻辑运算符(如&&、||、!等)。此外,还有位运算符(如&、|、^等)和指针运算符(如、等)。 3. 控制结构 C语言中常用的控制结构包括if语句、循环语句(如for、while等)和switch语句。通过这些控制结构,可以实现程序的分支、循环和多路选择等功能。 4. 函数 函数是C语言中用于封装代码的单元,可以实现代码的复用和模块化。C语言中定义函数使用关键字“void”或返回值类型(如int、float等),并通过“{”和“}”括起来的代码块来实现函数的功能。 5. 指针 指针是C语言中用于存储变量地址的变量。通过指针,可以实现对内存的间接访问和修改。C语言中定义指针使用星号()符号,指向数组、字符串和结构体等数据结构时,还需要注意数组名和字符串常量的特殊性质。 6. 数组和字符串 数组是C语言中用于存储同类型数据的结构,可以通过索引访问和修改数组中的元素。字符串是C语言中用于存储文本数据的特殊类型,通常以字符串常量的形式出现,用双引号("...")括起来,末尾自动添加'\0'字符。 7. 结构体和联合 结构体和联合是C语言中用于存储不同类型数据的复合数据类型。结构体由多个成员组成,每个成员可以是不同的数据类型;联合由多个变量组成,它们共用同一块内存空间。通过结构体和联合,可以实现数据的封装和抽象。 8. 文件操作 C语言中通过文件操作函数(如fopen、fclose、fread、fwrite等)实现对文件的读写操作。文件操作函数通常返回文件指针,用于表示打开的文件。通过文件指针,可以进行文件的定位、读写等操作。 总之,C语言是一种功能强大、灵活高效的编程语言,广泛应用于各种领域。掌握C语言的基本语法和数据结构,可以为编程学习和实践打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值