数据结构实验三
#include<iostream>
#include<string>
using namespace std;
const int MaxSize=10;
class Student
{
private:
int math[MaxSize];
int Chinese[MaxSize];
char *name[MaxSize];
int lenght;
public:
Student(){lenght=0;}
Student(char *a[],int b[],int c[],int x);
~Student(){};
void Insert(int i,char*n,int m,int c);
int Delete(int i);
char Locate(char *k);
void Print();
};
#include<iostream>
using namespace std;
#include "Student.h"
Student::Student(char *a[],int b[],int c[],int x)
{
if(x>MaxSize) throw"上溢";
for(int i=0;i<x;i++)
{
name[i]=a[i];
Chinese[i]=b[i];
math[i]=c[i];
}
lenght=x;
}
void Student::Insert(int i,char *n,int m,int c)
{
if(lenght>=MaxSize) throw"上溢";
if(i<1||i>lenght+1) throw"位置非法";
for(int j=lenght;j>=i;j--)
{
name[j]=name[j-1];
math[j]=math[j-1];
Chinese[j]=Chinese[j-1];
}
name[i-1]=n;
Chinese[i-1]=m;
math[i-1]=c;
lenght++;
}
int Student::Delete(int i)
{
if(lenght==0)throw"下溢";
if(i<1||i>lenght+1) throw"位置非法";
char g=*name[i-1];
for(int j=i;j<lenght;j++)
{
name[j-1]=name[j];
}
lenght--;
return g;
}
char Student::Locate(char *k)
{
for(int i=0;i<lenght;i++)
if(name[i]==k) return i+1;
return 0;
}
void Student::Print()
{
for(int i=0;i<lenght;i++)
cout<<name[i]<<" "<<math[i]<<" "<<Chinese[i]<<endl;
}
#include<iostream>
using namespace std;
#include "Student.h"
void main()
{
char *r[5]={"wang","du","huang","ye","deng"};
int s[5]={80,75,85,90,70};
int t[5]={85,88,77,66,91};
cout<<"执行插入操作前的数据为:"<<endl;
Student L(r,s,t,5);
L.Print();
try
{
L.Insert(2,"he",60,60);
}
catch(char *w)
{
cout<<w<<endl;
}
cout<<"执行插入操作后数据为:"<<endl;
L.Print();
cout<<"姓名为huang的学生的序号为:"<<endl;
cout<<L.Locate("huang")<<endl;
cout<<"执行删除第一个学生数据的操作,删除前数据为:"<<endl;
L.Print();
try
{
L.Delete(1);
}
catch(char *w)
{
cout<<w<<endl;
}
cout<<"删除后数据为:"<<endl;
L.Print();
}
链表:
#ifndef Student_H
#define Student_H
template <class DataType>
struct Node
{
DataType data;
Node<DataType> *next;
};
template <class DataType>
class Student
{
private:
Node<DataType> *first;
public:
Student();
Student(DataType *a[],int b[],int c[],int m);
~Student();
int Locate(DataType x);
void Insert(int i,char *r,int s,int t);
DataType Delete(int i);
void Print();
};
#endif
#include<iostream>
using namespace std;
#include "Student.h"
template<class DataType>
Student<DataType>::Student()
{
first=new Node<DataType>;
fitst->next=NULL;
}
template<class DataType>
Student<DataType>::Student(DataType *a[],int b[],int c[], int m)
{
Node<DataType> *r,*s;
first=new Node<DataType>;
r=first;
for(int i=0;i<m;i++)
{
s=new Node<DataType>;
s->data1=*a[i];
s->data2=b[i];
s->data3=c[i];
r->next=s;r=s;
}
r->next=NULL;
}
template<class DataType>
Student<DataType>::~Student()
{
Node<DataType>*q=NULL;
while(first!=NULL)
{
q=first;
first=first->next;
delete q;
}
}
template<class DataType>
void Student<DataType>::Insert(int i,char *r,int s,int t)
{
Node<DataType> *p=first,*s=NULL;
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL)throw"位置";
else
{
s=new Node<DataType>;s->data1=x;s->data2=*n;
s->next=p->next;p->next=s;
}
}
template<class DataType>
DataType Student<DataType>::Delete(int i)
{
Node<DataType>*p=first,*q=NULL;
DataType x;
int count=0;
while(p!=NULL&&count<i-1)
{
p=p->next;
count++;
}
if(p==NULL||p->next==NULL)
throw"位置";
else{
q=p->next;
x=q->data1;
p->next=q->next;
delete q;
return x;
}
}
template<class DataType>
int Student<DataType>::Locate(DataType x)
{
Node<DataType>*p=first->next;
int count=1;
while(p!=NULL)
{
if(p->data==x)return count;
p=p->next;
count++;
}
return 0;
}
template<class DataType>
void Student<DataType>::Print()
{
Node<DataType>*p=first->next;
while(p!=NULL)
{
cout<<p->data1<<p->data2<<" ";
p=p->next;
}
cout<<endl;
}
#include<iostream>
using namespace std;
#include "Student.h"
void main()
{
char *a[5]={"wang","du","huang","ye","deng"};
int b[5]={80,75,85,90,70};
int c[5]={85,88,77,66,91};
cout<<"执行插入操作前的数据为:"<<endl;
Student<char >L(a,b,c,5);
L.Print();
try
{
L.Insert(2,"he",60,60);
}
catch(char *w)
{
cout<<w<<endl;
}
cout<<"执行插入操作后数据为:"<<endl;
L.Print();
cout<<"姓名为huang的学生的序号为:"<<endl;
cout<<L.Locate("du")<<endl;
cout<<"执行删除第一个学生数据的操作,删除前数据为:"<<endl;
L.Print();
try
{
L.Delete(1);
}
catch(char *w)
{
cout<<w<<endl;
}
cout<<"删除后数据为:"<<endl;
L.Print();
}