Data Structures: Stacks and Their Applications, Study notes of Data Structures and Algorithms

The concept of stacks as a linear data abstraction, their implementation using vector and linkedlist, and various applications such as parentheses checking, towers of hanoi, and searching/backtracking. The document also discusses the use of stacks in recursive algorithms and visualization.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-ebq
koofers-user-ebq 🇺🇸

10 documents

1 / 5

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 inwhich they were
inserted
Stacks (LIFO):
addLast (push)
removeLast (pop)
–getLast(top)
Stack applications:
Web browser back button
Activation record stack ÆRecursion
Reverse Polish Notati on (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
•BothVector and LinkedList implement the operations
in constant time:
In practice, eitherimplementation works well
pf3
pf4
pf5

Partial preview of the text

Download Data Structures: Stacks and Their Applications 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: Parentheses Checking

public ParenCheck(InputStream in) throws IOException { int i = in.read(); while (i != -1) { if (i == ‘(‘ ) push (‘)’); else if (i == ‘{‘ ) push (‘}’); else if (i == ‘[‘ ) push (‘]’); else if (i == ‘)’ ) check(‘)’); else if (i == ‘}’ ) check(‘}’); else if (i == ‘]’ ) check(‘]’); i = i.read(); } if (stk.isEmpty()) System.out.println(“Parenthesis Balance”); else reportError(); } } // End of class ParenCheck.

Stack Applications: Towers of Hanoi

  • We’ve seen this recursive algorithm
  • The algorithm didn’t explicitly use a stack:
    • Recursive calls use activation record stack to keep track of

parameters and local variables

  • Stacks used for visualization to keep track of the size of the

disks on each pole

Move disks one at a time from A to B without placing a larger disk

on top of smaller one

A B C

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

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

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:

5h

4b

3e

2c

1a

Still No Free Squares:

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

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

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

No More Squares in 5:

  • Backup Æ Pop stack
  • Try another square in row 4 (to right of 4b)

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

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

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