您的位置:首页 > Web前端 > JavaScript

[Javascript Data Structures] LinkedList 链表

2015-12-02 19:35 686 查看

(1) 单向链表

就是对象之间的连接, 对象的属性有data & next指针(指向下一个node)

初始化head=null;

length=0; // 链表长度为0

var length=0;//链表长度为0
var head=null;//没有头结点




[1-1]加入Node

//add data into the linkedlist
this.append=function(data){
var node=new Node(data);
if (head===null) {
//如果没有head 那么将node作为头结点
head=node;

}else{
var current=head;
//从头结点开始找到最后能插入的位置
while(current.next){//true
current=current.next;
}//找到了最后的位置
current.next=node;
}
length++;
}




[1-2] 完整代码

<script type="text/javascript">
function LinkedList(){
//初始化节点
function Node(data){
//节点有 数据和指针
this.data=data;
this.next=null;
}
var length=0;//链表长度为0 var head=null;//没有头结点

//[1] print the linkedlist
this.print=function(){
if (head===null) {
console.log("LinkedList without data");
}else{
//当前指针指向head
var current=head;
while(current){//true
console.log(current.data);
//指向下一个node
current=current.next;
}
}
}

//[2] add data into the linkedlist
this.append=function(data){
var node=new Node(data);
if (head===null) {
//如果没有head 那么将node作为头结点
head=node;

}else{
var current=head;
//从头结点开始找到最后能插入的位置
while(current.next){//true
current=current.next;
}//找到了最后的位置
current.next=node;
}
length++;
}

//[3] remove the data from the linkedlist
this.remove=function(index){
//index 是从0-length-1
//如果越界了或者不是数字
if (typeof index!='number'||index<0||index>length) {
console.log("Not a number || Beyond the scope");
}else{
var current=head;//指针从头开始

if (index===0) {
head=current.next;
}else{
for (var i = 1; i < length; i++) {
if (index===i) {//找到了要替换的位置
current.next=current.next.next;
}else{
current=current.next;//指向下一个位置
}
}
}
length--;
}
}

//[4] insert the node
this.insert=function(index,data){
var node=new Node(data);

if (typeof index!='number'||index<0||index>length) {
console.log("Not a number || Beyond the scope");
}else{
var current=head;
//要插入头后面
if (index===0) {
node.next=current;
head=node;//新加入点 现在为新的head
}else{
for (var i = 1; i < length; i++) {
if (index===i) {
node.next=current.next;
current.next=node;
}else{
current=current.next;
}
}
}
4000

length++;
}
}

//[5] get指定位置值
this.getindex=function(index){
if (typeof index!='number'||index<0||index>length) {
console.log("Not a number || Beyond the scope");
}else{
var current=head;
if (index===0) {
console.log(current.data);
}else{
for (var i = 1; i < length; i++) {
if (index===i) {
console.log(current.next.data);
}else{
current=current.next;
}
}
}
}
}

//[6] this.getsize=function(){
console.log("the size of linkedlist:"+length);
}

}

//Test
var linkedlist=new LinkedList();
console.log("\nadd data:");
linkedlist.append("1jessica");
linkedlist.append("2krystal");
linkedlist.append("3yoonA");
linkedlist.append("4ljy");
linkedlist.print();

console.log("\nremove data:");
linkedlist.remove(0);
linkedlist.print();

console.log("\ninsert data:");
linkedlist.insert(2,"GG");
linkedlist.print();

console.log("\nget data:");
linkedlist.getindex(1);

console.log("\nget size:");
linkedlist.getsize();
</script>


Result:



(2) 双向列表

this.data=data;

this.next=null;

this.prev=null;//*******different

head=null;

length=0;

trail=null;//*******different—save the last .node

<script type="text/javascript">
function DoubleLinkedList(){
function Node(data){
this.data=data;
this.next=null;
this.prev=null;//*******different
}
head=null;
length=0;
trail=null;//*******different---save the last .node

//[1] add the node into the list
this.append=function(data){
var node=new Node(data);

if (head===null) {
//如果没有head 那么将node作为头结点
head=node;
/*different*/
trail=node;
/*different*/

}else{
var current=head;
//从头结点开始找到最后能插入的位置
while(current.next){//true
current=current.next;
}//找到了最后的位置
current.next=node;
trail=node;
}
length++;
}

//[2] print the doublelinkedlist
this.print=function(){
if (head===null) {
console.log("DoubleLinkedList without data");
}else{
//当前指针指向head
var current=head;
while(current){//true
console.log(current.data);
//指向下一个node
current=current.next;
}
}
}

//[3] insert the doublelinkedlist
this.insert=function(index,data){
var node=new Node(data);
if (head===null) {
//新增node = head
head=node;
tail=node;//******* different
}else if(index===0){
//插到head之前 node变为新的head
node.next=head;//node 之后是 head
head.prev=node;//head 之前是node
head=node;//再改变node为head
}else if(index===length){//最后一项
//******* different
trail.next=node;
node.prev=trail;
trail=node;
//******* different
}else if(index>0||index<length){
var current=head.next;
var previous=head;
for (var i = 1; i < length; i++) {
if (index===i) {
//******* different
node.next=current;
previous.next=node;
current.prev=node;
node.prev=previous;
//******* different
break;
}else{
previous=current;
current=current.next;
}
}
}
length++;
}
}

var doublelinkedlist=new DoubleLinkedList();
console.log("\nadd data:");
doublelinkedlist.append("1jessica");
doublelinkedlist.append("2krystal");
doublelinkedlist.print();

console.log("\ninsert data:");
doublelinkedlist.insert(2,"AAAAAA");
doublelinkedlist.print();
</script>


Result:





(3) 循环列表



头尾相连

head.prev=trail;

trail.next= head;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息