How can i sort the Last Names?? i used the Last Name to be the head so the properties or some info will follow...need help bum bump

#include <iostream>
#include <string.h>
#include <fstream>
using namespace std;
typedef struct Student 
{       
        char Fname[50];
        char Lname[50];
        char age[50];
        char ID[20];
        struct Student    *next;
}SI;

SI *head=NULL;
char YN;
void initList(SI **pos);
SI * createNode(char *Fname, char *Lname, char *age, char *ID);
void traverseList(SI *head);
void insertNode(SI **head, SI *node);
void YorN();           
void input();

int main(){

       initList(&head);
	   input();	   	  
	   //traverseList(head);
 
}

void input(){

char Fname[50],Lname[50],age[50],ID[50];
	
	cout << "First Name:\t";
    gets(Fname);

    cout << "Last Name:\t";
    gets(Lname);
    
    cout << "Age:\t";
    gets(age);
    
    cout << "ID:\t";
    gets(ID);
    
	insertNode(&head,createNode(Fname,Lname,age,ID));

		
	system("cls");
	YorN();
}



void YorN(){

	printf("Press ['Y' or 'y'] to continue or ['N' or 'n'] to not continue\n\n");
	scanf("%c",&YN);
	fflush(stdin);

		if(YN == 'Y' || YN == 'y'){
			system("CLS");
			input();
		}
		else if(YN == 'N' || YN == 'n'){
            system("CLS");
			traverseList(head);
			
		}
		else{
		
			YorN();
		}
}

void insertNode(SI **head, SI *node){

	SI *pos = NULL;

	if(*head == NULL){
		*head = node;
	}else{

		for(pos = *head; pos != NULL; pos = pos->next){
			if(pos->next == NULL){
				pos->next = node;
				break;
			}
		}
		
	}

}

void initList(SI **pos){
       *pos = NULL;
}
SI * createNode(char *Fname, char *Lname, char *age, char *ID){
  
       SI *temp = NULL;
       temp = (SI *) malloc (sizeof(SI));
	   strcpy(temp->Lname,Lname);
       strcpy(temp->Fname,Fname);
	   strcpy(temp->age,age);
       strcpy(temp->ID,ID);
       temp->next = NULL;

       return temp;
}

void traverseList(SI *head){

       SI *pos;
       cout<< "Student Information:\n\n";
       for(pos = head; pos !=NULL; pos = pos->next)
{
       
	cout<<"\n\n.Last Name:\t" << pos->Lname;
	cout<<"\nFirstName:\t" << pos->Fname;
	cout<<"\nAge:\t" << pos->age;
	cout<<"\nID:\t" << pos->ID;
     
	   ofstream myFile("test.txt");
                   if(myFile.is_open())
                   { 
                                       SI *pos;
                                       for(pos = head; pos !=NULL; pos = pos->next)
									   {
										   myFile<<"Last Name:" <<pos->Lname<<endl;
										   myFile<<"First Name:"<<pos->Fname<<endl;
										   myFile<<"Age:"<<pos->age<<endl;
										   myFile<<"ID:"<<pos->ID<<endl<<endl;
									   }                                          
                                               myFile.close();
                                               }
                                               else
                                               {
                                                   cout<<"Unable to open file.";
                                                   }
	 
       }
	     system("pause");
}

Edited 5 Years Ago by TheProgramer: n/a

This is a tutorial I wrote about sorting in C. It can easily be translated into C++, especially since you're using a a very C-centric subset of C++. Check out the insertion sort and merge sort sections for examples dealing with linked lists.

This article has been dead for over six months. Start a new discussion instead.