Project Rosalynde for KAIST student
#include "main.h"
int main() {
semester(20);
semester(10);
return 0;
}
void semester(int studentCnt) {
DormitorySystem dormSystem(studentCnt);
dormSystem.printList();
dormSystem.newWeek(3);
dormSystem.printList();
dormSystem.newWeek(7);
dormSystem.printList();
dormSystem.newWeek(7);
dormSystem.printList();
dormSystem.newWeek(6);
dormSystem.printList();
}
DormitorySystem::DormitorySystem(int studentCnt)
{
studentList=new Student;
studentList->next=NULL;
char a='a';
for(int i=0; i<studentCnt; i++)
{
Student* tmp= new Student;
tmp->id=a;
a++;
tmp->next=studentList->next;
studentList->next=tmp;
}
waitingHead=studentList->next;
for(int i=0; i<10; i++) waitingHead=waitingHead->next;
Student* tmp=studentList->next;
for(int i=0; i<10; i++)
{
dormList[i]=tmp;
tmp=tmp->next;
}
cout<<"Constructor"<<endl;
}
DormitorySystem::~DormitorySystem() {
for(int i=0; i<10; i++)
{
delete dormList[i];
}
Student* tmp=waitingHead;
while(tmp!=NULL)
{
Student* remove=tmp;
tmp=tmp->next;
delete remove;
}
cout<<"Destructor"<<endl;
}
void DormitorySystem::newWeek(int leavedStudent) {
delete dormList[leavedStudent];
dormList[leavedStudent]=NULL;
if(waitingHead!=NULL)
{
dormList[leavedStudent]=waitingHead;
waitingHead=waitingHead->next;
}
}
void DormitorySystem::printList() {
cout<<endl<<"=====print List====="<<endl;
cout<<"<Dormitory>"<<endl;
for(int i=0; i<10; i++)
{
if(dormList[i] !=NULL) cout << dormList[i]->id << " ";
}
cout<<endl;
cout<<"<Waiting>"<<endl;
int cnt = 0;
Student* tmp=waitingHead;
while(tmp!=NULL)
{
cout << tmp->id << " ";
tmp=tmp->next;
cnt++;
}
cout << endl;
cout<<"There are "<<cnt<<" waiting students"<<endl;
}