CS 202 - Computer Science II - Fall 2004
Stacks and Queues
Loyola College >
Department of Computer Science >
Dr. James Glenn >
CS 202 >
Examples and Lecture Notes >
Stacks and Queues
ImageModel.java
// ONLY THE NEW VERSION OF floodFill IS GIVEN HERE
public class ImageModel
{
/**
* Does a flood fill in this drawing starting at the given position.
* All positions 4-connected to the starting point through positions
* colored using the first color are recolored using the second color.
* This version uses a queue instead of recursion to do the fill
* (so essentially BFS instead of DFS).
*
* @param r the row to start filling from
* @param c the column to start filling from
* @param bgColor the color of the positions to change
* @param fillColor the color to change those positions to
*/
private void floodFill(int r, int c, Color bgColor, Color fillColor)
{
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
// make a linked list to use as the queue of points whose
// neighbors have to be checked
LinkedList q = new LinkedList();
// start the queue off with the initial point
pieces[r][c] = new GamePiece(fillColor);
q.addLast(new Point(c, r));
// keep going until the queue is empty
while (q.size() > 0)
{
// can change to removeLast to treat list as a stack
Point p = (Point)(q.removeFirst());
// check all neighbors of p
for (int dir = 0; dir < dx.length; dir++)
{
int x = p.x + dx[dir];
int y = p.y + dy[dir];
if (x >= 0 && x < getWidth()
&& y >= 0 && y < getHeight()
&& pieces[y][x].getColor().equals(bgColor))
{
// fill point and add it to the queue
// so we check its neighbors later
pieces[y][x] = new GamePiece(fillColor);
q.addLast(new Point(x, y));
}
}
}
}
}
Postfix.java
import java.util.*;
import java.io.*;
/**
* An arithmetic expression in postfix form.
*
* @author Jim Glenn
* @version 0.1 11/22/2004
*/
public class Postfix
{
private String exp;
/**
* Creates a postfix expression from the given prefix expression.
* This version does not handle errors and does not allow
* integer literals.
*
* @param infix a legal infix expression
*/
public Postfix(String infix)
{
exp = "";
// create a queue containing the tokens in the infix expression;
// we do this because StringTokenizer has no equivalent of peek
LinkedList queue = new LinkedList();
StringTokenizer tok = new StringTokenizer(infix);
while (tok.hasMoreTokens())
queue.addLast(tok.nextToken());
// create the operator stack
LinkedList stack = new LinkedList();
while (stack.size() + queue.size() > 0)
{
if (queue.size() == 0)
exp = exp + stack.removeFirst() + " ";
else if (isIdentifier((String)(queue.getFirst())))
exp = exp + queue.removeFirst() + " ";
else if (queue.getFirst().equals("("))
stack.addFirst(queue.removeFirst());
else if (stack.size() == 0)
stack.addFirst(queue.removeFirst());
else if (getPrecedence((String)(queue.getFirst()))
> getPrecedence((String)(stack.getFirst())))
stack.addFirst(queue.removeFirst());
else if (stack.getFirst().equals("(")
&& queue.getFirst().equals(")"))
{
stack.removeFirst();
queue.removeFirst();
}
else
exp = exp + stack.removeFirst() + " ";
}
}
/**
* Precedence levels for arithmetic operators and parentheses.
* Levels are spaced out to make it easier to add operators with
* in between precedences.
*/
private static final int ADDITIVE = 0;
private static final int MULTIPLICATIVE = 10;
private static final int PARENTHESES = -10;
/**
* Returns the precedence of the given operator.
*
* @param op an operator or parenthesis
* @return the precedence of the given operator.
*/
private static int getPrecedence(String op)
{
if (op.equals("+") || op.equals("-"))
return ADDITIVE;
else if (op.equals("*") || op.equals("/"))
return MULTIPLICATIVE;
else if (op.equals("(") || op.equals(")"))
return PARENTHESES;
else
throw new IllegalArgumentException();
}
/**
* Determines if the given string is an identifier.
*
* @param id a string
* @return true iff the given string is an identifier
*/
private static boolean isIdentifier(String id)
{
return Character.isJavaIdentifierStart(id.charAt(0));
}
/**
* Returns a string representation of this expression.
*
* @return a string representation of this expression.
*/
public String toString()
{
return exp;
}
public static void main(String[] args) throws IOException
{
BufferedReader in
= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a legal infix expression:");
System.out.println(new Postfix(in.readLine()));
}
}
This code can also be downloaded from the files
ImageModel.java,
and Postfix.java.