Here we will look at 3 examples to show how Stacks and Queues are used.
Example1
This program will read a list of character strings and output them once in
the order read and a 2nd time in the opposite order. We won't discuss reading
from files as we have done so before, we'll mainly concentrate on how a
stack can help us write a program to solve this problem.
In this code segment, we'll read an line of input as a string and display it
as well as storing it on a stack. The while loop is designed to allow us to
process are input line in the data file. As you can see, I have skipped
some of the details that are needed in the actual code segment, such as
the need for try/catch.
System.out.println("The List (1st)"); text = istream.readLine(); while (text != null) { System.out.println(text); s.push(text); text = istream.readLine(); }
Note that outputing the text in this loop allows us to satisfy the need for the list being displayed in the order read. Assume that the text lines read contained the following sequence: Hello How Are You Since the stack constructed is a dynamic, graphically, it should look like the following:
You-->Are-->How-->Hello--|| top_^
Now, we'll look at the code segment that will pop off the elements off the stack and display them. Because of the LIFO behavior of the stack, this list is displayed in the opposite order:
System.out.println("The List (2st)"); while (!s.empty()) { text = (String)s.peek(); s.pop(); System.out.println(text); }
This program will read a list of character strings and output them twice in
the order read. We'll concentrate on how a queue can help us write a
program to solve this problem.
In this code segment, we'll read an line of input as a string and display it
as well as storing it on a queue. The while loop is designed to allow us to
process are input line in the data file.
System.out.println("The List (1st)"); text = istream.readLine(); while (text != null) { System.out.println(text); q.enqueue(text); text = istream.readLine(); }
Note that outputing the text in this loop allows us to satisfy the need for the list being displayed in the order read. Assume that the text lines read contained the following sequence: Hello How Are You Since the queue constructed is a dynamic, graphically, it should look like the following:
Hello-->How-->Are-->You--|| front_^ back_^
Now, we'll look at the code segment that will dequeue the elements and display them. Because of the FIFO behavior of the queue, this list is displayed in the same order that was initially:
System.out.println("The List (2st)"); while (!q.empty()) { text = (String)q.peek(); q.dequeue(); System.out.println(text); }
This program will read a list of character strings and output them twice,
the 1st time in the opposite order read and the second time, the order
read. We'll need a queue and a stack in order to write a program to solve
this problem.
In this code segment, we'll read an line of input as a string and store
it in a stack as well as a queue.
System.out.println("The List (1st)"); text = istream.readLine(); while (text != null) { s.push(text); q.enqueue(text); text = istream.readLine(); }
Note that outputing the text in this loop allows us to satisfy
the need for the list being displayed in the order read. Assume that the
text lines read contained the following sequence: Hello How Are You
Since both the queue and the stack are constructed as dynamic, graphically,
they should look like
the following:
front_---->| |------>| |----->| |----->| |<----back_
| | | |
|Hello| |How| |Are| |You|
| | | |
| |------>| |----->| |----->| |<----top_
Example3 also has a code segment that pops and displays the stack content to conform to the display of the 1st list in the opposite order read followed by the code segment that dequeue and display the 2nd list. The code segment that were already discussed in the previous examples.