Title: ADT Stack
1ADT Stack
- What Is a Stack?
- Standard Template Library Stack Class
- Using a Stack
- Array Implementation of Stack Class
2Stack
- Which of the following structures are similar to
each other? - Books stored on book shelf
- Books piled up on desk
- Cargo plane with one opening
- Parking garage
- Stack of bath towels in linen closet
3Stack
- Linear data structure with insertion and removal
at one end called top - LIFOlast in, first out
4Stack Operations
- Push
- Insert an item at top
- Pop
- Remove an item from top
- Clear
- Make stack empty
- Top
- Return the top item without removal
- Empty
- Check for emptiness
5STL Stack
- void push(const T entry)
- Inserts an item at top
- void pop()
- Removes an item from top
- T top()
- Returns the top item without removal
- bool empty()
- Checks for emptiness
- Int size()
- Returns the number of items in stack
6Palindromes
- Madam, Im Adam.
- Able was I, ere I saw Elba.
- Do geese see God?
- Murder for a jar of red rum.
- A man, a plan, a canal Panama
- Go hang a salami I'm a lasagna hog!
- eye
- civic
- level
- radar
- rotor
- racecar
7Palindrome Check
word READER
s1
s2
- Push characters into s1 and s2
- Pop s2 items and push them to s3
8Palindrome Check
isPalind lt- true While (not s1.empty
isPalind) if (s1.top s3.top) s1.pop()
s3.pop() else isPalind false End
ifEnd while
9Main Program
Main expr READER print expr expr
normalize(expr) // remove non-alphas if
(isPalindrome(expr)) print " is a
palindrome" else print " is not a
palindrome. End ifEnd main // Reference
palindrome.cpp
10Balanced Parentheses
- (a b(c d) / e) parentheses balanced
- (a (b c))(d e) parentheses galanced
- (a (b c) /d) e) palrentheses not
balancedSimplified Problem - (()) parentheses balanced
- (())() parentheses balanced
- (())) parentheses not balanced
11Balanced Parentheses
string expr (())()
balanced true While (balanced more chars in
expr) if (char leftParen) then
stk.push(char) else if (stk.Empty) then
balanced false else stk.pop
End if End ifEnd loop return balanced
stk
12Main Program
Main expr ((()() print expr if
(isBalanced(expr)) print " is balanced"
else print " is not balanced. End
ifEnd main // Reference balancedParenced.cpp
13Stack Array Implementation
elemType dataMAX_SIZE int top
14stack.h (Declarations)
define MAX_SIZE 10 define EMPTY -1 typedef int
elemType class Stack public Stack()
void push(elemType item) void pop()
elemType top() int depth() bool
isEmpty() bool isFull() void
print() private elemType dataMAX_SIZE
int top
15Push() Operation
include stack.h void Stackpush(elemType
item) if (!isFull()) top
datatop item
16Efficiency of push() operation
- If it takes t seconds to push an element into a
stack of 1000 elements, how long does it take to
push one into a stack of 2000 elements? - The same time.
- Thus, O(n) 1.
17Pop() Operation
Void Stackpop() if (!isEmpty())
top--
18Efficiency of pop() operation
- If it takes t seconds to pop an element from a
stack of 1000 elements, how long does it take to
pop one from a stack of 2000 elements? - The same time.
- Thus, O(n) 1.
19Top() Operation
elementType Stacktop() if (!isEmpty())
return datatop
20The N-Queens Problem
- Suppose you have 8 chess queens...
- ...and a chess board
21The N-Queens Problem
- Can the queens be placed on the board so that no
two queens are attacking each other
?
22The N-Queens Problem
- Two queens are not allowed in the same row...
23The N-Queens Problem
- Two queens are not allowed in the same row, or
in the same column...
24The N-Queens Problem
- Two queens are not allowed in the same row, or
in the same column, or along the same diagonal.
25The N-Queens Problem
N Queens
- The number of queens, and the size of the board
can vary.
N columns
N rows
26The N-Queens Problem
- We will write a program which tries to find a way
to place N queens on an N x N chess board.
27How the program works
- The program uses a stack to keep track of where
each queen is placed.
28How the program works
- Each time the program decides to place a queen on
the board, the position of the new queen is
stored in a record which is placed in the stack.
29How the program works
- We also have an integer variable to keep track of
how many rows have been filled so far.
filled
30How the program works
- Each time we try to place a new queen in the next
row, we start by placing the queen in the first
column...
filled
31How the program works
- ...if there is a conflict with another queen,
then we shift the new queen to the next column.
filled
32How the program works
- If another conflict occurs, the queen is shifted
rightward again.
filled
33How the program works
- When there are no conflicts, we stop and add one
to the value of filled.
filled
34How the program works
- Let's look at the third row. The first position
we try has a conflict...
filled
35How the program works
- ...so we shift to column 2. But another conflict
arises...
filled
36How the program works
- ...and we shift to the third column.
- Yet another conflict arises...
filled
37How the program works
- ...and we shift to column 4. There's still a
conflict in column 4, so we try to shift
rightward again...
filled
38How the program works
- ...but there's nowhere else to go.
filled
39How the program works
- When we run out of
- room in a row
- pop the stack,
- reduce filled by 1
- and continue working on the
previous row.
filled
40How the program works
- Now we continue working on row 2, shifting the
queen to the right.
filled
41How the program works
- This position has no conflicts, so we can
increase filled by 1, and move to row 3.
filled
42How the program works
- In row 3, we start again at the first column.
filled
43Pseudocode for N-Queens
- Initialize a stack where we can keep track of our
decisions. - Place the first queen, pushing its position onto
the stack and setting filled to 0. - repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to
shift the current queen rightward... - else if there is a conflict and there is no room
to shift the current queen rightward...
44Pseudocode for N-Queens
- repeat these steps
- if there are no conflicts with the queens...
45Pseudocode for N-Queens
- repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to
shift the current queen rightward...
46Pseudocode for N-Queens
- repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to
shift the current queen rightward... - else if there is a conflict and there is no room
to shift the current queen rightward...
47Pseudocode for N-Queens
- repeat these steps
- if there are no conflicts with the queens...
- else if there is a conflict and there is room to
shift the current queen rightward... - else if there is a conflict and there is no room
to shift the current queen rightward...
48 Summary
- Stacks have many applications.
- The application which we have shown is called
backtracking. - The key to backtracking Each choice is recorded
in a stack. - When you run out of choices for the current
decision, you pop the stack, and continue trying
different choices for the previous decision.