How to get the address of 1st node in <list>
Reversing Linklist using Stack.
I have created linklist using <list> STL. Now I want to push address of each node in Stack. For that I want address of 1st node so that I will assign it to "temp" and I can use following loop.
HTML Code:
while (temp != NULL)
{
s.push(temp);
temp = temp->next;
}
But I am not getting address of 1st node. I tried function l1.front() which gives 1st element but not address.
Can anyone help me ?
Re: How to get the address of 1st node in <list>
You could have a stack of type list<T>::iterator, then iterate the list and push them onto stack. Something like this (not tried)
Code:
list<LT> mylist;
stack<list<LT>::iterator> mystack;
for (list<LT>::iterator li = mylist.begin(); li != mylist.end(); ++li)
mystack.push(li);
Note that if you perform further operations on the list you need to be careful not to invalidate any of the iterators stored in the stack.
Re: How to get the address of 1st node in <list>
Quote:
Originally Posted by
tush123
Reversing Linklist using Stack.
The STL containers don't give you access to implementation detail like say internal pointers.
What you could do is push the elements on stack, clear the list, and then rebuild it with the elements in reverse order.
A more efficient way would be to use one forward iterator and one reverse iterator to swap the list elements pairwise until the iterators meet in the middle.
Also note that std::list has a reverse function already.