Results 1 to 6 of 6
  1. #1
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default Reversing order of items in a stack

    Hello, i am still studying stack algorithms and i am curious as to how to go about reversing the order of items in a stack. I have a stack filled with items like this
    [1,2,3,4,5,6,7,8,9,10,11]
    and when i pop obviously the order becomes
    [11,10,9,87,6,5,4,3,2,1]

    but i am looking to maintain the initial order of the stack when printing to the screen I.E when i pop, so is there an easy way to pop items from one stack, and add them to another in their original order using just push and pop?

    I.E
    Java Code:
     for (int i=0;i < 10;i++)
            {
            stackex.push(numberlist.pop());
            }
    do
    {
    System.out.print("order is"+stackex.pop());
    }while(!stackex.empty());
    //results in numbers going in opposite direction
    So any helping on maintaining the order of the original would be good.

  2. #2
    Shoss's Avatar
    Shoss is offline Member
    Join Date
    Feb 2011
    Posts
    94
    Rep Power
    0

    Default Re: Reversing order of items in a stack

    I'm confused as to what you're asking. You asked how to go about reversing the order of items in a stack, but then say you want to make a duplicate stack with the same order?

    Why only push and pop? The clone() method creates an exact copy of the original stack in the same order.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Reversing order of items in a stack

    Be careful if you use the clone method as it only does a shallow copy. Research deep versus shallow copy if you do not understand.

  4. #4
    trishtren is offline Member
    Join Date
    Feb 2011
    Posts
    53
    Rep Power
    0

    Default Re: Reversing order of items in a stack

    hmm mainly i wanted to just use a for loop, i actually have achieved it by just copying the items into a temporary stack and then popping them out again causing them to go back in their original order, however thanks tons for you advise and i have read up on the clone method.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Reversing order of items in a stack

    You still haven't explained what you are trying to do. If you want to retain the elements in the original order then why make changes at all. They are already in their original order.

  6. #6
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,060
    Rep Power
    6

    Default Re: Reversing order of items in a stack

    It sounds like you don't want a stack at all, but a queue. Check out Deque (Java Platform SE 6), which you can use both ways.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. Reversing a Stack
    By Ciwan in forum New To Java
    Replies: 20
    Last Post: 06-30-2011, 02:16 AM
  2. Replies: 2
    Last Post: 08-24-2010, 04:48 PM
  3. compare items in Purse class, regardless of order
    By Jojogamer in forum New To Java
    Replies: 0
    Last Post: 03-14-2010, 06:57 PM
  4. Reversing the String
    By Inaam in forum New To Java
    Replies: 1
    Last Post: 03-30-2009, 08:35 PM
  5. Reversing
    By whosadork in forum New To Java
    Replies: 14
    Last Post: 11-06-2008, 04:29 AM

Posting Permissions

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