Download presentation
Presentation is loading. Please wait.
Published byArabella Martina Atkinson Modified over 9 years ago
1
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles
2
2 Software Life Cycle Activities l Problem analysisunderstand the problem l Requirements definition specify what program will do l High- and low-level designhow it meets requirements l Implementation of designcode it l Testing and verificationdetect errors, show correct l Deliveryturn over to customer l Operationuse the program l Maintenancechange the program
3
3 Software Engineering l A disciplined approach to the design, production, and maintenance of computer programs l that are developed on time and within cost estimates, l using tools that help to manage the size and complexity of the resulting software products.
4
4 An Algorithm Is... l A logical sequence of discrete steps that describes a complete solution to a given problem computable in a finite amount of time.
5
5 Goals of Quality Software l It works. l It can be read and understood. l It can be modified. l It is completed on time and within budget.
6
6 Detailed Program Specification l Tells what the program must do, but not how it does it. l Is written documentation about the program.
7
7 Detailed Program Specification Includes l Inputs l Outputs l Processing requirements l Assumptions
8
8 Preconditions and Postconditions l The precondition is an assertion describing what a function requires to be true before beginning execution. l The postcondition describes what must be true at the moment the function finishes execution. l The caller is responsible for ensuring the precondition, and the function code must ensure the postcondition.
9
9 Object-Oriented Design A 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 cincout
10
10 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation
11
11 Abstract Data Type (ADT) l A data type whose properties (domain and operations) are specified independently of any particular implementation.
12
12 l Application (or user) level: modeling real-life data in a specific context. l Logical (or ADT) level: abstract view of the domain and operations. WHAT l Implementation level: specific representation of the structure to hold the data items, and the coding for operations. HOW Data from 3 different levels
13
13 4 Basic Kinds of ADT Operations l Constructor -- creates a new instance (object) of an ADT. l Transformer -- changes the state of one or more of the data values of an instance. l Observer -- allows us to observe the state of one or more of the data values without changing them. l Iterator -- allows us to process all the components in a data structure sequentially. 13
14
14 C++ Built-In Data Types Composite array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double
15
15 Pass-by-value CALLING BLOCK FUNCTION CALLED sends a copy of the contents of the actual parameter SO, the actual parameter cannot be changed by the function. 15
16
16 Pass-by-reference sends the location (memory address) of the actual parameter can change value of actual parameter CALLING BLOCK FUNCTION CALLED 16
17
17 Nell Dale Chapter 3 ADTs Unsorted List and Sorted List C++ Plus Data Structures
18
18 What is a List? l A list is a homogeneous collection of elements, with a linear relationship between elements. l That is, each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor.
19
19 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).
20
20 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.
21
21 Class Interface Diagram UnsortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem GetNextItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos
22
22 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
23
23 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
24
24 InsertItem algorithm for SortedList ADT l Find proper location for the new element in the sorted list. l Create space for the new element by moving down all the list elements that will follow it. l Put the new element in the list. l Increment length.
25
25 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. {. }
26
void SortedType :: InsertItem ( ItemType item ) { bool moreToSearch ; int location = 0 ; // find proper location for new element moreToSearch = ( 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++ ; }
27
27 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?
28
28 Binary Seach in a Sorted List l 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. l Repeat the process in the half of the list that should be examined next. l Stop when item is found, or when there is nowhere else to look and item has not been found.
29
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; intlast = 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 }
30
30 Order of Magnitude of a Function The order of magnitude, or Big-O notation, of a function expresses the computing time of a problem as the term in a function that increases most rapidly relative to the size of a problem. 30
31
31 Names of Orders of Magnitude O(1) bounded (by a constant) time O(log 2 N) logarithmic time O(N) linear time O(N*log 2 N) N*log 2 N time O(N 2 ) quadratic time O( 2 N ) exponential time 31
32
32 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue
33
33 What is a Stack? l 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. l A stack is a LIFO “last in, first out” structure.
34
34 Class Interface Diagram StackType class StackType MakeEmpty Pop Push IsFull IsEmpty Private data: top [MAX_ITEMS-1]. [ 2 ] [ 1 ] items [ 0 ]
35
35 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]
36
36 What is a Queue? l 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). l A queue is a FIFO “first in, first out” structure.
37
37 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
38
38 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures
39
39 Another Stack Implementation l One advantage of an ADT is that the kind of implementation used can be changed. l The dynamic array implementation of the stack has a weakness -- the maximum size of the stack is passed to the constructor as parameter. l Instead we can dynamically allocate the space for each stack element as it is pushed onto the stack.
40
40 class StackType StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘C’ ‘V’
41
41 Push( ) l Allocate space for new item l Put new item into the allocated space l Put the allocated space into the stack
42
42 Push( ) l Allocate space for new item l Put new item into the allocated space l Put the allocated space into the stack
43
43 template 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; } 43 Implementing Push
44
44 Pop( ) l Set item to Info (top node) l Unlink the top node from the stack l Deallocate the old top node
45
45 template void StackType ::Pop ( ItemType& item ) // Removes element at the top of the stack and // returns it in item. { NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; } 45 Implementing Pop
46
46 class QueType QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’‘Z’ ‘T’
47
47 class UnsortedType MakeEmpty ~UnsortedType DeleteItem. InsertItem UnsortedType RetrieveItem GetNextItem ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos
48
48 class SortedType MakeEmpty ~SortedType DeleteItem. InsertItem SortedType RetrieveItem GetNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos
49
49 Nell Dale Chapter 6 Lists Plus C++ Plus Data Structures
50
50 What is a Circular Linked List? l 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
51
51 ‘A’ ‘C’ ‘F’ ‘T’ ‘Z’ What is a Doubly Linked List? l A doubly linked list is a list in which each node is linked to both its successor and its predecessor. listData
52
52 What are Header and Trailer Nodes? l A Header Node is a node at the beginning of a list that contains a key value smaller than any possible key. l A Trailer Node is a node at the end of a list that contains a key larger than any possible key. l Both header and trailer are placeholding nodes used to simplify list processing. listData INT_MIN 5 8 13 INT_MAX
53
53 Copy Structures
54
54 Pass by value makes a shallow copy 20 30 StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); // function call Private data: 7000 6000 topPtr 7000 Private data: topPtr 7000 MyStack SomeStack shallow copy
55
55 Shallow Copy vs. Deep Copy l A shallow copy copies only the class data members, and does not copy any pointed-to data. l A deep copy copies not only the class data members, but also makes separately stored copies of any pointed-to data.
56
56 What’s the difference? l A shallow copy shares the pointed to data with the original class object. l A deep copy stores its own copy of the pointed to data at different locations than the data in the original class object.
57
57 Making a deep copy 20 30 Private data: 7000 6000 topPtr 7000 SomeStack 20 30 Private data: 5000 2000 topPtr 5000 MyStack deep copy
58
58 Object-Oriented Design: l Composition l Inheritance l …
59
59 // DERIVED CLASS CountedQue FROM BASE CLASS QueType template class CountedQue : public QueType { 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 BE INVOKED FOR OBJECTS OF TYPE CountedQue
60
60 class CountedQue QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’‘Z’ ‘T’ CountedQue LengthIs Enqueue Dequeue. Private Data: length 3
61
61 End
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.