1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

61
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles

Transcript of 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

Page 1: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1

C++ Plus Data Structures

Nell Dale

Chapter 1

Software Engineering Principles

Page 2: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2

Software Life Cycle Activities

Problem analysis understand the problem

Requirements definition specify what program will do

High- and low-level design how it meets requirements

Implementation of design code it

Testing and verification detect errors, show correct

Delivery turn over to customer

Operation use the program

Maintenance change the program

Page 3: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3

Software Engineering

A disciplined approach to the design, production, and maintenance of computer programs

that are developed on time and within cost estimates,

using tools that help to manage the size and complexity of the resulting software products.

Page 4: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4

An Algorithm Is . . .

A logical sequence of discrete steps that describes a complete solution to a given problem computable in a finite amount of time.

Page 5: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5

Goals of Quality Software

It works. It can be read and understood.

It can be modified.

It is completed on time and within budget.

Page 6: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

6

Detailed Program Specification

Tells what the program must do, but not how it does it.

Is written documentation about the program.

Page 7: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

7

Detailed Program Specification Includes

Inputs

Outputs

Processing requirements

Assumptions

Page 8: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

8

Preconditions and Postconditions

The precondition is an assertion describing what a function requires to be true before beginning execution.

The postcondition describes what must be true at the moment the function finishes execution.

The caller is responsible for ensuring the precondition, and the function code must ensure the postcondition.

Page 9: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

9

Object-Oriented DesignA technique for developing a program in which

the solution is expressed in terms of objects -- self- contained entities composed of data and operations on that data.

Private data

<<

setf...

Private data

>>

get...

ignore

cin cout

Page 10: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1010

C++ Plus Data Structures

Nell Dale

Chapter 2

Data Design and Implementation

Page 11: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1111

Abstract Data Type (ADT)

A data type whose properties (domain and operations) are specified independently of any particular implementation.

Page 12: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1212

Application (or user) level: modeling real-life data in a specific context.

Logical (or ADT) level: abstract view of the domain and operations. WHAT

Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW

Data from 3 different levels

Page 13: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

13

4 Basic Kinds of ADT Operations

Constructor -- creates a new instance (object) of an ADT.

Transformer -- changes the state of one or more of the data values of an instance.

Observer -- allows us to observe the state of one or more of the data values without changing them.

Iterator -- allows us to process all the components in a data structure sequentially.

13

Page 14: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1414

C++ Built-In Data TypesC++ Built-In Data Types

Composite

array struct union class

Address

pointer reference

Simple

Integral Floating

char short int long enum

float double long double

Page 15: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

15

Pass-by-value

CALLINGBLOCK

FUNCTION CALLED

sends a copy of the contents of the actual parameter

SO, the actual parameter cannot be changed by the function.

15

Page 16: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

16

Pass-by-reference

sends the location (memory address)of the actual parameter

can change value ofactual parameter

CALLINGBLOCK FUNCTION

CALLED

16

Page 17: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1717

Nell Dale

Chapter 3

ADTs Unsorted List and Sorted List

C++ Plus Data Structures

Page 18: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1818

What is a List?

A list is a homogeneous collection of elements, with a linear relationship between elements.

That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.

Page 19: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

1919

Sorted and Unsorted Lists

UNSORTED LIST

Elements are placed into the list in no particular order.

SORTED LIST

List elements are in an order that is sorted in some way -- either numerically or alphabetically by the elements themselves, or by a component of the element (called a KEY member) .

Page 20: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2020

What is a Generic Data Type?

A generic data type is a type for which the operations are defined but the types of the items being manipulated are not defined.

One way to simulate such a type for our UnsortedList ADT is via a user-defined class ItemType with member function ComparedTo having enumerated type value LESS, GREATER, or EQUAL.

Page 21: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2121

Class Interface Diagram

UnsortedType class

IsFull

LengthIs

ResetList

DeleteItem

InsertItem

MakeEmpty

RetrieveItem

GetNextItem

Private data:

length

info [ 0 ] [ 1 ] [ 2 ]

[MAX_ITEMS-1]

currentPos

Page 22: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2222

SortedType Class Interface Diagram

SortedType class

IsFull

LengthIs

ResetList

DeleteItem

InsertItem

MakeEmpty

RetrieveItem

Private data:

length

info [ 0 ] [ 1 ] [ 2 ]

[MAX_ITEMS-1]

currentPos

GetNextItem

Page 23: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2323

Member functions

Which member function specifications and implementations must change to ensure that any instance of the Sorted List ADT remains sorted at all times?

» InsertItem

» DeleteItem

Page 24: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2424

InsertItem algorithm for SortedList ADT

Find proper location for the new element in the sorted list.

Create space for the new element by moving down all the list elements that will follow it.

Put the new element in the list.

Increment length.

Page 25: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2525

Implementing SortedType

member function InsertItem

// IMPLEMENTATION FILE (sorted.cpp)

#include “itemtype.h” // also must appear in client code

void SortedType :: InsertItem ( ItemType item )

// Pre: List has been initialized. List is not full. item is not in list.

// List is sorted by key member using function ComparedTo.

// Post: item is in the list. List is still sorted.{

.

.

.

}

Page 26: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

void SortedType :: InsertItem ( ItemType item ){ bool moreToSearch ; int location = 0 ;

// find proper location for new elementmoreToSearch = ( location < length ) ;while ( moreToSearch ){ switch ( item.ComparedTo( info[location] ) )

{ case LESS : moreToSearch = false ; break ;

case GREATER : location++ ; moreToSearch = ( location < length ) ; break ;

} } // make room for new element in sorted list

for ( int index = length ; index > location ; index-- )info [ index ] = info [ index - 1 ] ;

info [ location ] = item ; length++ ;}

Page 27: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2727

Improving member function RetrieveItem

Recall that with the Unsorted List ADT

we examined each list element beginning

with info[ 0 ], until we either found a

matching key, or we had examined all

the elements in the Unsorted List.

How can the searching algorithm be improved for Sorted List ADT?

Page 28: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

2828

Binary Seach in a Sorted List

Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array.

Repeat the process in the half of the list that should be examined next.

Stop when item is found, or when there is nowhere else to look and item has not been found.

Page 29: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

void SortedType::RetrieveItem ( ItemType& item, bool& found ) // Pre: Key member of item is initialized.// Post: If found, item’s key matches an element’s key in the list and a

copy // of that element has been stored in item; otherwise, item is unchanged.{ int midPoint ;

int first = 0; int last = length - 1 ;

bool moreToSearch = ( first <= last ) ;

found = false ;while ( moreToSearch && !found ){ midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT

switch ( item.ComparedTo( info [ midPoint ] ) ) {

case LESS : . . . // LOOK IN FIRST HALF NEXT case GREATER : . . . // LOOK IN SECOND HALF NEXT case EQUAL : . . . // ITEM HAS BEEN FOUND

} }}

Page 30: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

30

Order of Magnitude of a Function

The order of magnitude, or Big-O notation, of a function expresses the computing timeof a problem as the term in a function that increases most rapidly relative to the sizeof a problem.

30

Page 31: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

31

Names of Orders of Magnitude

O(1) bounded (by a constant) time

O(log2N) logarithmic time

O(N) linear time

O(N*log2N) N*log2N time

O(N2) quadratic time

O( 2N ) exponential time 31

Page 32: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3232

C++ Plus Data Structures

Nell Dale

Chapter 4

ADTs Stack and Queue

Page 33: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3333

What is a Stack?

Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of stack items can take place only at the top of the stack.

A stack is a LIFO “last in, first out” structure.

Page 34: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3434

Class Interface Diagram

StackType class

StackType

MakeEmpty

Pop

Push

IsFull

IsEmpty

Private data:

top

[MAX_ITEMS-1] . . .

[ 2 ]

[ 1 ]

items [ 0 ]

Page 35: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3535

DYNAMIC ARRAY IMPLEMENTATION

StackType

~StackType

Push

Pop . . .

class StackType

Private Data:

top 2

maxStack 5

items

50

43

80

items [0]

items [1]

items [2]

items [3]

items [4]

Page 36: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3636

What is a Queue?

Logical (or ADT) level: A queue is an ordered 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.

Page 37: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3737

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]

RE

SE

RV

ED

Page 38: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3838

C++ Plus Data Structures

Nell Dale

Chapter 5

Linked Structures

Page 39: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

3939

Another Stack Implementation

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

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.

Page 40: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4040

class StackType<char>

StackType

MakeEmpty

Pop

Push

IsFull

IsEmpty Private data:

topPtr

~StackType

‘C’ ‘V’

Page 41: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4141

Push( )

Allocate space for new item

Put new item into the allocated space

Put the allocated space into the stack

Page 42: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4242

Push( )

Allocate space for new item

Put new item into the allocated space

Put the allocated space into the stack

Page 43: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4343

template<class ItemType>

void StackType<ItemType>::Push ( ItemType newItem )

// Adds newItem to the top of the stack.

{

NodeType<ItemType>* location;

location = new NodeType<ItemType>;

location->info = newItem;

location->next = topPtr;

topPtr = location;

}

43

Implementing Push

Page 44: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4444

Pop( )

Set item to Info (top node)

Unlink the top node from the stack

Deallocate the old top node

Page 45: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4545

template<class ItemType>void StackType<ItemType>::Pop ( ItemType& item )

// Removes element at the top of the stack and// returns it in item.

{NodeType<ItemType>* tempPtr;

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

}

45

Implementing Pop

Page 46: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4646

class QueType<char>

QueType

~QueType

Enqueue

Dequeue . . .

Private Data:

qFront

qRear

‘C’ ‘Z’ ‘T’

Page 47: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4747

class UnsortedType<char>

MakeEmpty

~UnsortedType

DeleteItem . . .

InsertItem

UnsortedType

RetrieveItem

GetNextItem

‘X’ ‘C’ ‘L’

Private data:

length 3

listData

currentPos

Page 48: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4848

class SortedType<char>

MakeEmpty

~SortedType

DeleteItem . . .

InsertItem

SortedType

RetrieveItem

GetNextItem

‘C’ ‘L’ ‘X’

Private data:

length 3

listData

currentPos

Page 49: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

4949

Nell Dale

Chapter 6

Lists Plus

C++ Plus Data Structures

Page 50: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5050

What is a Circular Linked List?

A circular linked list is a list in which every node has a successor; the “last” element is succeeded by the “first” element.

‘B’ ‘C’ ‘L’ ‘T’ ‘V’ ‘Y’listData

Page 51: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5151

‘A’ ‘C’ ‘F’ ‘T’ ‘Z’

What is a Doubly Linked List?

A doubly linked list is a list in which each node is linked to both its successor and its predecessor.

listData

Page 52: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5252

What are Header and Trailer Nodes?

A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key.

A Trailer Node is a node at the end of a list that contains a key larger than any possible key.

Both header and trailer are placeholding nodes used to simplify list processing.

listData INT_MIN 5 8 13 INT_MAX

Page 53: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5353

Copy Structures

Page 54: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5454

Pass by value makes a

shallow copy

20 30

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

MyFunction( MyStack ); // function call

Private data: 7000 6000

topPtr 7000

Private data:

topPtr 7000

MyStack SomeStack

shallow copy

Page 55: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5555

Shallow Copy vs. Deep Copy

A shallow copy copies only the class data members, and does not copy 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 56: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5656

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 57: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5757

Making a deep copy

20 30

Private data: 7000 6000

topPtr 7000SomeStack

20 30

Private data: 5000 2000

topPtr 5000

MyStack

deep copy

Page 58: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

5858

Object-Oriented Design:

Composition

Inheritance

Page 59: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

59

// DERIVED CLASS CountedQue FROM BASE CLASS QueType

template<class ItemType>

class CountedQue : public QueType<ItemType>

{

public:

CountedQue( );

void Enqueue( ItemType newItem );

void Dequeue( ItemType& item );

int LengthIs( ) const;

// Returns number of items on the counted queue.

private:

int length;

};

SAYS ALL PUBLIC MEMBERS OF QueType CAN BEINVOKED FOR OBJECTS OF TYPE CountedQue

Page 60: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

6060

class CountedQue<char>

QueType

~QueType

Enqueue

Dequeue . . .

Private Data:

qFront

qRear

‘C’ ‘Z’ ‘T’

CountedQue

LengthIs

Enqueue

Dequeue . . .

Private Data:

length 3

Page 61: 1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles.

6161

End