Can we sort elements inside the linked list using qsort, if not what must we do to sort elements inside the linked list?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct elephant
{
char name[10];
struct elephant *next;
}ELEPHANT;
void print_elephants( const ELEPHANT* ptr);
int main()
{
/* define 3 ELEPHANT variables and 1 pointer to elephant. */
ELEPHANT elephant1, elephant2, elephant3, *start;
/* Store elephants' name */
strcpy( elephant1.name, "Jake" );
strcpy( elephant2.name, "Elmer" );
strcpy( elephant3.name, "Kent" );
/* link elephants */
elephant1.next = &elephant2; /* Edna points to Elmer */
elephant2.next = &elephant3; /* Eloise is last */
/* start contains the address of the first node */
start = &elephant1;
print_elephants( start );
return EXIT_SUCCESS;
}
void print_elephants( const ELEPHANT* ptr)
{
int count = 1;
printf( "\n\n\n");
while ( ptr != NULL )
{
printf( "\nElephant number %d is %s.", count++, ptr -> name );
ptr = ptr -> next;
}
}