




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Material Type: Notes; Class: DATA STRUCTURES; Subject: Computer Science; University: Oregon State University; Term: Spring 2003;
Typology: Study notes
1 / 8
This page cannot be seen from the preview
Don't miss anything!





Stack Implementation
the stack interface:
in constant time:
Stack Applications: Parentheses Checking
// Import statements. public class ParenCheck { private Stack stk = new Vector(); private void push(char c) { stk.addLast(new Character(c)); }
private void check(char c) { // Check stack for balanced parentheses. if (stk.isEmpty()) reportError(); // Didn’t balance! Character s = (Character)stk.getLast(); // Get character on stack stk.removeLast(); // and remove it! if (c != s.charValue()) reportError(); // If not same Æ error! }
public static void main(String [] args) { try { ParenCheck world = new ParenCheck(System.in); } catch (IOException e) { System.out.println(“Received IO Exception “ + e); } } ...
Stack Applications: Towers of Hanoi
private Stack pole1 = new LinkedList(); private Stack pole2 = new LinkedList(); private Stack pole3 = new LinkedList();
public void run() { int size = 10; for(int i = size; i > 0; i--) pole1.addLast(new Integer(i)); solveHanoi(size, pole1, pole2, pole3); // Solve it! }
public void solveHanoi(int n, Stack a, Stack b, Stack c) { // Move n disks from a to b using c. if (n > 0) { solveHanoi(n-1, a, c, b); // Move n -1 disks from a to c using b. Object disk = a.getLast(); // Move one disk from a to b. a.removeLast(); b.addLast(disk); repaint(); // Paint the contents of each stack. solveHanoi(n-1, c, b, a) // Move n –1 disks from c to b using a. } }
Stack Applications: Towers of Hanoi
public void paint(Graphics h) { paintStack(pole1, ...) paintStack(pole2, ...) paintStack(pole3, ...) }
private void paintStack(Stack stk, ...) { Enumeration e = stk.elements(); while(e.hasMoreElements()) { Integer disk = (Integer)e.nextElement(); int size = disk.intValue(); // Draw a rectangle this size! } }
Stack Applications: Searching/Backtracking
decision tree):
valid next state:
Place 8 queens on a chessboard so that none can capture
another (no two in the same row, column, or diagonal):
Stack Applications: 8 Queens
2 3 4 5 6 7 8
a b c d e f g h
Place 8 queens on a chessboard so that none can capture
another (no two in the same row, column, or diagonal):
Stack Applications: 8 Queens
2 3 4 5 6 7 8
a b c d e f g h
Place 8 queens on a chessboard so that none can capture
another (no two in the same row, column, or diagonal):
Stack Applications: 8 Queens
1 2 3 4 5 6 7 8
a b c d e f g h
you may have to backup all the way to the first row)
Stack Applications: Knight’s Tour
Can a knight visit each square on the chessboard exactly
once (using only legal chess moves):