您的位置:首页 > 理论基础 > 数据结构算法

数据结构基础1:线性表(1)

2011-08-14 18:55 357 查看
1.线性表的定义:

是一个数据元素的有序集,拥有唯一的第一个元素,拥有唯一的一个最后一个元素,除了第一个元素和最后一个元素外,集合中的每个元素都有唯一的前驱和后续。

2.数据操作

(1)初始化操作

InitialList(L&)

(2)销毁操作

Destory(L&)

(3)引用型操作

ListEmpty(L)

ListLength(L)

PriorElement(&L, cur_e, &pre)

NextElement(&L, cur_e, &next)

LocateElement(&L, e, compare())

Traverse(&L, visit())

GetElement(&L, i, &e)

(4)加工型操作

ListInsert(&L, i, &e)

ListDelete(&L, i, &e)

ClearList(&L)

PutElement(&L, i, &e)

3.应用

(1)有两个集合A和B,求一个新的集合“A=A并B”。

问题分析:a.我们需要用到2中的哪些操作?

取出B集合中的元素:GetElement(&L, i, &e)

将B中取出的元素与A集合中的元素比较:LocateElement(&L, e, compare())

如果A中不存在此元素,将此元素插入A集合中:InsertElement(&L, i, e)

得到A集合 和 B集合的长度 ListLength(&L)

b.实现此算法

Merger(&List la, &List lb)

{

int aLength = ListLength(&la);

int bLength = ListLength(&lb);

int i=0; j = 0;

for(j = 1; j <= bLength; j++)

{

GetElement(lb, j, &e);

if(!LocateElement(la, e, equal()))

{

InsertElement(la, ++aLentgh, e);

}

}

}

(2)已知一个非纯集合B,构造一个纯集合A,使A中只包含B中值不相同的元素,且B为有序集合

a.我们将用到基本操作中的哪些操作?

创建一个新集合A:InitialList(&L)

取出B中的元素:GetElement(&L, i, &e)

判读A集合是否为空: ListEmpty(&L)

若不存在,将此元素插入A中:InsertElement(&L, i, e)

得到B集合的长度:ListLength(&L)

b.实现

List Merge(List& lb)

{

InitialLisnt(la);

int bLength = ListLength(lb);

int j = 1, i = 0;

int en= 0;

for (j = 1; j <= bLength, j++)

{

GetElement(lb, j, &e);

if(ListEmpty(la) || equal(en, e))

{

InsertElement(la, i++, e);

en = e;

}

}

return la;

}

(3)有两个有序表la, lb,元素按非递减排列,求得新有序表lc也有此性质

a.需要哪些基本操作?

新建lc:InitialList(L)

读取la,lb中的元素:GetElement(&L, i, &e)

求得la,lb的长度:ListLength(&L)

将元素插入lc中:InsertElement(&L, i, &e)

b.如何实现?

List Merge(&List la, &List lb)

{

InitialList(lc);

int aLength = ListLength(la);

int bLength = ListLength(lb);

int cLength = ListLength(lc);

int i = 1; j = 1;

while(i <= aLength && j <= bLength)

{

GetElement(la, i, e);

GetElement(lb, j, e1);

if (e < e1)

{

InsertElement(lc, ++cLength, e);

i++;

}

else if(e > e1)

{

Insert Element(lc, ++cLength, e1);

j++;

}

else

{

InsertElement(lc, ++cLength, e);

i++;

j++;

}

}

while(i <= aLength)

{

GetElement(la, i, &e);

InsertElement(lc, ++cLength, e);

i++;

}

while(j <= bLength)

{

GetElement(lb, j, &e);

InsertElement(lc, ++cLength, e);

j++;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: