1. 程式人生 > >定義順序表類,表示集合,實現求集合的並(C++)

定義順序表類,表示集合,實現求集合的並(C++)

定義順序表類,表示集合,實現求集合的並。

#include <iostream>
using namespace std;
const int MaxSize=100;  //100只是示例性的資料,可以根據實際問題具體定義
template <class T>      //定義模板類SeqList
class SeqList
{
public:
   SeqList( );       //無參建構函式
   SeqList(T a[], int n);       //有參建構函式
   ~SeqList();             //解構函式為空
   int Length();           //求線性表的長度
   T Get(int i);         //按位查詢,取線性表的第i個元素
   int Locate(T x);       //按值查詢,求線性表中值為x的元素序號
   void Insert(int i, T x);  //線上性表中第i個位置插入值為x的元素
   T Delete(int i);        //刪除線性表的第i個元素
   void PrintList();       //遍歷線性表,按序號依次輸出各元素
private:
   T data[MaxSize];      //存放資料元素的陣列
   int length;            //線性表的長度
};
/*
*前置條件:順序表不存在
*輸    入:無
*功    能:構建一個順序表
*輸    出:無
*後置條件:構建一個順序表
*/
template <class T>
SeqList<T>:: SeqList( )
{
  length=0;
}
/*
*前置條件:順序表不存在
*輸    入:順序表資訊的陣列形式a[],順序表長度n
*功    能:將陣列a[]中元素建為長度為n的順序表
*輸    出:無
*後置條件:構建一個順序表
*/
template <class T>
SeqList<T>:: SeqList(T a[], int n)
{
  if (n>MaxSize) throw "引數非法";
  for (int i=0; i<n; i++)
    data[i]=a[i];
  length=n;
}
/*
*前置條件:無
*輸    入:無
*功    能:無
*輸    出:無
*後置條件:無
*/
template <class T>
SeqList<T>:: ~SeqList( )
{
}
/*
*前置條件:順序表存在
*輸    入:插入元素x,插入位置i
*功    能:將元素x插入到順序表中位置i處
*輸    出:無
*後置條件:順序表插入新元素
*/
template <class T>
void SeqList<T>::Insert(int i, T x)
{
    int j;
  if (length>=MaxSize) throw "上溢";
    if (i<1 || i>length+1) throw "位置";
  for (j=length; j>=i; j--)
  data[j]=data[j-1];   //注意第j個元素存在陣列下標為j-1處
  data[i-1]=x;
  length++;
}

/*
*前置條件:順序表存在
*輸    入:要刪除元素位置i
*功    能:刪除順序表中位置為i的元素
*輸    出:無
*後置條件:順序表刪除元素
*/
template <class T>
T SeqList<T>::Delete(int i)
{
    int x,j;
  if (length==0) throw "下溢";
  if (i<1 || i>length) throw "位置";
  x=data[i-1];
  for (j=i; j<length; j++)
    data[j-1]=data[j];   //注意此處j已經是元素所在的陣列下標
  length--;
  return x;
}
/*
*前置條件:順序表存在
*輸    入:無
*功    能:輸出順序表長度
*輸    出:順序表長度
*後置條件:順序表不變
*/
template <class T>
int SeqList<T>::Length()
{
     return length;
}
/*
*前置條件:順序表存在
*輸    入:查詢元素位置i
*功    能:按位查詢位置為i的元素並輸出值
*輸    出:查詢元素的值
*後置條件:順序表不變
*/
template <class T>
T SeqList<T>::Get(int i)
{
  if (i<1 && i>length) throw "查詢位置非法";
    else return data[i-1];
}
/*
*前置條件:順序表存在
*輸    入:查詢元素值x
*功    能:按值查詢值的元素並輸出位置
*輸    出:查詢元素的位置
*後置條件:順序表不變
*/
template <class T>
int SeqList<T>::Locate(T x)
{
      for (int i=0; i<length; i++)
       if (data[i]==x)
         return i+1 ;  //下標為i的元素等於x,返回其序號i+1
      return 0;  //退出迴圈,說明查詢失敗
}
/*
*前置條件:順序表存在
*輸    入:無
*功    能:順序表遍歷
*輸    出:輸出所有元素
*後置條件:順序表不變
*/
template <class T>
void SeqList<T>::PrintList()
{
    for(int i=0;i<length;i++)
    cout<<data[i]<<endl;
}

template <typename T>
SeqList <T> UnionSet(SeqList <T> a,SeqList <T> b)
{
    T temp;
    for(int i=1;i<=b.Length();i++)
    {    temp=b.Get(i);
        if(!a.Locate(temp))a.Insert(a.Length()+1,temp);
    }
  return a;
}

template <typename T>
SeqList <T> UnionSet1(SeqList <T> &a,SeqList <T> &b,SeqList <T> &c)
{
    T temp;
    for(int i=1;i<=a.Length();i++)
        c.Insert(i,a.Get(i));
    for(int i=1;i<=b.Length();i++)
    {    temp=b.Get(i);
        if(!a.Locate(temp))c.Insert(c.Length()+1,temp);
    }
  return a;
}
#include <iostream>
using namespace std;
int main( )
{
    int ra[]={1,2,3,4,5};
    SeqList <int> a(ra,5);     //根據陣列建立順序表
    int rb[]={0,1,2,8};
    SeqList <int> b(rb,4);     //根據陣列建立順序表
    cout<<"Element in a:"<<endl;
    a.PrintList();            //輸出順序表a所有元素
    cout<<"Element in b:"<<endl;
    b.PrintList();            //輸出順序表b所有元素
    cout<<"UnionSet1 a and b:"<<endl;
    SeqList <int> c;
    UnionSet1(a,b,c);
    cout<<"Element in UnionSet c:"<<endl;
    c.PrintList();
    cout<<"UnionSet a and b:"<<endl;
    a=UnionSet(a,b);
    cout<<"Element in UnionSet a:"<<endl;
    a.PrintList();            //輸出順序表a所有元素
}