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(); }
actually, i've posted this in five forums.. :P you can answer me anywhere.. please help me with this.. thanks a lot.
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
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.