数据结构学习连载2(实现上篇中的提高要求)

1.在SeqList.h中添加声明:void DeleteItemAll(const int item);//删除所有等于item的记录

2.在SeqList.cpp中实现该函数如下:

/*
* 函数名称: DeleteItemAll
* 输 入:item
*item:需要删除数据
* 输 出:
* 功能描述: 删除所有等于item的记录
* 作 者:吴友强
* 日 期:2009年10月12日
* 修 改:
* 日 期:
*/
void SeqList::DeleteItemAll(const int item)
{
if (0 == size)
{
cerr << "顺序表已空无元素可删!" << endl;
exit(1);
}

for (int i=size-1; i>=0; i--)
{
if (data[i] == item)
{
Delete(i);
}
else
{
continue;
}
}

3.SeqListTest.cpp测试程序:

/*
* Copyright (c) 2009,FreshAir团队嵌入式软件研发组
* All rights reserved.
*
* 文件名称:SeqListTest.cpp
* 摘 要:测试顺序表的功能
*
* 当前版本:1.0
* 作 者:吴友强
* 完成日期:2009年10月12日
*
* 取代版本:
* 原作者 :
* 完成日期:
*/

#include "SeqList.h"

int main(int argc, char *argv[])
{
SeqList seqlist;
int i;

for (i=0; i<10; i++)
{
seqlist.Insert(i+10, i);
}

cout << "测试GetData()成员函数结果如下:" << endl;
for(i=0; i<10; i++)
{
cout << seqlist.GetData(i) << " ";
}

cout<< endl << "测试Delete()成员函数结果如下" << endl;
for(i=0; i<10; i++)
{
cout<<seqlist.Delete(0)<<" "<<endl;
}

seqlist.Insert(10, 0);
seqlist.Insert(6, 0);
seqlist.Insert(10, 0);
seqlist.Insert(8, 0);
seqlist.Insert(10, 0);

for (i=0; i<5; i++)
{
cout << seqlist.GetData(i) << " ";
}
cout << endl << "测试DeleteItemAll()成员函数结果如下:" << endl;
seqlist.DeleteItemAll(10);

for (i=0; i<2; i++)
{
cout << seqlist.GetData(i) << " ";
}
return 0;
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值