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

Thread: how could I display a stack as FIFO instead of LIFO

  1. #1
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default how could I display a stack as FIFO instead of LIFO

    guys, if you have a stream of numbers: 1 2 20 -4 99 -3 -20. And you need to insert them into an Stack and display the positive numbers in the order they were insert them(if 1 is first, then 1 is first out: FIFO), and for negatives numbers display them in the order they have in the stack. 1) do I need two stacks for this scenario Stack<Integer> negatives, Stack<Integer> positives, or can I get away with putting all numbers in one stack? my implementation is with two stacks, but I would like to know if can be done with only one stack, and what would it be the solution for the displaying of positive numbers in (FIFO). By the way I display positive numbers in FIFO using a tempStack, is there any other way to display a stack in reverse order without creating a temp stack. here's the code

    public static void main(String[] args) throws IOException {
     
            Stack<Integer> pNum = new Stack();
            Stack<Integer> nNum = new Stack();       
     
            int num;
     
            Scanner in = new Scanner(System.in);  
            System.out.println("enter series of number: ");         
            int i = 0;
            while(in.hasNextInt()) {
               num = in.nextInt(); 
               if(num ==  -1) {
                   nNum.push(num);
                   break;
               }               
               else if(num > 0)
                   pNum.push(num);
               else
                   nNum.push(num);            
            }       
     
            Stack<Integer> tempNum = new Stack();
     
            while(!pNum.isEmpty()) {
                tempNum.push(pNum.top());
                pNum.pop();
            }
     
            while(!tempNum.isEmpty()) {
                System.out.print(tempNum.top()+" ");
                tempNum.pop();
            }
     
            System.out.println();
     
            while(!nNum.isEmpty()){
                System.out.print(nNum.top()+" ");
                nNum.pop();
            }       
     
        }


  2. #2
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Lightbulb Re: how could I display a stack as FIFO instead of LIFO

    Quote Originally Posted by mia_tech View Post
    By the way I display positive numbers in FIFO using a tempStack, is there any other way to display a stack in reverse order without creating a temp stack. here's the code
    Answer: Try using a LinkedList instead or even convert your Stack to a LinkedList... This gives you the libraries of both Stacks & Queues, Stacks being LIFO, and Queues being FIFO.

    LinkedList<Integer> link = new LinkedList<Integer>(yourStack);
    f34r th3 kut3 1z

  3. #3
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how could I display a stack as FIFO instead of LIFO

    that line didn't work... it didn't take a stack!

  4. #4
    Junior Member hackthisred's Avatar
    Join Date
    Apr 2012
    Posts
    18
    Thanks
    1
    Thanked 1 Time in 1 Post

    Unhappy Re: how could I display a stack as FIFO instead of LIFO

    Quote Originally Posted by mia_tech View Post
    that line didn't work... it didn't take a stack!
    Then You must not have changed the type params or didn't import LinkList

    Stack<Integer> pNum = new Stack<Integer>();
    LinkedList<Integer> link = new LinkedList<Integer>(pNum);

    Is perfectly working code, I verified it myself, if you still get an error you are doing something wrong lol
    f34r th3 kut3 1z

  5. #5
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how could I display a stack as FIFO instead of LIFO

    Quote Originally Posted by hackthisred View Post
    Then You must not have changed the type params or didn't import LinkList
    I have imported java.uitl.* which includes LinkedList. May be si b/c I have my own implementation of Stack class, which is basically the same with the basic methods top, pop, isEmpty
    Last edited by mia_tech; July 25th, 2012 at 01:10 AM.

  6. #6
    Member
    Join Date
    Mar 2009
    Posts
    91
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: how could I display a stack as FIFO instead of LIFO

    After changing type parameters Collections.reverse(pNum); is the way to go

  7. #7
    Junior Member
    Join Date
    Jul 2012
    Posts
    17
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: how could I display a stack as FIFO instead of LIFO

    There are plenty of ways to do this. I'd advise you avoid using relics of the past like Stack in favor of using the newer Deque collection. I quote:
    ...
    A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example:
    ...
    You can even convert a Deque into a LIFO Queue using Collections.asLifoQueue(), if need be.

    Anyways, here's one way to solve your problem.
    import java.io.PrintStream;
     
    import java.util.ArrayDeque;
    import java.util.Deque;
    import java.util.Iterator;
    import java.util.Scanner;
     
    public final class EchoSequences {
     
      private EchoSequences() { }
     
      private static void print(final Deque<Integer>... sequences) {
        final PrintStream output = System.out;
        for (final Deque<Integer> seq : sequences) {
          final Iterator<Integer> cursor = seq.iterator();
          while (cursor.hasNext()) {
            output.print(cursor.next());
            output.print(' ');
          }
          output.println();
        }
      }
     
      public static void main(String[] argv) {
        final Deque<Integer> positives = new ArrayDeque<Integer>();
        /* technically not strictly negatives as it also may contain 0 */
        final Deque<Integer> negatives = new ArrayDeque<Integer>();
     
        final Scanner input = new Scanner(System.in);
        while (input.hasNextInt()) {
          int n = input.nextInt();
          if (n > 0) {
            positives.add(n);
          } else {
            negatives.push(n);
            if (n == -1) {
              break;
            }
          }
        }
     
        print(positives, negatives);
      }
    }

    On the topic of your own code, I'd advise not calling top()/pop() separately, as you can merely replace the top() call with a pop() and keep the same functionality.

Similar Threads

  1. stack
    By ridg18 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: January 22nd, 2011, 12:45 PM
  2. Stack
    By AmyH in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 7th, 2010, 04:04 PM
  3. Stack Array..
    By qaromi in forum Collections and Generics
    Replies: 2
    Last Post: December 26th, 2009, 12:54 PM
  4. Stack Order?
    By TimW in forum AWT / Java Swing
    Replies: 2
    Last Post: September 19th, 2009, 08:33 AM
  5. Error of "cannot access InToPost" in 3 and 5 code
    By jaysoncutie in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 25th, 2009, 10:12 AM