Need help with this C++ program, can\'t get mine to run and everything that I fo
ID: 3580944 • Letter: N
Question
Need help with this C++ program, can't get mine to run and everything that I found on chegg won't compile:
(A) You are going to write a class called Prof that stores a professor's name, office location (room number) and phone number. This part is very easy.
(B) You will write a linked list class that can be used to maintain a sorted list of professors. We talked about this in class in details. Now you have to read and implement. This class should
1) sort the names in alphabetical order of the last names. If there is a tie, look at the first name to break the tie.
2) should have the capability to both insert and delete.
3) search for a professor in the list by the full name. (Other keys for the search is possible but not necessary to implement because the essential functionality is same.).
4) should be able to display the list. Overload the insertion operator (<<) so that you can both display the list on the screen or save it in a file. The display should look "nice". Choose a format that is pleasing to the eye.
5) Overload the extraction operator (>>) so that you can receive inputs from the console or read from a file.
(C) Write a driver program that uses the list class to enter data and to display it. The driver program should be able to receive data from a file or user input. (It is essentially calling the overloaded operator provided by the list class).
(D) Test using the list of professors in any department
Explanation / Answer
#include<iostream>
#include<cstdlib>
#include<conio.h>
using namespace std;
//creation of structure for list
struct node{
//object of professor class
Prof prof;
struct node *next;
};
//linked list class
public class Link{
node *list;
public:
Link();
Link::Link(){
list = NULL;
}
//insert method to insert details of professor
void Link::insert(Prof pf){
node *p = new node;
p->prof = pf.fname;
p->next = list;
list = p;
}
//delete method to delete details of professor from the begining
void Link::delete_at_begining(){
node *p = new node;
if(list == NULL){
cout<<" List is Empty ";
}else{
p = list;
list = list->next;
delete p;
}
}
//delete method to delete details of professor from the end
void Link::delete_at_end(){
node *p = new node;
node *l = new node;
if(list == NULL){
cout<<" List is Empty ";
}else{
p = list;
if(p->next == NULL){
list = NULL;
delete p;
}else{
while(p->next!= NULL){
l = p;
p = p->next;
}
l->next = NULL;
delete p;
}
}
}
//sort method to sort details of professor according to name
void Link::sort_by_name(){
node *p = new node;
p = list;
node*q,top;
q=p->next;
if(list==NULL){
cout<<" list is empty ";
}else{
if( (top = (LIST *)malloc(sizeof(LIST))) == NULL) {
fprintf( stderr, "Memory Allocation error. " );
// In Windows, replace following with a return statement.
exit(1);
}
top->next = start;
while( p->next != NULL ) {
/* push bigger items down */
if( p->prof.name > p->next->prof.name ) {
p->next = p2->next->next;
p->next->next = p;
q->next = p->next->next;
}
q = p;
if( p->next != NULL )
p = p->next;
}
}
}
p = top->next;
free( top );
return p;
}
}
//display method
void Link::display(){
node *p = new node;
p = list;
if(list==NULL){
cout<<" Nothing to Display ";
}else{
cout<<" The contents of list ";
while(p!=NULL){
cout<<p->info<<endl;
p = p->next;
}
}
}
//serch method
void Link::search(string name){
node *p = new node;
p = list;
while (list != NULL)
{
if (list->prof.fname == name)
{
printf("name found ");
cout<<"first name is "<<list->prof.fname;
cout<<"last name is "<<list->prof.lname;
return;
}
list = list->next;
}
};
// professor class
class Prof{
public:
string fname,lname,location;
int phoneno;
Prof{
cout<<"Enter first name";
cin>>fname;
cout<<"Enter last name";
cin>>lname;
cout<<"Enter location";
cin>>location;
cout<<"Enter phone number";
cin>>phoneno;
}
};
int main(){
//object of professor class
Prof prof;
string name;
//object creation of linked list
Link link;
link.insert(prof);
link.delete_at_begining();
link.delete_at_end();
link.sort_by_name();
link.display();
cout<<"enter name that u want to search";
cin>>name;
link.search(name);
return 0;
}