I have a structure which I name passengers, which is sorted in alphabetical order, and I copy it to a linked list with the same elements as structure. What would be the best algorithm to reverse the order of the elements of the list, so that I can print it out in alphabetical order easily? Now of course my printing doesn't work if the first value it encounters is NULL
typedef struct
{
char fullname[40];
unsigned short phonenr[10];
unsigned int seatnr;
}PASSENGERS;
typedef struct list1
{
char fullname[40];
struct list1 *next;
}LIST1;
int main()
{
selectsortnm(passenger); /*A function I use to sort alphabetically*/
LIST1 *list1, *start=NULL;
int i=0;
while (strcmp(passenger[i].fullname,"\0")!=0);
{
list1 = (LIST1 *) malloc (sizeof(LIST1));
list1->next = NULL;
strcpy(list1->fullname,passenger[i].fullname);
if (start ==NULL)
{
start = list1;
i++;
}
else
{
list1->next = start;
start = list1;
i++;
}
}
//Recursive algorithm
LIST1 *current = list1;
while (current !=NULL)
{
printf("%s",current->fullname);
current = current->next;
}
}
passengersarray backwards when creating the list, but you would need 2 loops: 1 to get last index (the last record whose fullname is notNULL), and the 2nd: looping from the index found by previous loop to 0. Hmm, but there's recursivity in neither 2 approaches.//Recursive algorithmis not recursive.