数据结构实验(代码加运行结果).doc
上传人:sy****28 上传时间:2024-09-10 格式:DOC 页数:20 大小:350KB 金币:16 举报 版权申诉
预览加载中,请您耐心等待几秒...

数据结构实验(代码加运行结果).doc

数据结构实验(代码加运行结果).doc

预览

免费试读已结束,剩余 10 页请下载文档后查看

16 金币

下载此文档

如果您无法下载资料,请参考说明:

1、部分资料下载需要金币,请确保您的账户上有足够的金币

2、已购买过的文档,再次下载不重复扣费

3、资料包下载后请先用软件解压,在使用对应软件打开

集合的差、并运算#include<iostream.h>#include<stdlib.h>constintdefaultSize=100;template<classT>classSeqList{protected:T*date;//存放数组intmaxSize;intlast;voidreSize(intnewSize);//改变数组空间大小public:SeqList(intsz=defaultSize);//构造函数SeqList(SeqList<T>&L);//复制构造函数intSize()const{returnmaxSize;}//计算表最大可容纳表项个数intLength()const{returnlast+1;}//计算表长度intSearch(T&x)const;//搜索x在表现中位置intLocate(inti)const;//定义第i个表项,函数返回表项序号boolgetDate(inti,T&x)const//取第i个表项的值{if(i>0&&i<=last+1){x=date[i-1];returntrue;}elsereturnfalse;}voidsetDate(inti,T&x)//修改第i个表项的值{if(i>0&&i<=last+1)date[i-1]=x;}boolInsert(inti,T&x);//插入x在第i个表项后boolRemove(inti,T&x);//删除第i个表项的值boolIsEmpty(){return(last==-1)?true:false;}//判空boolIsFull(){return(last==maxSize-1)?true:false;}//判满voidinput();//invoidoutput();//outSeqList<T>operator=(SeqList<T>&L);//表整体赋值};template<classT>//构造函数SeqList<T>::SeqList(intsz){if(sz>0){maxSize=sz;last=-1;date=newT[maxSize];if(date==NULL){cerr<<"存储分配出错!"<<endl;exit(1);}}};template<classT>//复制构造函数SeqList<T>::SeqList(SeqList<T>&L){maxSize=L.Size();last=L.Length()-1;Tvalue;date=newT[maxSize];if(date==NULL){cerr<<"存储分配出错!"<<endl;exit(1);}for(inti=1;i<=last+1;i++){L.getDate(i,value);date[i-1]=value;}};template<classT>//改变date数组大小voidSeqList<T>::reSize(intnewSize){if(newSize<=0){cerr<<"无效的数组大小"endl;return;}if(newSize!=maxSize){T*newarray=newT[newSize];if(newarray==NULL){cerr<<"存储分配出错!"<<endl;exit(1);}intn=last+1;T*srcptr=date;T*destptr=newarray;while(n--)*destptr++=*srcptr++;delete[]date;date=newarray;maxSize=newSize;}};template<classT>intSeqList<T>::Search(T&x)const//搜索x在表项中位置,返回表项序号{for(inti=0;i<=last;i++)if(date[i]==x)returni+1;return0;}template<classT>intSeqList<T>::Locate(inti)const//定位第i个表项,返回表项序号{if(i>=1&&i<=l