I have a stack method that's supposed to return a reversed copy of the this object. I need this to link this to that, the reversed copy of the this stack. Thanks

public FancyStack<E> reversed()
{
    FancyStack<E> that= new FancyStack<E>();
    if(this.isEmpty()){
        return this;
    }
    else{
        while(top!=null)
            {
                revertStack.push(pop());
            }
        }
    return revertStack;
    }

Recommended Answers

All 4 Replies

if stack is empty , you should return some kind of error or throw some exceptions imo.

 revertStack.push(pop());

i dont know what those are , but assuming they do the work they are supposed to , AND its an in-place operation , you are returning a reversed version of stack , but its not a copy of the original stack , because the stack itself has been reversed. here is something you can do :

  1. make FancyStack iterable
  2. in your else() condition , have a for() loop that takes in each member of FancyStack() and put that in a new copy.
  3. apply revertStack.push(pop()) to this newly made copy , and return it.
I want to return a reversed version of the this stack. I basically want this to point to revertStack. Ultimately I want to return a copy of the this object, only in reversed form.

you want to do that ? or you want us to do that ? the later one isnt really an option here.

Also , this is two words , one english , and another java . You say copy and then you say this . that confuses me a bit.

Pyler: it seems to me like you have three or four threads about this particular question. it's pretty hard to follow what has already been suggested, if it's spread over several threads. could you please pick one thread, copy all answers to that, and put the others on 'solved'?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.