Results 1 to 4 of 4
  1. #1
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Question How to pop the element of stack to a list?

    Hi,everyone.

    Now i am doing the assignment about i need to transfer all the elements from list1 to list2 using a stack as an intermediate data structure. Now the question is if list1 is empty then pop elements of s onto the end of list2 until s is empty.

    Anyone can give me some tips how to pop the elements of stack to a list?

    Thanks!

    The folowing java code is my thinking structure:
    Java Code:
    public void stackArrange(ArrayList list1,ArrayList list2){
    Stack s=new Stack;
    if(list1.isEmpty()){
    
    
    }
    }

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

    Default Re: How to pop the element of stack to a list?

    Not sure I understand the question. You're transferring elements from list1 to list2? Sounds like if list1 is empty, you're done.

    If there's more to it than that, check out the add(...) methods in List (Java Platform SE 6).
    Get in the habit of using standard Java naming conventions!

  3. #3
    hjxlpp is offline Member
    Join Date
    May 2013
    Posts
    17
    Rep Power
    0

    Default Re: How to pop the element of stack to a list?

    Hi,kjkrum

    I also think just tranfer the elements from list1 to list2. But the question say that using a stack as an intermediate data structure. I solved it two days and google search, all about some simple example using stack. Could you help me think and give me some more tips?

    Thanks very much!

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

    Default Re: How to pop the element of stack to a list?

    Do you understand what a stack is? Can you visualize what it means to push and pop elements on a stack?

    I'm having a hard time understanding the point of this assignment. Transferring elements between ArrayLists is so simple, there's no obvious reason why a stack should be involved at all. Are there more requirements you haven't mentioned? Are the elements supposed to end up in some particular order in list2?

    Also, you should be aware that Stack has fallen out of favor. Its preferred replacement is Deque.
    Get in the habit of using standard Java naming conventions!

Similar Threads

  1. last element of recursive list
    By rokko in forum New To Java
    Replies: 21
    Last Post: 04-02-2012, 05:14 PM
  2. ArrayList remove an element from the list
    By ralf in forum New To Java
    Replies: 37
    Last Post: 07-12-2011, 07:08 PM
  3. Replies: 4
    Last Post: 02-21-2011, 09:34 AM
  4. Stack Implemented Linked List HELP!!!!!!!!!
    By dudedatroz in forum NetBeans
    Replies: 1
    Last Post: 02-19-2010, 10:56 PM
  5. adding list to an array element
    By Preethi in forum New To Java
    Replies: 5
    Last Post: 09-25-2008, 04:23 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
  •