P
ptq2238
Hi, I've been experimenting further to understand about ordering
strings.
I had changed the code which tested ordered a list of char ( which
worked ) to string
but it's not giving the results I was expected. I was expecting to
have the strings in an increasing order.
Now, I'm getting confused and would appreciate if someone can explain
why I'm getting these results is greatly appreciated. Am I going about
this the correct way ?
struct testRec
{
char name[5];
struct testRec *nextrec;
};
typedef struct testRec records;
typedef records *recordptr;
void addName(recordptr *start, char *item);
int main ()
{
recordptr start = NULL;
char item[5];
.......
gets(item);
addRec(&start, item);
.....
return 0;
}
void addRec(recordptr *start, char *item)
{
recordptr newptr, prevptr, thisptr;
newptr = malloc(sizeof(records));
if(newptr != NULL)
{
strcpy(newptr->name, item);
newptr->nextrec = NULL;
prevptr = NULL;
thisptr = *start;
while (thisptr != NULL)
{
if (item > thisptr->name)
{
prevptr = thisptr;
thisptr = thisptr->nextrec;
}
}
if (prevptr == NULL)
{
newptr->nextrec = *start;
*start = newptr;
}
else
{
prevptr->nextrec = newptr;
newptr->nextrec = thisptr;
}
}
}
My output comes as
Enter a record :aa
List is : aa ->
Enter a record : dd
List is : aa -> dd ->
Enter a record : cc
List is :aa -> dd -> cc ->
Enter a record : bb
List is :aa -> dd -> cc -> bd ->
strings.
I had changed the code which tested ordered a list of char ( which
worked ) to string
but it's not giving the results I was expected. I was expecting to
have the strings in an increasing order.
Now, I'm getting confused and would appreciate if someone can explain
why I'm getting these results is greatly appreciated. Am I going about
this the correct way ?
struct testRec
{
char name[5];
struct testRec *nextrec;
};
typedef struct testRec records;
typedef records *recordptr;
void addName(recordptr *start, char *item);
int main ()
{
recordptr start = NULL;
char item[5];
.......
gets(item);
addRec(&start, item);
.....
return 0;
}
void addRec(recordptr *start, char *item)
{
recordptr newptr, prevptr, thisptr;
newptr = malloc(sizeof(records));
if(newptr != NULL)
{
strcpy(newptr->name, item);
newptr->nextrec = NULL;
prevptr = NULL;
thisptr = *start;
while (thisptr != NULL)
{
if (item > thisptr->name)
{
prevptr = thisptr;
thisptr = thisptr->nextrec;
}
}
if (prevptr == NULL)
{
newptr->nextrec = *start;
*start = newptr;
}
else
{
prevptr->nextrec = newptr;
newptr->nextrec = thisptr;
}
}
}
My output comes as
Enter a record :aa
List is : aa ->
Enter a record : dd
List is : aa -> dd ->
Enter a record : cc
List is :aa -> dd -> cc ->
Enter a record : bb
List is :aa -> dd -> cc -> bd ->