Chapter 1 Arrays, Pointers, and Structures - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

Chapter 1 Arrays, Pointers, and Structures

Description:

Two Implementations of Stack. As Vector. Store the items contiguously in a vector. As list. Store items noncontiguously in a linked list ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 26
Provided by: Ken695
Learn more at: http://www.cs.gsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Chapter 1 Arrays, Pointers, and Structures


1
chapter 16
Stacks Queues
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
Stack Vector Implementation
A stack can be implemented with an vector and an
integer that indicates the index of the top
element.
  • 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

//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
5
How stack works
pop(b)
push(a)
push(b)
Empty stack
tos2
tos1
tos0
tos -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()
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 4. dequeue()
Back
Step 1. makeEmpty()
Back
After dequeue()
Step 2. enqueue(a)
Back
Back
Step 3. enqueue(b)
Back
9
Better Idea
  • Keep a Front index.
  • To Dequeue, increment Front. Therefore, Dequeue
    takes constant time now.

10
Queue
Step 4. dequeue()
Back
Step 1. makeEmpty()
Back
Front
Front
Step 2. enqueue(a)
Back
after dequeue()
Back
Front
Step 3. enqueue(b)
Back
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 QueueltObjectgtenqueu
e(const Object x) if(currentSize
theArray.size()) doubleQueue()
increment( back) theArrayback
x currentSize template ltclass Objectgt void
QueueltObjectgtdoubleQueue() theArray.resize(th
eArray.size() 2 1) if(front ! 0)
for(int i0 iltfront i) theArrayicurrent
Size theArrayi back
currentSize
15
Queue Vector Implementation cont.
  • template ltclass Objectgt
  • void QueueltObjectgtincrement(int x) const
  • x
  • if(x theArray.size())
  • x 0

template ltclass Objectgt Object QueueltObjectgtdequ
eue() if( isEmpty()) throw
UnderflowException() currentSize-- Object
frontItem theArrayfront increment(front) r
eturn frontItem
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 that 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
  • Stack Interface and Implementation by Linked list
  • StackLi.h
  • http//www.cs.fiu.edu/weiss/adspc2/code/StackLi
    .h
  • StackLi.cpp
  • http//www.cs.fiuedu/weiss/adspc2/code/StackLi.
    cpp
  • Test Program
  • TestStackLi.cpp
  • http//www.cs.fiu.edu/weiss/adspc2/code/TestSta
    ckLi.cpp

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
Front
Adding b to double queue
Back
Front
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)

25
Summary
  • Stack and Queue implementations are overviewed.
Write a Comment
User Comments (0)
About PowerShow.com