I have a problem here, I want to sort my Linked List with 3 data input, but when I executed this, only 1 data that be sorted. I have tried to mapping the temporary that will be replaced to a new nodes, but mapping only work on num_id data. Where is
For example:
- Data need to be sorted
21507 - John - Mathematics
21477 - Andrew - Biology
21905 - James - Physics
21322 - Sophia - Chemistry
- Expected result
21322 - Sophia - Chemistry
21477 - Andrew - Biology
21507 - John - Mathematics
21905 - James - Physics
- What I got
21322 - John - Mathematics
21477 - Andrew - Biology
21507 - James - Physics
21905 - Sophia - Chemistry
This is my script:
Nodes
struct nodes{
int num_id;
char name[30], lesson[30];
struct nodes *link;
}*head, *current, *temp, *tail;
Sorted Linked List
void linked_list_sorted() {
struct nodes *node, *temp_sorted;
int temp_sortedvar_num_id, count_data=0;
char temp_sortedvar_name[30], temp_sortedvar_lesson[50];
node = head;
while(node != NULL)
{
temp_sorted=node;
while (temp_sorted->link !=NULL)
{
if(temp_sorted->num_id > temp_sorted->link->num_id)
{
temp_sortedvar_num_id = temp_sorted->num_id;
temp_sorted->num_id = temp_sorted->link->num_id;
temp_sorted->link->num_id = temp_sortedvar_num_id;
}
else if(temp_sorted->name > temp_sorted->link->name)
{
strcpy(temp_sortedvar_name, temp_sorted->name);
strcpy(temp_sorted->name, temp_sorted->link->name);
strcpy(temp_sorted->link->name, temp_sortedvar_name);
}
else if(temp_sorted->lesson > temp_sorted->link->lesson)
{
strcpy(temp_sortedvar_lesson, temp_sorted->lesson);
strcpy(temp_sorted->lesson, temp_sorted->link->lesson);
strcpy(temp_sorted->link->lesson, temp_sortedvar_lesson);
}
temp_sorted = temp_sorted->link;
}
node = node->link;
}
temp_sorted = head;
while(temp_sorted != NULL) {
count_data++;
printf("%d. %d - %s - %s\n", count_data, temp_sorted->num_id, temp_sorted->name, temp_sorted->lesson);
temp_sorted = temp_sorted->link;
}
}
and the last, this is my script to push the data to Linked List:
void push_data (int nim, char name[], char lesson[]) {
// Push Step (Head, Mid, Tail)
current = (struct nodes*)malloc(sizeof(struct nodes));
current->nim = nim;
strcpy(current->name, name);
strcpy(current->lesson, lesson);
if (head == NULL){
head = tail = current;
} else if (current->nim < head->nim) {
current->link = head;
head = current;
} else{
tail->link = current;
tail = current;
}
}
strcpyof data? That does not make any sense to me. Just reorder the nodes and don't touch the names.current->nimdid you meancurrent->num_id?