一、链表
1、概念
??链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。(即在数据逻辑上是线性的),它的每个结点由两个域组成:①存储数据元素的数据域。②存储下一个结点地址的指针域。
2、特点
??①和线性表相比,链表在添加和删除结点上的效率更高,只需要修改指针信息即可完成操作,不用像线性表(数组)那样移动元素。链表操作的时间复杂度仅为O(1)。
??②因为链表在内存中不是连续存储的,所以可以充分利用内存中的碎片空间。
??③链表的长度在理论上是无限的,可以动态变化长度。
??④哈希表是基于链表来实现的
二、链表类型
1、单链表
function LinkedList(){
let Node=function (element){
this.element = element
this.next = null
}
let head = new Node('head')
this.findLast = function (){
let currNode = head
while (currNode.next){
currNode = currNode.next
}
return currNode
}
this.append = function (element){
let newNode = new Node(element)
let currNode = this.findLast()
if(!head){
head = newNode
}else{
currNode.next = newNode
}
}
this.find = function(item){
let currNode = head
while(currNode.next&&currNode.element!=item){
currNode = currNode.next
}
return currNode
}
this.insert = function(item, element){
let itemNode = this.find(item)
if(!itemNode){
return
}
let newNode = new Node(element)
newNode.next = itemNode.next
itemNode.next = newNode
}
this.remove = function(item){
let currNode = head
while(currNode.next.element!==item){
if(!currNode.next){
return
}
currNode = currNode.next
}
currNode.next=currNode.next.next
}
this.display = function () {
let result = []
let currNode = head
while (currNode ) {
result.push(currNode.element)
currNode = currNode.next
}
return result
}
}
let myList = new LinkedList()
let arr = [1, 4, 5, 2, 3]
for (i of arr) {
myList.append(i)
}
console.log(myList.display());
console.log(myList.find(5));
console.log(myList.findLast());
console.log(myList.remove(4));
console.log(myList.display());
2、双链表
function doubleLinkedList(){
let Node = function (element){
this.element = element
this.next = null
this.previous = null
}
let head = new Node('head')
this.findLast = function(){
let currNode = head
while(currNode.next){
currNode = currNode.next
}
return currNode
}
this.find = function(item){
let currNode = head
while(currNode.element != item){
if(currNode.next == null){
return
}
currNode = currNode.next
}
return currNode
}
this.insert = function(item,element){
let currNode = this.find(item)
let newNode = new Node(element)
if(currNode = null){
return
}
if(currNode.next == null){
currNode.next = newNode
newNode.previous = currNode
}else{
newNode.next = currNode.next
currNode.next.previous = newNode
currNode.next = newNode
newNode.previous = currNode
}
}
this.append = function(element){
let newNode = new Node(element)
let currNode =this.findLast()
if(!head){
head = newNode
}else{
currNode.next = newNode
newNode.previous =currNode
}
}
this.remove = function(item){
let currNode = this.find(item)
if(!currNode){
return
}
if(currNode.next != null && currNode.previous != null){
currNode.previous.next = currNode.next
currNode.next.previous = currNode.previous
currNode.next = null
currNode.previous = null
}else if(currNode.previous == null){
head = currNode.next
currNode.next.previous = null
currNode.previous = null
}else if(currNode.next == null){
currNode.previous.next = null
currNode.previous = null
}
}
this.display = function () {
let result = []
let currNode = head
while (currNode) {
result.push(currNode.element)
currNode = currNode.next
}
return result
}
}
let myList = new doubleLinkedList()
let arr = [1, 4, 5, 2, 3];
for (i of arr) {
myList.append(i)
}
console.log(myList.display());
console.log(myList.find(5))
console.log(myList.remove(4));
console.log(myList.display());
|