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. FlatBuffers入门

    1.下载flatbuffers 从https://github.com/google/flatbuffers地址下载flatbuffers-master.zip文件. 2.编译flatbuffers ...

  2. java回调机制(写的很好)

    本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17483273) 以前不理解什么叫回调,天天听人家说加一个回调方法啥的 ...

  3. ACE Socket Wrapper Facade

    ACE Socket Wrapper Facade 1:Socket API 1.1 IPC(进程间通信) IPC分为本地IPC(同一计算机上的不同进程)和远程IPC(网络互联的不同计算机),本地IP ...

  4. echarts 折柱混合图 (绑数据后)

    html: <div class="flot-chart-content" id="flot-dashboard-chart"></div&g ...

  5. Oracle查询和解锁表

    一些ORACLE中的进程被杀掉后,状态被置为"killed",但是锁定的资源很长时间不释放,有时实在没办法,只好重启数据库.现在提供一种方法解决这种问题,那就是在ORACLE中杀不 ...

  6. MVC创建

    [1]创建ASP.NET_MVC应用程序   1>新建项目>web>Visual Studio 2012>ASP.NET MVC4 Web应用程序   2>填写名称,位置 ...

  7. openstack私有云布署实践【15 创建租户网络+实例】

    这里以办公网测试环境为例,   (一)创建租户demo的网络   使用admin用户 source admin-openrc.sh 创建public公网 neutron net-create 1040 ...

  8. 【第四篇】Volley修改之GsonRequest

    json解析工具类的引入,这里引用lite马天宇的解析json的工具类: public class GsonImpl extends Json { private Gson gson = new Gs ...

  9. hdu1020

    #include <stdio.h> int main(void){ int n,i,c; char txt[10001]; scanf("%d", &n); ...

  10. 老司机的奇怪noip模拟T3-zhugeliang

    3. 诸葛亮(zhugeliang.cpp/c/pas )[问题描述]xpp 每天研究天文学研究哲学,对于人生又有一些我们完全无法理解的思考.在某天无聊学术之后, xpp 打开了 http://web ...