1 Data Structures and Algorithms Stacks and Queues

Preview:

Citation preview

1

Data Structures and Algorithms

Stacks and Queues

Guidelines for Selecting ADT Operations

• Completeness -- Every ADT must be complete. It must include enough operations, including ADT-constructors, to build every possible value of the domain.

• Testability -- The preconditions of all ADT operations must be testable. The ADT must provide enough test operations for the client to check all

preconditions of the ADT operations.

2

Types of ADT Operations to Include

• ADT-Constructors -- to build values of the type

• Test Operations -- to test some condition about an ADT value

• I/O Operations -- to input or output an ADT value

• Copy Operations -- to copy one ADT value to another ADT object

• Selector Operations (Access Functions) -- to retrieve a portion of an ADT value

• Destructor Operations -- to deallocate any dynamic data created by an ADT

3

4

Definition of ADT Stack

• A stack is a linear data structure with homogeneous components (elements), in which all insertions and deletions occur at one end, called the top of the stack.

• A stack is a LIFO “Last In, First Out” structure.

StackType ADT Operations

• StackType -- (Constructor) Creates an empty stack.

• IsEmpty -- Determines whether the stack is currently

empty.

• IsFull -- Determines whether the stack is currently full.

• Push (ItemType newItem) -- Adds newItem to the top of the stack.

• Pop -- Removes the item at the top of the stack.

• Top -- Returns a copy of item currently at top of stack.

5

ADT Stack Operations

Transformers – StackType – Push– Pop

Observers – IsEmpty

– IsFull– Top

change state

observe state

6

7

Vector Implementation of Stack

StackType class

StackType

IsEmpty

Top

Pop

Push

IsFull

Private data:

top

[MAX_ITEMS-1] . . .

[ 2 ]

[ 1 ]

items [ 0 ]

Stack of int items

top 3

[MAX_ITEMS-1] . . .

[ 3 ] 789

[ 2 ] -56

[ 1 ] 132

items [ 0 ] 5670

8

top 3

[MAX_ITEMS-1] . . .

[ 3 ] 3456.8

[ 2 ] -90.98

[ 1 ] 98.6

items [ 0 ] 167.87

9

Stack of float items

top 3

[MAX_ITEMS-1] . . .

[ 3 ] Bradley

[ 2 ] Asad

[ 1 ] Rodrigo

items [ 0 ] Max

10

Stack of string items

11

//----------------------------------------------------------

// SPECIFICATION FILE (bstack.h)

//----------------------------------------------------------

#include "bool.h"

#include "ItemType.h" // for MAX_ITEMS and

// class ItemType definition

class StackType {

public:

StackType( );

// Default constructor.

// POST: Stack is created and empty.

Boolean IsEmpty( ) const;

// PRE: Stack has been initialized.

// POST: FCTVAL == (stack is empty)

Boolean IsFull( ) const;

// PRE: Stack has been initialized.

// POST: FCTVAL == (stack is full)

11

12

// SPECIFICATION FILE continued (bstack.h)

void Push( ItemType newItem );

// PRE: Stack has been initialized and is not full.

// Assigned (newItem).

// POST: newItem is at the top of the stack.

void Pop( );

// PRE: Stack has been initialized and is not empty.

// POST: Top element has been removed from stack.

ItemType Top( ) const;

// PRE: Stack has been initialized and is not empty.

// POST: FCTVAL == copy of item at top of stack.

private:

int top; // subscript of current top item

ItemType items[MAX_ITEMS]; // array of ItemType

};

12

13

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘V’

14

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘V’

Private data:

top -1

[MAX_ITEMS-1] . . .

[ 2 ]

[ 1 ]

items [ 0 ]

15

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘V’

Private data:

top 0

[MAX_ITEMS-1] . . .

[ 2 ]

[ 1 ]

items [ 0 ] ‘V’

16

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘V’

Private data:

top 1

[MAX_ITEMS-1] . . .

[ 2 ]

[ 1 ] ‘C’

items [ 0 ] ‘V’

17

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘V’

Private data:

top 2

[MAX_ITEMS-1] . . .

[ 2 ] ‘S’

[ 1 ] ‘C’

items [ 0 ] ‘V’

18

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘V’

Private data:

top 2

[MAX_ITEMS-1] . . .

[ 2 ] ‘S’

[ 1 ] ‘C’

items [ 0 ] ‘V’

19

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘S’

Private data:

top 2

[MAX_ITEMS-1] . . .

[ 2 ] ‘S’

[ 1 ] ‘C’

items [ 0 ] ‘V’

20

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

Tracing Client Codeletter ‘S’

Private data:

top 1

[MAX_ITEMS-1] . . .

[ 2 ] ‘S’

[ 1 ] ‘C’

items [ 0 ] ‘V’

21

char letter = ‘V’;StackType charStack;

charStack.Push(letter);

charStack.Push(‘C’);

charStack.Push(‘S’);

if ( !charStack.IsEmpty( )) {

letter = charStack.Top( );

charStack.Pop( );}

charStack.Push(‘K’);

letter ‘S’

Private data:

top 2

[MAX_ITEMS-1] . . .

[ 2 ] ‘K’

[ 1 ] ‘C’

items [ 0 ] ‘V’

End of Trace

22

//-------------------------------------------------------// IMPLEMENTATION FILE (bstack.cpp)//------------------------------------------------------// Private data members of class:// int top;// ItemType items[MAX_ITEMS];//-------------------------------------------------------#include “bool.h”#include “ItemType.h”

StackType::StackType( )

//------------------------------------------------// Default Constructor

//------------------------------------------------{

top = -1;}

22

23

// IMPLEMENTATION FILE continued (bstack.cpp)

//----------------------------------------------------------

Boolean StackType::IsEmpty( ) const

//---------------------------------------------------

// PRE: Stack has been initialized.

// POST: FCTVAL == (stack is empty)

//---------------------------------------------------

{

return ( top == -1 );

}

Boolean StackType::IsFull( ) const

//---------------------------------------------------

// PRE: Stack has been initialized.

// POST: FCTVAL == (stack is full)

//---------------------------------------------------

{

return ( top == MAX_ITEMS-1 );

}23

24

// IMPLEMENTATION FILE continued (bstack.cpp)

//----------------------------------------------------------

void StackType::Push ( ItemType newItem )

//---------------------------------------------------

// PRE: Stack has been initialized and is not full.

// POST: newItem is at the top of the stack.

//---------------------------------------------------

{

top++;

items[top] = newItem;

}

25

// IMPLEMENTATION FILE continued (bstack.cpp)

//----------------------------------------------------------

ItemType StackType::Top( ) const

//---------------------------------------------------

// PRE: Stack has been initialized && top >= 0.

// POST: FCTVAL == copy of item at top of stack.

//---------------------------------------------------

{

return items[top];

}

26

// IMPLEMENTATION FILE continued (bstack.cpp)

//----------------------------------------------------------

void StackType::Pop ( )

//---------------------------------------------------

// PRE: Stack has been initialized && top >= 0.

// POST: Top element has been removed from stack.

//---------------------------------------------------

{

top--;

}

27

Another Stack Implementation

• One advantage of an ADT is that the kind of implementation used can be changed.

• The array implementation of the stack has a weakness -- the maximum size of the stack is fixed at compile time.

• Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.

28

class StackType

StackType

Pop

Push

IsFull

IsEmpty Private data:

topPtrcat dog

~StackType

Top

29

Tracing Client Codeletter ‘V’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

30

Tracing Client Codeletter ‘V’

Private data:

topPtr NULL

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

31

Tracing Client Codeletter

‘V’

‘V’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

Private data:

topPtr

32

Tracing Client Codeletter

‘C’ ‘V’

‘V’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

Private data:

topPtr

33

Tracing Client Codeletter

‘S’ ‘C’ ‘V’

‘V’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

Private data:

topPtr

34

Tracing Client Codeletter ‘V’

‘S’ ‘C’ ‘V’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

Private data:

topPtr

35

Tracing Client Codeletter ‘S’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

Private data:

topPtr ‘S’ ‘C’ ‘V’

36

Tracing Client Codeletter

‘C’ ‘V’

‘S’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

Private data:

topPtr

37

Tracing Client Codeletter

‘K’ ‘C’ ‘V’

‘S’

char letter = ‘V’;

StackType< char > myStack;

myStack.Push(letter);

myStack.Push(‘C’);

myStack.Push(‘S’);

if ( !myStack.IsEmpty( ) ) {

letter = myStack.Top( ); myStack.Pop( );}myStack.Push(‘K’);

Private data:

topPtr

38

// SPECIFICATION OF STACK (ustack.h)

#include "bool.h"#include "ItemType.h" // for ItemType

struct NodeType {ItemType info;NodeType* next;

}

Dynamically Linked Stack

. info . next

‘A’ 6000

39

Pointer Dereferencing and Member Selection

. info . next

‘A’ 6000 ptr

ptr

ptr

. info . next

‘A’ 6000

*ptr

ptr

. info . next

(*ptr).info

ptr->info

‘A’ 6000

40

// SPECIFICATION OF STACK continued (ustack.cpp)

class StackType {public:

StackType( ); // constructor// Default constructor.// POST: Stack is created and empty.

Boolean IsEmpty( ) const;// PRE: Stack has been initialized.// POST: FCTVAL == (stack is empty)

Boolean IsFull( ) const;// PRE: Stack has been initialized.// POST: FCTVAL == (stack is full)

StackType(const StackType& otherStk ); // Copy-constructor// POST: Created stack as deep copy of

otherStk

41

// SPECIFICATION OF STACK continued (ustack.cpp)

void Push( ItemType item );

// PRE: Stack initialized && Stack is not full.

// POST: newItem is at the top of the stack.

void Pop( );

// PRE: Stack initialized && Stack is not empty.

// POST: Top element has been removed from stack.

ItemType Top( ) const;

// PRE: Stack initialized && Stack is not empty.

// POST: FCTVAL == copy of item currently at top.~StackType( ); // destructor

// PRE: Stack has been initialized.

// POST: Memory allocated for nodes has been

// deallocated.

private:

NodeType char* topPtr ;

};

41

42

// DYNAMIC LINKED IMPLEMENTATION OF STACK (ustack.cpp)

// member function definitions for class StackType

StackType::StackType( ) // constructor{

topPtr = NULL;}

void StackType::IsEmpty( ) const// Returns true if there are no elements // on the stack; false otherwise

{return ( topPtr == NULL );

}

Using operator new

If memory is available in an area called the free store (or heap), operator new allocates the requested object, and returns a pointer to the memory allocated.

The dynamically allocated object exists until the delete operator destroys it.

43

44

Adding newItem to the stack

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

topPtr ‘X’ ‘C’ ‘L’

‘B’newItem

45

Adding newItem to the stack

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

topPtr ‘X’ ‘C’ ‘L’

‘B’newItem

location

46

Adding newItem to the stack

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

topPtr ‘X’ ‘C’ ‘L’

‘B’newItem

location

47

Adding newItem to the stack

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

topPtr ‘X’ ‘C’ ‘L’

‘B’newItem

location ‘B’

48

Adding newItem to the stack

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

topPtr ‘X’ ‘C’ ‘L’

‘B’newItem

location ‘B’

49

Adding newItem to the stack

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

topPtr ‘X’ ‘C’ ‘L’

‘B’newItem

location ‘B’

50

void StackType::Push ( ItemType newItem )

// Adds newItem to the top of the stack.

{

NodeType* location;

location = new NodeType;

location->info = newItem;

location->next = topPtr;

topPtr = location;

}

Implementing Push

The object currently pointed to by the pointer is deallocated, and the pointer is considered unassigned. The memory is returned to the free store.

Using operator delete

51

52

Deleting item from the stack

NodeType* tempPtr;

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

topPtr

item

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

tempPtr

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’

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’

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’

56

Deleting item from the stack

NodeType* tempPtr;

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

topPtr

item

‘X’ ‘C’ ‘L’

tempPtr

‘B’

57

void StackType::Pop ( )

// Removes element at the top of the stack.

{

NodeType* tempPtr;

tempPtr = topPtr;

topPtr = topPtr->next;

delete tempPtr;

}

void StackType::Top ( ) const

// FCTVAL == copy of element at the top of stack.

{

return topPtr->info;

}

58

Boolean StackType::IsFull( ) const

// Returns true if there is no room for

// another NodeType node on the free store;

// false otherwise.

{

location = new NodeType<ItemType>;

if ( location == NULL )

return true;

else

{

delete location;

return false;

}

}

59

Why is a destructor needed?

When a local stack variable goes out of scope, the memory space for data member topPtr is deallocated. But the nodes that topPtr points to are not automatically deallocated.

A class destructor is used to deallocate the dynamic memory pointed to by the data member.

60

StackType::~StackType( ) // destructor

// Post: Stack is empty; all elements deallocated.

{

NodeType* tempPtr;;

while ( topPtr != NULL )

{

tempPtr = topPtr;

topPtr = topPtr->next;

delete tempPtr;

}

}

61

class StackType

StackType

Pop

Push

IsFull

IsEmpty Private data:

topPtr

~StackType

20 30

Top

62

What happens . . .

• When a function is called that uses pass by value for a

class object like our dynamically linked stack?

StackType

MakeEmpty

Pop

Push

IsFull

IsEmpty Private data:

topPtr

~StackType

20 30

63

void MyFunction( StackType SomeStack ) // FUNCTION CODE

// Uses pass by value{

. .

.

.}

Passing a class object by value

64

Pass by value makes a shallow copy

20 30

StackType MyStack; // CLIENT CODE . . .

MyFunction( MyStack ); // function call

Private data:

topPtr 7000

MyStack SomeStack

shallow copy

Private data: 7000 6000

topPtr 7000

65

Shallow Copy vs. Deep Copy

• A shallow copy copies only the class data members, and does not make a copy of any pointed-to data.

• A deep copy copies not only the class data members, but also makes separately stored copies of any pointed-to data.

66

What’s the difference?

• A shallow copy shares the pointed to data with the original class object.

• A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.

67

Making a deep copy

20 30

Private data: 7000 6000

topPtr 7000SomeStack

20 30

Private data: 5000 2000

topPtr 5000

MyStack

deep copy

68

// FUNCTION CODE

void MyFunction( StackType SomeStack )

// Uses pass by value

{

SomeStack.Pop( ); .

.

.

}

WHAT HAPPENS IN THE SHALLOW COPY SCENARIO?

Suppose MyFunction Uses Pop

69

MyStack.topPtr is left dangling

StackType<int> MyStack; // CLIENT CODE . . .

MyFunction( MyStack );

Private data:

topPtr 6000

MyStack SomeStack

shallow copy

? 30

Private data: 7000 6000

topPtr 7000

70

MyStack.topPtr is left dangling

? 30

Private data:

topPtr 6000

MyStack SomeStack

shallow copy

Private data: 7000 6000

topPtr 7000

71

As a result . . .

• This default method used for pass by value is not the best way when a data member pointer points to dynamic data.

• Instead, you should write what is called a copy constructor, which makes a deep copy of the dynamic data in a different memory location.

72

More about copy constructors

• When there is a copy constructor provided for a class, the copy constructor is used to make copies for pass by value.

• You do not call the copy constructor.

• Like other constructors, it has no return type.

• Because the copy constructor properly defines pass by value for your class, it must use pass by reference in its definition.

73

Copy Constructor

• Copy constructor is a special member function of a class that is implicitly called in these 3 situations:

– passing object parameters by value,

– initializing an object variable in its declaration,

– returning an object as the return value of a function.

74

// DYNAMIC LINKED IMPLEMENTATION OF STACK

class StackType {public:

StackType( ); // Default constructor.// POST: Stack is created and empty.

StackType( const StackType& otherStk );// Copy constructor.// POST: this Stack is a deep copy of otherStk// Is implicitly called for pass by value.

.

.

.

~StackType( ); // Destructor.// POST: Memory for nodes has been deallocated.

private:NodeType* topPtr ;

};

74

75

CONSTRUCTOR

COPY CONSTRUCTOR

DESTRUCTOR

Classes with Data Member Pointers Need

76

// COPY CONSTRUCTORStackType::StackType( const StackType& otherStk ){ NodeType* fromPtr ; // traverses otherStk nodes

NodeType* toPtr ; // traverses this Stack nodesif (otherStk.topPtr == NULL )

topPtr = NULL ;else // allocate memory to copy first node{ topPtr = new NodeType;

topPtr->info = otherStk.topPtr->info ;fromPtr = otherStk.topPtr->next ;toPtr = topPtr ;while ( fromPtr != NULL ) // deep copy other

nodes{ toPtr->next = new NodeType;

toPtr = toPtr->next ;toPtr->info = fromPtr->info ;fromPtr = fromPtr->next ;

}

toPtr->next = NULL ;}

}

76

77

What about the assignment operator?

• The default method used for assignment of class objects makes a shallow copy.

• If your class has a data member pointer to dynamic data, you should write a member function to overload the assignment operator to make a deep copy of the dynamic data.

78

// DYNAMIC LINKED IMPLEMENTATION OF STACK

class StackType {public:

StackType( ); // Default constructor.

StackType( const StackType& otherStk );// Copy constructor.

void operator= ( StackType otherStk);// Overloads assignment operator.

.

.

.

~StackType( ); // Destructor.

private:NodeType* topPtr ;

};78

79

// DYNAMIC LINKED IMPLEMENTATION OF STACK

void StackType::operator= ( StackType otherStk )

// Overloads assignment operator// to create a deep copy of otherStk.

{

.

.

.

}

Overloading the assignment operator

80

C++ Operator Overloading Guides

1 All operators except :: . sizeof ?: may be overloaded.

2 At least one operand must be a class instance.

3 You cannot change precedence, operator symbols, or number of operands.

4 Overloading ++ and -- requires prefix form use by default, unless special mechanism is used.

5 To overload these operators = ( ) [ ] member functions (not friend functions) must be used.

6 An operator can be given multiple meanings if the data types of operands differ for different meanings.

Using overloaded binary operator=

After defining member function operator=

myStack = yourStack;

Is translated by compiler into function call

myStack.operator=(yourStack);

81

Using overloaded binary operator+

When a Member Function was defined

myStack + yourStack

myStack.operator+(yourStack)

When a Friend Function was defined

myStack + yourStack

operator+(myStack, yourStack)

82

83

What is ADT Queue?

• A queue is a linear data structure with homogeneous components (elements), in which all insertions occur at the rear, and all deletions occur at the front.

• A queue is a FIFO “First In, First Out” structure.

Queue ADT Operations

• QueueType -- (Constructor) Creates an empty queue.

• IsEmpty -- Determines whether queue is currently empty.

• IsFull -- Determines whether queue is currently full.

• Enqueue (ItemType newItem) -- Adds newItem to the rear of queue.

• Dequeue -- Removes the item at the front of queue.

• Front -- Returns a copy of item at the front of queue.

84

ADT Queue Operations

Transformers – QueueType– Enqueue– Dequeue

Observers – IsEmpty

– IsFull– Front

change state

observe state

85

86

class QueueType

QueueType

~QueueType

Enqueue

Dequeue . . .

Private Data:

front

rear

‘C’ ‘Z’ ‘T’

87

// SPECIFICATION OF QUEUE (uqueue.cpp)

#include "ItemType.h" // for ItemType

class QueueType {

public:

QueueType( ); // CONSTRUCTOR

~QueueType( ) ; // DESTRUCTORBoolean IsEmpty( ) const;

Boolean IsFull( ) const;

void Enqueue( ItemType item );

void Dequeue( );

ItemType Front( ) const;

QueueType( const QueueType& otherQue );

// COPY-CONSTRUCTOR

private:

NodeType* front;

NodeType* rear;

};87

88

// DYNAMIC LINKED IMPLEMENTATION OF QUEUE (uqueue.cpp)

// member function definitions for class QueueType

QueueType::QueueType( ) // CONSTRUCTOR{

front = NULL;rear = NULL;

}

Boolean QueueType::IsEmpty( ) const{

return ( front == NULL )}

89

void QueueType::Enqueue( ItemType newItem )

// Adds newItem to the rear of the queue.

// Pre: Queue has been initialized.

// Queue is not full.

// Post: newItem is at rear of queue.

{

NodeType* ptr;

ptr = new NodeType;

ptr->info = newItem;

ptr->next = NULL;

if ( rear == NULL )

front = ptr;

else

rear->next = ptr;

rear = ptr;

}

90

void QueueType::Dequeue( )

// Removes element from from front of queue

// Pre: Queue has been initialized.

// Queue is not empty.

// Post: Front element has been removed from queue.

{

NodeType* tempPtr;

tempPtr = front;

front = front->next;

if ( front == NULL )

rear = NULL;

delete tempPtr;

}

Recommended