CHAPTER3-12.cpp
上传用户:fjc899
上传日期:2007-07-03
资源大小:187k
文件大小:1k
- //文件名:CHAPTER3-12.cpp
- #include<iostream.h>
- #include<string.h>
- class Student
- { int number;
- static Student *ip;
- Student *p;
- public:
- Student(){p=NULL;}
- Student(int n);
- static Student* get_first(){return ip;}
- int get_number(){return this->number;}
- Student* get_next(){return this->p;}
- };
- Student::Student(int n):number(n) //依据学号的大小顺序将学生对象插入链表
- { p=NULL;
- if(ip==NULL)ip=this; //如果是第一个则使头指针指向该对象
- else{Student *temp=ip;
- if(n<ip->number){ip=this;p=temp;}//如学号小于第一个学生对象的学号则使头指针指向该对象
- else { while(temp){
- if(n<temp->p->number)
- { p=temp->p; //链中间对象的插入
- temp->p=this; break;
- }else { if(temp->p->p==NULL) //最后一个链的插入
- {temp->p->p=this;break; } }
- temp=temp->p;
- }
- }
- }
- }
- Student* Student::ip=NULL;
- template<class T>
- class Class
- { int num;
- T *p;
- public:
- Class(){}
- Class(int n):num(n){p=NULL;}
- T* insert(int n){p=new T(n);return p;}
- void list_all_member(T* x)
- { T *temp=x;
- while(temp) { cout<<temp->get_number()<<",";temp=temp->get_next();}
- }
- };
- void main()
- { Class<Student> x97x(9707);
- x97x.insert(23);
- x97x.insert(12);
- x97x.insert(38);
- x97x.insert(22);
- x97x.insert(32);
- x97x.list_all_member(Student::get_first());
- }