Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   how to sort by last name (http://www.go4expert.com/forums/sort-t6378/)

bengt23648 16Sep2007 15:01

how to sort by last name
 
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();
}


bengt23648 16Sep2007 15:03

Re: how to sort by last name
 
can somebody give me any idea on how to bubble sort it (or any sorting algorithms..)

thanks..

DaWei 16Sep2007 17:42

Re: how to sort by last name
 
Should we answer you here, or on one of the other of the three forums, or not at all? Just curious.

bengt23648 16Sep2007 17:48

Re: how to sort by last name
 
Quote:

Originally Posted by DaWei
Should we answer you here, or on one of the other of the three forums, or not at all? Just curious.

actually, i've posted this in five forums.. :P

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

thanks a lot.

DaWei 16Sep2007 23:28

Re: how to sort by last name
 
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

shabbir 17Sep2007 09:22

Re: how to sort by last name
 
Quote:

Originally Posted by bengt23648
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..

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

navneet85 17Sep2007 15:04

Re: how to sort by last name
 
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.


All times are GMT +5.5. The time now is 10:51.