链表是由一组
节点
组成的集合。数据结构
实现ide
咱们设计的链表包含两个类。Node类用来表示节点,LinkedList类提供了插入节点、删除节点、显示列表元素的方法,以及一些辅助方法。this
Node类spa
Node类包含连个属性:element用来保存节点上的数据,next用来保存指向下一个节点的连接。操作系统
class Node { constructor (element) { this.element = element this.next = null } }
LinkedList类设计
class LinkedList { constructor () { this.head = new Node('head') } // 查找节点 find (item) { let currNode = this.head while (currNode.element != item) { currNode = currNode.next } return currNode } // 插入 insert (newElement, item) { let newNode = new Node(newElement) let current = this.find(item) newNode.next = current.next current.next = newNode } // 显示链表中的元素 display () { let currNode = this.head while (currNode.next !== null) { console.log(currNode.next.element) } currNode = currNode.next } // 删除 remove () { } }
双向链表code
每个节点有一个previous指向前一个节点队列
循环链表进程
建立的时候须要element
this.head.next = this.head
插入元素以后,链表的末端指向链表的头部。
队列是一种列表,不一样的是队列只能在队尾插入元素,在队首删除元素。队列是一种先进先出的数据结构。
用途:
实现
class Queue { constructor () { this.dataStore = [] } // 向队尾添加一个元素 enqueue (element) { this.dataStore.push(element) } // 向队首删除一个元素 dequeue () { return this.dataStore.shift() } // 读取队首的元素 front () { return this.dataStore[0] } // 读取队尾的元素 back () { return this.dataStore[this.dataStore.length - 1] } }