• 01-22-2012
[Student]
Please I Need help in retrieving the value of the node of its given position

here's my code, but there's is a problem in returning the value . .

Code:

```int n=1;       Node *newNode=head;       while(newNode->next!=NULL)       {             if(newNode->data==location) {               newNode=newNode->next;                 n++;             } else {                 break;           }       }       return newNode->data;```
• 01-22-2012
[Student]
if i have 7 2 3

I enter position 1 and it must return 7....

but i have a new element at first so 2 7 2 3

I enter position 1 why is it returning the same value "7". .. instead of 2 . . that's my problem
• 01-22-2012
whiteflags
It would be better to start completely over.

You're ok until we get to here:
if(newNode->data==location)
Will compare the data in the node to the location you want to find. These two things don't really coincide. Plus, you break the loop at the point the comparison is false, which will be most of the time.

You should be counting the number of nodes you've passed over, and break when you reach the location. Data has nothing to do with it.
• 01-26-2012
seanJ
I'm new also but i think this is about what you are trying to do

Code:

```int location;  // which list do you want to access? int ctr;        // needs a counter to keep track of the list we are currently in node* conductor = head;  // start at the beginning for(ctr = 1; ctr != location; ctr++) //run this loop until you are at the nth list {     if(conductor->next == (node*)NULL) // seeing if there is a next list     {         // location does not exist, insert some error handling stuff         break;     }     conductor = conductor->next; // moving thru the next link } if((ctr == location) && (conductor != (node*)NULL))  // pointer is good, and we should be in the right list { return conductor->data;  // return some data }```
• 01-26-2012
Elysia
The term "list" is used inappropriately here. The term you seek is "node."
There is only one list, and it links several nodes together.
• 01-27-2012
Elkvis
Quote:

Originally Posted by seanJ
I'm new also but i think this is about what you are trying to do

you're new, so the forum rules should be fresh in your memory, having read them very recently, right? so why did you post a complete solution, which is against the rules?
• 01-28-2012
seanJ
Sorry, I didn't know that
• 01-28-2012
seanJ
So the *head is the list, *conductor is a node, and conductor->next is a link? Is there a special name for a list that loops, where the last link points to the first node? I will read the forum rules when I get home, wasn't trying to agitate anyone. List's are kind of a weird concept, at least they seemed that way when I first read about them because of how they are accessed. It didn't make any sense to me until I really focused on a few functions In my book about them.
• 01-28-2012
laserlight
Quote:

Originally Posted by seanJ
Is there a special name for a list that loops, where the last link points to the first node?