accessing classes and reversing inputs

This is a discussion on accessing classes and reversing inputs within the C++ Programming forums, part of the General Programming Boards category; yes, i know the latter topic is common. but theres a twist: a predefined class. i have a predefined class ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    15

    accessing classes and reversing inputs

    yes, i know the latter topic is common. but theres a twist: a predefined class.

    i have a predefined class for a stack that brings up a menu where you can push, pop, and peek. now i have to use this class in order to display what the user has pushed onto the stack, in reverse.

    now this class uses new concepts like the 'new' operator in conjustion with pointers to allow any number of...numbers, to be input. ive read, and read, and read, but pointers are still weird to me in usage (although i do understand the concepts).

    i was thinking of using the pop function to display the stack in reverse(loop it till it hits the null charcter), but in order to do that, i would have to allow for another user option. is this possible to do in my custom function that im writing or do i need to modify the existing class?

    im also not sure how to access the actual inputs for things like sequencing and ordering, since theyre coupled with the 'new' operator...

    i can provide code but was hoping to avoid the easy way out (someone doing the actual coded solution)

    thanks!

  2. #2
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    A Stack element would be something like
    Code:
    template<class T> struct StackNode{
       struct StackNode* next;
       T element;
    }
    and to print it in both normal and reverse order
    Code:
    //normal
    template<class T> void print_node(struct StackNode<T> *node){
        if(node==NULL)
            return;
        //print element. maybe std::cout<<node->element;
        print_node(node->next);
    }
    //reverse
    template<class T> void print_node(struct StackNode<T> *node){
        if(node==NULL)
            return;
        print_node(node->next);
        //print element. maybe std::cout<<node->element;
    }
    It very simple as you can see...

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    When I think of stacks I think of access being restricted to the last item entered. That access is by popping the last element off, thereby altering the stack. That type of access differs from arrays or lists where you can access any member you want without altering the contents of the container using using the []operaotor or pointers or iterators, whatever. Therefore I wouldn't use pointers/iterators to access the elements of the stack as xErath did. I'd pop elements off the stack one by one and display them as they are popped off to display the contents of the stack in reverse order of entry. To display the contents in order of entry I'd pop elements off the first stack one by one and push them onto a second stack as they are popped from the first. Then I'd pop and display elements of the second stack one by one.

  4. #4
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    The pop push and peek functions are only an outside interface for the class, because the programmer doesn't need to know how the info is stored in the stack. Plus I think that any container/collection class should allow acess to ALL elements at any time, by the class itself. Imagine that you want to convert and collection to another one, like stack to bynary tree? The pop methods work but it is very ineficient, because memory has to deallocated, and reallocated for each node, plus inserting and removing the objects...
    Another example would be a Queue. The class only allows acess to the first and last elements, but internaly the info can be stored in a dynamicly allocated array, or linked list style, with nodes connected together.
    Last edited by xErath; 12-05-2004 at 02:06 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    so...have a function that automatically pops off every node, then as it does, display it? i know youd have to set up a loop:

    Code:
    while node does not equal NULL
         pop node
         print node
         if the current node equals NULL
            return
    right?

    i see what yer sayin xErath, just flip the order of the print lines (current then next current for the reverse). but, at this point, the way youve done it is a bit over my head in terms of pointers/iterators (although im sure its easier if you understand what youre doing! )

  6. #6
    #include<xErath.h> xErath's Avatar
    Join Date
    Jun 2004
    Posts
    722
    That way you'll have to do this.
    Code:
    Stack thestack//the original
    Stack backup//empty
    while thestack.size() !=0
        element = thestack.pop()
        print element
        backup.push(element)
    //then restore the original stack back
    while backup.size() !=0
        element = backup.pop()
        thestack.push(element)
    If you don't do this, you'll loose you orbjects, if you don't store them, and you'll have a reversed stack if you don't rebuilt the original one from the backup.
    Again, I strongly advise my method, but by method would have to be friend of the class or a class method... method, method

  7. #7
    Registered User
    Join Date
    Sep 2004
    Posts
    15
    ah, so i cant just do this in a different function, i have to modify the original class in order to access the private data

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21