i want a DeleteDepartment and AddDepartment function that fits into this project
ID: 660125 • Letter: I
Question
i want a DeleteDepartment and AddDepartment function that fits into this project
A department should be a child node of a spasific college
this is my code
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
class Department
{
public:
string name;
int numOfStudents;
Department* next;
Department(){name[0] ; numOfStudents=0 ; next=NULL ;}
Department( string n , int numS){ name =" "; n ;numOfStudents = numS ; next=NULL;}
void Print(){cout<<name<<" "<<numOfStudents;}
};
class College
{
public :
string name; //for name of the colleg
int numOfColleges;
int numDepartments; //number of departments in this college
Department* dep; //this will point to the department in this college
College * next; //the next pointer to point to the next college
College(){name =" "; numDepartments=0 ;dep=NULL; next=NULL;}
College (string n, int numD ){name=n ;next=NULL;}
void Print(){cout<<name<<" ";}
};
void AddCollege( College* &head)
{
string n;
int numD;
cout<<"Enter the name of the College : ";
cin>>n;
cout<<"Enter the number of Departments : ";
cin>>numD;
College* tmp = new College(n,numD);
if(!head)
{
head=tmp;
return;
}
College * t=head;
while(t->next) t=t->next;
t->next=tmp;
cout<<"college added";
}
void DeleteCollege(College*&head)
{
string name;
cout<<"enter name of college:";
cin>>name;
if((!head)||(!head->next && head->name!=name))
{cout<<"could not find "<<name<<" in the list "; return;}
if(head->next && head->name==name)
{
College *tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
return;
}
College* tmp = head;
College* t;
while(tmp->next)
{
if(tmp->name==name)
{
College* tmp = head;
head = head->next;
delete tmp;
tmp->next=NULL;
}
if(tmp->next->name == name)
{
t = tmp->next;
tmp->next = tmp->next->next;
delete t;
return;
}
tmp=tmp->next;
}
cout<<"could not find "<<name<<" in the list ";
};
/*Print the list of colleges int the college head*/
void printColleges(College*& head)
{
cout<<"ALL Colleges in Database : ";
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
}
void AddDepartment( College* &head)
{ if(!head)
{ cout<<"NO COLLEGES ADDED!!";
return;}
string n,Dname;
int numS;
College* tmp = head;
College*tmp2 = tmp;
;
while(tmp)
{
cout<<"->"<<tmp->name;
tmp=tmp->next;
cout<<endl;
}
cout<<"Type a College name to Add a Department inside ";
cin>>n;
cout<<"Enter Department name:";
cin>>Dname;
cout<<"Enter the number of students :";
cin>>numS;
while(n!=tmp->name)
{
tmp=tmp->next;
}
if(!tmp->dep)
{
College * tmpD = new Department(Dname,numS);
Department*tmpDD = tmpD;
head=tmp;
return;
}
/*if(tmp->dep)
{
Department *tmp3 = tmp->dep->next;
t=tmp->dep;
dep->next=tmp3;
}
*/
};
//void DeleteDepartment(College* head )
//{
//
//
// ;}
void PrintAll(College*head)
{
College * temp = head;
while(temp!=NULL)
{
temp->Print();
temp=temp->next;
}
};
int main()
{
int choice=0;
College * h = NULL;
while(choice!=11) //Menu choice repeat
{
cout<<" Menu ";
cout<<"1: Add a new college in the list ";
cout<<"2: Delete a college from the list ";
cout<<"3: Add a department in a college ";
cout<<"4: Delete a department from a college. ";
cout<<"5: Print all the colleges along with their departments ";
cout<<"6: Delete all the departments of a particular college ";
cout<<"7: Delete all the colleges from the list. ";
cout<<"8: Print the total number of students in a college. ";
cout<<"9: Find and print the college that has the highest number of students. ";
cout<<"10: Find and print the department that has the highest number of students. ";
cout<<"EXIT ";
cin>>choice;
switch(choice)
{
case 1: AddCollege(*&h);
break;
case 2: DeleteCollege(*&h);
break;
case 3: printColleges(*&h);
break;
case 4:
AddDepartment(*&h);
break;
/* case 4:
DeleteDepartment(*&h);
break;
case 5:
PrintAll(*&h);
break;
case 6:
DeleteDepartment(*&h);
break;
case 7:
DeleteAllColleges(*&h);
break;
case 8:
NumOfStudentsInCollege(*&h);
break;
case 9:
HighestNumOfStudentsInCollege(*&h);
break;
case 10:
HighestNumOfStudentsInDep(*&h);
break;
case 11:
cout<<"bye";
break;*/
default:
cout<<" Invalid menu choice";
}
}
};
Explanation / Answer
//Implementation of deleting department
Void DeleteDepartment(College* head )
string name;
cout<<"enter name of department:";
cin>>name;
if((!head)||(!head->next && head->name!=name))
{cout<<"could not find "<<name<<" in the list "; return;}
if(head->next && head->name==name)
{
Department *tmp=head;
head=head->next;
delete tmp;
tmp=NULL;
return;
}
Department* tmp = head;
Department* t;
while(tmp->next)
{
if(tmp->name==name)
{
Department* tmp = head;
head = head->next;
delete tmp;
tmp->next=NULL;
}
if(tmp->next->name == name)
{
t = tmp->next;
tmp->next = tmp->next->next;
delete t;
return;
}
tmp=tmp->next;
}
cout<<"could not find "<<name<<" in the list ";
};
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.