当前位置:文档之家› 中国矿业大学 空间数据结构上机实验报告

中国矿业大学 空间数据结构上机实验报告

《空间数据结构基础》上机实验报告(2010级)姓名班级学号环境与测绘学院1.顺序表的定义与应用(课本P85习题)【实验目的】熟练掌握顺序表的定义与应用,通过上机实践加深对顺序表概念的理解。

【实验内容】设有两个整数类型的顺序表A(有m个元素)和B(有n个元素),其元素均从小到大排列。

试编写一个函数,将这两个顺序表合并成一个顺序表C,要求C的元素也从小到大排列。

【主要代码】#include<iostream.h>//定义在头文件“SeqList.h”中#include<stdlib.h>const int defaultSize=100;template<class T>class SeqList{protected:T *data;//存放数组int maxSize;//最大可容纳表象的项数int Last;//当前已存表象的项数void reSize(int newSize);//改变data数组空间大小public:SeqList(int sz=defaultSize);SeqList(SeqList<T>& L);~SeqList(){delete[]data;}int Size() const{return maxSize;}int Length()const{return Last+1;}int Search(T& x)const;int Locate(int i) const;T getData(int i) const;bool setData(int i,T& x){if(i>0&&i<=Last+1) data[i-1]=x;}bool Insert(int i,T& x);bool Remove(int i,T& x);bool IsEmpty(){return (Last==-1)?true:false;}bool IsFull(){return(Last==maxSize-1)?true:false;}void input();void output();SeqList<T> operator=(SeqList<T>& L);friend void rank(SeqList<int>& L);friend void hebing(SeqList<int>& LA,SeqList<int>& LB);};//构造函数,通过指定参数sz定义数组的长度template<class T>SeqList<T>::SeqList(int sz){if(sz>0){maxSize=sz;Last=-1;data=new T[maxSize];if(data==NULL){cerr<<"存储分配错误"<<endl;exit(1);}}}//复制构造函数,用参数表中给出的已有顺序表初始化新建的顺序表template<class T>SeqList<T>::SeqList(SeqList<T>& L){maxSize=L.Size();Last=L.Length()-1;data=new T[maxSize];if(data==NULL){cerr<<"存储分配错误"<<endl;exit(1);}for(int i=1;i<=Last+1;i++)data[i-1]=L.getData(i);}//用于取第i个表项的值template <class T>T SeqList<T>::getData(int i) const{if (i<1 || i>Last+1){cerr<<"存储分配错误"<<endl;exit(1);}else return data[i-1];}//私有函数,扩充顺序表的存储空间大小,新数组的元素个数为newsize template<class T>void SeqList<T>::reSize (int newSize){if (newSize<=0){cerr<<"无效的数组大小"<<endl;return;}if(newSize!=maxSize){T*newarray=new T[newarray];if(newarray=NULL){cerr<<"存储分配错误"<<endl;exit(1);}int n=Last+1;T*srcptr=data;T*destptr=newarray;while(n--)*destptr++=*srcptr++;delete []data;data=newarray;maxSize=newSize;}}//搜索函数template<class T>int SeqList<T>::Search(T& x)const{for(int i=0;i<=Last;i++)if(data[i]==x)return i+1;return 0;}//定位函数template<class T>int SeqList<T>::Locate(int i)const{if(i>=1&&i<=Last+1)return i;else return 0;}//插入函数template<class T>bool SeqList<T>::Insert(int i,T& x){if(Last==maxSize-1) return false;if(i<0||i>Last+1) return false;for(int j=Last;j>=i;j--)data[j]=data[j-1];data[i]=x;Last++;return true;}//删除函数template<class T>bool SeqList<T>::Remove(int i,T&x){if(Last==-1)return false;if(i<1||i>Last+1)return false;x=data[i-1];for(int j=i;j<=Last;j++)data[j-1]=data[j];Last--;return true;}//输入函数template<class T>void SeqList<T>::input(){cout<<"开始建立顺序表,请输入表中元素的最后位置:";while(1){cin>>Last;Last--;if(Last<=maxSize-1) break;cout<<"表元素个数有误,个数不能超过"<<maxSize-1<<":";}for(int i=0;i<=Last;i++){cin>>data[i];}}//输出函数template<class T>void SeqList<T>::output(){cout<<"顺序表中当前元素的最后位置为:"<<Last<<endl;for(int i=0;i<=Last;i++)cout<<data[i]<<endl;}//重载操作,顺序表整体赋值template<class T>SeqList<T> SeqList<T>::operator=(SeqList<T>& L){maxSize=L.Size();Last=L.Length()-1;data=new T[maxSize];if(data==NULL){cerr<<"存储分配错误"<<endl;exit(1);}for(int i=1;i<=Last+1;i++)data[i-1]=L.getData(i);}//合并函数,用于合并顺序表LA,LB。

结果存于LA。

重复元素只留一个。

void hebing(SeqList<int>& LA,SeqList<int>& LB){int n=LA.Length(),m=LB.Length(),k,x;for(int i=1;i<=m;i++){x=LB.getData(i);k=LA.Search(x);if(k==0){LA.Insert(n,x);n++;}}}//对合并后的顺序表进行排序void rank(SeqList<int>& L){int i,j,temp;for(i=1;i<L.Length();i++)for(j=0;j<L.Length()-i;j++){if(L.data[j]>L.data[j+1]){temp=L.data[j];L.data[j]=L.data[j+1];L.data[j+1]=temp;}}}void main(){SeqList<int>LA;//定义顺序表类对象LASeqList<int>LB;//定义顺序表类对象LBLA.input();LB.input();hebing(LA,LB);SeqList<int>LC(LA);rank(LC);LC.output();}运行结果:【实验体会】通过本次试验,我熟练掌握顺序表的定义与应用。

此次实验通过对顺序表的类定义,进行两顺序表的合并,并在合并后删除相同的元素,然后对新数组进行从小到大的排序。

因为顺序表是基于一维数组的储存,所以可以选择冒泡法进行排序。

类代码中包含三个数据成员,多个作为外部接口的成员函数,完成顺序表的搜索,定位,插入和删除等操作。

运用类模板,Seqlist的数据成员不使用固定的类型定义,而是用class说明的虚拟类型名T作为变量的类型,在定义Seqlist类的对象时,再用C++的基本类型将对象的数据成员的类型实例化。

相关主题