1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

how to sort by last name

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

  1. bengt23648

    bengt23648 New Member

    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

    can somebody give me any idea on how to bubble sort it (or any sorting algorithms..)

    thanks..
     
  3. DaWei

    DaWei New Member

    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

    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

    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

    Sort Linked List article should help you what you are looking for.
     
  7. navneet85

    navneet85 New Member

    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