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

Thread: stacks

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default stacks

    I apologize but I have no idea exactly what I'm doing and I apologize now in advance if it his hard to read since I'm posting this via cellphone since I do not have access to the internetfrom my pc. I am using Netbeans attempting to write a code with the list of these colors. yellow red white red yellow white white green red white. I need to store the colors Ina stack or perhaps multiple stacks. I cannot print them prior to storing them in the stack.
    2. print the colors in reverse input sequence.
    3. print the colors in input sequence
    I cannot use arrays or files for temporary storage. and to use multiple ArrayStacks<T>objects.
    this is what I had which I know is not nearly right

    import java.util.*;
    public class colorStack {
    * * public static void main(String[] args) {

    * * * * Stack<String> stack = new Stack<String>();
    * * * * stack.push("white");
    stack.push("red");
    * stack.push("green");
    stack.push("white");
    }
    }
    I understand I need to push the strings to stack just no idea how. any help would be greatly appreciated thanks


  2. #2
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: stacks

    you are actually pushing it inside the stack, if you know the data structure of stacks(last in, first out), there are 2 major ways to handle data inside of it, the "push" and "pop", you can read its API(for java.util.Stack) for further information, or any basic data structures about Stacks.


    Push - pushes the data into the stack, starts from the bottom most
    Pop - pops(takes) the current data on the top of stack
    Last edited by chronoz13; September 9th, 2012 at 11:12 PM.

  3. #3
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: stacks

    Quote Originally Posted by chronoz13 View Post
    you are actually pushing it inside the stack, if you know the data structure of stacks(last in, first out), there are 2 major ways to handle data inside of it, the "push" and "pop", you can read its API(for java.util.Stack) for further information, or any basic data structures about Stacks.


    Push - pushes the data into the stack, starts from the bottom most
    Pop - pops(takes) the current data on the top of stack
    so I am doing it backwards then right? then when I want to do different sequences I would start to pop. I sorry I'm very new to java and taking a course recommended by an advisor that required java pre requisites and it's too late to drop.

  4. #4
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: stacks

    for number 3.) it would be easy for you, yes, just pop everything you pushed on the stack so you can print it in sequence
    for number 2.) i never tried it but i think you need another stack, or you can use Queue(first in first out structure) so you can print it in reverse sequence..

    Edit for number 2.) Everytime you pop a data from stack 1 , you might push it in another stack (e.g stack 2), and see the order of elements
    Last edited by chronoz13; September 9th, 2012 at 11:48 PM.

  5. #5
    Java kindergarten chronoz13's Avatar
    Join Date
    Mar 2009
    Location
    Philippines
    Posts
    659
    Thanks
    177
    Thanked 30 Times in 28 Posts

    Default Re: stacks

    let me correct my statement, sorry, i wasnt reading it carefully..

    For you to print it in "INPUT" sequence which is for number 3.) you have to see the bottom most, then you need another stack
    For you to print it in "REVERSED INPUT" sequence which is for number 2.) just pop everything from that stack

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: stacks

    Quote Originally Posted by chronoz13 View Post
    let me correct my statement, sorry, i wasnt reading it carefully..

    For you to print it in "INPUT" sequence which is for number 3.) you have to see the bottom most, then you need another stack
    For you to print it in "REVERSED INPUT" sequence which is for number 2.) just pop everything from that stack
    what dO you mean you need to see the bottom most?

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: stacks

    He means, when you push some things on to the stack, and you want them back in the same order, the 'bottom most' is the first one you pushed.

    For example, an array of integers. myInts = [0,1,2,3,4,5].
    push myInts[0]
    push myInts[1]
    push myInts[2]
    push myInts[3]
    push myInts[4]
    push myInts[5]

    The 'bottom most' is the value 0. The first one pushed to the stack. The 'top most' is the value 5, or the last thing pushed to the stack. So if you called pop you would get the 5 back first.

  8. #8
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: stacks

    Reference Stack (abstract data type) - wikipedia

    Next-to-bottom line:
    If you push some objects onto a stack, the last one that you pushed is the first one that will be popped.
    Therefore, if you push stuff on a stack and then pop the objects and print each one as it is popped, you will print them in the reverse order.

    Now... A big intuitive leap to get to the
    Bottom line:
    Create a second stack. When you print each object that you pop off of the first stack, push it onto the second stack.

    Now, what happens when you pop the objects off of the second stack and print each one as it is popped?

    Try it! Use pencil and paper; no computer needed just yet.

    Next: How to do it in Java? Reference Java Stack Class

    I mean, your code for instantiating a Stack <String> object and pushing some Strings looks pretty good for starters (assuming your cell phone put '*' where you intended spaces).

    So...

    For the first part of the program, to print the Strings in reverse order:
    Declare an object of type Stack<String>
    Push some Strings onto the stack, using the Stack.push() method
    Make a loop using the Stack.empty() method as loop control:
        While the stack is not empty
        BEGIN LOOP
            Use the Stack.pop() method to pop the top value from the stack:
               Set a String equal to the value popped from the stack.
               Print the popped String's value and do whatever else you need to do
        END LOOP

    Now, implement that and take a look at what it prints. The Strings in reverse order, right?

    So, now do the second part.


    Cheers!


    Z
    Last edited by Zaphod_b; September 10th, 2012 at 12:49 PM.

Similar Threads

  1. POSTFIX NOTATION USING STACKS... [EXAM IN 4 HRS HELP!!!]
    By Medo Almasry in forum What's Wrong With My Code?
    Replies: 7
    Last Post: October 23rd, 2011, 12:16 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