C++学校実験(チェーンテーブル実装)
最近もう试験が近づいて、思い出して、私はもう一つのデータ构造のチェーンテーブルについての実験をしていないで、昨日一晩中振り回して、しかし1つの问题はまだ解决していません.
問題は次のとおりです.
学生の情報:
1、名前
2、学号
3、性別
4、年齢
チェーンテーブルで接続し、外部から年齢を入力すると、その年齢と同じ学生がチェーンテーブルから削除されます.
分析:この実験に対して1つの
ADT student
≪アクション・オブジェクト|Action Objects|oem_src≫:基本情報(プライベート・メンバー変数)
基本操作:
student();//デフォルトパラメータの学生を作成
student(char *name,char *sex,int age);//指定したパラメータを作成する学生
~student()://学生の削除
display();//学生情報の表示
student &operator=(student &s);//リロード=後のチェーンテーブルの割り当て
チェーンテーブルの構造
ADT Link
操作対象:学生Studio
Link()/空表の作成
Delete()/要素を削除
Add(Student&s)/チェーンテーブルにsを追加
Display()/チェーンテーブルの表示
~Link();//リリースチェーンテーブル
コードは次のとおりです.
でも学生番号の問題は解決しなかった...
問題は次のとおりです.
学生の情報:
1、名前
2、学号
3、性別
4、年齢
チェーンテーブルで接続し、外部から年齢を入力すると、その年齢と同じ学生がチェーンテーブルから削除されます.
分析:この実験に対して1つの
ADT student
≪アクション・オブジェクト|Action Objects|oem_src≫:基本情報(プライベート・メンバー変数)
基本操作:
student();//デフォルトパラメータの学生を作成
student(char *name,char *sex,int age);//指定したパラメータを作成する学生
~student()://学生の削除
display();//学生情報の表示
student &operator=(student &s);//リロード=後のチェーンテーブルの割り当て
チェーンテーブルの構造
ADT Link
操作対象:学生Studio
Link()/空表の作成
Delete()/要素を削除
Add(Student&s)/チェーンテーブルにsを追加
Display()/チェーンテーブルの表示
~Link();//リリースチェーンテーブル
コードは次のとおりです.
#include<iostream>
#include<string>
using namespace std;
class Link;
/*
** , 、 , 1,
*/
class Student{
friend class Link;
public:
Student();
Student(Student &);
Student(char *name,char* sex,int age);
void display();
Student &operator=(Student &s);
~Student();
private:
char *Name;
int age;
char *Sex;
int no;
Student *next;
int static Stu_no;
};
int Student::Stu_no=2009000;
Student::Student(){
no=Stu_no++;
Name=new char[2];
strcpy(Name,"X");
Sex=new char[4];
strcpy(Sex,"Boy");
age=20;
}
Student::Student(char *name,char* sex,int age){
no=Stu_no++;
this->age=age;
Name=new char[strlen(name)+1];
strcpy(Name,name);
Sex=new char[strlen(sex)+1];
strcpy(Sex,sex);
}
Student::Student(Student &s){
no=Stu_no++;
this->age=s.age;
Name=new char[strlen(s.Name)+1];
strcpy(Name,s.Name);
Sex=new char[strlen(s.Sex)+1];
strcpy(Sex,s.Sex);
next=new Student;
}
Student &Student::operator =(Student &s){
this->age=s.age;
Name=new char[strlen(s.Name)+1];
strcpy(Name,s.Name);
Sex=new char[strlen(s.Sex)+1];
strcpy(Sex,s.Sex);
return *this;
}
Student::~Student(){
delete []Name;
delete []Sex;
Stu_no--;
}
void Student::display(){
cout<<Name<<" "<<no<<" "<<Sex<<" "<<age<<endl;
}
class Link{
public:
Link();
void Delete(int);
void Add(Student& s);
void Display();
~Link();
private:
Student *pHead;
Student *pTail;
Student *pivot;
};
Link::Link(){//
pHead=NULL;;
pTail=NULL;
pivot=NULL;
}
Link::~Link(){//
pivot=pHead;
Student *p;
while(pivot){
p=pivot;
pivot=pivot->next;
delete p;
}
}
void Link::Add(Student &s){// s
if(pHead==NULL){
pHead = new Student(s);
pTail=pHead;
pTail->next=NULL;
}
else{
Student *st=new Student(s);
pTail->next=st;
pTail=st;
pTail->next=NULL;
}
}
void Link::Display(){//
pivot=pHead;
while(pivot){
pivot->display();
pivot=pivot->next;
}
if(pHead)// ,
cout<<"-------------------"<<endl;
}
void Link::Delete(int age){// age
int yes=0;// age
Student *p=pHead,*q;
if(p&&p->age==age){// age
do{
cout<<" ";
pHead->display();
yes=1;
pHead=p->next;
cout<<"-------------------"<<endl;
delete p;
p=pHead;
}while(p&&p->age==age);
}
while(p){//
q=p->next;//q ,p
if(q&&q->age==age){
p->next=q->next;
cout<<" ";
q->display();
yes=1;
delete q;
cout<<"-------------------"<<endl;
}
else if(!q&&yes==0){
cout<<" "<<age<<" "<<endl;
cout<<"-------------------"<<endl;
return;
}
else
p=p->next;
}
}
void main(){
Student s1("X","Boy",22);
Student s2("Y","Boy",20);
Student s3("Z","Boy",21);
Student s4("U","Girl",22);
Link l;
l.Add(s1);
l.Add(s2);
l.Add(s3);
l.Add(s4);
l.Display();
l.Delete(21);
l.Display();
}
でも学生番号の問題は解決しなかった...