Lecture Slides on Stacks - Data Structures | CS 261, Study notes of Data Structures and Algorithms

Material Type: Notes; Class: DATA STRUCTURES; Subject: Computer Science; University: Oregon State University; Term: Spring 2003;

Typology: Study notes

Pre 2010

Uploaded on 08/31/2009

koofers-user-u2s
koofers-user-u2s 🇺🇸

9 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS261–DataStructures
Stacks
Stacks
Linear data abstraction:
Elements added only at ends and retain order in which they were
inserted
•Stacks(LIFO):
–addLast(push)
removeLast (pop)
–getLast(top)
Stack applications:
Web browser back button
Activation record stack ÆRecursion
Reverse Polish Notation (RPN) calculator
Undo, backspace key, searching/backtracking
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture Slides on Stacks - Data Structures | CS 261 and more Study notes Data Structures and Algorithms in PDF only on Docsity!

CS 261 – Data Structures

Stacks

Stacks

• Linear data abstraction:

  • Elements added only at ends and retain order in which they were

inserted

• Stacks (LIFO):

  • addLast (push)
  • removeLast (pop)
  • getLast (top)

• Stack applications:

  • Web browser back button
  • Activation record stack Æ Recursion
  • Reverse Polish Notation (RPN) calculator
  • Undo, backspace key, searching/backtracking

Stack Implementation

  • We’ve already seen two data structures that implement

the stack interface:

  • Vector: Stack stk = new Vector();
  • LinkedList: Stack stk = new LinkedList();
  • We will study another later called IndexedDeque
  • Both Vector and LinkedList implement the operations

in constant time:

  • In practice, either implementation works well

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

  • Backtracking is a problem solving technique
  • Useful when searching for a solution in a state space (or

decision tree):

  • Each state (or decision tree node) defines a partial solution
  • Solution found by progressing from start state (root of tree) to

adjoining valid state until reaching a solution to entire problem

  • Often have multiple options for next state (multiple branches in tree)
  • Some states represent an invalid solution or they have no

valid next state:

  • Mark state as invalid and backup to previous valid state
  • Use a stack to hold states as they are visited and then back up

Place 8 queens on a chessboard so that none can capture

another (no two in the same row, column, or diagonal):

Implemented using a Stack to backtrack when it gets stuck

Stack Applications: 8 Queens

1 Q

2 3 4 5 6 7 8

a b c d e f g h

Stack:Stack:

1a

Q

Q

Q

Q

Stack:

2c

1a

Stack:

3e

2c

1a

Stack:

4b

3e

2c

1a

Stack:

5d

4b

3e

2c

1a

No Free Squares in 6:

  • Backup Æ Pop stack
  • Try another square in row 5 (to right of 5d)

Place 8 queens on a chessboard so that none can capture

another (no two in the same row, column, or diagonal):

Implemented using a Stack to backtrack when it gets stuck

Stack Applications: 8 Queens

1 Q

2 3 4 5 6 7 8

a b c d e f g h

Stack:Stack:

1a

Q

Q

Q

Q

Q

Q

Stack:

2c

1a

Stack:

3e

2c

1a

Stack:

4g

3e

2c

1a

Stack:

5b

4b

3e

2c

1a

Stack:

6d

5b

4b

3e

2c

1a

Stack:

7f

6d

5b

4b

3e

2c

1a

No Free Squares in 8:

  • Backup and try again

Place 8 queens on a chessboard so that none can capture

another (no two in the same row, column, or diagonal):

Implemented using a Stack to backtrack when it gets stuck

Stack Applications: 8 Queens

Q

Q

Q

Q

Q

Q

Q

Q

1 2 3 4 5 6 7 8

a b c d e f g h

Eventually, you will

find a solution (though

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):

Implemented using a Stack to backtrack when it gets stuck