Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: My reverse method wont work

  1. #1
    Junior Member pyler's Avatar
    Join Date
    Sep 2012
    Posts
    23
    My Mood
    Busy
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Default My reverse method wont work

    This method is supposed to reverse a stack by making a new stack and pushing popped items from this stack to that and then returning the reverted stack. When I run some tests, it returns an empty stack in stead. Any clues?

    public LinkedStack<E> reversed()
    {
        LinkedStack<E> that= new LinkedStack<E>();
        if(this.isEmpty()==true){
            return this;
        }
        else{
            while(this.isEmpty()==false)
                {
                Node<E> snode=this.top;
                that.push(snode.getData());
                this.pop();
                snode=snode.getLink();
                }
            return that;
            }
        }


  2. #2
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: My reverse method wont work

    How are you calling the reverse method? Also how are your pop and push methods setup? For example is there a need to create a new node get its value then push and pop or can you do it all in a single line?

  3. #3
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: My reverse method wont work

    I'm pretty sure your problem was solved in your other thread:
    Some of my methods don't run properly
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  4. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,337
    Thanks
    182
    Thanked 830 Times in 773 Posts
    Blog Entries
    5

    Default Re: My reverse method wont work

    Please keep all your discussions regarding this question to the same thread to avoid confusion
    Method returns empty stack when called
    Thread locked.

Similar Threads

  1. Why wont this method work?
    By cbplayer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 25th, 2013, 01:30 AM
  2. Replies: 9
    Last Post: February 26th, 2013, 10:36 AM
  3. File IO Compiles, but wont work?
    By StarKannon in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: February 2nd, 2011, 08:05 AM
  4. [SOLVED] Simple While loop wont work??
    By chuckie987 in forum Loops & Control Statements
    Replies: 1
    Last Post: January 31st, 2011, 01:58 PM
  5. [SOLVED] Simple Grahpic porgram wont work.
    By kogo50 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 8th, 2010, 12:46 PM

Tags for this Thread