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
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();
}
}
Re: how could I display a stack as FIFO instead of LIFO
Quote:
Originally Posted by
mia_tech
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. :confused:
Code java:
LinkedList<Integer> link = new LinkedList<Integer>(yourStack);
Re: how could I display a stack as FIFO instead of LIFO
that line didn't work... it didn't take a stack!
Re: how could I display a stack as FIFO instead of LIFO
Quote:
Originally Posted by
mia_tech
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
Code java:
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 :-s
Re: how could I display a stack as FIFO instead of LIFO
Quote:
Originally Posted by
hackthisred
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
Re: how could I display a stack as FIFO instead of LIFO
After changing type parameters Collections.reverse(pNum); is the way to go
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:
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.
Code java:
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.