Can I have some idea's as to how to do that?

Like say you have a piece of code that created n lists that are linked. Then you ask which node would you like to change the data?

How would one do it?

My Idea was the node contains N and data.
We can have the N start out at one when it's created, then have the next node that' the first one is pointing to have thta N +1 making it 2.

Then have some number outside, compare iet to that N, if that matches, then pointer moves to that node and change the data?

I'm not sure if it's possible doing it this way though.

You have to start at the beginning of the linked list and search for the node you want -- somethng llike this:

struct* node = head;
while(node->next)
{
   // check if this is the node you want
   //
   // now advance to the next node
   node = node->next;
}

Yeah, I know that.
So the idea I have is good?

Or is there another way that seems better.

Dear linked list traversal architect, don't waste a time, make a code ;)

this is the code I have made

int n;
cout<<"Which list would you like to go to? \n";
cin>>n;

if(n==2)
{
node *ptr;
ptr = cur_node -> next;  
//cur_node was the node I named for a linkst list
while( ptr != NULL)
{
    if(ptr == n)   //not sure what to put here
    {
       cout<<"you're here";
              }
      else
      {
         ptr++;  //moves pointer along.
                }

}

}

it says pointer can't point to an integer.
So I'm not sure what to put in that if statement.

Is n supposed to be the node number, that is, when there are 10 nodes and n = 3, then it wants the 3d node?

node *ptr = cur_node;
int count = 0;
for( count = 0; count < n && ptr != NULL; count++)
{
    ptr = ptr->next;
}
// now ptr points to the nth node.

does

ptr++
do the same as ptr = ptr-> next

meaning does ++ move the ptr +1?

No, they are two different things. ptr++ does not increment the pointer to the next node, but mearly advances the pointer to the next memory location.

>>meaning does ++ move the ptr +1?
Yes, but that's not the same as ptr = ptr->next;

Is n supposed to be the node number, that is, when there are 10 nodes and n = 3, then it wants the 3d node?

node *ptr = cur_node;
int count = 0;
for( count = 0; count < n && ptr != NULL; count++)
{
    ptr = ptr->next;
}
// now ptr points to the nth node.

I think the for loop is wrong.

cause n is in the struct.
therefore it's not declared, so i'll get a compiler error.

But you declared n in your post #5, above.

int n;
cout<<"Which list would you like to go to? \n";
cin>>n;

No, they are two different things. ptr++ does not increment the pointer to the next node, but mearly advances the pointer to the next memory location.

>>meaning does ++ move the ptr +1?
Yes, but that's not the same as ptr = ptr->next;

or so it should have been ++ptr?
or ptr++.

if it's ptr+1, how isn't that the same as ptr = ptr -> next.
the ptr points to the node who's +1 from the current node right?
which isn't that the same as ptr = ptr -> next;

nvm about the for loop post,l i figured it out.

or so it should have been ++ptr?
or ptr++.

if it's ptr+1, how isn't that the same as ptr = ptr -> next.
the ptr points to the node who's +1 from the current node right?
which isn't that the same as ptr = ptr -> next;

ptr->next is a pointer at some completly different memory location, which is probably not at (ptr+1). Lets say the address of ptr is 1000, so ptr+1 might be 1001. But ptr->next may have an address of 2000 because when you build a linked list each node is allocated with the new operator, or possibly malloc() function.

Wow, totally forgot about that. Sleep deprivation is serious busienss.

i don't think the for loop is working.

can the for loop be this?

for(node *ptr = cur_Node->head; ptr!=NULL; ptr= ptr->next)
{

}

The loop can be this: ;)

/// get a pointer to i-th node (from 0... ).
Node* getIthNode(Node* head, int i)
{
    while (head && i-->0)
        head = head->next;
    return root;
}

That doesn't look right to me.

That doesn't look right to me.

It's interesting, why?
That's the essence of your problem ;)
It's the same loop as in your previous post but it's incapsulated (in compact form) into the function body. Remember: if you have a well-defined use case (get a pointer to i-th node, for example), make a function (old good C rule).

What's the root?

I been tinkering the code AD helped me with .

node *ptr = cur_node;
int count = 0;
for( count = 0; count < n && ptr != NULL; count++)
{
    ptr = ptr->next;
}
// now ptr points to the nth node.

doesn't work perfectly.
say the user types in 4 restaurants.

and it asks which restaurant to go.
1-3 doesn't work.
only 4 works. When I debug it.

I say if customer_number == cur->next->data
then cout<<"Hi What can I get you";

that only comes out when it's at the end of the list.
which is 4, since there's 4 restaurants for this example.

What's the root?

Evidently, it's a misprint ;)
Should be return head; Well, if it's the only That doesn't look right to me reason, it's clear why you can't get a pointer to i-th node for the present ;)

node *ptr = cur_node;
int count = 0;
for( count = 0; count < n && ptr != NULL; count++)
{
    ptr = ptr->next;
}
// now ptr points to the nth node.

is it because count is <n ?
should it be

count == n?

Are you doing this for a class assignment in school? Have you not studied for loops yet? If you have not, then you should do that before continuing.

>>count < n
That means that the loop will continue as long as the value of count is less than the value of n.

It should work perfectly as long as n is less than the number of nodes in the linked list. If there are 5 resturants and you ask for the 6th node then my code will not work because there are not 6 nodes in the list.

I been tinkering the code AD helped me with .

doesn't work perfectly.
say the user types in 4 restaurants.

and it asks which restaurant to go.
1-3 doesn't work.
only 4 works. When I debug it.

I say if customer_number == cur->next->data
then cout<<"Hi What can I get you";

that only comes out when it's at the end of the list.
which is 4, since there's 4 restaurants for this example.

struct Node {
int data;
Node*head;
Node*tail;
Node*next;
};

int main()
{

int boxes;
cout<<"How many boxes?"<<endl;
cin>>boxes;

Node *Box = new Node;
Box -> tail= NULL;

for( int i =1; i<= boxes; i++)
{

Box->next = NULL;
if(Box->head == NULL)
{
Box ->head = Box -> tail = Box;
}
else
{
Box->tail -> next = Box;
Box -> tail = Box;
Box-> data = i;
}

}

int number;
cout<<"which box number do you want?"<<endl;
cin>>number;

Node *ptr = Box;
int count = 0;
for( count = 0; count < number && ptr != NULL; count++)
{
    ptr = ptr->next;
}
// now ptr points to the nth node.
cout<< ptr->next->data<<endl;

system("PAUSE");
return 0;
}

I entered 4 boxes. so currently there are 4 boxes in the link list.

next, i ask which number do you want?

i say I want to go to 3

I cout if I get 3.
But i get 4.
say I type in 2.
I get 4 still.

The problem is NOT with my loop but with the way you created the linked list. All nodes in the list point to the same node. Each node has to be allocated each time it is added to the list. I know I have shown you how to do this a couple times before -- go back and study your other thread to see what you did wrong here.

I don't see the line of code that says all pointer points to the same node.

if(Box->head == NULL)
{
Box ->head = Box -> tail = Box;
}
else
{
Box->tail -> next = Box;
Box -> tail = Box;
Box-> data = i;
}

All the nodes point to Box.

if(Box->head == NULL)
{
Box ->head = Box -> tail = Box;
}
else
{
Box->tail -> next = Box;
Box -> tail = Box;
Box-> data = i;
}

would this be a better fix to it?

if(Box->head == NULL)
{
Box ->head = Box -> tail = Box;
}
else
{
while(Box->next !=NULL)
{
Box= Box->next;
Box->next = new Node;
Box->next ->data = i;
cout<<Box->data<<endl;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.