//1.追加方法 append
LinkedList.prototype.append=function(data){// 先判断是否添加的是第一个节点//先创建一个新的节点let newNode =newNode(data);if(this.length ==0){this.head = newNode;}else{//当前指针指向的是第上一个节点,将他赋给变量currentlet current =this.head;//判断current的next是不是为空,要是为空就是最后一个节点了 while(current.next){
current = current.next
}//让最后一个节点的next等于新的节点
current.next = newNode;}//3.让链表的长度+1this.length +=1;}
2.toString()方法
主要是获取链表中每个元素
从head开始
循环遍历每一个节点,并且去除里面的element,拼接成字符串
返回最终字符串
//2.toString方法
LinkedList.prototype.toString=function(){//1.先定义一个变量let current =this.head;//定义一个接受字符串let resultString ='';//2.循环获取一个个的节点while(current){
resultString += current.data +' ';
current = current.next;}return resultString;}
3.insert()
传入position
传入data
//3.insert方法
LinkedList.prototype.insert=function(position, data){//1.对position进行越界判断//2.长度判断if(position <0|| position >this.length){returnfalse;}//3.根据data创建节点let newNode =newNode(data);//4.插入数据//判断插入的位置是否是第一个if(position ==0){
newNode.next =this.head;this.head = newNode;}else{let index =0;let current =this.head;let previous =null;while(index ++< position){
previous = current;
current = current.next;}
newNode.next = current;
previous.next = newNode;}//5.length ++this.length +=1;returntrue;}
4.get方法
传入一个position信息
找到相应的数据并且返回
//4.get方法
LinkedList.prototype.get=function(position){//1.越界判断if(position <0|| position >=this.length){returnfalse;}//2.获取对应的datalet current =this.head;let index =0;while(index++< position){
current = current.next;}return current.data;}
5.indexOf方法
传入某个元素data
返回元素在链表中的位置,没有的话返回-1
//5.indexOf方法
LinkedList.prototype.indexOf=function(element){//1.定义变量let current =this.head;let index =0;//2.开始查找while(current){if(current.data == element){return index;}
current = current.next;
index +=1}//3.找到最后没有找到,返回-1}
6.updata方法
//6.updata方法
LinkedList.prototype.updata=function(position, newDate){//1.越界判断if(position <0|| position >=this.length){returnfalse;}//2.先查找到正确的节点let current =this.head;let index =0;while(index ++< position){
current = current.next;}//3.修改数据
current.data = newDate;returntrue;}
7.removeAt()方法
传入一个参数position
返回被删除的节点信息
//7.removeAt
LinkedList.prototype.removeAt=function(position){//1.越界判断if(position <0|| position >=this.length){returnnull;}let current =this.head;//2.判断是否删除的是第一个节点if(position ==0){this.head =this.head.next;}else{//3.先根据position找到元素let index =0;let previous =null;while(index++< position){
previous = current;
current = current.next;}//前一个节点的next指向下一个节点的next
previous.next = current.next;}//4.length -1 this.length -=1;return current.data;}
8.remove方法
传入数据data
删除节点返回被删除节点的data
//8.remove方法
LinkedList.prototype.remove=function(data){//1.获取data在列表中的位置let position =this.indexOf(data);//2.根据位置信息,删除节点//3.length - 1//this.length -= 1;returnthis.removeAt(position);}
6.链表完美封装 和 相应测试代码
functionLinkedList(){//属性:至少包含一个head属性,默认指向nullthis.head =null;//记录链表的长度this.length =0;//封装node内部类functionNode(data){this.data = data;this.next =null;}//1.追加方法 append
LinkedList.prototype.append=function(data){// 先判断是否添加的是第一个节点//先创建一个新的节点let newNode =newNode(data);if(this.length ==0){this.head = newNode;}else{//当前指针指向的是第上一个节点,将他赋给变量currentlet current =this.head;//判断current的next是不是为空,要是为空就是最后一个节点了 while(current.next){
current = current.next
}//让最后一个节点的next等于新的节点
current.next = newNode;}//3.让链表的长度+1this.length +=1;}//2.toString方法
LinkedList.prototype.toString=function(){//1.先定义一个变量let current =this.head;//定义一个接受字符串let resultString ='';//2.循环获取一个个的节点while(current){
resultString += current.data +' ';
current = current.next;}return resultString;}//3.insert方法
LinkedList.prototype.insert=function(position, data){//1.对position进行越界判断//2.长度判断if(position <0|| position >this.length){returnfalse;}//3.根据data创建节点let newNode =newNode(data);//4.插入数据//判断插入的位置是否是第一个if(position ==0){
newNode.next =this.head;this.head = newNode;}else{let index =0;let current =this.head;let previous =null;while(index++< position){
previous = current;
current = current.next;}
newNode.next = current;
previous.next = newNode;}//5.length ++this.length +=1;returntrue;}//4.get方法
LinkedList.prototype.get=function(position){//1.越界判断if(position <0|| position >=this.length){returnfalse;}//2.获取对应的datalet current =this.head;let index =0;while(index++< position){
current = current.next;}return current.data;}//5.indexOf方法
LinkedList.prototype.indexOf=function(element){//1.定义变量let current =this.head;let index =0;//2.开始查找while(current){if(current.data == element){return index;}
current = current.next;
index +=1}//3.找到最后没有找到,返回-1}//6.updata方法
LinkedList.prototype.updata=function(position, newDate){//1.越界判断if(position <0|| position >=this.length){returnfalse;}//2.先查找到正确的节点let current =this.head;let index =0;while(index++< position){
current = current.next;}//3.修改数据
current.data = newDate;returntrue;}//7.removeAt
LinkedList.prototype.removeAt=function(position){//1.越界判断if(position <0|| position >=this.length){returnnull;}let current =this.head;//2.判断是否删除的是第一个节点if(position ==0){this.head =this.head.next;}else{//3.先根据position找到元素let index =0;let previous =null;while(index++< position){
previous = current;
current = current.next;}//前一个节点的next指向下一个节点的next
previous.next = current.next;}//4.length -1 this.length -=1;return current.data;}//8.remove方法
LinkedList.prototype.remove=function(data){//1.获取data在列表中的位置let position =this.indexOf(data);//2.根据位置信息,删除节点//3.length - 1this.length -=1;returnthis.removeAt(position);}}//测试代码//创建一个LinkListlet list =newLinkedList();
list.append('abc');
list.append('123');
list.append('nba');
list.append('NBA');
list.insert(2,'KJH')
console.log(list);
console.log(list.get(2));//KJH
console.log(list.indexOf('KJH'));//2
list.updata(0,'000');
list.updata(2,'111');
list.removeAt(2)
console.log(list.removeAt(1))//123
console.log(list.remove('NBA'))
console.log(list)