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 11 of 11

Thread: Java Stacks question

  1. #1
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Wink Java Stacks question

    Hi everone, I have been doing java for around a year now
    I'm not looking for somebody to give me an answer but for guidance and help.

    This is the question:

    I have a stack called 's'.
    push(s,m) -> pushes element n onto the stack .
    pop(s) -> pops element from the stack s.
    peek(s) -> return value of top element on the stack.

    (Other info: may use other variables, a loop and/or second stack, s2)

    Question = Set j to the third element of a stack leaving the stack unchanged.

    This is my attempt:

    I'm assuming there are two elements already on a stack as it doesn't say there should or shouldn't be!

    Stack 1 : {m,n} -> (m being top element on stack)

    push (s2, pop(s)) //This is supposed to pop the element on stack 1 sending it to stack 2 (s2)

    Stack 1 : { n }
    Stack 2 : { m }

    push (s2, pop(s)) //pop element on stack 1, send to stack 2 again

    stack 1 : { }
    stack 2: { n,m}

    push (s,j) //This pushes element j onto stack 1

    stack 1 : { j }
    stack 2: { n,m}

    push (s, pop(s2)) // pops element on stack 2, pushing it back onto stack 1

    stack 1 : { n,j }
    stack 2: { m}

    push (s, pop(s2)) // pops element on stack 2, pushing it again back onto stack 1

    stack 1 : { m,n,j }
    stack 2: { }

    Stack 1 is now unchanged but has j set to the third element. Would you say this is correct?
    Also is 'push (s, pop(s2))' the right format ??

    Thanks for any help I would appreciate it so much, just trying to make sure i'm on the right lines


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Stacks question

    Is this a different question than the one you posted here: Can somebody help me with stacks?

    Anyway, the best way to test this is to throw together a little example program that does exactly what you described.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Stacks question

    Yes this is a different question. How would I go about making a program that does what I want?

    Creating my stacks (s) and (s2) - Stack s = new Stack();
    Stack s2 = new Stack();

    Would i then push n & m on stack to begin with?

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Stacks question

    Quote Originally Posted by sim18 View Post
    Yes this is a different question. How would I go about making a program that does what I want?

    Creating my stacks (s) and (s2) - Stack s = new Stack();
    Stack s2 = new Stack();

    Would i then push n & m on stack to begin with?
    What happened when you tried exactly that?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Stacks question

    It created two stacks s & s2.

    I then added s.push("n"); and s.push("m"); giving n & m in the stack1.... is this about right so far?

    thanks

  6. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Stacks question

    Quote Originally Posted by sim18 View Post
    It created two stacks s & s2.

    I then added s.push("n"); and s.push("m"); giving n & m in the stack1.... is this about right so far?

    thanks
    Seems reasonable to me, but really, the best way to test is to simply let the code do the talking. It doesn't matter what we think, just what the program does. Try stepping through it with a debugger or at least adding some print statements to figure out exactly what's going on, if you aren't sure.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  7. #7
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Stacks question

    Thanks very much for the replies

    I have added print statements and I can see exactly what is going on!

    If I was going to take this one step further, (given integer 'n') and set j to the nth element of the stack, leaving the stack without its top 'n' elements.

    All i have trouble with is getting my head around the wording and once that's done like you said I can start doing the code to check its right.

    set j to the nth element of the stack... would I chose what the nth element is, for example I would set j to the 4th element in the stack, and get rid of the top 3 elements, or am i reading this wrong?

  8. #8
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Stacks question

    Quote Originally Posted by sim18 View Post
    Thanks very much for the replies

    I have added print statements and I can see exactly what is going on!

    If I was going to take this one step further, (given integer 'n') and set j to the nth element of the stack, leaving the stack without its top 'n' elements.

    All i have trouble with is getting my head around the wording and once that's done like you said I can start doing the code to check its right.

    set j to the nth element of the stack... would I chose what the nth element is, for example I would set j to the 4th element in the stack, and get rid of the top 3 elements, or am i reading this wrong?
    Sometimes the best way to figure that part out is to draw some examples. So say we have this as a stack:

    {1, 2, 3, 4, 5}

    ...and n = 3. What should happen?

    Well, first off, what is the 3rd (nth) element of the stack? In this example it's easy because it's 3. So what does it mean to set j equal to 3 and leave the stack without its 3 top values?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  9. #9
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Stacks question

    {1, 2, 3, 4, 5}

    n = 3

    nth element = 3rd element in stack, which is 3.

    So you would pop() three times leaving {4,5}.

    If it was j it would be exactly the same then? i.e

    {l,m,j,n,o} n = 3

    nth element = 3rd element in stack, which is j.
    So you would pop() three times leaving {n,o}.

    OR, would 'j' not be there to start with, because it says set j to the nth (3rd) element of the stack. {l,m,n,o}

    Therefore I would have to do what I did before and use stack 2 to pop the first two elements, push 'j' into the stack, push the others from stack 2, then pop removing top n elements (3)

    Thanks again for the help

  10. #10
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Stacks question

    That's really a question for your instructor, but the way I take it, the j in the question is just a temp variable name and has nothing to do with any 'j' values that happen to be in the stack. But I suppose I could be reading it wrong, so I'd recommend asking your instructor for clarification or some more examples.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    Member
    Join Date
    Oct 2012
    Posts
    68
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Java Stacks question

    Thanks very much for your help

Similar Threads

  1. stacks
    By bd0652 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 10th, 2012, 11:48 AM
  2. copying and comparing stacks
    By colerelm in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 22nd, 2011, 01:22 AM
  3. Help with Stacks
    By animelook in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 12:58 AM
  4. help for stacks
    By msa0127a in forum Algorithms & Recursion
    Replies: 0
    Last Post: October 3rd, 2010, 12:11 AM
  5. Palindrome Stacks
    By mgutierrez19 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 7th, 2010, 03:05 AM