how to sort by last name

Discussion in 'C++' started by bengt23648, Sep 16, 2007.

  1. bengt23648

    bengt23648 New Member

    Joined:
    Mar 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    i found this code and i it doesnt have a sort function.. i want to know how to sort the entries by their last name..

    it's a linked list..

    Code:
    # include<iostream.h>
    # include<stdlib.h>
    # include<conio.h>
    # include<string.h>
    
    
    struct ListEntry
    {
    	int id;
    	char first_name[15]; 
    	char last_name[15];
    	char tel[11];
    	int age;
    
    	struct ListEntry *next;
    }
    	start,*node,*prev;
    
    
    /******************FUNCTION PROTOTYPES******************************/
    
    void add();
    void viewdb();
    
    
    
    /********************MAIN FUNCTION***********************************/
    
    void main()
    {
    	int choice;
    	clrscr();
    	cout<<"\n\n\n\t\t\t============================\n";
    	cout<<"\t\t\tEMPLOYEE INFORMATION SYSYTEM\n";
    	cout<<"\t\t\t============================\n\n";
    	cout<<"\n\t\t\t[1] Add Employee\n";
    	cout<<"\t\t\t[2] View Employee\n";
    	cout<<"\t\t\t[3] Exit\n";
    	cout<<"\n\n\t\t\tChoose an option: ";
    	cin>>choice;
    
    	switch(choice)
    	{
    		case 1:		add();
    
    							break;
    		case 2:		viewdb();
    		case 3:		exit(1);
    	}
    }
    
    
    /*****************************ADD EMPLOYEE********************************/
    
    void add()
    {
    	char choice;
    	start.next=NULL; //Empty List
    	node=&start; //Point to the start of the list
    	do
    	{
    		clrscr();
    		cout<<"\t\t\t==============================\n";
    		cout<<"\t\t\t     NEW EMPLOYEE MENU\n";
    		cout<<"\t\t\t==============================\n\n";
    		node -> next = (struct ListEntry *) malloc (sizeof (struct ListEntry));
    		node = node -> next;
    		cout<<"First Name\t\t";
    		cin>>node -> first_name;
    		cout<<"Last Name\t\t";
    		cin>>node -> last_name;
    		cout<<"Age\t\t\t";
    		cin>>node -> age;
    		cout<<"Tel. Number\t\t";
    		cin>>node -> tel;
    		cout<<"\n\nRecord saved. Input another? [y/n]         ";
    		cin>>choice;
    		node -> next = NULL;
    	}while(choice == 'Y'||choice == 'y');
    	clrscr();
    	main();
    }
    
    
    /*******************************VIEW EMPLOYEE*******************************/
    
    void viewdb()
    {
    	node = start.next;
    	clrscr();
    	cout<<"\t\t\t==============================\n\n";
    	cout<<"\t\t\t         EMPLOYEE LIST        \n\n";
    	cout<<"\t\t\t==============================\n\n";
    	cout<<"NAME\t\t\t\tAGE\tTELEHONE NUMBER\n\n";
    	cout<<"_________________________________________________________________________\n";
    
    	while(node)
    	{
    		cout<<node -> last_name<<", "<<node -> first_name<<"\t\t\t"<<node -> age<<"\t"<<node -> tel<<"\n";
    		node = node -> next;
    	} 
    	cout<<endl;
    	getch();
    	main();
    }
    
    
    
     
  2. bengt23648

    bengt23648 New Member

    Joined:
    Mar 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    can somebody give me any idea on how to bubble sort it (or any sorting algorithms..)

    thanks..
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Should we answer you here, or on one of the other of the three forums, or not at all? Just curious.
     
  4. bengt23648

    bengt23648 New Member

    Joined:
    Mar 20, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    actually, i've posted this in five forums.. :P

    you can answer me anywhere.. please help me with this..

    thanks a lot.
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    I don't think so. It would seem to be a lot of redundant work. You might not understand that until you become an answerer instead of an asker, but I assure you that it's somewhat ofputting that you splatter the entire internet with one question
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,336
    Likes Received:
    377
    Trophy Points:
    83
    Sort Linked List article should help you what you are looking for.
     
  7. navneet85

    navneet85 New Member

    Joined:
    Sep 17, 2007
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    by comparing the (node->last name) of different nodes in the linked list, the sorting could be done.This type of implementation would be the bubble sort method.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice