Review: STACKS - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

Review: STACKS

Description:

... identify three different kinds of expressions: Preconditions ... A violation of an invariant may indicate a bug in either the client or the software component. ... – PowerPoint PPT presentation

Number of Views:16
Avg rating:3.0/5.0
Slides: 28
Provided by: josemg2
Category:
Tags: stacks | bug | end | is | kind | of | rear | review | this | what

less

Transcript and Presenter's Notes

Title: Review: STACKS


1
Review STACKS
  • Data structures that have only one end, the Top
    of the stack
  • Insertions and removals of items are carried out
    through the Top of the stack
  • Are also called last-in-first-out structures,
    because the last item inserted is the first one
    that can be removed.

2
A Stack As An ADT
  • An abstract data type in which items are inserted
    and removed from a single end a last in, first
    out (LIFO) structure

3
OPERATIONS ON A STACK
  • isEmpty, to test if the stack is empty
  • isFull, to test if the stack is full
  • Push, to insert an item into the stack
  • Pop, to remove the item at the TOP of the stack
  • Top, to get a copy of the item at the top of the
    stack

4
IMPLEMENTATION OF STACKS
  • Arrays
  • ArrayList (Dynamic arrays)
  • Linked lists

5
A STACK ABSTRACT CLASS
class Stack private int data private
final int MAX_ITEMS private int top
// index of the item at the top // public
Stack() // postcond stack is empty
public boolean Isempty() public boolean
IsFull() public void push( int newItem)
// postcond size of stack incremented by 1,
stack is not empty public int top () //
precond stack is not empty // postcond
stack is not changed public int pop() //
precond stack is not empty // postcond
size of stack is decreased by 1
6
Design by Contract
  • The Design by Contract (DBC) software development
    technique ensures high-quality software by
    guaranteeing that every component of a system
    lives up to its expectations.
  • As a developer using DBC, you specify component
    contracts as part of the component's interface.
  • The contract specifies what that component
    expects of clients and what clients can expect of
    it.

7
DBC in Programming Languages
  • DBC is a valuable design technique for all
    programming languages, including Java.
  • Central to DBC is the notion of an assertion -- a
    Boolean expression about the state of a software
    system.
  • At runtime we evaluate the assertions at specific
    checkpoints during the system's execution. In a
    valid software system, all assertions evaluate to
    true.
  • If any assertion evaluates to false, we consider
    the software system invalid or broken.

8
Assertions
  • In DBC, we identify three different kinds of
    expressions
  • Preconditions
  • Postconditions
  • Invariants

9
Preconditions
  • Preconditions specify conditions that must hold
    before a method can execute. As such, they are
    evaluated just before a method executes.
  • Preconditions involve the system state and the
    arguments passed into the method.
  • Preconditions specify obligations that a client
    of a software component must meet before it may
    invoke a particular method of the component.
  • If a precondition fails, a bug is in a software
    component's client.

10
Postconditions
  • Postconditions specify conditions that must hold
    after a method completes. Consequently,
    postconditions are executed after a method
    completes.
  • Postconditions involve the old system state, the
    new system state, the method arguments, and the
    method's return value.
  • Postconditions specify guarantees that a software
    component makes to its clients.
  • If a postcondition is violated, the software
    component has a bug.

11
Invariants
  • An invariant specifies a condition that must hold
    anytime a client could invoke an object's method.
  • Invariants are defined as part of a class
    definition. In practice, invariants are evaluated
    anytime before and after a method on any class
    instance executes.
  • A violation of an invariant may indicate a bug in
    either the client or the software component.

12
Assertions, Inheritance, and Interfaces
  • All assertions specified for a class and its
    methods apply to all subclasses as well.
  • You can also specify assertions for interfaces.
    As such, all assertions of an interface must hold
    for all classes that implement the interface.

13
Java Example
  • By convention, preconditions on public methods
    are enforced by explicit checks that throw
    particular, specified exceptions. For example
  • /
  • Sets the refresh rate.
  • _at_param rate refresh rate, in frames per
    second.
  • _at_throws IllegalArgumentException if rate
    lt 0 or
  • rate gt MAX_REFRESH_RATE.
  • /
  • public void setRefreshRate(int rate)
  • // Enforce specified precondition in
    public method
  • if (rate lt 0 rate gt MAX_REFRESH_RATE)
  • throw new IllegalArgumentException("I
    llegal rate " rate)
  • setRefreshInterval(1000/rate)

14
QUEUE
  • A data structure that consists of two ends, the
    head (front) and the tail (rear).
  • It is a container ADT in which items are inserted
    at the tail and removed at the head
  • The ordering of the items is first in first out
    (FIFO)
  • It is a higher-level data structure

15
QUEUE IMPLEMENTATION
  • A queue can be implemented with
  • Arrays
  • ArrayList (Dynamic arrays)
  • Linked lists

16
  • What is a queue?
  • A collection of items that can be inserted at the
    tail end removed at the head
  • Items must be removed in the same order of that
    in which they are inserted into the queue
  • FIFO first in and first out

17
  • Queue Specification
  • isEmpty()
  • Return true if the queue is empty o.w, false
  • peekfront()
  • Return the head item without removing it from the
    queue
  • remove
  • Return the head item and remove it from the queue
  • insert
  • Insert a new item into tail of the queue
  • size
  • Return the number of items in the queue

18
  • Problem?
  • two references head and tail
  • Changing the head and tail, figure 4.7 on page
    135
  • when head moves up to the tail
  • Can you insert a new item?
  • How to solve the problem?
  • Figure 4.8, 4.9 on page 136, 137

19
Solutions
  • After a removal, shift all the items in queue one
    position to the front
  • Use a circular queue implementation

20
Circular Queue
  • Using an array implementation
  • The Head and Tail indices of the queue should
    wrap around to the beginning of the array

21
  • class Queue private int maxSize
    private long queArray private int front
    private int rear private int nItems
    public void insert(long j) // put item at rear
    of queue if(rear maxSize-1)
    // deal with wrap around rear -1
  • queArrayrear j // increment
    rear and insert nItems
    // one more item public long
    remove() // take item from front of
    queue long temp
    queArrayfront // get value and incr front
    if(front maxSize) // deal with
    wraparound front 0 nItems--
    // one less item return
    temp public long peekFront() //
    peek at front of queue return
    queArrayfront public int size()
    // number of items in queue
    return nItems // end class Queue

22
QUEUE ADT
  • A queue consists of a lower level data structure
    to contain the items in the queue
  • The relevant operations are
  • Insert item at the tail (rear) of the queue
  • Remove item from the head (front) of the queue
  • Get the size of the queue
  • Get condition of emptyness

23
A QUEUE ABSTRACT CLASSUsing a Linked List
Implementation
class Queue private Node head // ref to
front of queue private Node tail // ref
to rear of queue private int size //
number of items in queue public Queue()
public void insert ( Item entry) public Item
remove () public int get_size () public
boolean empty ()
24
  • What is priority queue?
  • Priority queue and queue are two different data
    structures
  • Items are stored in a Sorted order
  • Item with the highest priority (lowest key or
    highest key) always at the head (not FIFO)
  • Highly restricted access!
  • New item is inserted in the proper position to
    maintain the order, not necessarily at the tail
    (similar to sorted array)

25
  • Priority queue operations
  • Insert
  • Find proper position for the new elements
  • Shift elements with lower key (higher key) up
  • Delete
  • Always the head element of the priority queue
  • Peek
  • Return a copy of head element without removing it

26
  • class PriorityQ private int maxSize
    private long queArray private int nItems
    //-----------------------------------------------
    -------------- public void insert(long item)
    // insert item int j
    if(nItems0) // if no
    items, queArraynItems item
    // insert at 0 else
    // if items,
    for(jnItems-1 jgt0 j--) // start at
    end, if( item gt
    queArrayj ) // if new item larger,
    queArrayj1 queArrayj // shift
    upward else
    // if smaller, break
    // done shifting // end
    for queArrayj1 item //
    insert it nItems // end
    else (nItems gt 0) // end
    insert()//---------------------------------------
    ---------------------- public long remove()
    // remove minimum item return
    queArray--nItems //--------------------------
    ----------------------------------- public
    long peekMin() // peek at minimum
    item return queArraynItems-1
    //----------------------------------------------
    --------------- // end class PriorityQ

27
public void insertionSort() int in,
outfor(out1 outltnElems out)
long temp aout in out
while(ingt0 ain-1 gt temp)
ain ain-1
--in ain temp //
end for // end insertionSort()
public void insert(long item) int
j if(nItems0),
queArraynItems item else

for(jnItems-1 jgt0 j--),
if( item gt queArrayj ),
queArrayj1 queArrayj else
break queArrayj1 item
nItems // end else
(nItems gt 0) // end insert()
Excise make insert() O(1) while remove() takes
longer time
Write a Comment
User Comments (0)
About PowerShow.com