C++学习(26)

  1 //异质单链表
  2 //大学人员问题
  3 //设计要求:
  4 //大学人员分为两类:
  5 //一类是教学人员,另一类是非教学人员,这两类人员的信息管理系统中,一部分信息内容是相同的
  6 //但另一部分内容是不相同的,
  7 //设教学人员的信息包括姓名、年龄和专员编号
  8 //非教学人员的信息包括姓名、年龄和业绩评级
  9 //现要求设计一个能同时存储学校教学人员的异质单链表类,并设计一个程序进行测试
 10 #include<string.h>
 11 #include<iostream.h>
 12 #include<stdlib.h>
 13 
 14 class DLinList;
 15 //大学人员类
 16 class Person{
 17   friend class DLinList;
 18   protected:
 19     char name[10];
 20     int age;
 21     Person *next;
 22     static Person *point;
 23   public:
 24     Person(char *nm,int ag);
 25     ~Person(){}
 26     virtual void CreateNode(){}
 27     virtual void Print();
 28 };
 29 
 30 Person::Person(char *nm,int ag){
 31   strcpy(name,nm);
 32   age=ag;
 33   next=NULL;
 34 }
 35 
 36 void Person::Print(){
 37   cout<<"姓名: "<<name<<endl;
 38   cout<<"年龄: "<<age<<endl;
 39 }
 40 
 41 
 42 //教学人员类
 43 class Professor:public Person{
 44   private:
 45     int SpecialNo;
 46   public:
 47     Professor(char *nm,int ag,int sn):Person(nm,ag){
 48       SpecialNo=sn;
 49     }
 50     ~Professor(){}
 51     void CreateNode();//虚函数
 52     void Print();//虚函数
 53 };
 54 
 55 void Professor::CreateNode(){
 56   point=new Professor(name,age,SpecialNo);
 57 }
 58 
 59 void Professor::Print(){
 60   Person::Print();
 61   cout<<"专业编号: "<<SpecialNo<<endl;
 62 }
 63 
 64 //非教学人员类
 65 class Staff:public Person{
 66   private:
 67     char Comment;//业绩评级
 68   public:
 69     Staff(char *nm,int ag,char cm):Person(nm,ag){
 70       Comment=cm;
 71     }
 72     ~Staff(){}
 73     void CreateNode();
 74     void Print();
 75 };
 76 
 77 void Staff::CreateNode(){
 78   point=new Staff(name,age,Comment);
 79 }
 80 
 81 void Staff::Print(){
 82   Person::Print();
 83   cout<<"业绩评级: "<<Comment<<endl<<endl;
 84 }
 85 
 86 class DLinList{
 87   private:
 88     Person *head;
 89     int size;
 90   public:
 91     DLinList(){
 92       head=NULL;
 93       size=0;
 94     }
 95     ~DLinList();
 96     Person *Index(int pos)const;//定位pos
 97     void Insert(Person *p,int pos);//在第pos节点前插入指针p所指的节点
 98     void Delete(int pos);//删除第pos个节点
 99     void Print();//输出异质单链表的节点的数据域值
100 };
101 
102 DLinList::~DLinList(){
103   Person *curr,*prev;
104   curr=head;
105   while(curr!=NULL){
106     prev=curr;
107     curr=curr->next;
108     delete prev;
109   }
110   size=0;//节点个数设为0
111 }
112 
113 //定位pos
114 Person *DLinList::Index(int pos)const{
115   if(pos<-1 || pos>size){
116     cout<<"参数pos越界出错!"<<endl;
117     exit(0);
118   }
119   if(pos==-1){
120     return head;
121   }
122   Person *curr=head;
123   int i=0;
124   while(curr!=NULL && i<pos ){
125     curr=curr->next;
126     i++;
127   }
128   return curr;//返回第pos个节点
129 }
130 
131 //在第pos节点前插入指针p所指的节点
132 void DLinList::Insert(Person *p,int pos){
133   if(pos<0 || pos>size){
134     cout<<"参数pos越界出错!"<<endl;
135     exit(0);
136   }
137 
138   Person *prev=Index(pos-1);
139   p->CreateNode();//创建新的节点
140   if(pos==0){
141     Person::point->next=head;
142     head=Person::point;
143   }else{
144     Person::point->next=prev->next;
145     prev->next=Person::point;
146   }
147   size++;
148 }
149 
150 //删除第pos个节点
151 void DLinList::Delete(int pos){
152   if(size==0){
153     cout<<"链表已空没有元素可以删除"<<endl;
154     exit(0);
155   }
156   if(pos<0 || pos>size){
157     cout<<"错误!参数pos越界"<<endl;
158     exit(0);
159   }
160   Person *kill;
161   Person *prev=Index(pos-1);
162   if(pos==0){//如果删除的是头结点
163     kill=head;
164     head=head->next;
165   }else{//如果删除的不是头结点
166     kill=prev->next;
167     prev->next=prev->next->next;
168   }
169   delete kill;
170   size--;
171 }
172 
173 //输出异质单链表的节点的数据域值
174 void DLinList::Print(){
175   Person *curr=head;
176   while(curr!=NULL){
177     curr->Print();
178     curr=curr->next;
179   }
180 }
181 
182 Person *Person::point=NULL;//static静态变量需要手动赋值
183 
184 int main(){
185   DLinList personList;
186 
187   Professor pro1("张三",40,2);
188   Professor pro2("李四",50,4);
189 
190   Staff staf1("王五",30,'A');
191   Staff staf2("赵六",20,'B');
192 
193   personList.Insert(&pro1,0);
194   personList.Insert(&pro2,1);
195   personList.Insert(&staf1,2);
196   personList.Insert(&staf2,3);
197 
198   personList.Delete(0);
199   personList.Print();
200 
201   return 0;
202 }

猜你喜欢

转载自www.cnblogs.com/Tobi/p/9250759.html