1 Data Structures and Algorithms Stacks and Queues.

90
1 Data Structures and Algorithms Stacks and Queues

Transcript of 1 Data Structures and Algorithms Stacks and Queues.

Page 1: 1 Data Structures and Algorithms Stacks and Queues.

1

Data Structures and Algorithms

Stacks and Queues

Page 2: 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

Page 3: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 4: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 5: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 6: 1 Data Structures and Algorithms Stacks and Queues.

ADT Stack Operations

Transformers – StackType – Push– Pop

Observers – IsEmpty

– IsFull– Top

change state

observe state

6

Page 7: 1 Data Structures and Algorithms Stacks and Queues.

7

Vector Implementation of Stack

StackType class

StackType

IsEmpty

Top

Pop

Push

IsFull

Private data:

top

[MAX_ITEMS-1] . . .

[ 2 ]

[ 1 ]

items [ 0 ]

Page 8: 1 Data Structures and Algorithms Stacks and Queues.

Stack of int items

top 3

[MAX_ITEMS-1] . . .

[ 3 ] 789

[ 2 ] -56

[ 1 ] 132

items [ 0 ] 5670

8

Page 9: 1 Data Structures and Algorithms Stacks and Queues.

top 3

[MAX_ITEMS-1] . . .

[ 3 ] 3456.8

[ 2 ] -90.98

[ 1 ] 98.6

items [ 0 ] 167.87

9

Stack of float items

Page 10: 1 Data Structures and Algorithms Stacks and Queues.

top 3

[MAX_ITEMS-1] . . .

[ 3 ] Bradley

[ 2 ] Asad

[ 1 ] Rodrigo

items [ 0 ] Max

10

Stack of string items

Page 11: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 12: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 13: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 14: 1 Data Structures and Algorithms Stacks and Queues.

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 ]

Page 15: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 16: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 17: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 18: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 19: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 20: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 21: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 22: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 23: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 24: 1 Data Structures and Algorithms Stacks and Queues.

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;

}

Page 25: 1 Data Structures and Algorithms Stacks and Queues.

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];

}

Page 26: 1 Data Structures and Algorithms Stacks and Queues.

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--;

}

Page 27: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 28: 1 Data Structures and Algorithms Stacks and Queues.

28

class StackType

StackType

Pop

Push

IsFull

IsEmpty Private data:

topPtrcat dog

~StackType

Top

Page 29: 1 Data Structures and Algorithms Stacks and Queues.

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’);

Page 30: 1 Data Structures and Algorithms Stacks and Queues.

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’);

Page 31: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 32: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 33: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 34: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 35: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 36: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 37: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 38: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 39: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 40: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 41: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 42: 1 Data Structures and Algorithms Stacks and Queues.

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 );

}

Page 43: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 44: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 45: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 46: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 47: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 48: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 49: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 50: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 51: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 52: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 53: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 54: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 55: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 56: 1 Data Structures and Algorithms Stacks and Queues.

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’

Page 57: 1 Data Structures and Algorithms Stacks and Queues.

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;

}

Page 58: 1 Data Structures and Algorithms Stacks and Queues.

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;

}

}

Page 59: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 60: 1 Data Structures and Algorithms Stacks and Queues.

60

StackType::~StackType( ) // destructor

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

{

NodeType* tempPtr;;

while ( topPtr != NULL )

{

tempPtr = topPtr;

topPtr = topPtr->next;

delete tempPtr;

}

}

Page 61: 1 Data Structures and Algorithms Stacks and Queues.

61

class StackType

StackType

Pop

Push

IsFull

IsEmpty Private data:

topPtr

~StackType

20 30

Top

Page 62: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 63: 1 Data Structures and Algorithms Stacks and Queues.

63

void MyFunction( StackType SomeStack ) // FUNCTION CODE

// Uses pass by value{

. .

.

.}

Passing a class object by value

Page 64: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 65: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 66: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 67: 1 Data Structures and Algorithms Stacks and Queues.

67

Making a deep copy

20 30

Private data: 7000 6000

topPtr 7000SomeStack

20 30

Private data: 5000 2000

topPtr 5000

MyStack

deep copy

Page 68: 1 Data Structures and Algorithms Stacks and Queues.

68

// FUNCTION CODE

void MyFunction( StackType SomeStack )

// Uses pass by value

{

SomeStack.Pop( ); .

.

.

}

WHAT HAPPENS IN THE SHALLOW COPY SCENARIO?

Suppose MyFunction Uses Pop

Page 69: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 70: 1 Data Structures and Algorithms Stacks and Queues.

70

MyStack.topPtr is left dangling

? 30

Private data:

topPtr 6000

MyStack SomeStack

shallow copy

Private data: 7000 6000

topPtr 7000

Page 71: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 72: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 73: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 74: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 75: 1 Data Structures and Algorithms Stacks and Queues.

75

CONSTRUCTOR

COPY CONSTRUCTOR

DESTRUCTOR

Classes with Data Member Pointers Need

Page 76: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 77: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 78: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 79: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 80: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 81: 1 Data Structures and Algorithms Stacks and Queues.

Using overloaded binary operator=

After defining member function operator=

myStack = yourStack;

Is translated by compiler into function call

myStack.operator=(yourStack);

81

Page 82: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 83: 1 Data Structures and Algorithms Stacks and Queues.

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.

Page 84: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 85: 1 Data Structures and Algorithms Stacks and Queues.

ADT Queue Operations

Transformers – QueueType– Enqueue– Dequeue

Observers – IsEmpty

– IsFull– Front

change state

observe state

85

Page 86: 1 Data Structures and Algorithms Stacks and Queues.

86

class QueueType

QueueType

~QueueType

Enqueue

Dequeue . . .

Private Data:

front

rear

‘C’ ‘Z’ ‘T’

Page 87: 1 Data Structures and Algorithms Stacks and Queues.

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

Page 88: 1 Data Structures and Algorithms Stacks and Queues.

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 )}

Page 89: 1 Data Structures and Algorithms Stacks and Queues.

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;

}

Page 90: 1 Data Structures and Algorithms Stacks and Queues.

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;

}