链表有单链表和双链表,概念可百度,这里主要是以js的形式实现。
以下是我大概实现的思路
我们先定义一个类,说明一下这个链表的数据结构
所以我们需要定义节点,节点里面有value(我们存储的值)以及next(指向下个一节点)
function node(value){
this.value = value;
this.next = null; //在没有下一个节点的时候,next总是指向null
}
那现在我们什么不要想那么复杂,一步一步思考,把需要的东西写出来
第一步:要添加节点
- 判断是否为头结点 所以需要一个head变量来标识头结点,当然初始化为空;
- 要跟踪链表的长度 所以需要变量length来标识长度
所以先写添加节点的方法: append(),我们把用到的所有方法写到 singleList()类里面, 作为成员方法
function singleList(){
let head = null; //这定义头结点,并且初始化为null
let Length = 0; //这定义头链表的长度,方便查看当前的长度
this.append = function(value){ //定义增加节点的方法
let currentNode; //定义当前节点
let node = new Node(value); //实例出一新的节点
if(head ===null){
head = node;
Length++;
}else{
currentNode = head;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = node;
Length++;
}
return 'ok';
}
}
第二步:获取节点的长度
- 直接返回Length就可以
function singleList(){
this.getLength = function(){ //直接返回Length
return Length;
}
}
第三步:展示已经添加的节点(数组形式展示)
this.showNode = function(){
let arr = []; //定义数组来放链表的节点值
let currentNode = head;
while(currentNode.next!=null){
arr.push(currentNode.value);
currentNode = currentNode.next;
}
arr.push(currentNode.value);
arr.push(1)
return arr;
}
第四步:删除节点
- 删除的是第几个节点,这里就要指定一个下标index ,(和数组的下标一样从0开始算起的)
- 根据提供的index,找到节点,然后根据下图示例
代码实现如下:
this.delete = function(index){
let currentNode;
currentNode = head;
if(index>-1 && index <Length){ //判断index合法不
if(index == 0){
head = currentNode.next;
}
else{
let current = 0;
while(current<index-1){
currentNode = currentNode.next
current++;
}
currentNode.next = currentNode.next.next; //直接指向下下个
}
Length--; //记得长度要自减
return this.showNode();
}
else{
throw new Error('index超过链表的长度啦!!!')
}
}
第五步:查找节点(根据下标index查找)
实现如下:
this.find = function(index){
let currentNode = head;
if(index>-1 && index <Length){
if(index ==0){
return head.value;
}
else{
let current =0
while(current<index){
currentNode = currentNode.next;
current++;
}
return {"当前节点数据为":currentNode.value};
}
}
}
完整的代码如下:
function singleList(){
function Node(value){
this.value = value;
this.next = null;
}
let head = null;
let Length = 0;
this.append = function(value){
let currentNode;
let node = new Node(value);
if(head ===null){
head = node;
Length++;
}else{
currentNode = head;
while(currentNode.next){
currentNode = currentNode.next;
}
currentNode.next = node;
Length++;
}
return 'ok';
}
this.getLength = function(){
return Length;
}
this.showNode = function(){
let arr = [];
let currentNode = head;
while(currentNode.next!=null){
arr.push(currentNode.value);
currentNode = currentNode.next;
}
arr.push(currentNode.value);
arr.push(1)
return arr;
}
this.delete = function(index){
let currentNode;
currentNode = head;
if(index>-1 && index <Length){
if(index == 0){
head = currentNode.next;
}
else{
let current = 0;
while(current<index-1){
currentNode = currentNode.next
current++;
}
currentNode.next = currentNode.next.next;
}
Length--;
return this.showNode();
}
else{
throw new Error('index超过链表的长度啦!!!')
}
}
this.find = function(index){
let currentNode = head;
if(index>-1 && index <Length){
if(index ==0){
return head.value;
}
else{
let current =0
while(current<index){
currentNode = currentNode.next;
current++;
}
return {"当前节点数据为":currentNode.value};
}
}
}
}