数据结构(C++)顺序表的实现

#include"iostream.h"

#include"string.h"

 

//顺序表的定义

template<class T> class SeqList

{

    T *data;

    int MaxSize;//最长度大

    int last;//当前最后元素下标

public:

    SeqList(int);

    ~SeqList()

    {

       delete[] data;

    }

    int Length() const

    {

       return last+1;

    }

    int Find(T &x);//查找

    int Insert(T &x,int i);//插入

    int Remove(T &x);//删除

    void Display();//打印

    int IsEmpty()

    {

       return last==-1;

    }

    int IsFull()

    {

       return last==MaxSize-1;

    }

    T Get(int i)//获取

    {

       if(i>last||i<0) {cout<<"位置异常"<<endl; }

            else return data[i];

    }

};

 

//顺序表的实现

template<class T>

SeqList<T>::SeqList(int sz)

{

    if(sz>0)

    {

       MaxSize=sz;

       last=-1;

       data=new T[MaxSize];

       if(data==NULL)

       {

           MaxSize=0;

           last=-1;

           return;

       }

    }

}

template<class T>

int SeqList<T>::Find(T &x)

{

    int i;

    for(i=0;i<=last;i++)

      if (data[i]==x)

         return i;   

    return -1;

}

template<class T>

int SeqList<T>::Insert(T &x,int i)

{

    if(i<0||i>last+1||last==MaxSize-1)

       return 0;

    else

    {

       last++;

       for(int j=last;j>i;j--)

           data[j]=data[j-1];

       data[i]=x;

       return 1;

    }

}

template<class T>

int SeqList<T>::Remove(T &x)

{  

    int i=Find(x);

    if(i>=0)

    {

       last--;

       for(int j=i;j<=last;j++)

           data[j]=data[j+1];

       return 1;

    }

    return 0;

}

template<class T>

void SeqList<T>::Display()

{

    for(int i=0;i<=last;i++)

    {

       cout<<data[i]<<endl;

    }

}

 

//Student类定义

class Student

{

public:

    int id;

    char name[20];

    int year;

    int score;

    bool operator==(const Student &);

 

};

//Student类的实现

bool Student::operator==(const Student &s)//重载==运算符

{

    if(this==&s)

       return true;

    if((id==s.id)&&(strcmp(name,s.name)==0)&&(year==s.year)&&(score==s.score))

       return true;

    else return false;

}

ostream& operator<<(ostream &os,Student &s)//重载<<运算符

{

    os<<s.id<<"    "<<s.name<<"    "<<s.year<<"      "<<s.score<<endl;

    return os;

}

istream& operator>>(istream &is,Student &s)//重载>>运算符

{

    cout<<"请输入此学生的学号,姓名,入学年份,分数"<<endl;

    is>>s.id>>s.name>>s.year>>s.score;

    return is;

}

 

//主函数

void main()

{

    int i,j,k,m;

    SeqList<Student> S(10);

    Student S1,S2,S3,S4,S5,S6,S7,S8;

    int flag=0;

    while(flag==0)

    {

       cout<<"顺序表的实现(模板类)"<<endl;

       cout<<"************************************"<<endl;

       cout<<"*  1:创建表格                      *"<<endl;

       cout<<"*  2:插入记录                      *"<<endl;

       cout<<"*  3:删除记录                     *"<<endl;

       cout<<"*  4:查询记录                     *"<<endl;

       cout<<"*  5:打印表格                     *"<<endl;

       cout<<"*  6exit.退出                    *"<<endl;

       cout<<"************************************"<<endl;

       cout<<"请输入您的选择:";

       cin>>j;

       switch(j)

       {

       case 1:

           cin>>S1>>S2;

           S.Insert(S1,0);

           S.Insert(S2,1);

           break;

       case 2:

           cin>>S3;

            i=S.Find(S1);

           S.Insert(S3,i+1);

           cin>>S4;

           S.Insert(S4,i);

           i=S.Find(S2);

           cin>>S5;

           S.Insert(S5,i+1);

           break;

       case 3:

            S6=S.Get(0);

           S.Remove(S6);

           k=S.Length();

           S7=S.Get(k-1);

           S.Remove(S7);

           break;

        case 4:

           cout<<"请输入您要查询的记录:";

           cin>>S8;

           m=S.Find(S8);

           if(m<0)

              cout<<"不存在此记录!";

           else

              cout<<"存在记录:"<<S8;

           break;

       case 5:

           S.Display();

           break;

       case 6:

           flag=1;

           break;

       default:

           cout<<"没有此命令!"<<endl;

       }

    }

      

}

你可能感兴趣的:(数据结构,C++,delete,Class,insert,include)