數據結構C++ 線性表的設計

#include<iostream>
#include<cstring>
using namespace std;
const int MaxSize=100;  ios

struct Student
{
 char num[15];
 char name[10];
 int score[3];
}student[3]={
 {"201123060115","吳家喜",80,    90     ,92},
 {"201123060116","王興華",85,    95     ,89},
 {"201123060117","胡志超",89,    90     ,94}}; 算法

 

template<class DataType>                                     //定義模板類Seqlist
class SeqList
{
 public:
  SeqList(){length=0;}
  SeqList(DataType a[],int n);
  ~SeqList(){}
  int Length(){return length;}
  DataType Get(int i);
  int Locate(char x[]);
  void Insert(int i,DataType x);
  DataType Delete(int i);
  void PrintList();
 private:
  DataType data[MaxSize];
  int length;
}; 函數

template<class DataType>                                   //順序表有參構造函數SeqList
SeqList<DataType>::SeqList(DataType a[],int n)
{
 if(n>MaxSize) throw"參數非法";
 for(int i=0;i<n;i++)
  data[i]=a[i];
 length=n;
} spa

template<class DataType>                                   //順序表按位查找算法Get
DataType SeqList<DataType>::Get(int i)
{
 if(i<1||i>length) throw "查找位置非法";
 else
  return data[i-1];
} ci

template<class DataType>                                   //順序表按值查找算法Locate
int SeqList<DataType>::Locate(char x[])
{
 for(int s=0;s<length;s++)
  if(strcmp(data[s].name,x)==0)
  {
      cout<<"   "<<x<<"   同窗的信息爲:     "<<data[s].num<<"   "<<data[s].name<<"   "<<data[s].score[0]<<"   "<<data[s].score[1]<<"   "<<data[s].score[2]<<endl;
   return s+1;
  }
     else cout<<"不存在這我的的信息"<<endl;
  return 0; string

} io


template<class DataType>                                        //順序表插入算法Insert
void SeqList<DataType>::Insert(int i,DataType x)
{
 if(length>=MaxSize) throw "上溢";
 if(i<1||i>length+1) throw "位置";
 for(int j=length;j>=i;j--)
  data[j]=data[j-1];
 data[i-1]=x;
 length++;
} 模板

template<class DataType>                                      //順序表刪除算法Delete
DataType SeqList<DataType>::Delete(int i)
{
 if(length==0) throw "下溢";
 if(i<1||i>length) throw "位置";
 DataType x=data[i-1];
 for(int j=i;j<length;j++)
  data[j-1]=data[j];
 length--;
 return x;
} class

template<class DataType>                                    //順序表遍歷算法PrintList
void SeqList<DataType>::PrintList ()
{
 for(int i=0;i<length;i++)
  cout<<data[i].num<<"     "<<data[i].name<<"     "<<data[i].score[0]<<"     "<<data[i].score[1]<<"     "<<data[i].score[2]<<" "<<endl;
} stream

void main()
{
    SeqList<Student>m(student,3);

 
 
 Student c;

 cout<<"            實驗一:線性表的相關操做"<<endl;   //輸出線性表
 cout<<endl;
 cout<<"(1)  輸出該線性表:"<<endl;
    m.PrintList();
    cout<<endl;

 cout<<"(2) 求表長:該線性表的長度是:   "<<m.Length()<<endl;//該線性表長
    cout<<endl;

    int i;
 cout<<"(3) 按位查找:請輸入查找第i個元素!     ";//查找元素
 cout<<"i=";
 cin>>i;
 cout<<"    查找出第"<<i<<"個元素的值爲 "<<m.Get(i).num<<"    "<<m.Get(i).name<<"    "<<m.Get(i).score[0]<<"  "<<m.Get(i).score[1]<<"  "<<m.Get(i).score[2]<<endl;
    cout<<endl;

    char x[10];
 cout<<"(4) 按值查找:請輸入查找元素的姓名:       ";   //按值查找
 cin>>x;
    cout<<"   "<<x<<"   同窗的信息爲:   "<<m.Locate(x)<<endl;
    cout<<endl;

    int k;
 cout<<"(5) 插入操做:請輸入插入位置和元素";       //插入操做
 cin>>k>>c.num>>c.name>>c.score[0]>>c.score[1]>>c.score[2];
 cout<<endl;
    m.Insert(k,c);
 cout<<"在第"<<k<<"個位置插入元素後,獲得的新表是:";
 cout<<endl;
    m.PrintList();
 cout<<endl;


 cout<<"(6) 刪除操做:請輸入刪除表中第幾個元素";  //刪除操做
 cin>>i;
 m.Delete(i);
 m.PrintList();
    cout<<"]"<<endl;
 cout<<endl;

}

相關文章
相關標籤/搜索