// Copyright 1999 Brian J. Reithel, Ph.D. #include #include class building { char BuildingName[40]; float BuildingSqFeet; building* NextBuilding; public: void info_in(void); // Enter building information. void info_out(void); // Display building information void setname(char*); // Update building name with passed value char* getname(void); // Return building name void setsqfeet(float); // Update square feet with passed value float getsqfeet(void); // Return square feet void setnext(building*); // Update pointer to next instance with value building* getnext(void); // Return pointer to next instance operator= (building); // Copy building name and square feet }; void building::info_in(){ char nl; cout << "Enter Building Name: "; cin.get(BuildingName,40,'\n'); cin.get(nl); cout << "Enter Building Square Feet: "; cin >> BuildingSqFeet; cin.get(nl); } void building::info_out(){ cout << "Building Name: " << BuildingName << endl; cout << "Square Feet: " << BuildingSqFeet << endl; } void building::setname(char* tname){ strcpy(BuildingName,tname); } char* building::getname(){ return(BuildingName); } void building::setsqfeet(float tsqfeet){ BuildingSqFeet=tsqfeet; } float building::getsqfeet(){ return(BuildingSqFeet); } void building::setnext(building* nextbldg){ NextBuilding=nextbldg; } building* building::getnext(){ return(NextBuilding); } building::operator= (building dup){ strcpy(BuildingName, dup.getname()); BuildingSqFeet=dup.getsqfeet(); // note: leave pointer structure alone to avoid // damage to the linked list of instances // (i.e., simply swap the user data values) } int main(){ building* first=NULL; building* temp=NULL; building* prev=NULL; char nl; char another; // --------------------------------------------------------------- // enter the list cout << "Do you want to enter a building? (Y/N) "; cin.get(another); cin.get(nl); while(another=='y'||another=='Y'){ temp=new building; cout << endl; // on-screen spacing temp->info_in(); if(first==NULL){ // if this is the first instance, store its address first=temp; prev=first; } else { // otherwise make the next link in the list prev->setnext(temp); prev=temp; } cout << "Do you want to enter a building? (Y/N) "; cin.get(another); cin.get(nl); } if(first!=NULL) prev->setnext(NULL); // mark the end of the list // --------------------------------------------------------------- // sort the list building tbuilding; // temporary instance for swap building* checkit; // temporary pointer to follow // through the rest of the list temp=first; while(temp!=NULL){ checkit=temp->getnext(); while(checkit!=NULL){ if(strcmp(temp->getname(),checkit->getname())>0){ // swap their data values, leave pointers unchanged tbuilding= *temp; *temp= *checkit; *checkit= tbuilding; } checkit=checkit->getnext(); } temp=temp->getnext(); } // --------------------------------------------------------------- // display the list temp=first; while(temp!=NULL){ cout << endl; // spacing temp->info_out(); temp=temp->getnext(); } return(0); }