Hi, I am currently doing program where it is linked list and the user will need to input the name in the list. What I am stuck with is that I can't arrange those names alphabetically:confused: I tried arranging it but it only arrange the letters of the first element. For example, the elements are 'mitch, jane, kay, ron' the output will be 'chimt, jane, kay, ron'. But what output I am aiming to is 'jane, kay, mitch, ron'
Can you guys help me check how can this one be fixed? Below is my code:
Code:
#include<stdio.h>#include<stdlib.h>
#include<string.h>
typedef struct listNode {
char data[10];
struct listNode *nextNode;
} list; //'list' is datatype for listNode
typedef list *nodeptr; //having 'nodeptr' as datatype
nodeptr current, head, tail, newnode,temp, temp2; //all of this variables are pointer
int main() {
/* getting an input from a user and putting it on as a head
since this is an unsorted list, the first element will be the head*/
printf("Enter a name to be listed>> ");
current = malloc((10) *sizeof(char));
scanf("%s", ¤t->data);
current->nextNode = NULL;
head=current;
tail=current;
//and the element follows will either be tail or the current.next
printf("Enter a name to be listed>> ");
current = malloc((10) *sizeof(char));
scanf("%s", ¤t->data);
current->nextNode = NULL;
tail->nextNode=current;
tail=current;
printf("Enter a name to be listed>> ");
current = malloc((10) *sizeof(char));
scanf("%s", ¤t->data);
current->nextNode = NULL;
tail->nextNode=current;
tail=current;
printf("Enter a name to be listed>> ");
current = malloc((10) *sizeof(char));
scanf("%s", ¤t->data);
current->nextNode = NULL;
tail->nextNode=current;
tail=current;
printf("Enter a name to be listed>> ");
current = malloc((10) *sizeof(char));
scanf("%s", ¤t->data);
current->nextNode = NULL;
tail->nextNode=current;
tail=current;
current = head;
int i, j, tmp;
int n=5;
for (i = 0; i<n; i++){
for(j = i+1; j<n; j++){
if (current->data[i]>current->data[j]){
tmp = current->data[i];
current->data[i] = current->data[j];
current->data[j] = tmp;
}
}
}
printf("\nNames:\n");
while(current != NULL) {
printf("\t%s\n", current->data);//displaying element
printf("\t%d\n\n", ¤t->data);//displaying the address of an element
current=current->nextNode;
}
}