顺序表的问题C++
这个程序的作用是把两个顺序表合并并排序输出,我写的好像其中的Sort()和Union()函数不执行,很奇怪,希望大家办一下忙,感激不尽!!!注:把上面的两个成员函数和main()函数改一下好就可以了,其余好像都是对的,还有两天就要交了,帮帮忙!!#include <iostream.h>
#include <stdlib.h>
const int defaultSize = 120;
template <class T>
class SeqList {
private:
T *data;
int maxSize;
int last;
void reSize(int newSize);
public:
SeqList( int sz = defaultSize );
~SeqList() {delete[ ]data;}
int Size() const {return maxSize;}
int Length() const {return last+1;}
int Search(T &x) const;
int Locate(int i) const;
bool getData(int i)const;
void SetData(int i, T &x);
bool Insert(int i, T x);
bool Remove(int i, T& x);
bool IsEmpty() const
{ return (last == -1) ? true : false; }
bool IsFull()const
{ return (last = maxSize-1) ? true : false; }
void Sort (int);
void Union ( SeqList<int>& LA, SeqList<int>& LB );
void input();
void output();
};
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>
bool SeqList<T>::getData(int i) const
{ if (i<1 || i>last+1)
{ cerr<<"Invalid index of seqlist"<<endl;
exit(0); }
else return data[i-1];
}
template <class T>
void SeqList<T>::SetData(int i, T &x)
{ if(i>0 && i<= last+1)
data[i-1] = x;
}
template <class T>
int SeqList<T>::Search(T& x) const {
for (int i = 1; i <= last+1; i++) //顺序搜索
if ( data[i-1] == x ) return i;
return 0; //搜索失败
}
template<class T>
int SeqList<T>::Locate( int i) const
{ //定位函数:函数返回第i(1<=i<=1ast+1)个表项的位置,否则函数返回0,表示定位失败。
if (i>=1 && i<=last+1) return i;
else return 0;
}
template <class T>
bool SeqList<T>::Insert (int i, T x) {
//将新元素x插入到表中第i (0≤i≤last+1) 个表项之
//后。插入成功,返回true;否则返回false;
if (last == maxSize-1) return false; //表满
if (i < 0 || i > last+1) return false; //参数i不合理
for (int j = last; j >= i; j--) //依次后移
data[j+1] = data[j];
data[i] = x; //插入
last++;
return true; //插入成功
}
template <class T>
bool SeqList<T>::Remove (int i, T& x)
{//从表中删除第i(1≤i≤last)个表项,通过引用型
//参数x返回被删元素。函数返回删除成功信息;
if (last == -1) return false; //表空
if (i < 1 || i > last+1) return false; //参数i不合理
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(true)
{ cin>>last; last--;
if (last<=maxSize-1)
break;
cout<<"元素的个数不能超过:"<<maxSize<<endl; }
for(int i=0; i<=last;i++)
{ cout<<"请输入第"<<i+1<<"个数的值:";
cin>>data[i]; }
}
template <class T>
void SeqList<T>::output()
{
for ( int i=0; i<=last; i++)
{
cout<<data[i]<<" ";
}
}
template <class T>
void SeqList<T>::Union( SeqList<int>& LA, SeqList<int>& LB )
{ int n = LA.Length ( ), m = LB.Length ( );
int i, k, x;
for ( i = 1; i <=m; i++ )
{ x = LB.getData(i); //在LB中取一元素
k == LA.Search(x); //在LA中搜索它
if (k == 0) //若在LA中未找到插入它
{ LA.Insert(n, x); n++; }
//插入到第n个表项之后
}
}
template <class T>
void SeqList<T>::Sort (int n)
{
for (int i = 0;i<n-1;i++)
{
for (int j=i+1;j<=n-1;j++)
if (data[i]>data[j])
{
T temp;
temp = data[i];
data[i]=data[j];
data[j]=temp;
}
}
}
void main ()
{ SeqList<int> A,B,c;
int x;
cout<<"请输入A的元素:"<<endl;
A.input();A.output();
cout<<endl;
cout<<"请输入B的元素:"<<endl;
B.input();B.output();
c.Union(A,B);x=c.Length();
c.Sort(x);c.output();
}
追问:那么下面的这个重新编写的程序为啥不执行合并和排序的任务呢?
#include <iostream.h>
#include <stdlib.h>
const int defaultSize = 120;
template <class T>
class SeqList {
private:
T *data;
int maxSize;
int last;
void reSize(int newSize);
public:
SeqList( int sz = defaultSize );
~SeqList() {delete[ ]data;}
int Size() const {return maxSize;}
int Length() const {return last+1;}
int Search(T &x) const;
int Locate(int i) const;
bool getData(int i,T&x)const
{
if (i>0&&i<=last+1){x=data[i-1];return true;}else return false;}
void SetData(int i, T &x)
{
if (i>0&&i<=last+1)data[i-1]=x;}
bool Insert(int i, T x);
bool IsEmpty() const
{ return (last == -1) ? true : false; }
bool IsFull()const
{ return (last = maxSize-1) ? true : false; }
void sort(int );
void hebing(SeqList<int> &A, SeqList<int> &B);
void input();
void output();
};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>
int SeqList<T>::Search(T& x) const {for (int i = 1; i <= last+1; i++) //顺序搜索
if ( data[i-1] == x ) return i;
return 0; //搜索失败
}
template<class T>
int SeqList<T>::Locate( int i) const
{ //定位函数:函数返回第i(1<=i<=1ast+1)个表项的位置,否则函数返回0,表示定位失败。
if (i>=1 && i<=last+1) return i;
else return 0;
}
template <class T>
bool SeqList<T>::Insert (int i, T x) {
//将新元素x插入到表中第i (0≤i≤last+1) 个表项之
//后。插入成功,返回true;否则返回false;
if (last == maxSize-1) return false; //表满
if (i < 0 || i > last+1) return false; //参数i不合理
for (int j = last; j >= i; j--) //依次后移
data[j+1] = data[j];
data[i] = x; //插入
last++;
return true; //插入成功
}template <class T>
void SeqList<T>::input()
{ cout<<"开始建立顺序表,请输入表中的元素个数:";
while(true)
{ cin>>last; last--;
if (last<=maxSize-1)
break;
cout<<"元素的个数不能超过:"<<maxSize<<endl; }
for(int i=0; i<=last;i++)
{ cout<<"请输入第"<<i+1<<"个数的值:";
cin>>data[i]; }
}
template <class T>
void SeqList<T>::output()
{
for ( int i=0; i<=last; i++)
{
cout<<data[i]<<" ";
}
}template <class T>
void SeqList<T>::hebing(SeqList<int> &A, SeqList<int> &B)
{ int n = A.Length ( ), m = B.Length ( );
int i, k, x;
for ( i = 1; i <=m; i++ )
{ x = B.getData(i,x); //在LB中取一元素
k = A.Search(x); //在LA中搜索它
if (k == 0) //若在LA中未找到插入它
{ A.Insert(n, x); n++; }
//插入到第n个表项之后
}}
template <class T>
void SeqList<T>::sort(int SIZE)
{
for (int pass = 0;pass<SIZE-1;pass++)
{
for (int i=pass+1;i<=SIZE-1;i++)
if (data[pass]>data[i])
{
int hold;
hold = data[pass];
data[pass]=data[i];
data[i]=hold;
}
}
}
void main()
{
SeqList<int> A,B,C; int x;
cout<<",输入顺序表A的表项,";;
A.input();
cout<<"输入顺序表B的表项,";
B.input();
int m=A.Length(), n=B.Length(), mpn=m+n;
cout<<"顺序表A和B合并后的表C的表项:"<<endl;
C.hebing(A,B);x=C.Length();
C.sort(x);
C.output();}