Chapter 5 ADTs Stack and Queue Stacks of Coins and Bills Stacks - - PowerPoint PPT Presentation

chapter 5
SMART_READER_LITE
LIVE PREVIEW

Chapter 5 ADTs Stack and Queue Stacks of Coins and Bills Stacks - - PowerPoint PPT Presentation

Chapter 5 ADTs Stack and Queue Stacks of Coins and Bills Stacks of Boxes and Books TOP OF THE STACK TOP OF THE STACK Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition


slide-1
SLIDE 1

Chapter 5

ADTs Stack and Queue

slide-2
SLIDE 2

Stacks of Coins and Bills

slide-3
SLIDE 3

Stacks of Boxes and Books

TOP OF THE STACK TOP OF THE STACK

slide-4
SLIDE 4
  • Logical (or ADT) level: A stack is an
  • rdered group of homogeneous items

(elements), in which the removal and addition of stack items can take place

  • nly at the top of the stack.
  • A stack is a LIFO “last in, first out”

structure.

slide-5
SLIDE 5

Stack ADT Operations

  • MakeEmpty -- Sets stack to an empty state.
  • IsEmpty -- Determines whether the stack is currently

empty.

  • IsFull -- Determines whether the stack is currently full.
  • Push (ItemType newItem) -- Throws exception if stack

is full; otherwise adds newItem to the top of the stack.

  • Pop -- Throws exception if stack is empty; otherwise

removes the item at the top of the stack.

  • ItemType Top -- Throws exception if stack is empty;
  • therwise returns a copy of the top item
slide-6
SLIDE 6

ADT Stack Operations

Transformers

  • Push
  • Pop
  • Observers
  • IsEmpty
  • IsFull
  • change state
  • bserve state
slide-7
SLIDE 7

class StackType { public:

  • StackType( );

bool IsFull () const;

bool IsEmpty() const; void Push( ItemType item ); void Pop();

  • private:

ItemType Top(); private: int top; ItemType items[MAX_ITEMS]; };

What are the pre and post conditions?

slide-8
SLIDE 8

// File: StackType.cpp

  • #include "StackType.h"

#include <iostream> StackType::StackType( ) { top = -1; } bool StackType::IsEmpty() const { return(top == -1); }

  • bool StackType::IsFull() const

{ return (top = = MAX_ITEMS-1); }

slide-9
SLIDE 9

void StackType::Push(ItemType newItem) { if( IsFull() ) throw FullStack(): top++; items[top] = newItem; } void StackType::Pop() { if( IsEmpty() ) throw EmptyStack(); top--; }

  • ItemType StackType::Top()

{ if (IsEmpty()) throw EmptyStack(); return items[top]; }

slide-10
SLIDE 10

Class Interface Diagram


(Memory reversed to better illustrate concept)

StackType class

StackType Pop Push IsFull IsEmpty

Private data:

  • top
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ]
  • [ 1 ]
  • items [ 0 ]

Top

slide-11
SLIDE 11

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

Tracing Client Code

Private data:

  • top
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ]
  • [ 1 ]
  • items [ 0 ]

letter

‘V’

slide-12
SLIDE 12

Tracing Client Code

letter

‘V’

Private data:

  • top -1
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ]
  • [ 1 ]
  • items [ 0 ]

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-13
SLIDE 13

Tracing Client Code

letter

‘V’

Private data:

  • top 0
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ]
  • [ 1 ]
  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-14
SLIDE 14

Tracing Client Code

letter

‘V’

Private data:

  • top 1
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ]
  • [ 1 ] ‘C’
  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-15
SLIDE 15

Tracing Client Code

letter

‘V’

Private data:

  • top 2
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘S’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-16
SLIDE 16

Tracing Client Code

letter

‘V’

Private data:

  • top 2
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘S’
  • [ 1 ] ‘C’
  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-17
SLIDE 17

Tracing Client Code

letter

‘V’

Private data:

  • top 1
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘S’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’0

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-18
SLIDE 18

Tracing Client Code

letter

‘V’

Private data:

  • top 2
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-19
SLIDE 19

Tracing Client Code

letter

‘V’

Private data:

  • top 2
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-20
SLIDE 20

Tracing Client Code

letter

‘K’

Private data:

  • top 2
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-21
SLIDE 21

Tracing Client Code

letter

‘K’

Private data:

  • top 1
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-22
SLIDE 22

Tracing Client Code

letter

‘K’

Private data:

  • top 1
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-23
SLIDE 23

Tracing Client Code

letter

‘C’

Private data:

  • top 0
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-24
SLIDE 24

Tracing Client Code

letter

‘C’

Private data:

  • top 0
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’
  • [ 1 ] ‘C’
  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-25
SLIDE 25

Tracing Client Code

letter

‘V’

Private data:

  • top -1
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-26
SLIDE 26

End of Trace

letter

‘V’

Private data:

  • top -1
  • [MAX_ITEMS-1]
  • .

. .

  • [ 2 ] ‘K’

[ 1 ] ‘C’

  • items [ 0 ] ‘V’

char letter = ‘V’; StackType charStack;

  • charStack.Push(letter);
  • charStack.Push(‘C’);
  • charStack.Push(‘S’);
  • if ( !charStack.IsEmpty( ))

charStack.Pop( );

  • charStack.Push(‘K’);
  • while (!charStack.IsEmpty( ))

{ letter = charStack.Top(); charStack.Pop(0)}

slide-27
SLIDE 27


 Another Stack Implementation

  • One advantage of an ADT is that the

implementation can be changed without the program using it knowing about it.

  • The dynamic array implementation of the stack

has a weakness -- the maximum size of the stack is passed to the constructor as parameter.

  • Instead we can dynamically allocate the space

for each stack element as it is pushed onto the stack.

slide-28
SLIDE 28

ItemType is char

class StackType

StackType Top Pop Push IsFull IsEmpty

Private data:

  • topPtr
  • ~StackType

‘C’ ‘V’

slide-29
SLIDE 29

class StackType

StackType Top Pop Push IsFull IsEmpty

Private data:

  • topPtr
  • ~StackType

23.4 -7.9

ItemType is float

slide-30
SLIDE 30

ItemType is string

class StackType

StackType Top Pop Push IsFull IsEmpty

Private data:

  • topPtr
  • ~StackType

cat dog

slide-31
SLIDE 31

Tracing Client Code

letter

‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-32
SLIDE 32

Tracing Client Code

letter

‘V’

Private data:

  • topPtr NULL
  • char

letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-33
SLIDE 33

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘V’

‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-34
SLIDE 34

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘C’ ‘V’

‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-35
SLIDE 35

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘S’ ‘ C’ ‘ V’

‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-36
SLIDE 36

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘S’ ‘ C’ ‘V’

‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-37
SLIDE 37

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘C’ ‘V’

‘S’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-38
SLIDE 38

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘C’ ‘V’

‘S’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-39
SLIDE 39

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘V’

‘C’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-40
SLIDE 40

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘V’

‘C’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-41
SLIDE 41

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-42
SLIDE 42

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-43
SLIDE 43

Tracing Client Code

letter

Private data:

  • topPtr
  • ‘K’

‘V’

char letter = ‘V’;

  • StackType myStack;
  • myStack.Push(letter);
  • myStack.Push(‘C’);
  • myStack.Push(‘S’);
  • If (!myStack.IsEmpty() )

{ letter = myStack.Top( ); myStack.Pop(); }

  • myStack.Push(‘K’);
slide-44
SLIDE 44

// DYNAMICALLY LINKED IMPLEMENTATION OF STACK Struct NodeType; //Forward declaration

  • class StackType

{ public: //Identical to previous implementation private: NodeType* topPtr; }; . . . Struct NodeType { ItemType info; NodeType* next; };

slide-45
SLIDE 45

Adding newItem to the stack

newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location;

  • topPtr

‘X’ ‘C’ ‘L’ ‘B’

newItem

slide-46
SLIDE 46

Adding newItem to the stack

newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location;

  • topPtr

‘X’ ‘C’ ‘L’ ‘B’

newItem location

slide-47
SLIDE 47

Adding newItem to the stack

newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location;

  • topPtr

‘X’ ‘C’ ‘L’ ‘B’

newItem location

slide-48
SLIDE 48

Adding newItem to the stack

newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location;

  • topPtr

‘X’ ‘C’ ‘L’ ‘B’

newItem location

‘B’

slide-49
SLIDE 49

Adding newItem to the stack

newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location;

  • topPtr

‘X’ ‘C’ ‘L’ ‘B’

newItem location

‘B’

slide-50
SLIDE 50

Adding newItem to the stack

newItem = ‘B’; NodeType* location; location = new NodeType<char>; location->info = newItem; location->next = topPtr; topPtr = location;

  • topPtr

‘X’ ‘C’ ‘L’ ‘B’

newItem location

‘B’

slide-51
SLIDE 51
  • void StackType::Push ( ItemType newItem )

// Adds newItem to the top of the stack. {

if (IsFull())

throw FullStack(); NodeType* location; location = new NodeType<ItemType>; location->info = newItem; location->next = topPtr; topPtr = location; }

Implementing Push

Do we need IsFull?

slide-52
SLIDE 52

Deleting item from the stack

NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr;

  • topPtr

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

item

slide-53
SLIDE 53

Deleting item from the stack

NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr;

  • topPtr

item

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

‘B’

slide-54
SLIDE 54

Deleting item from the stack

NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr;

  • topPtr

item

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

‘B’

slide-55
SLIDE 55

Deleting item from the stack

NodeType* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr;

  • topPtr

item

‘B’ ‘X’ ‘C’ ‘L’

tempPtr

‘B’

slide-56
SLIDE 56

Deleting item from the stack

NodeType<ItemType>* tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr;

  • topPtr

item

‘X’ ‘C’ ‘L’

tempPtr

‘B’

slide-57
SLIDE 57

void StackType::Pop() // Remove top item from Stack. { if (IsEmpty()) throw EmptyStack(); else { NodeType* tempPtr; tempPtr = topPtr; topPtr = topPtr ->next; delete tempPtr; } }

  • ItemType StackType::Top()

// Returns a copy of the top item in the stack. { if (IsEmpty()) throw EmptyStack(); else return topPtr->info; }

Implementing Pop / Top

slide-58
SLIDE 58

Implementing IsFull

bool StackType::IsFull() const // Returns true if there is no room for another // ItemType on the free store; false otherwise { NodeType* location; try { location = new NodeType; delete location; return false; } catch(std::bad_alloc exception) { return true; } }

slide-59
SLIDE 59

Example of a Queue

slide-60
SLIDE 60
  • Logical (or ADT) level: A queue is an
  • rdered group of homogeneous items

(elements), in which new elements are added at one end (the rear), and elements are removed from the other end (the front).

  • A queue is a FIFO “first in, first out”

structure.

slide-61
SLIDE 61

Queue ADT Operations

  • MakeEmpty -- Sets queue to an empty state.
  • IsEmpty -- Determines whether the queue is currently

empty.

  • IsFull -- Determines whether the queue is currently full.
  • Enqueue (ItemType newItem) -- Adds newItem to the

rear of the queue.

  • Dequeue (ItemType& item) -- Removes the item at the

front of the queue and returns it in item.

slide-62
SLIDE 62

ADT Queue Operations

Transformers

  • MakeEmpty
  • Enqueue
  • Dequeue
  • Observers
  • IsEmpty
  • IsFull

change state

  • bserve state
slide-63
SLIDE 63

typedef char ItemType; class QueType { public: QueType(int max); // max is the size of the queue. QueType(); // Default size of 500. ~QueType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Enqueue(ItemType item); void Dequeue(ItemType& item);

Public Interface of QueType

slide-64
SLIDE 64

SIMPLE ARRAY IMPLEMENTATION

QueType ~QueType Enqueue Dequeue

. . .

class QueType

Private Data:

  • front 1
  • rear 4
  • maxQue 5
  • items

‘C’‘X’ ‘J’

items [0] [1] [2] [3] [4]

RESERVED

slide-65
SLIDE 65

What is the private part of class QueType for this design?

  • What troubles do we encounter?
slide-66
SLIDE 66

DYNAMIC ARRAY IMPLEMENTATION

QueType ~QueType Enqueue Dequeue

. . .

class QueType

Private Data:

  • front 1
  • rear 4
  • maxQue 5
  • items

‘C’ ‘X’ ‘J’

items [0] [1] [2] [3] [4]

RESERVED

slide-67
SLIDE 67

What is the private part of class QueType for this design?

  • Does this design have the same

problems as the previous design?

slide-68
SLIDE 68

class QueType

QueType ~QueType Enqueue Dequeue

. . .

Private Data:

  • qFront
  • qRear

‘C’ ‘Z’ ‘T’

slide-69
SLIDE 69

What is the private part of class QueType for this design?

slide-70
SLIDE 70

CountedQueType inherits from QueType

  • // DERIVE CLASS CountedQueType FROM BASE CLASS QueType
  • class CountedQueType : QueType;

{ public: CountedQueType( ); void Enqueue( ItemType newItem ); void Dequeue( ItemType& item ); int GetLength( ) const; // Returns number of items on the counted queue.

  • private:

int length; };

slide-71
SLIDE 71

What additional fields does CountedQueType need?

  • What functions must be

changed?

slide-72
SLIDE 72