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

前言

我非CS科班出身,大学时有CS相关课程,慢慢对它产生了兴趣。毕业之后,想找一份WEB编程方面的工作,不过未找到,为了生存选择了一家工厂做普工。也许是我的运气好吧,第一次找编程的工作面试官是自己的校友,然后给了我实习的机会,让我踏上了编程之路,现在想来非常的感激!入行也有几年了,发现IT行业技术日新月异,新技术层出不穷,一个人的经历有限,想学好所有的技术,几乎是不可能完成的,但是不管怎么变,支撑新技术新语言的那些基础没变,数据结构,算法,数学,C语言,编译原理,汇编,操作系统原理,编程思想,设计模式等都没变,因此只要学好了这些不变的东西,等需要用到这些新语言新技术再学习也不迟,上手肯定会很快。因此,我决定花时间学习数据结构方面的知识,希望能熟悉科学家总结出来的那些智慧结晶,希望能为工作中碰到的疑难杂症提供一些解决思路。不久之前买了一本书《大话数据结构》,决定每天尽量抽时间啃这本书。

数据

那什么是数据呢?数据就是指表示现实生活中具体事物的标记或者符号。那什么是数据结构呢?数据结构就是同种类型数据元素之间的关联关系。那什么是数据类型呢?数据类型是指一个数学模型以及这个模型上的操作,比如整数类型,有加减乘除的操作。那什么是数学模型呢?我理解的数学模型就是指具有相同性质数据的抽象数学范式吧。

线性表

零个或多个元素组成的有限序列,它是最简单的数据结构。逻辑上一个元素接着一个元素有顺序的排列。

 

线性表顺序存储结构

开辟一块连续的内存存储空间存储线性表的元素

 

线性表顺序存储结构JS代码实现

 1 /**
 2  * @desc 线性表之顺序存储结构
 3  *
 4  * @author WadeYu
 5  * @date 2014-12-13
 6  * @copyright by WadeYu
 7  */
 8 var ERROR = 0;
 9 var OK = 1;
10 var MAXSIZE = 20;
11 var ELEMTYPE = 'number';
12 var SqList = function(){
13     this.init();
14 };
15 SqList.prototype.init = function(){
16     this.length = 0;
17     this.data = new Array(MAXSIZE);
18 };
19 SqList.prototype.clear = function(){
20     this.init();
21 };
22 SqList.prototype.isEmpty = function(){
23     return this.length == 0 ? true : false;
24 };
25 SqList.prototype.locate = function(elem){
26     var idx = -1;
27     for (var i = 0; i < this.length; i++){
28         if (this.data[i] == elem){
29             idx = i + 1;
30             break;
31         }
32     }
33     return idx;
34 };
35 SqList.prototype.insert = function(i,elem){
36     i = parseInt(i);
37     if (isNaN(i)){
38         return ERROR;
39     }
40     if ( (i < 1) || (i > (this.length+1)) || (this.length == MAXSIZE)){
41         return ERROR;
42     }
43     if (typeof elem != ELEMTYPE){
44         return ERROR;
45     }
46     for (var j = this.length; j >= i; j--){
47         this.data[j] = this.data[j-1];
48     }
49     this.data[i-1] = elem;
50     this.length++;
51     return OK;
52 };
53 SqList.prototype.del = function(i){
54     i = parseInt(i);
55     if ( isNaN(i) || ( i < 1) || (i > this.length)){
56         return ERROR;
57     }
58     var elem = this.data[i-1];
59     for (var j = i; j < this.length; j++){
60         this.data[j - 1] = this.data[j];
61     }
62     this.length--;
63     return elem;
64 };
65 SqList.prototype.getLen = function(){
66     return this.length;
67 };
68 SqList.prototype.get = function(i){
69     i = parseInt(i);
70     if ( isNaN(i) || (i < 1) || (i > this.length)){
71         return ERROR;
72     }
73     return this.data[i-1];
74 };
75 SqList.prototype.print = function(){
76     var s = '';
77     for(var i = 0; i < this.length; i++){
78         s += this.data[i] + ',';
79     }
80     console.log(s);
81 };

测试用例

var elem;
var iPos;
var oSqList = new SqList();

console.log("Test start.");

var len = oSqList.getLen();
for(var i = 0; i < MAXSIZE - 10; i++){
    oSqList.insert(i+1,Math.ceil(Math.random()*100));
}
console.log("The init sqlist is:");
oSqList.print();
console.log("The List len is:%s",oSqList.getLen());

len = oSqList.getLen();
for(i = 0; i < 5; i++){
    elem = Math.ceil(Math.random()*100);
    iPos = Math.ceil(Math.random()*100) % (len + 1) + 1;
    console.log("insert No. is: %s, insert elem is: %s, ",iPos,elem);
    oSqList.insert(iPos,elem);
    console.log("After insert list is:");
    oSqList.print();
    console.log("The List len is:%s",oSqList.getLen());
}

console.log('---------------------------------------------');

for(i = 1; i <= 5; i++){
    elem = Math.ceil(Math.random()*100);
    console.log("The elem %s idx is:%s",elem,oSqList.locate(elem));
}

console.log('-----------------------------------------------');

loop = 5;
while (loop > 0){
    len = oSqList.getLen();
    iPos = Math.ceil(Math.random()*100) % (len+1);
    elem = oSqList.del(iPos);
    console.log("The del idx is:%s, The del elem is:%s",iPos,elem);
    console.log("The sqlist after del is:");
    oSqList.print();
    console.log("The sqlist len is:%s",oSqList.getLen());
    loop--;
}

console.log("Test end.");

 

测试结果

 

优点

存以及取时间复杂度是O(1)

缺点

插入或者删除需要遍历元素,需要频繁的移动元素,存储空间固定

后记

对于非科班出身的我来说,学习数据结构需要很大的毅力和决心,希望自己能够坚持下去吧。

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