Thread: Linked list Stack question

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    Linked list Stack question

    I'm working on a Printing Function for my stack, where I create a temporary stack to store the (popped) values of the user implemented stack. They get pushed onto the temp stack, then popped off the temp stack (to retain element order) and printed before the pop back to the original Stack.

    The Problem resides in calling a class member function for the temporary stack.
    I could just code out my pop & push again for my temp stack, but my instructer would yell at me.

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why use a temporary stack. take a leaf out of the STL and use iterators. Add a member to your stack class, just a basic pointer will do nicely if your class is array based. use it to iterate upwards thru the stack printing as you go.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    The Assignment has a set header file, and the requirements of the assignment are there to make it a pain, so I'm stuck with the temp Linked list solution, I can hard code the temp stack but if there is a way to use my prebuilt functions on the temp stack my grade won't be affected negatively.

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    if your stack and tempstack are the same class you can use the same member funcs, you wont need to rewrite them.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Another idea for you is if you cant change your class you can use a std::deque<TYPEOFSTACK> and push_front the popped off the stack value, then copy the deque to cout using the copy algorithm and ostream_iterators
    for instance if you have a stack of ints...
    Code:
    std::deque<int> cont;
    Stack thestack;
    // fill stack here
    while(!thestack.empty())
    {
       // fill deque with popped values
       cont.push_front(thestack.pop());
    }
    std::copy(cont.begin(),cont.end(),std::ostream_iterator<int>(std::cout," "));
    // now you would have to put the values in the deque back in your stack
    while(!cont.empty())
    {
       thestack.push(cont.front());
       cont.pop_front();
    }
    Last edited by Stoned_Coder; 09-23-2005 at 06:26 AM.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linked List Not Saving Value as Int
    By bar338 in forum C Programming
    Replies: 4
    Last Post: 05-04-2009, 07:53 PM
  2. single linked list to double linked list (help)
    By Countfog in forum C Programming
    Replies: 8
    Last Post: 04-29-2008, 08:04 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Anyone good with linked list.....I am not....
    By chadsxe in forum C++ Programming
    Replies: 11
    Last Post: 11-10-2005, 02:48 PM
  5. Question about a stack using array of pointers
    By Ricochet in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2003, 10:12 PM