Chapter 16 Stacks - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Chapter 16 Stacks

Description:

Chapter 16 Stacks & Queues Bernard Chen Spring 2006 Objective In this chapter we will learn: Stacks Queues Different implementations (arrays and linked list) of both ... – PowerPoint PPT presentation

Number of Views:89
Avg rating:3.0/5.0
Slides: 25
Provided by: gsu
Category:
Tags: chapter | queues | stacks

less

Transcript and Presenter's Notes

Title: Chapter 16 Stacks


1
Chapter 16 Stacks Queues
  • Bernard Chen
  • Spring 2006

2
Objective
  • In this chapter we will learn
  • Stacks
  • Queues
  • Different implementations (arrays and linked
    list) of both
  • Comparison of implementation

3
Two Implementations of Stack
  • As Vector
  • Store the items contiguously in a vector.
  • As list
  • Store items noncontiguously in a linked list

4
How stack works
pop(b)
push(a)
push(b)
Empty stack







a


b
a



a
tos1
tos0
tos0
tos -1
5
Stack Vector Implementation
  • template ltclass Objectgt
  • class Stack
  • public
  • Stack( )
  • bool isEmpty( ) const
  • const Object top( ) const
  • void makeEmpty( )
  • void pop( )
  • void push( const Object x )
  • Object topAndPop( )
  • private
  • vectorltObjectgt theArray
  • int topOfStack

A stack can be implemented with an vector and an
integer that indicates the index of the top
element.
//construct the stack Template ltclass
Objectgt StackltObjectgtStack()the
Array(1) topOfStack -1
//test if the stack is logically empty Template
ltclass Objectgt StackltObjectgtisEmpty() const
return topOfStack -1
6
The push/pop function (vector-based)
  • template ltclass Objectgt
  • void StackltObjectgtpush( const Object x )
  • if( topOfStack theArray.size( ) - 1 )
  • theArray.resize( theArray.size( ) 2
    1 )
  • theArray topOfStack x
  • // If there is no vector doubling, push
    takes constant time, otherwise it
  • // takes O(N) time. But it does not happen
    often.

template ltclass Objectgt void StackltObjectgtpop(
const Object x ) if( isEmpty())
throw UnderflowException() topOfStack--
7
Queues Simple Idea
  • Store items in an vector with front item at index
    zero and back item at index Back.
  • Enqueue is easy increment Back.
  • Dequeue is inefficient all elements have to be
    shifted. (If use only one index)
  • Result Dequeue will be O( N ).

8
Queue
Step 1. makeEmpty()

Back
Step 4. dequeue()
Back
Step 2. enqueue()
Back
a
b
Step 3. enqueue()
After dequeue()
Back
Back
a b
b
9
Better Idea
  • Keep a Front index.
  • To Dequeue, increment Front. Therefore, Dequeue
    takes constant time now.

10
Queue
Step 1. makeEmpty()
Step 4. dequeue()
Back

Back
Front
b
Step 2. enqueue()
Back
Front
a
After dequeue()
Front
Step 3. enqueue()
Back
Back
b
a b
Front
Front
11
Circular Implementation
  • Previous implementation is O( 1 ) per operation.
  • However, after vector.size() times enqueues, we
    are full, even if queue is logically nearly
    empty.
  • Solution use wraparound to reuse the cells at
    the start of the vector. To increment, add one,
    but if that goes past end, reset to zero.

12
Circular Example
  • Both Front and Back wraparound as needed.

13
QUEUE--Vector Implementation
  • Mostly straightforward maintain
  • Front
  • Back
  • CurrentSize Current number of items in queue
  • Only tricky part is vector doubling because the
    queue items are not necessarily stored in an
    array starting at location 0, and the contiguity
    of wraparound must be maintained.

14
Queue Vector Implementation
  • Template ltclass Objectgt
  • Class Queue
  • public
  • Queue()
  • bool isEmpty() const
  • const Object getFront() const
  • void makeEmpty()
  • Object dequeue()
  • void enqueue (const Ojbect x)
  • private
  • vectorltObjectgt theArray
  • int currentSize
  • int front
  • int back
  • void increment (int x) const
  • void doubleQueue()
  • template ltclass Objectgt
  • void QueueltObjectgtenqueue(const Object x)
  • if(currentSize theArray.size())
  • doubleQueue()
  • increment( back)
  • theArrayback x
  • currentSize
  • template ltclass Objectgt
  • void QueueltObjectgtdoubleQueue()
  • theArray.resize(theArray.size() 2 1)
  • if(front ! 0)
  • for(int i0 iltfront i)
  • theArrayicurrentSize theArrayi
  • back currentSize

15
Queue Vector Implementation cont.
template ltclass Objectgt Object QueueltObjectgtdequ
eue() if( isEmpty()) throw
UnderflowException() currentSize-- Object
frontItem theArrayfront increment(front) r
eturn frontItem
template ltclass Objectgt void QueueltObjectgtincrem
ent(int x) const x if(x
theArray.size()) x 0
template ltclass Objectgt const Object
QueueltObjectgtgetFront() const if
(isEmpty()) throw UnderflowException()
return theArrayfront
template ltclass Objectgt void QueueltObjectgtmakeEm
pty() currentSize 0 front 0 back
theArray.size() 1
16
Linked List Implementation
  • Advantage of the linked list is excess memory is
    only one pointer per item.
  • In contrast, a contiguous vector implementation
    uses excess space.

17
Stack
  • The stack class can be implemented as a linked
    list in which the top of the stack is represented
    by the first item in the list.

topOfStack
18
Stack
  • Each stack item stores
  • element value
  • pointer to next element

19
Queue
  • Same idea, but has front and back

back
20
Comparison of the two methods
  • Both of them run in constant time per operation.
  • The vector version is likely to be faster.
  • But it has two drawbacks
  • The wraparound is a little confusing
  • It might waste more space.

21
Deque
  • A deque is a double-ended queue.
  • A deque is a kind of sequence that, like a
    vector, supports random access iterators. In
    addition, it supports constant time insert and
    erase operations at the beginning or the end
    insert and erase in the middle take linear time.
  • That is, a deque is especially optimized for
    pushing and popping elements at the beginning and
    end. As with vectors, storage management is
    handled automatically.

22
Access both ends is allowed.
Back
a
Front
Adding b to double queue
Back
Front
a b
b a
Front
Back
23
Common errors (Page 561)
  • Do not delete the top node directly before
    adjusting the top of the stack pointer
  • Be aware of memory leaks
  • Access is constant time in both of these
    implementations.

24
In class exercises
  • Draw the stack and queue data structures for each
    step in the following
  • add(1), add(2), remove, add(3), add(4), remove,
    remove, add(5)
Write a Comment
User Comments (0)
About PowerShow.com