sort my linked list alphabetically
hello, I have a linked list and I want to add some things to it. I want to be able to sort my list alphabetically by the person's last name. I want it so that the user can see a list of last names in alphabetical order, so that she/he can than access that last name to see that persons record. That leads me to another thing i also want to be able to modify the person's record, for example change their gpa or thei advisors name.
I have and example of a sorted link list in alphbetical order, I just dont know how to implent it to my program. I dont know where to put it or what to change on it so that it can fit my need.
My program is at the top and the sample sorted list is at the bottom. Thankyou for your help.
My program:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <ctype.h>
#include <conio.h>
void addNewAccount(void);
void listAll(void);
void deleteAccount(void);
struct account {
int number;
char lastname[15];
char firstname[15];
char advisorname[15];
char advisornum[15];
char major[15];
float gpa;
struct account *next;
};
struct account *first,*current,*news;
int anum = 0;
void main()
{
char ch;
/* Initialize all the pointers */
first = (struct account *)NULL;
current = (struct account *)NULL;
news = (struct account *)NULL;
do
{
puts("\nA - Add a student");
puts("D - Delete a student");
puts("L - List all students");
puts("Q - Quit this program\n");
printf("\tYour choice:");
ch = toupper(getch());
switch(ch)
{
case 'A':
puts("Add new student\n");
addNewAccount();
break;
case 'D':
puts("Delete a student\n");
deleteAccount();
break;
case 'L':
puts("List All students\n");
listAll();
break;
case 'Q':
puts("Quit\n");
default:
break;
}
}
while(ch != 'Q');
}
void addNewAccount(void)
{
char buffer[100];
news = (struct account *)malloc(sizeof(struct account));
/* Check to see if this is the first record
* If so, then initialize all the pointers to this,
* first structure in the database
*/
if(first==(struct account *)NULL)
first = current = news;
/* Otherwise, you must find the end of the structure list
* (Easily spotted by the NULL pointer) and add on the
* new structure you just allocated memory for
*/
else
{
current = first; //make the first record the current one
//and loop through all records:
while(current->next != (struct account *)NULL)
current = current->next;
//the last record is found
current->next = news; //save the address of the new record
current = news; //and make the current record the new one
}
/* Now, you just fill in the new structure */
anum++;
printf(" student number: %5i\n",anum);
current->number = anum;
printf("Enter student's last name: ");
gets(current->lastname);
printf("Enter students's first name: ");
gets(current->firstname);
printf("Enter advisors's name: ");
gets(current->advisorname);
printf("Enter advisors's phone number: ");
gets(current->advisornum);
printf("Enter major: ");
gets(current->major);
printf("Enter student's GPA: ");
current->gpa = atof(gets(buffer));
/* Finally, cap the new record with a NULL pointer
* so that you know it's the last record:
*/
current->next = (struct account *)NULL;
}
void listAll(void)
{
if(first==(struct account *)NULL)
puts("There are no records to print!");
else
{
current=first;
do
{
printf("stundent #: %5i\n%-15s, %-15s\nadvisor: %-15s\nadvisor number: %-15s\nmajor: %-15s\ngpa: %1.2f\n",\
current->number,\
current->lastname,\
current->firstname,\
current->advisorname,\
current->advisornum,\
current->major,\
current->gpa);
}
while((current=current->next) != (struct account *)NULL);
}
}
void deleteAccount(void)
{
char ch;
struct account *previous;
if(first==(struct account *)NULL)
puts("There are no records to delete!");
else
{
current=first;
do
{
printf("stundent #: %5i\n%-15s, %-15s\nadvisor: %-15s\nadvisor number: %-15s\nmajor: %-15s\ngpa: %1.2f\n",\
current->number,\
current->lastname,\
current->firstname,\
current->advisorname,\
current->advisornum,\
current->major,\
current->gpa);
printf("DELETE THIS RECORD?");
ch = toupper(getch());
if(ch=='Y')
{
puts("Yes!");
if(current==first)
{
first=current->next;
break;
}
else
{
previous->next = current->next;
break;
}
}
else
{
puts("No!");
previous=current;
}
}
while((current=current->next) != (struct account *)NULL);
}
}
Sample program:
#include <stdio.h>
#include <string.h>
int main()
{
struct oz {
char actor[20];
char character[30];
int age;
};
struct oz cast[4] = {
"Judy Garland", "Dorothy", 17,
"Ray Bolger", "Scarecrow", 35,
"Bert Lahr", "Cowardly Lion", 44,
"Jack Haley", "Tin Woodsman", 40
};
struct oz *sorter[4];
struct oz *swapper;
int i,a,b,size;
/* Initialize the pointer array */
for(i=0;i<4;i++) {
sorter[i] = &cast[i];
}
/* Sort the list (From Chapter 9)*/
size = 4; /* the size of the array */
for(a=0;a<size-1;a++)
for(b=a+1;b<size;b++)
if(strcmp(sorter[a]->actor,sorter[b]->actor)>0)
{
swapper = sorter[b];
sorter[b] = sorter[a];
sorter[a] = swapper;
}
/* Print the sorted list */
printf("The Sorted Cast List:\n");
for(i=0;i<4;i++) {
printf("%s played the %s\n",sorter[i]->actor,sorter[i]->character);
}
return(0);
}