ADT Stack - PowerPoint PPT Presentation

1 / 48
About This Presentation
Title:

ADT Stack

Description:

ADT Stack What Is a Stack? Standard Template Library Stack Class Using a Stack Array Implementation of Stack Class – PowerPoint PPT presentation

Number of Views:165
Avg rating:3.0/5.0
Slides: 49
Provided by: RKM8
Category:
Tags: adt | book | stack

less

Transcript and Presenter's Notes

Title: ADT Stack


1
ADT Stack
  1. What Is a Stack?
  2. Standard Template Library Stack Class
  3. Using a Stack
  4. Array Implementation of Stack Class

2
Stack
  • 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

3
Stack
  • Linear data structure with insertion and removal
    at one end called top
  • LIFOlast in, first out

4
Stack 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

5
STL 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

6
Palindromes
  • 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

7
Palindrome Check
word READER
s1
s2
  • Push characters into s1 and s2
  • Pop s2 items and push them to s3

8
Palindrome Check
isPalind lt- true While (not s1.empty
isPalind) if (s1.top s3.top) s1.pop()
s3.pop() else isPalind false End
ifEnd while
9
Main 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
10
Balanced 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

11
Balanced 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
12
Main Program
Main expr ((()() print expr if
(isBalanced(expr)) print " is balanced"
else print " is not balanced. End
ifEnd main // Reference balancedParenced.cpp
13
Stack Array Implementation
elemType dataMAX_SIZE int top
14
stack.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
15
Push() Operation
include stack.h void Stackpush(elemType
item) if (!isFull()) top
datatop item
16
Efficiency 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.

17
Pop() Operation
Void Stackpop() if (!isEmpty())
top--
18
Efficiency 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.

19
Top() Operation
elementType Stacktop() if (!isEmpty())
return datatop
20
The N-Queens Problem
  • Suppose you have 8 chess queens...
  • ...and a chess board

21
The N-Queens Problem
  • Can the queens be placed on the board so that no
    two queens are attacking each other

?
22
The N-Queens Problem
  • Two queens are not allowed in the same row...

23
The N-Queens Problem
  • Two queens are not allowed in the same row, or
    in the same column...

24
The N-Queens Problem
  • Two queens are not allowed in the same row, or
    in the same column, or along the same diagonal.

25
The N-Queens Problem
N Queens
  • The number of queens, and the size of the board
    can vary.

N columns
N rows
26
The 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.

27
How the program works
  • The program uses a stack to keep track of where
    each queen is placed.

28
How 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.

29
How the program works
  • We also have an integer variable to keep track of
    how many rows have been filled so far.

filled
30
How 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
31
How the program works
  • ...if there is a conflict with another queen,
    then we shift the new queen to the next column.

filled
32
How the program works
  • If another conflict occurs, the queen is shifted
    rightward again.

filled
33
How the program works
  • When there are no conflicts, we stop and add one
    to the value of filled.

filled
34
How the program works
  • Let's look at the third row. The first position
    we try has a conflict...

filled
35
How the program works
  • ...so we shift to column 2. But another conflict
    arises...

filled
36
How the program works
  • ...and we shift to the third column.
  • Yet another conflict arises...

filled
37
How 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
38
How the program works
  • ...but there's nowhere else to go.

filled
39
How 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
40
How the program works
  • Now we continue working on row 2, shifting the
    queen to the right.

filled
41
How the program works
  • This position has no conflicts, so we can
    increase filled by 1, and move to row 3.

filled
42
How the program works
  • In row 3, we start again at the first column.

filled
43
Pseudocode 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...

44
Pseudocode for N-Queens
  • repeat these steps
  • if there are no conflicts with the queens...

45
Pseudocode 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...

46
Pseudocode 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...

47
Pseudocode 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.
Write a Comment
User Comments (0)
About PowerShow.com