views:

396

answers:

6

Hi,


int LinkedList::DoStuff()
{
Node *Current    = next_;
while ( Current != NULL )
    {
     Current = Current->next_;
     length_++;
    }
    // At the last iteration we have reached the end/tail/last node
    return length_;
}


there are no more nodes beyond the last. How can i traverse to the tail-end to the front-head?

A: 

Is your linked list class doubly-linked or singly-linked? If there is no previous pointer inside each node, you can't traverse backwards.

I also suggest you post more code and take the time to make your question readable.

In addition, if this is a homework question, you need to use the homework tag.

rlbond
paxdiablo
+6  A: 

Unless your linked list is a doubly-linked one, this is difficult to do. Recursion is one way, assuming you don't have lists so big that you'll run out of stack space, something like this (pseudo-code):

DoStuffBackwards (currNode) {
    if (currNode != NULL) {
        DoStuffBackwards (currNode->next);
        // Process currNode here.
    }
}

DoStuffBackwards (firstNode);

This works because you keep calling DoStuffBackwards() for the next node until you exhaust the list then, as you roll back up the recursion stack, you process each node.

paxdiablo
Really cool method. I've never thought of this. However, a bit unsafe, isn't it?
alvatar
How is it unsafe, other than the aforementioned possibility of stack overflow? And, in that case, you can expand the stack or do "iterative recursion" by maintaining your own stack of node pointers in, for example, a dynamic array.
paxdiablo
+1  A: 

If you just want to go backwards from last node to current node, than Pax's answer (using recursion) is your best bet, also see my version below. If your current node is not the head of your non-circular-singly-linked-list, and you want to go from current node to head node, it is impossible.

int LinkedList::DoStuff()
{
    return DoStuffBackward(next_, 0);
}

int LinkedList::DoStuffBackward(Node* node, int n)
{
    if (!node)
    {
        return n;
    }

    int len = DoStuffBackward(node->next_, n + 1);
    std::cout << "doing stuff for node " << n << std::endl;
    return len;
}
Shing Yip
+1  A: 

This has the smell of homework, so no code, but here's an overview of a solution that doesn't require recursion:

If you want to run through the list backward one option to relink the list to point backwards as you're traversing it to find the end. Then as you re-traverse the list (which visits the nodes in the reverse order from the original list) you repeat the relinking same as before and the list ends up in its original order.

This is simple in concept, but handling the pointers and links correctly (especially at the start and end of the list) can be a bit tricky.

Michael Burr
"This has the smell of homework, so no code". I don't really understand the point. I you thing this is a problem that the guy is not doing his homework, then don't post to get Rep. Otherwise do things properly and post everything so everyone else can learn. This is a community, and we shouldn't care of what the people do with their homework. Besides, why it is not bad if I'm asking a question for my own personal knowledge instead of a homework???
alvatar
+1 for the solution. @Álvaro: Michael just offered a different approach to the problem. Take it for what it is worth. It is not hard to implement if you really want. If you are really interested I can write it for you, but the important part is that he offered a different approach for the particular that does not have the same limitations as recursion. Now, about rep hunting... to many of us rep points are not a goal, and I don't think that Michael at 20k is trying to scratch a couple of rep points out of this.
David Rodríguez - dribeas
@Álvaro - it's not bad to ask questions about homework, however I believe that homework questions should be clear that that's what they are and they should show what's been tried so far (or at least an indication that a good faith attempt has been made). Actually, that's true for most questions. I also think that giving pointers and explanations of the ideas to possibly solve a problem are more appropriate answers to homework-style questions than complete code examples (usually). In any case, I have no obligation to code something up. And no one needs to up-vote if it's an incomplete answer.
Michael Burr
Oh - and of course if this isn't homework the real answer is to use a different data structure than a linked list if you want to be able to traverse the list from back to front.
Michael Burr
And for the Stack Overflow FAQ's guidance on homework-style questions: http://stackoverflow.com/questions/230510/homework-on-stackoverflow/230575
Michael Burr
A: 

Recursion can work, as can building an auxiliary data structure, such as an array with one entry for each element of the original list. If you want a solution for a single-threaded list without requiring O(n) extra storage, the best bet is to reverse the list in place as Michael suggests. I wrote an example for this, [but I'll leave it out given the concern about homework]. One caution about reversing the list: if there are any other data structures that hold pointers to the original list, and you might be accessing them during your traversal, they won't work if they need to access the list while it's reversed, and this might lead to data corruption if they try to modify the list.

Update: Ok, here's the (C++) routine to reverse a list in place. It hasn't been tested, though. And I'll note that if this is homework, the poster still needs to figure out how to use this routine correctly to get a complete answer.

Node *ReverseList(Node *head) {
    // Reverse a single-threaded list in place, return new head
    Node *prev=NULL;
    Node *cur=head;

    while (Node *next=cur->next_) {
     cur->next_ = prev;
     prev = cur;
     cur = next;
    }
    cur->next_ = prev;
    return cur;
}
dewtell
"I'll leave it out given the concern about homework". I don't really understand the point. I you thing this is a problem that the guy is not doing his homework, then don't post to get Rep. Otherwise do things properly and post everything so everyone else can learn. This is a community, and we shouldn't care of what the people do with their homework. Besides, why it is not bad if I'm asking a question for my own personal knowledge instead of a homework???
alvatar
A: 

push the list on a stack and then pop them off.

kenny