关闭

线性表的类型定义

267人阅读 评论(0) 收藏 举报

<iframe style="WIDTH: 89.81%; HEIGHT: 64px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750500&amp;lmt=1192819750&amp;prev_fmts=468x60_as&amp;format=468x15_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>

<iframe style="WIDTH: 44.33%; HEIGHT: 259px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCB94-nvUZWENUB9QugJQtwRQTVAgULcCUB4&amp;oe=gb2312&amp;dt=1192720966468&amp;lmt=1192720966&amp;format=250x250_as&amp;output=html&amp;correlator=1192720966453&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-67-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192720940&amp;ga_hid=1155529757&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe>

一、线性表的定义

线性表是最常用且最简单的一种数据结构。

一个线性表是n个数据元素的有限序列。

数据元素可以是一个数、一个符号、也可以是一幅图、一页书或更复杂的信息。

线性表例:

1、

1 2 3 4 5 6 7

2、

      

3、

学号 姓名 语文 数学 C语言
6201001 张三 85 54 92
6201002 李四 92 84 64
6201003 王五 87 74 73
6201004        
...        

数据元素也可由若干个数据项组成(如上例3)。这时常把数据元素称为记录。含有大量记录的线性表又称文件。

线性表中的数据元素类型多种多样,但同一线性表中的元素必定具有相同特性,即属同一数据对象,相邻数据元素之间存在着序偶关系。

a1 ... ai-1 ai ai+1 ... an

ai是ai+1的直接前驱元素,ai+1是ai的直接后继元素。

线性表中元素的个数n定义为线性表的长度,为0时称为空表。在非空表中的每个数据元素都有一个确定的位置。ai是第i个元素,把i称为数据元素ai在线性中的位序。

二、线性表的类型定义

1、抽象数据类型线性表的定义如下:

ADT List{

数据对象: D={ai| ai(-ElemSet,i=1,2,...,n,n>=0}

数据关系: R1={<ai-1,ai>| ai-1,ai(- D,i=2,...,n}

基本操作:

InitList(&L)
DestroyList(&L)
ClearList(&L)
ListEmpty(L)
ListLength(L)
GetElem(L,i,&e)
LocateElem(L,e,compare())
PriorElem(L,cur_e,&pre_e)
NextElem(L,cur_e,&next_e)
ListInsert(&L,i,e)
ListDelete(&L,i,&e)
ListTraverse(L,visit())
union(List &La,List &Lb)

}ADT List

2、部分操作的类C实现:

InitList(&L)

{L.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));

if(!L.elem)exit(OVERFLOW);

L.length=0;

L.listsize=LIST_INIT_SIZE;

return OK;

}//InitList

GetElem(L,i,&e)

{*e=L.lem

}//GetElem

ListInsert(List &L,int i,ElemType e)

{if(i<1||i>L.length+) return ERROR;

q=&(L.elem[i-1]);

for(p=&(L.elem[L.length-1]);p>=q;--p) *(p+1)=*p;

*q=e;

++L.length;

return OK;

}//ListInsert

void union(List &La,List &Lb)

{La_len=ListLength(La);Lb_len=ListLength(Lb);

for(i=1;i<=Lb_len;i++){

GetElem(Lb,i,e);

if(!LocateElem(La,e,equal))

ListInsert(La,++La_len,e);

}//union

void MergeList(List La,List Lb,List &Lc)

{InitList(Lc);

i=j=1;k=0;

La_len=ListLength(La);Lb_len=ListLength(Lb);

while((i<=La_len)&&(j<Lb_len)){

GetElem(La,i,ai);GetElem(Lb,j,bj);

if(ai<=bj){ListInsert(Lc,++k,ai);++i;}

else{ListInsert(Lc,++k,bj);++j;}

}

while(k<=La_len){

GetElem(La,i++,ai);ListInsert(Lc,++k,ai);

}

while(j<=Lb_len){

GetElem(Lb,j++,bj);ListInsert(Lc,++k,bj);

}

}//MergeList

3、部分操作的C语言实现,下面是程序运行的结果:

-------------------List Demo is running...----------------
First is InsertList function.
name stuno age score
stu1 100001 80 1000
stu2 100002 80 1000
List A length now is 2.
name stuno age score
stu1 100001 80 1000
stu2 100002 80 1000
stu3 100003 80 1000
List A length now is 3.
name stuno age score
zmofun 100001 80 1000
bobjin 100002 80 1000
stu1 100001 80 1000
List B length now is 3.

Second is UnionList function.
Now union List A and List B.....
name stuno age score
stu1 100001 80 1000
stu2 100002 80 1000
stu3 100003 80 1000
zmofun 100001 80 1000
bobjin 100002 80 1000 

<iframe style="WIDTH: 52.39%; HEIGHT: 263px" src="http://pagead2.googlesyndication.com/cpa/ads?client=ca-pub-4577827332549849&amp;cpa_choice=CAEaCKcC4yuPlq5lUDRQDVAtUK4BUENQCA&amp;oe=gb2312&amp;dt=1192819388296&amp;lmt=1192819388&amp;format=300x250_as&amp;output=html&amp;correlator=1192819388281&amp;channel=2735220158&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fviewthread.php%3Ftid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;region=_google_cpa_region_&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fpost.php%3Faction%3Dedit%26fid%3D16%26tid%3D70%26pid%3D70%26page%3D1%26extra%3Dpage%253D1&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1527165455&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=5&amp;u_java=true"></iframe>

<iframe style="WIDTH: 82.23%; HEIGHT: 74px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192818497343&amp;lmt=1192818497&amp;format=468x60_as&amp;output=html&amp;correlator=1192818497343&amp;channel=1741427766&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ad_type=text_image&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforumdisplay.php%3Ffid%3D16%26page%3D1&amp;ui=rc%3A6&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1000484876&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=6&amp;u_java=true"></iframe>

<iframe style="WIDTH: 58.41%; HEIGHT: 156px" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4577827332549849&amp;dt=1192819750656&amp;lmt=1192819750&amp;prev_fmts=468x60_as%2C468x15_0ads_al_s%2C234x60_as&amp;format=200x90_0ads_al_s&amp;output=html&amp;correlator=1192819750437&amp;channel=1741427766&amp;pv_ch=1741427766%2B&amp;url=http%3A%2F%2Fyzkzoo.5d6d.com%2Fthread-68-1-1.html&amp;color_bg=FFFFFF&amp;color_text=000000&amp;color_link=0000FF&amp;color_url=008000&amp;color_border=336699&amp;ref=http%3A%2F%2Fyzkzoo.5d6d.com%2Fforum-16-1.html&amp;cc=100&amp;ga_vid=1025434795.1192631677&amp;ga_sid=1192817968&amp;ga_hid=1871659779&amp;ga_fc=true&amp;flash=8&amp;u_h=768&amp;u_w=1024&amp;u_ah=738&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_his=2&amp;u_java=true"></iframe> 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场