1、Definition

Arry数组是一种连续储存的List

储存方式:将线性表中的元素一次储存在连续的储存空间中。

Computer's logical structure:
逻辑位置上相邻的元素在物理位置上也相邻

2、Implementation

template<class List_entry>
class List
{
public:
List();
int size();
bool full();
bool empty();
...
protected:
int last;
List_entry entry[max_list];
};

3、Operations

(1)Insert element e at i

InsertElem(L, e, i)
input: i:location, e: ElementType;
Pre-condition: 1<=i<=L.last+1;
Pos-condition: insert element e at i and add 1 to last;
----------------------------------------------------------------------------------------- int InsertElem(List *L, DataType e, int i)
{
if(((*L).last)>=(maxsize-1))
{
cout<<"overflow"<<endl;
return ERROR; //溢出
}
else if((i<0)||(i>((*L).last+1)))
{
cout<<"ERROR"<<endl;
return ERROR; //非法位置
}
else
{
for(int j=((*L).last);j>=i;j--)
(*L).data[j+1]=(*j).data[j];
(*L).data[i]=e;
(*L).last+=1;
return(1);
}
}

(2)Delete element e at location i

DeleteElem(i)
input: i: location
Pre-condition: 0<=i<=last;
Pos-condition: delete the element at location i and last-1;
---------------------------------------------------------------------------------------------
int Delete(List *L, int i)
{
if(((*L).last)>=(maxsize-1))
{
cout<<"overflow"<<endl;
return ERROR; //溢出
}
else if((i<0)||(i>((*L).last+1)))
{
cout<<"ERROR"<<endl;
return ERROR; //非法位置
}
else
{
for(int j=i;j<((*L).last);j++)
(*L).data[j]=(*L).data[j+1];
(*L).last-=1;
return (1);
}
}

Insert and delete

delete:

最好情况为删除最后一个元素,其他元素不用移动,时间复杂度为O(1)

最坏情况为删除第一个元素,要移动所有元素,时间复杂度为O(N)

insert:

最好情况为在最后一个位置插入,其他元素不用移动,时间复杂度为O(1)

 最坏情况为在第一个位置插入,要移动所有元素,时间复杂度为O(N) 

(3)Search

按值查找

Search(e, i)
input: e:ElementType;
output: i:location;
Pre-condition: L is not empty;
Pos-condition: output the location of e;
-----------------------------------------------------------------------
int Search(List *L, DataType e)
{
for(int i=0;i<=((*L).last);i++)
if(((*L).data[i])==e)
return i;
return 0;
}

按位查找

Search(i)
input: i:location;
output: e:ElementType;
Pre-condition: 0<=i<=last;
Pos-condition: output the element in the location i;
----------------------------------------------------------------------------------------
DataType Search(List *L, int i)
{
if((i<0)||(i>(*L).last+1))
{
cout<<"ERROR"<<endl;
return NULL;
}
return (*L).data[i];
}

4、Analysis of Array

Advantages:

顺序表的结构比较简单,储存效率高,无需储存元素之间的关系

Disadvantages:

在进行插入和删除操作时时间复杂度高

对长度较大的线性表,须预先分配较大的的空间或经常扩充线性表,不方便。

List-----Array的更多相关文章

  1. javascript中的Array对象 —— 数组的合并、转换、迭代、排序、堆栈

    Array 是javascript中经常用到的数据类型.javascript 的数组其他语言中数组的最大的区别是其每个数组项都可以保存任何类型的数据.本文主要讨论javascript中数组的声明.转换 ...

  2. ES5对Array增强的9个API

    为了更方便的对Array进行操作,ES5规范在Array的原型上新增了9个方法,分别是forEach.filter.map.reduce.reduceRight.some.every.indexOf ...

  3. JavaScript Array对象

    介绍Js的Array 数组对象. 目录 1. 介绍:介绍 Array 数组对象的说明.定义方式以及属性. 2. 实例方法:介绍 Array 对象的实例方法:concat.every.filter.fo ...

  4. 了解PHP中的Array数组和foreach

    1. 了解数组 PHP 中的数组实际上是一个有序映射.映射是一种把 values 关联到 keys 的类型.详细的解释可参见:PHP.net中的Array数组    . 2.例子:一般的数组 这里,我 ...

  5. 关于面试题 Array.indexof() 方法的实现及思考

    这是我在面试大公司时碰到的一个笔试题,当时自己云里雾里的胡写了一番,回头也曾思考过,最终没实现也就不了了之了. 昨天看到有网友说面试中也碰到过这个问题,我就重新思考了这个问题的实现方法. 对于想进大公 ...

  6. javascript之活灵活现的Array

    前言 就如同标题一样,这篇文章将会灵活的运行Array对象的一些方法来实现看上去较复杂的应用. 大家都知道Array实例有这四个方法:push.pop.shift.unshift.大家也都知道 pus ...

  7. 5.2 Array类型的方法汇总

    所有对象都具有toString(),toLocaleString(),valueOf()方法. 1.数组转化为字符串 toString(),toLocaleString() ,数组调用这些方法,则返回 ...

  8. OpenGL ES: Array Texture初体验

    [TOC] Array Texture这个东西的意思是,一个纹理对象,可以存储不止一张图片信息,就是说是是一个数组,每个元素都是一张图片.这样免了频繁地去切换当前需要bind的纹理,而且可以节省系统资 ...

  9. Merge Sorted Array

    Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. Note:Yo ...

  10. C++ std::array

    std::array template < class T, size_t N > class array; Code Example #include <iostream> ...

随机推荐

  1. qt5.6 for android

    尝试在qt下写android的程序.测试,没有编译成功. 在选项-android配置页面,提示有"Qt version for 3 architectures are missing&quo ...

  2. HDU 1517 A Multiplication Game 博弈

    题目大意:从1开始Stan与Ollie经行博弈,stan先手,每次将当前数乘上(2~9)间的任意数,最后一次操作后大于等于n的人获胜. 题目思路: 1-9 stan 胜 10-18 ollie胜 19 ...

  3. libimobiledevice命令

    Mac 安装 1. 安装HomeBrew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/m ...

  4. hadoop 2.2.0 关于map和reduce的个数的设置

    关于hadoop中的map过程,我的理解是每一个map系统会开启一个JVM进程来处理,map之间相互并行,map函数内串行.这样的想法是否正确? 由于想在hadoop集群上算一个初始输入数据不多,但是 ...

  5. linux下安装php的mcrypt拓展

    安装步骤: 1,#wget  http://museum.php.net/php5/php-5.3.3.tar.gz 2,解压:#tar -zxvf  php-5.3.3.tar.gz 3,#cd   ...

  6. Var x;---定义变量

    变量定义有多种格式 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> < ...

  7. pigcms 微外卖下单加数量bug

    bug:加数量的时候结算金钱出现NAN 先给一个简单粗暴的解决办法. 找到/tpl/static/dishout/js/main.js 把  65行 disPrice = parseFloat(sig ...

  8. JS的异步回调函数

    hi :)几日不见,趁着周末和父母在广州走走逛逛,游山玩水,放松身心,第一天上班就被一个问题难住了,不废话,以下是关于JS函数回调方面的知识,今天的查阅看的也是一知半解,摘录下来日后慢慢琢磨! js中 ...

  9. discuz使用总结

    使用xampp作为运行环境 xampp的初始目录. xampp中mysql root账户的密码是空

  10. git config and options core.bare hard

    In Lynda course Building a Web Interface with React.js 003 Using the exercises > git clone --bare ...