数据结构基础1:线性表(1)
来源:互联网 发布:手机转换格式软件 编辑:程序博客网 时间:2024/06/10 03:41
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++;
}
}
- 数据结构基础1:线性表
- 数据结构基础1:线性表(1)
- 数据结构线性表1
- 数据结构--1、线性表
- 数据结构1--线性表
- 数据结构(1):线性表
- 数据结构(1)-线性表
- 数据结构基础:线性表的应用(1)
- 基础数据结构--线性表实现
- 数据结构基础--线性表操作
- 数据结构基础之线性表
- 数据结构基础【02】线性表
- 数据结构基础之线性表
- 数据结构之线性表1
- 大话数据结构:线性表(1)
- 数据结构线性表(1)
- 顺序线性表-数据结构(1)
- 数据结构学习小结(1)-----线性结构-----线性表
- Hibernate中的component的用法
- ASP.NET MVC 2.0 Html.DropDownListFor默认项设置的小技巧
- 自描
- 1 /// <summary>
- Java 7七大新功能预览
- 数据结构基础1:线性表(1)
- 不修改源代码实现Libjpeg内存编解码jpeg
- 指针和数组
- 网络游戏程序员须知 基础概念篇
- 通过超级程序员的唯一道路
- 关键字驱动测试框架
- fedora15 ati 显卡驱动安装
- char *转换为WCHAR[]
- C#常用日期格式处理转换