how to sort by last name

bengt23648's Avatar, Join Date: Mar 2007
Light Poster
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's Avatar, Join Date: Mar 2007
Light Poster
can somebody give me any idea on how to bubble sort it (or any sorting algorithms..)

thanks..
DaWei's Avatar, Join Date: Dec 2006
Team Leader
Should we answer you here, or on one of the other of the three forums, or not at all? Just curious.
bengt23648's Avatar, Join Date: Mar 2007
Light Poster
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's Avatar, Join Date: Dec 2006
Team Leader
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's Avatar, Join Date: Jul 2004
Go4Expert Founder
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's Avatar, Join Date: Sep 2007
Newbie 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.