CS2007: Stack - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

CS2007: Stack

Description:

Replace java's Stack class in reverse example with 'ArrayStack' Still works! ... Java's implementation. Based on arrays, which are replaced by bigger arrays if needed ... – PowerPoint PPT presentation

Number of Views:96
Avg rating:3.0/5.0
Slides: 35
Provided by: computin7
Category:
Tags: cs2007 | javas | stack

less

Transcript and Presenter's Notes

Title: CS2007: Stack


1
CS2007 Stack
  • Collections, Abstract Data Types
  • Stacks
  • Another example reversing a sentence
  • Implementing stacks
  • Reading Malik, first part of chap 17 (Stacks)

2
Prog Aside double vs Double
  • Three similar things
  • double a number
  • Double object that holds a number
  • Double class with methods that work on doubles.
  • double a number
  • Can add, subtract, etc
  • double x 2
  • double y xx

3
Double
  • Double an object which holds a number
  • Has methods such as toString
  • Double x new Double(2)
  • String s x.toString()
  • Also a class with static methods for doubles
  • double x 2
  • String s Double.toString(x)

4
Similar for other numbers
  • int, Integer
  • float, Float
  • boolean, Boolean
  • char, Character
  • But just String (no string!)

5
Why wont this work?
  • double num 2345
  • textField.setText(num.toString())

6
Which to use
  • Suggestion use variables which are numbers
    (double, int, float, etc)
  • Use static methods such as Double.toString
  • Avoid variables which are objects that hold
    numbers (Double, Integer, etc)
  • double x //good
  • Double x // bad

7
Collections
  • Java uses collections to store sets of objects
  • Need to import java.util.
  • Collections differ by
  • Functionality
  • Type of object stored
  • Implementation

8
Major Collection Types
  • Set unordered collection (no first, no last)
  • List an ordered collection (first is first item
    added)
  • SortedSet ordered by items values (first is
    smallest item)
  • Stack a list, only last element is seen
  • Queue a list only first, last elements seen
  • Map a set of objects have unique keys (and are
    accessed by these keys)

9
Types of Object Stored
  • Collections usually are for specific types of
    objects
  • Specified in ltgt
  • StackltStringgt -- stack of Strings
  • ListltIntegergt -- list of Integers
  • MapltInteger,Stringgt -- map from integers to
    Strings
  • etc

10
Abstract Data Types
  • Collections are Abstract Data Types
  • They are defined by their functionality (API)
  • Eg push(), peek(), pop() for Stacks
  • They can be implemented in different ways
  • Arrays? Long strings? Files?
  • Affects performance, not functionality

11
Stack
  • Type of Java collection
  • Implementation as well as Abstract Data Type
    (unusual)
  • Can only access the top (end) element of the
    stack
  • A very limited collection

12
Declaring a Stack
  • StackltXXXgt new StackltXXXgt()
  • XXX is the type (String, GUIState, etc)
  • Can omit, default type is Object
  • Example
  • StackltStringgt stringStack new StackltStringgt()

13
Stack methods (repeat)
  • push (element) -- add element to the top of the
    stack
  • peek() -- return object at top of stack (dont
    change stack)
  • pop() return object at top of stack (and take
    it off the stack)
  • size() number of things in stack
  • empty() is stack empty?

14
Application 2 Reversing
  • Goal reverse the words in a sentence
  • I had lunch -gt lunch had I
  • Mary had a little lamb -gt lamb little a had Mary

15
Pseudocode
  • Read sentence from GUI
  • Break it up into words (use Scanner)
  • Push each word onto a Stack
  • Pop words off stack, add to result
  • Put result on GUI

16
Example
17
Code
  • String sentence reverseField.getText()
  • StackltStringgt reverseStack new
    StackltStringgt()
  • Scanner scan new Scanner(sentence)
  • while (scan.hasNext())
  • reverseStack.push(scan.next())
  • String result ""
  • while (!reverseStack.empty())
  • result result " "
    reverseStack.pop()
  • reverseField.setText(result)

18
Implementing a Stack
  • Consider two possibilities
  • Array
  • Big String
  • Assume Stack holds Strings
  • Ignore errors (eg push from empty stack)
  • See Malik for more details
  • Arbitrary types, error handling, etc

19
Array Implementation
  • Define a single class, ArrayStack
  • Contains two variables
  • String data // holds data
  • int stackTop // number of things on stack
  • Initialisation
  • Create data array
  • Set stackTop to 0

20
Methods
  • Push
  • Set datastackTop to parameter
  • Add 1 to stackTop
  • Pop
  • Subtract 1 from stackTop
  • Return datastackTop
  • Empty
  • Return TRUE if stackTop is 0

21
Code
  • public class ArrayStack
  • private String data // data on stack
  • private int stackTop // number of
    things on Stack
  • public ArrayStack()
  • data new String100
  • stackTop 0
  • public void push(String element)
  • datastackTop element
  • stackTop stackTop 1
  • public String pop ()
  • stackTop stackTop - 1
  • return datastackTop
  • public boolean empty ()
  • return (stackTop 0)

22
Usage in reverse
  • Replace javas Stack class in reverse example
    with ArrayStack
  • Still works!
  • Weve changed implementation, and hence speed,
    memory usage, etc
  • We havent changed functionality
  • ArrayStack does same thing as StackltStringgt
  • Same methods (API)

23
Big String Implementation
  • Define a class BigStringStack
  • Contains a String which is a concatenation of the
    stack elements, separated by
  • Tom, Sam -gt TomSam

24
Methods
  • Push
  • Append element to end of string
  • Pop
  • Find last , return what is after this and also
    remove this from string
  • Empty
  • Return TRUE if string is empty

25
Code
  • private String data // data on stack
  • public StringStack()
  • data ""
  • public void push(String element)
  • data data "" element
  • public String pop ()
  • String result data.substring(data.lastIn
    dexOf("") 1)
  • data data.substring(0,
    data.lastIndexOf(""))
  • return result
  • public boolean empty ()
  • return (data.equals(""))

26
Usage in reverse
  • Replace javas Stack class in reverse example
    with StringStack
  • Once again, still works!
  • StringStack has same functionality (methods, API)
    as ArrayStack, StackltStringgt

27
Which Implementation to Use
  • Student suggestions?

28
Which Implementation to Use?
  • ArrayStack
  • Fast
  • But will crash if stack is too large
  • Can waste memory if array is large and stack is
    small
  • StringStack
  • Much slower, but stack can be any size

29
Can We Fix ArrayStack
  • Can we modify ArrayStack so that stacks can be
    arbitrary size?
  • Then have implementation which is fast and not
    size-limited
  • Ideas?
  • Algorithmic Problem Solving Challenge

30
Soln Make Array Bigger if Needed
  • Change push code so that if stack gets too big
    for the array, we create a new array which is
    bigger, and use it instead
  • Must also copy contents from old array to new
    array
  • Essentially how Java Stack class works

31
Increasing Array Size
  • public void push(String element)
  • if (stackTop gt data.length)
  • String newData new Stringdata.length2
  • for (int i 0 i lt data.length i)
  • newDatai datai
  • data newData
  • datastackTop element
  • stackTop stackTop 1

32
3 Stack Implementations!
  • ArrayStack
  • Our array-based stack implementation
  • StringStack
  • Our string based implementation
  • StackltStringgt
  • Javas implementation
  • Based on arrays, which are replaced by bigger
    arrays if needed
  • Also LinkedListStack,

33
Implementation
  • Many ways of implementing a particular Abstract
    Data Type such as Stack
  • Person using this probably doesnt care much
    about details, as long as the key methods (push,
    pop, empty, etc) work
  • Algorithmic Problem Solving challenge is creating
    implementation which is
  • efficient (speed and memory)
  • Flexible (eg, no constraints on Stack size)

34
Summary
  • Abstract Data Type specifies a set of methods
    (API) which someone using the class can use
  • Stack push, pop, peek, empty
  • Usually a Java interface
  • Implementation is code for these methods
  • Java class, not interface
  • Different implementations possible, choose one
    which is efficient and flexible
Write a Comment
User Comments (0)
About PowerShow.com