主题:请高手帮忙修改一下C++ 关于线性表
No.1.h
#define MaxListSize 20;//顺序表最长
#define EQUAL 1;
typedef struct Student{
char name[10];//姓名
char stuno[10];//学号
int age;//年龄
int score;//成绩
}ElemType;
class List//List类定义
{
private:
ElemType elem[MaxListSize];//存List元素的数组
int Length;//当前长度
int MaxSize;//最长
public:
void init(List **L,int ms);//初始化List
void DestroyList(List &L)//删除List
{free(&L);}
void Clearlist()//List置空
{Length=0;}
bool ListEmpty()//判断List是否为空
{return Length==0;}
bool ListFull()//判断List是否为满
{return Lenth==MaxSize;}
ElemType PriorElem(ElemType cur_e,ElemType &pre_e);//返回元素pre_e的前驱
ElemType NextElem(ElemType cur_e,ElemType &next_e);//返回元素next_e的后继
bool ListDelete(int,ElemType &e);//从List删除表头表尾或给定值的元素
void ListTraverse();//遍历List
int ListLength();//返回List长度
void GetElem(int,ElemType *);//读取第i个元素
bool EqualList(ElemType *,ElemType *);//判断两元素是否相等
bool Less_EqualList(ElemType *,ElemType *);//判断两元素是否不相等
bool LocateElem(ElemType,int);//查找元素
bool UpdateList(ElemType &e,ElemType);//List元素的更新
void MergeList(List *,List *);//List的合并
bool ListInsert(int,ElemType &);//List中插入元素
void UnionList(List *,List *);//List的归并
void PrintList(int);//对List按升序或降序打印
}
No.2.cpp
#include"No.1.h"
void List::init(List **L,int ms)
{
(*L)=(List*)malloc(sizeof(List));
(*L)->Length=0;
(*L)->MaxSize=ms;
}
int List::ListLength()
{
return Length;
}
ElemType List::PriorElem(ElemType cur_e,ElemType &pre_e)
{
for(int i=0;i<Length;i++)
if(i!=0&&strcmp(cur_e.name,elem[i].name)==0)
{
pre_e=elem[i-1];return pre_e;
}
return cur_e;
}
ElemType List::NextElem(ElemType cur_e,ElemType &next_e)
{
for(int i=0;i<Length;i++)
if(i!=Length-1&&strcmp(cur_e.name,elem[i].name)==0)
{
next_e=elem[i+1];return next_e;
}
return cur_e;
}
bool List::ListDelete(int mark,ElemType &e);
{
int i,j;
if(ListEmpty())return false;
if(mark>0)
{
//删除表头元素
e=elem[0];
for(i=1;i<length;i++)
elem[i-1]=elem[i];
}
else//删除表尾元素
if(mark<0) e=elem[length-1];
else
{
//删除值为e的元素
for(i=0;i<Lenth;i++)
if(strcmp(elem[i].name,e.name)==0)break;
if(i>=lenth)return false;
else e=elem[i];
for(j=i+1;j<length;j++)
elem[j-1]=elem[j];
}
Lenth--;
return true;
}
void List::ListTraverse()
{
for(int i=0;i<Length;i++)
{
cout<<setw(8)<<elem[i].name;
cout<<setw(10)<<elem[i].stuno;
cout<<setw(9)<<elem[i].age;
cout<<setw(8)<<elem[i].score<<endl;
}
}
void List::GetElem(int i,ElemType *e)
{
*e=elem[i];
}
bool List::EqualList(ElemType *e1,ElemType *e2)
{
if(strcmp(e1->name,e2->name))
return false;
if(strcmp(e1->stuno,e2->stuno))
return false;
if(e1->age!=e2->age)
return false;
if(e1->score!=e2->score)
return false;
return true;
}
bool List::Less_EqualList(ElemType *e1,ElemType *e2)
{
if((strcmp(e1->name,e2->name)==0)
return false;
else return true;
}
bool List::LocateElem(ElemType e,int type)
{
int i;
switch(type)
{
case EQUAL:
for(i=0;i<Length;i++)
if(EqualList(&elem[i],&e))
return true;
break;
default:break;
}
return false;
}
//更新线性表中给定元素
bool List::UpdateList(ElemType &e,ElemType e1)
{
for(int i=0;i<Length;i++)
if(strcmp(elem[i].name,e.name)==0)
{
elem[i]=e1;
return true;
}
return false;
}
bool List::ListInsert(int i,ElemType &e)
{
ElemType *p,*q;
if(i<1||i>Length+1)return false;
q=&elem[i-1];
for(p=&elem[Lengh-1];p>=q;--p)
*(p+1)=*q;
*q=e;
++Length;
return true;
}
void List::MergeList(List *La,List *Lb)
{
int i,j,La_len,Lb_len;
La_len=La->ListLength();
Lb_len=Lb->ListLength();
for(i=0;i<La_len;i++)
elem[i]=La->elem[i];
for(j=0;j<=Lb_len;j++)
elem[i+j]=Lb->elem[j];
Length=La_len+Lb_len;
}
void List::UnionList(List *La,List *Lb)
{
int i,La_len,Lb_len;
ElemType e;
La_len=La->ListLength();
Lb_len=Lb->ListLength();
for(i=0;i<Lb_len;i++)
{
Lb->GetElem(i,&e);
if(!LocateElem(e,EQUAL))
ListInsert(++La_len,e);
}
}
//对线性表按升序或降序输出
void PrintList(int mark)
{
int*b=new int[Length];
int i,k;
cout<<"姓名 学号 年龄 成绩 "<<endl;
if(mark!=0)
{
for(i=0;i<Length;i++)b[i]=i;
for(i=0;i<Length;i++)
{
k=i;
for(int j=i+1;j<length;j++)
{
if(mark==1&&elem[b[j]].score<elem[b[k]].score)k=j;
if(mark==-1&&elem[b[k]].score<elem[b[j]].score)k=j;
}
if(k!=i)
{
int x=b[i];b[i]=b[k];b[k]=x;
}
}
for(int i=0;i<Length;i++)
{
cout<<setw(8)<<elem[b[i]].name;
cout<<setw(10)<<elem[b[i]].stuno;
cout<<setw(9)<<elem[b[i]].age;
cout<<setw(8)<<elem[b[i]].score<<endl;
}
}
else
{
for(int i=0;i<Length;i++)
cout<<setw(8)<<elem[i].name;
cout<<setw(10)<<elem[i].stuno;
cout<<setw(9)<<elem[i].age;
cout<<setw(8)<<elem[i].score<<endl;
}
}
No.3.cpp
#include<iostream>
//using namespace std;
#include<iomanip.h>
#include<malloc.h>
#include<string.h>
#include"No.2.cpp"
void main()
{
cout<<"N02.cpp运行结果:"<<endl;
ElemType e,e1,e2,e3,e4,e5,e6;
List *La,*Lb,*Lc;
int k;
cout<<"首先调入插入函数"endl;
La->init(&La,4);
strcpy(e1.name,"stu1");
strcpy(e1.stuno,"100001");
e1.age=22;
e1.score=88;
La->ListInsert(1,e1);
strcpy(e2.name,"stu2");
strcpy(e2.stuno,"100002");
e2.age=21;
e2.score=79;
La->ListInsert(2,e2);
strcpy(e3.name,"stu3");
strcpy(e3.stuno,"100003");
e3.age=19;
e3.score=87;
La->ListInsert(3,e3);
La->PrintList(0);
cout<<"表La长:"<<La->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
Lb->init(&Lb,4);
strcpy(e4.name,"gfsdg");
strcpy(e4.stuno,"100001");
e4.age=20;
e4.score=94;
Lb->ListInsert(1,e4);
strcpy(e5.name,"boji");
strcpy(e5.stuno,"100002");
e5.age=23;
e5.score=69;
Lb->ListInsert(2,e5);
strcpy(e6.name,"stu1");
strcpy(e6.stuno,"100001");
e6.age=22;
e6.score=88;
Lb->ListInsert(3,e6);
Lb->PrintList(0);
cout<<"表Lb长:"<<Lb->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
cout<<"表La和Lb合并为Lc:"<<endl;
Lc->init(&Lc,6);
Lc->MergeList(La,Lb);
Lc->PrintList(0);
cout<<"表Lc长:"<<Lc->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
cout<<"表La和Lb联合为表La:"<<endl;
La->UnionList(La,Lb);
La->printList(0);
cout<<"表La长:"<<La->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
k=Lc->ListDelete(-1,e6);
if(k==0)cout<<"删除失败"<<endl;
else cout<<"删除成功!"<<endl;
cout<<"输出表Lc:"<<endl;
Lc->printList(0);
cin.get();//按回车键继续浏览后面结果
strcpy(e.name,"NoName");
La->PriorElem(e2,e);
if(strcmp(e.name,"NoName")==0);
cout<<"e2无前驱!"<<endl;
else
cout<<"e2的前驱e.name="<<e.name<<endl;
strcpy(e.name,"NoName");
La->NextElem(e3,e);
if(strcmp(e.name,"NoName")==0);
cout<<"e3无后继!"<<endl;
else
cout<<"e2的后继e.name="<<e.name<<endl;
cin.get();//按回车键继续浏览后面结果
cout<<"按升序输出表Lc:"<<endl;
Lc->PrintList(1);
cin.get();//按回车键继续浏览后面结果
cout<<"按降序输出表Lc:"<<endl;
Lc->PrintList(-1);
cin.get();//按回车键结束程序
}
#define MaxListSize 20;//顺序表最长
#define EQUAL 1;
typedef struct Student{
char name[10];//姓名
char stuno[10];//学号
int age;//年龄
int score;//成绩
}ElemType;
class List//List类定义
{
private:
ElemType elem[MaxListSize];//存List元素的数组
int Length;//当前长度
int MaxSize;//最长
public:
void init(List **L,int ms);//初始化List
void DestroyList(List &L)//删除List
{free(&L);}
void Clearlist()//List置空
{Length=0;}
bool ListEmpty()//判断List是否为空
{return Length==0;}
bool ListFull()//判断List是否为满
{return Lenth==MaxSize;}
ElemType PriorElem(ElemType cur_e,ElemType &pre_e);//返回元素pre_e的前驱
ElemType NextElem(ElemType cur_e,ElemType &next_e);//返回元素next_e的后继
bool ListDelete(int,ElemType &e);//从List删除表头表尾或给定值的元素
void ListTraverse();//遍历List
int ListLength();//返回List长度
void GetElem(int,ElemType *);//读取第i个元素
bool EqualList(ElemType *,ElemType *);//判断两元素是否相等
bool Less_EqualList(ElemType *,ElemType *);//判断两元素是否不相等
bool LocateElem(ElemType,int);//查找元素
bool UpdateList(ElemType &e,ElemType);//List元素的更新
void MergeList(List *,List *);//List的合并
bool ListInsert(int,ElemType &);//List中插入元素
void UnionList(List *,List *);//List的归并
void PrintList(int);//对List按升序或降序打印
}
No.2.cpp
#include"No.1.h"
void List::init(List **L,int ms)
{
(*L)=(List*)malloc(sizeof(List));
(*L)->Length=0;
(*L)->MaxSize=ms;
}
int List::ListLength()
{
return Length;
}
ElemType List::PriorElem(ElemType cur_e,ElemType &pre_e)
{
for(int i=0;i<Length;i++)
if(i!=0&&strcmp(cur_e.name,elem[i].name)==0)
{
pre_e=elem[i-1];return pre_e;
}
return cur_e;
}
ElemType List::NextElem(ElemType cur_e,ElemType &next_e)
{
for(int i=0;i<Length;i++)
if(i!=Length-1&&strcmp(cur_e.name,elem[i].name)==0)
{
next_e=elem[i+1];return next_e;
}
return cur_e;
}
bool List::ListDelete(int mark,ElemType &e);
{
int i,j;
if(ListEmpty())return false;
if(mark>0)
{
//删除表头元素
e=elem[0];
for(i=1;i<length;i++)
elem[i-1]=elem[i];
}
else//删除表尾元素
if(mark<0) e=elem[length-1];
else
{
//删除值为e的元素
for(i=0;i<Lenth;i++)
if(strcmp(elem[i].name,e.name)==0)break;
if(i>=lenth)return false;
else e=elem[i];
for(j=i+1;j<length;j++)
elem[j-1]=elem[j];
}
Lenth--;
return true;
}
void List::ListTraverse()
{
for(int i=0;i<Length;i++)
{
cout<<setw(8)<<elem[i].name;
cout<<setw(10)<<elem[i].stuno;
cout<<setw(9)<<elem[i].age;
cout<<setw(8)<<elem[i].score<<endl;
}
}
void List::GetElem(int i,ElemType *e)
{
*e=elem[i];
}
bool List::EqualList(ElemType *e1,ElemType *e2)
{
if(strcmp(e1->name,e2->name))
return false;
if(strcmp(e1->stuno,e2->stuno))
return false;
if(e1->age!=e2->age)
return false;
if(e1->score!=e2->score)
return false;
return true;
}
bool List::Less_EqualList(ElemType *e1,ElemType *e2)
{
if((strcmp(e1->name,e2->name)==0)
return false;
else return true;
}
bool List::LocateElem(ElemType e,int type)
{
int i;
switch(type)
{
case EQUAL:
for(i=0;i<Length;i++)
if(EqualList(&elem[i],&e))
return true;
break;
default:break;
}
return false;
}
//更新线性表中给定元素
bool List::UpdateList(ElemType &e,ElemType e1)
{
for(int i=0;i<Length;i++)
if(strcmp(elem[i].name,e.name)==0)
{
elem[i]=e1;
return true;
}
return false;
}
bool List::ListInsert(int i,ElemType &e)
{
ElemType *p,*q;
if(i<1||i>Length+1)return false;
q=&elem[i-1];
for(p=&elem[Lengh-1];p>=q;--p)
*(p+1)=*q;
*q=e;
++Length;
return true;
}
void List::MergeList(List *La,List *Lb)
{
int i,j,La_len,Lb_len;
La_len=La->ListLength();
Lb_len=Lb->ListLength();
for(i=0;i<La_len;i++)
elem[i]=La->elem[i];
for(j=0;j<=Lb_len;j++)
elem[i+j]=Lb->elem[j];
Length=La_len+Lb_len;
}
void List::UnionList(List *La,List *Lb)
{
int i,La_len,Lb_len;
ElemType e;
La_len=La->ListLength();
Lb_len=Lb->ListLength();
for(i=0;i<Lb_len;i++)
{
Lb->GetElem(i,&e);
if(!LocateElem(e,EQUAL))
ListInsert(++La_len,e);
}
}
//对线性表按升序或降序输出
void PrintList(int mark)
{
int*b=new int[Length];
int i,k;
cout<<"姓名 学号 年龄 成绩 "<<endl;
if(mark!=0)
{
for(i=0;i<Length;i++)b[i]=i;
for(i=0;i<Length;i++)
{
k=i;
for(int j=i+1;j<length;j++)
{
if(mark==1&&elem[b[j]].score<elem[b[k]].score)k=j;
if(mark==-1&&elem[b[k]].score<elem[b[j]].score)k=j;
}
if(k!=i)
{
int x=b[i];b[i]=b[k];b[k]=x;
}
}
for(int i=0;i<Length;i++)
{
cout<<setw(8)<<elem[b[i]].name;
cout<<setw(10)<<elem[b[i]].stuno;
cout<<setw(9)<<elem[b[i]].age;
cout<<setw(8)<<elem[b[i]].score<<endl;
}
}
else
{
for(int i=0;i<Length;i++)
cout<<setw(8)<<elem[i].name;
cout<<setw(10)<<elem[i].stuno;
cout<<setw(9)<<elem[i].age;
cout<<setw(8)<<elem[i].score<<endl;
}
}
No.3.cpp
#include<iostream>
//using namespace std;
#include<iomanip.h>
#include<malloc.h>
#include<string.h>
#include"No.2.cpp"
void main()
{
cout<<"N02.cpp运行结果:"<<endl;
ElemType e,e1,e2,e3,e4,e5,e6;
List *La,*Lb,*Lc;
int k;
cout<<"首先调入插入函数"endl;
La->init(&La,4);
strcpy(e1.name,"stu1");
strcpy(e1.stuno,"100001");
e1.age=22;
e1.score=88;
La->ListInsert(1,e1);
strcpy(e2.name,"stu2");
strcpy(e2.stuno,"100002");
e2.age=21;
e2.score=79;
La->ListInsert(2,e2);
strcpy(e3.name,"stu3");
strcpy(e3.stuno,"100003");
e3.age=19;
e3.score=87;
La->ListInsert(3,e3);
La->PrintList(0);
cout<<"表La长:"<<La->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
Lb->init(&Lb,4);
strcpy(e4.name,"gfsdg");
strcpy(e4.stuno,"100001");
e4.age=20;
e4.score=94;
Lb->ListInsert(1,e4);
strcpy(e5.name,"boji");
strcpy(e5.stuno,"100002");
e5.age=23;
e5.score=69;
Lb->ListInsert(2,e5);
strcpy(e6.name,"stu1");
strcpy(e6.stuno,"100001");
e6.age=22;
e6.score=88;
Lb->ListInsert(3,e6);
Lb->PrintList(0);
cout<<"表Lb长:"<<Lb->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
cout<<"表La和Lb合并为Lc:"<<endl;
Lc->init(&Lc,6);
Lc->MergeList(La,Lb);
Lc->PrintList(0);
cout<<"表Lc长:"<<Lc->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
cout<<"表La和Lb联合为表La:"<<endl;
La->UnionList(La,Lb);
La->printList(0);
cout<<"表La长:"<<La->ListLength()<<endl;
cin.get();//按回车键继续浏览后面结果
k=Lc->ListDelete(-1,e6);
if(k==0)cout<<"删除失败"<<endl;
else cout<<"删除成功!"<<endl;
cout<<"输出表Lc:"<<endl;
Lc->printList(0);
cin.get();//按回车键继续浏览后面结果
strcpy(e.name,"NoName");
La->PriorElem(e2,e);
if(strcmp(e.name,"NoName")==0);
cout<<"e2无前驱!"<<endl;
else
cout<<"e2的前驱e.name="<<e.name<<endl;
strcpy(e.name,"NoName");
La->NextElem(e3,e);
if(strcmp(e.name,"NoName")==0);
cout<<"e3无后继!"<<endl;
else
cout<<"e2的后继e.name="<<e.name<<endl;
cin.get();//按回车键继续浏览后面结果
cout<<"按升序输出表Lc:"<<endl;
Lc->PrintList(1);
cin.get();//按回车键继续浏览后面结果
cout<<"按降序输出表Lc:"<<endl;
Lc->PrintList(-1);
cin.get();//按回车键结束程序
}