i have created a c++ link list using classes but now i want to sort the links by employee id number but have no clue how to do it.see files.
the sorting should be done in the EmployeeLink.cpp file
thank you
i have created a c++ link list using classes but now i want to sort the links by employee id number but have no clue how to do it.see files.
the sorting should be done in the EmployeeLink.cpp file
thank you
Sorry, but first you've got other issues that need to be resolved:
#1
Ugh!Code:void main()
#2
No point having those using std::string/setw bits in there if you've already got the using namespace std piece above them. That bit should likely go after your EmployeeLink header which would mean you'd need to modify that header to explicitly qualify and objects, std::string for example, that needed them as it currently relies on the aforementioned using std::string to make it work correctly. No header should have any using whatever declaration in them anywhere, if it needs something from the std namespace then like I said, explicitly qualify it as required.Code:#include<iostream> using namespace std; #include<string> using std::string; #include <iomanip> using std::setw; #include "EmployeeLink.h"
#3
Ick... this is a completely inappropriate use of recursion. You are asking for trouble.Code:void EmployeeLink::DeleteNode(int key) { EmployeeInfo *temp=head; EmployeeInfo *previousnode=NULL; while(temp !=NULL) { if(temp->getIdNumber()==key)//search for key { if(temp==head) { head=temp->getnextEmployeeInfo(); delete temp;//delete selected record cout<<" ***RECORD DELETED***"<<endl<<endl; menu();//diplay employee message return; } else { previousnode->setnextEmployeeInfo(temp->getnextEmployeeInfo()); delete temp; return; } } previousnode=temp; temp=temp->getnextEmployeeInfo(); } cout<<"***RECORD NOT FOUND***"<<endl<<endl; menu(); } ... void EmployeeLink::DisplayNode(int key) { EmployeeInfo *temp=head; while(temp!=NULL) { if(temp->getIdNumber()==key)//search for record to be displayed { temp->display(); menu();//display main menu return; } temp=temp->getnextEmployeeInfo(); } cout<<endl<<endl<<"***RECORD NOT FOUND***"<<endl<<endl; menu(); } //use to modify employee information void EmployeeLink::ModifyNode(int key) { EmployeeInfo *temp=head; while(temp!=NULL) { if(temp->getIdNumber()==key) { //displays menu for update options cout<<endl; char fname1[30],lname1[30];int dept;char pos[30];double hr_wrk; cout<<endl; cout<<" 1-->CHANGE FIRST NAME"<<endl; cout<<" 2-->CHANGE LAST NAME"<<endl; cout<<" 3-->CHANGE DEPARTMENT CODE"<<endl; cout<<" 4-->CHANGE POSITION"<<endl; cout<<" 5-->CHANGE HOURS WORK"<<endl; cout<<" 6-->BACK TO MAIN MENU"<<endl<<endl; cout<<" ENTER CHOICE: "; int choice; cin>>choice; cout<<endl<<endl; if (choice==1) { cout<<endl<<endl<<"ENTER NEW FIRST NAME: "; cin>>fname1; temp->setFirstName(fname1); cout<<endl<<" ***RECORD UPDATED***"<<endl; menu(); } if(choice==2) { cout<<"ENTER NEW LAST NAME: "; cin>>lname1; temp->setLastName(lname1); cout<<endl<<" ***RECORD UPDATED***"<<endl; menu(); } if(choice==3) { cout<<"ENTER NEW DEPARTMENT CODE: "; cin>>dept; temp->setDeptCode(dept); cout<<endl<<" ***RECORD UPDATED***"<<endl; menu(); } if(choice==4) { cout<<"ENTER NEW POSITION: "; cin>>pos; temp->setPosition(pos); cout<<endl<<" ***RECORD UPDATED***"<<endl; menu(); } if(choice==5) { cout<<"ENTER NEW HOURS WORK"; cin>>hr_wrk; cout<<endl<<" ***RECORD UPDATED***"<<endl; menu(); } } temp=temp->getnextEmployeeInfo(); } cout<<endl<<endl<<" ***RECORD NOT FOUND***"<<endl; menu(); } void EmployeeLink::menu() { int choice; EmployeeInfo *temp=head; cout<<endl; cout<<"--------------------------------------------------"<<endl<<endl; cout<<" EMPLOYEE PAYROLL RECORDS MENU"<<endl<<endl; cout<<"--------------------------------------------------"<<endl<<endl; cout<<" 1--> TO ADD NEW EMPLOYEE PAYROLL RECORD"<<endl; cout<<" 2--> TO UDATE AN EXISTING RECORD"<<endl; cout<<" 3--> TO VIEW A SINGLE EMPLOYEE PAYROLL RECORD"<<endl; cout<<" 4--> VIEW SORTED:"<<endl; cout<<" 5--> TO DELETE A RECORD"<<endl<<endl; cout<<endl<<endl<<" ENTER CHOICE: "; cin>>choice; cout<<endl<<endl; if (choice==1) { Input_Employee_Info();//call the add data function } if(choice==2) { int key1; cout<<endl<<endl<<" ENTER ID NUMBER TO MODIFY: "; cin>>key1; cout<<endl<<endl; ModifyNode(key1);//call the modify node funtion } if(choice==3) { int key2; cout<<endl<<endl<<" ENTER ID NUMBER TO DISPLAY: "; cin>>key2; cout<<endl<<endl; DisplayNode(key2); } if(choice==5) { int key3; cout<<endl<<endl<<" ENTER ID NUMBER TO DELETE: "; cin>>key3; cout<<endl<<endl; DeleteNode(key3); } if(choice==4) { sort(); } if(choice<0||choice>5) { cout<<"***IMPROPER CHOICE***"<<endl<<endl; menu(); } }
#4
Do you not plan on freeing any of the dynamically allocated memory the list maintains?Code:EmployeeLink::~EmployeeLink() { }
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
> i have created a c++ link list using classes but now i want to sort the links by employee id number but have no clue how to do it.see files.
You should create a much more simplified version of the problem:
- a bare minimum class with a ctor, a sort function and a print function
- a simple main() to drive it.
a) you'll find it much easier to understand yourself
b) you might just figure out the answer yourself
c) we'll be much more inclined to help you. Dumping several hundred lines of code and a 1-liner "fix this!" comment is one good way to ........ everyone off.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Except where you must have a `using' declaration to have correct behavior. If you do a lot of template programming you'll eventually arrive at a situation where it is a necessity.No header should have any using whatever declaration in them anywhere
You should instead say "You should never use `using' declarations in a header that may pollute the global namespace.". A `using' declaration local to some scope is perfectly acceptable and usually to be preferred.
[Edit]
Déjà vu!
[/Edit]
Soma
Last edited by phantomotap; 03-09-2010 at 06:37 PM. Reason: O_o