Thread: sorting a c++ link list

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    1

    sorting a c++ link list

    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

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Sorry, but first you've got other issues that need to be resolved:

    #1
    Code:
    void main()
    Ugh!




    #2
    Code:
    #include<iostream>
    using namespace std;
    #include<string>
    using std::string;
    #include <iomanip>
    using std::setw;
    
    #include "EmployeeLink.h"
    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.




    #3
    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();
    	}
    }
    Ick... this is a completely inappropriate use of recursion. You are asking for trouble.



    #4
    Code:
    EmployeeLink::~EmployeeLink()
    {
    	
    }
    Do you not plan on freeing any of the dynamically allocated memory the list maintains?
    "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

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > 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.

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    No header should have any using whatever declaration in them anywhere
    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.

    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. c program that accepts and executes commands?
    By Cimposter in forum C Programming
    Replies: 3
    Last Post: 09-30-2009, 02:58 PM
  2. sorting linked list using link manipulation
    By roaan in forum C Programming
    Replies: 9
    Last Post: 07-23-2009, 05:15 PM
  3. sorting link list
    By behzad_shabani in forum C Programming
    Replies: 22
    Last Post: 12-02-2008, 05:47 PM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM