数据结构学习系列之线性表(五)

前言

单向链表查找下一个元素很方便,要查找上一个元素时,需要从头开始向下遍历,很是麻烦。如何解决这个问题呢?使用双向链表结构可以解决这个问题。

双向链表

在单向链表的基础上,增加一个指向上一个节点的指针,这就形成了双向链表结构。因增加了一个指针域,故需要占用更多的内存空间,换就话说,用空间换时间。现在硬件越来越强,而价格越来越低,这种思想的应用案例越来越普遍。

代码实现

  1 /**
  2  * @desc 双向链表
  3  *
  4  * @date 2015/08/23
  5  * @copyright by WadeYu
  6  */
  7 var DblLinkListAdt = function(){
  8     this.head = this.node(0);
  9     this.init();
 10 };
 11 DblLinkListAdt.prototype.node = function(data){
 12     return {
 13         data:data,
 14         prev:null,
 15         next:null
 16     };
 17 };
 18 DblLinkListAdt.prototype.init = function(){
 19     return true;
 20 };
 21 DblLinkListAdt.prototype.insert = function(i,node){
 22     i = i -1;
 23     if( (i < 0) || (i > this.getLength())){
 24         return false;
 25     }
 26     var pos = 0;
 27     var node1 = this.head,tmp;
 28     while(true){
 29         if(pos == i){//找到第i-1个节点
 30             break;
 31         }
 32         node1 = node1.next;
 33         pos++;
 34     }
 35     node.prev = node1;
 36     node.next = node1.next;
 37     node1.next.prev = node;
 38     node1.next = node;
 39     return true;
 40 };
 41 DblLinkListAdt.prototype.delete = function(i){
 42     if((i < 1) || (i > this.getLength())){
 43         return false;
 44     }
 45     var pos = 1;
 46     var node1 = this.head;
 47     while(true){
 48         if(pos == (i-1)){
 49             break;
 50         }
 51         node1 = node1.next;
 52     }
 53     var node2 = node1.next;
 54     node2.next.prev = node1;
 55     node1.prev = node2.next;
 56     return true;
 57 };
 58 DblLinkListAdt.prototype.getLength = function(){
 59     var len = 0;
 60     var tmp = this.head.next;
 61     while(tmp){
 62         len++;
 63         tmp = tmp.next;
 64     }
 65     return len;
 66 };
 67 DblLinkListAdt.prototype.getElem = function(i){
 68     if((i < 1) || (i > this.getLength())){
 69         return false;
 70     }
 71     var pos = 0;
 72     var tmp = this.head;
 73     while(true){
 74         if(pos == i){
 75             break;
 76         }
 77         pos++;
 78         tmp = tmp.next;
 79     }
 80     return tmp;
 81 };
 82 DblLinkListAdt.prototype.empty = function(){
 83     return this.head.next ? false : true;
 84 };
 85 DblLinkListAdt.prototype.clear = function(){
 86     this.head.next = null;
 87 };
 88 DblLinkListAdt.prototype.locate = function(data){
 89     if(this.empty()){
 90         return -1;
 91     }
 92     var node1 = this.head.next;
 93     var i = 1;
 94     var pos = -1;
 95     while(node1){
 96         if(node1.data == data){
 97             pos = i;
 98             break;
 99         }
100         node1 = node1.next;
101         i++;
102     }
103     return pos;
104 };
105 DblLinkListAdt.prototype.print = function(){
106     var aTmp = [];
107     var tmp = this.head.next;
108     while(tmp){
109         aTmp.push(tmp.data);
110         tmp = tmp.next;
111     }
112     console.log(aTmp.join(','));
113 };
114 DblLinkListAdt.prototype.padding = function(len){
115     for(i = 0; i < len; i++){
116         this.insert(i+1,this.node(i+1));
117     }
118     return true;
119 };

后记

数据结构基础线性表基本上学完了,后面还需要再接再励,学习更复杂的数据结构。

作者:WadeYu
出处:http://www.cnblogs.com/wadeyu/
本文版权归本人和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/wadeyu/p/4908748.html