// copyright 1999 Brian J. Reithel, Ph.D. #include #include class building { char BuildingName[40]; float BuildingSqfeet; building* NextBuilding; // pointer to next building in llist public: float getsqfeet(); char* getname(); void info_in(void); void info_out(void); void setnext(building*); // modify pointer value building* getnext(void); // retrieve pointer value }; 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::setnext(building* nextbldg){ NextBuilding=nextbldg; } building* building::getnext(){ return(NextBuilding); } int main(){ building* first=NULL; // track address of 1st bldg building* temp=NULL; // temporary pointer building* prev=NULL; // temporary pointer to last instance created char nl; char another; cout << "Do you want to enter a buildindg? (y/n) "; cin.get(another); cin.get(nl); while(another=='Y'||another=='y') { temp=new building; temp->info_in(); if(first==NULL) first=temp; // keep track of first item else prev->setnext(temp); // store address of current item in previous item prev=temp; cout << "Do you want to enter another building? (y/n) "; cin.get(another); cin.get(nl); } if(prev!=NULL) prev->setnext(NULL); // mark end of llist /*int c; int maxc; cout << "How many buildings do you want to enter? "; cin >> maxc; cin.get(nl); first=new building; // store address of first item in llist first->info_in(); prev=first; for(c=1;cinfo_in(); prev->setnext(temp); // store address of current item in the previous item prev=temp; } prev->setnext(NULL); // mark the current item as the last in the llist */ /*temp=first; for(c=0;cinfo_out(); temp=temp->getnext(); }*/ temp=first; while(temp!=NULL) { temp->info_out(); temp=temp->getnext(); // get address of next item in llist } // save linked list to disk file fstream outfile; outfile.open("buildings.dat",ios::out); temp=first; while(temp!=NULL) { outfile << temp->getname() << endl; outfile << temp->getsqfeet() << endl; temp=temp->getnext(); // get address of next item in llist } outfile.close(); return(0); } char* building::getname() { return(BuildingName); } float building::getsqfeet() { return(BuildingSqfeet); }