This question already has an answer here:
- Sorting a linked list in C 6 answers
I would to ask you, if it is possible to simple sort alphabetically linked list with names? I think that it is possible, but i dont know how. Can you help me with that? I will be very thankful.
"i" pressed should scan new name and add this name to linked list and then to sort this list alphabetically "d" pressed should to display entire sorted list
"k" pressed program ends
I did this with array of struct and it work well, but i dont know how to do same with linked list...
Thank you very much :)
here is code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
typedef struct list{
char name[100];
struct list *next;
}LIST;
int main()
{
int i, n, k = 0, v = 0, m = 0, j = 0;
char str[100], c;
LIST *p_first = NULL, *p_act = NULL, *p_prev = NULL;
while((c=getchar())!='k')
{
if(c=='i')
{
if(k == 0)
{
p_first = (LIST *) malloc(sizeof(LIST)); //scan first element of struct
scanf("%s", p_first->name);
p_act = p_first;
}
else
{
p_act->next = (LIST *) malloc(sizeof(LIST)); //scan next element of struct
p_act = p_act->next;
scanf("%s", p_act->name);
//here should be code to sort text alphabetically
}
p_act->next = NULL;
k++;
}
else if(c=='d')
{
//display all elements of linked list
for(i = 0; i < k; i++) {
if(i == 0)
p_act = p_first;
else
p_act = p_act->next;
printf("%s\n", p_act->name);
}
}
}
getchar();
return 0;
}