Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am Working on a project about university database and i got stuck in DeleteCol

ID: 659871 • Letter: I

Question

I am Working on a project about university database and i got stuck in DeleteCollege(College*&head) funcion that would take the head pointer and search for the name of the college and delete it by it's name without losing other colleges from the linked list

I have created 2 classes and an add function

#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<<" num Of Departments :"<<numDepartments;}


};


  
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) ;
   tmp->next = head;
   head=tmp;
      
       cout<<"college added";}
  
  
void Delete(College*& head) // this is where I'm stuck

int main() // Main should look like this
{
   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:
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";
}

};

These are "next" pointers pointing at object type "college" PMU CCE Eng Busi Arts Cor Me These are "departments" college object, pointing at object of type "department" IT Eco CS These are "next pointers inside department object, pointing at object of type "department

Explanation / Answer

//College header file

//College.h
#ifndef COLLEGE_H  
#define COLLEGE_H

#include<iostream>
#include<string>
#include "Department.h"
using namespace std;
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 ;
       numDepartments=numD;
       next=NULL;
   }
   void Print()
   {
       cout<<"College Name : "<<name<<" Num Of Departments :"<<numDepartments<<endl;
   }
};

#endif COLLEGE_H

--------------------------------------------------------

//Deparment header file

//Department.h
#ifndef DEPT_H  
#define DEPT_H
#include<iostream>
#include<string>
using namespace std;

class Department
{
public:
   string name;
   int numOfStudents;
   Department* next;

   Department()
   {
       name="";
       numOfStudents=0 ;
       next=NULL ;
   }
   Department( string n , int numS)
   {
       name =n;       
       numOfStudents = numS ;
       next=NULL;
   }
   void Print()
   {
       cout<<name<<" "<<numOfStudents;
   }

};

#endif DEPT_H
--------------------------------------------------------

/*Cpp program that add college name and number of departments
to the College list and also has method to print the list
of colleges in the college linked list*/
#include<iostream>
#include<string>
#include<string.h>
#include "College.h"
using namespace std;
//function prototype
void AddCollege( College* &head);
//function prototype for DeleteCollege
bool DeleteCollege(College*& head);
void printColleges( College* &head);

int main()                            // Main should look like this
{
   int choice=0;
   College * h = NULL;

   while(choice!=11) //Menu choice repeat
   {
       //Add delete and print options to menu choice
       cout<<" Menu ";
       cout<<"1:    Add a new college in the list ";
       cout<<"2:    Delete a college from the list ";
       cout<<"3:    Print all colleges ";
       cout<<"4:    Exit ";

       cout<<"EXIT ";
       cin>>choice;
       switch(choice)
       {

       case 1: AddCollege(*&h);
           break;
           //calling method DeleteCollege
       case 2: DeleteCollege(*&h);
           //calling method printColleges
           break;
       case 3: printColleges(*&h);
           break;
       case 4: exit(0);          
       }
   }
}

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) ;
   tmp->next = head;
   head=tmp;

   cout<<"college added";
}

//The method DeleteCollege that takes the head as input argument
//and prompts for the name of the college to delete from college list.
//and return true if college is deleted successfully otherwise return false
bool DeleteCollege(College*& head)
{
   string searchName;  
   cout<<"Enter the name of the College to delete: ";
   cin>>searchName;

   College *current, *previous;

   /* set previous node to NULL */
   previous = NULL;

   /*
   * traveset to each nod of list
   */
   for (current = head;current != NULL;previous = current, current = current->next)
   {
       //search for the name of college
       if (current->name.compare(searchName)==0)
       { /* check if previous is NULL */
           if (previous == NULL)
           {
               /* set current pointer to head*/
               head = current->next;
           }
           else
           {
               /*
               *
               * Otherwise set previouse node to curr over the removed node.
               */
               previous->next=current->next;
           }

           delete current; /* Deallocate the node. */
           cout<<"College is deleted successfully"<<endl;
           return true;
       }
   }
   cout<<"College is not found"<<endl;
   return false;
  
}

/*Print the list of colleges int the college head*/
void printColleges(College*& head)
{
   College * temp = head;
   while(temp!=NULL)
   {
       temp->Print();
       temp=temp->next;
   }
}

--------------------------------------------------------
sample output:


Menu
1:    Add a new college in the list
2:    Delete a college from the list
3:    Print all colleges
4:    Exit
EXIT
1
Enter the name of the College : oxford
Enter the number of Departments : 10
college added

Menu
1:    Add a new college in the list
2:    Delete a college from the list
3:    Print all colleges
4:    Exit
EXIT
1
Enter the name of the College : pollocks
Enter the number of Departments : 5
college added

Menu
1:    Add a new college in the list
2:    Delete a college from the list
3:    Print all colleges
4:    Exit
EXIT
2
Enter the name of the College to delete: oxford
College is deleted successfully


Menu
1:    Add a new college in the list
2:    Delete a college from the list
3:    Print all colleges
4:    Exit
EXIT
3
College Name : pollocks Num Of Departments :5


Menu
1:    Add a new college in the list
2:    Delete a college from the list
3:    Print all colleges
4:    Exit
EXIT
1
Enter the name of the College : Downtown
Enter the number of Departments : 12
college added

Menu
1:    Add a new college in the list
2:    Delete a college from the list
3:    Print all colleges
4:    Exit
EXIT
3
College Name : Downtown Num Of Departments :12
College Name : pollocks Num Of Departments :5


Menu
1:    Add a new college in the list
2:    Delete a college from the list
3:    Print all colleges
4:    Exit
EXIT


Note : Organize your project files College and Department into different header files as .h(header defition files)
and .cpp(implementation files) so that project will be well orangised.

Hope this helps you.