CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums
Results 1 to 3 of 3
  1. #1
    Join Date
    May 2015

    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)
                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 ?

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    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)
    list<LT> mylist;
    stack<list<LT>::iterator> mystack;
       for (list<LT>::iterator li = mylist.begin(); li != mylist.end(); ++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.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.2.4)

  3. #3
    Join Date
    Jul 2013

    Re: How to get the address of 1st node in <list>

    Quote Originally Posted by tush123 View Post
    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.
    Last edited by razzle; May 17th, 2015 at 01:37 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width