1. ## Another Linked List Question

I have a linked list. Each node has 2 pieces of data (a name and a number) plus the pointer to the next node.

One of the pieces of data is an arbitrary number. I need to somehow create a function that can print out the names in the ascending order of their corresponding numbers.

for example if:

Code:
```NODE 1: brad,5
NODE 2: joe, 7
NODE 3: ralph, 2```
then my function should print out:
Code:
```ralph
joe```
does anyone know how I might go about doing this, I'm new to link lists and this is confusing for me, I'm not asking for code just ideas.

Thank You

2. The easiest way to do this is simply to insert the node into the list wherever it falls numerically. Sort it as you build the list. Loop your way through the list checking the value of the node you're on (or the next one, either way, but be careful and always check for NULL) against what you're inserting.

Otherwise, you can loop through the list N times, where N is the size of the entire list, finding the "next lowest" each time, and printing it.

Quzah.

3. I don't understand, this one has got me really confused. You suggested to ways to do this. I'm not sure how to implement either.

For the first one, my linked list is already made... if I created another one from it.... I don't see how I can scroll a pointer around a linked list to look for things, cause it seems like I can only momve forward and once I do I can't move back to the previous node

I do have the number of nodes available to me, but I still dont see how I can loop through my list and look for things

This is terribly confusing to me

4. Code:
```struct node *ptr;
if( nodetoadd->data < topoflist->data )
prepend this to the top of the list
else
for( ptr = topoflist; ptr != NULL; ptr = ptr->next ) /* walk through the list */
{
if( ptr->next == NULL )
add the node to the end of the current node
ie: the end of the list
else
if( nodetoadd->data < ptr->next->data )
insert between 'ptr' and 'ptr->next'
/* else, keep moving down the list... */
}```
Something like that.

Quzah.

5. I tried to do this.... but it didn't work, I don't know why it doesn't work though

Code:
```void sort_list(STUDENT* stud) {

STUDENT* first;
STUDENT* second;
STUDENT temp_student;

first = stud;

while( first != NULL ) {

second = first -> studentPtr ;

while( second != NULL ) {

if ( (first -> ID) < (second -> ID) ) {

temp_student.ID = first->ID;
strcpy(temp_student.name,first->name);
first->ID = second->ID;
second->ID=temp_student.ID;
strcpy(second->name,temp_student.name);

strcpy(temp_student.name,"");
temp_student.ID = 0;

}
second = second -> studentPtr;
}
first = first -> studentPtr;
}
}```

6. ## Error /* In red */

Code:
```void sort_list(STUDENT* stud)
{
STUDENT* first;
STUDENT* second;
STUDENT temp_student;

first = stud;

while( first != NULL )
{
second = first -> studentPtr ;
while( second != NULL )
{
if ( (first -> ID) < (second -> ID) )
{
temp_student.ID = first->ID;
strcpy(temp_student.name,first->name);

first->ID = second->ID;
strcpy(first->name, seconed->name);
second->ID=temp_student.ID;
strcpy(second->name,temp_student.name);

strcpy(temp_student.name,"");
temp_student.ID = 0;
}
second = second -> studentPtr;
}
first = first -> studentPtr;
}
}```

7. What's wrong with the part in red? It moves my data from the second node being tested to the first, if the first is less than the second....

8. ## I added this line

I added this line

Popular pages Recent additions