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

Slides:



Advertisements
Similar presentations
Sorted Lists CS Data Structures Sections 4.1, 4.2 & 4.3.
Advertisements

Stack and Queues using Linked Structures Kruse and Ryba Ch 4.
Computer Science and Software Engineering University of Wisconsin - Platteville 5. LinkedList Yan Shi CS/SE 2630 Lecture Notes.
Lists + CS3240, L. Grewe 1. 2 Goals Use the C++ template mechanism fr defining generic data types Implement a circular linked list Implement a linked.
Chapter 3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element except the.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
What is a Queue? A queue is a FIFO “first in, first out” structure.
Chapter 5 ADTs Stack and Queue. 2 Goals Describe a stack and its operations at a logical level Demonstrate the effect of stack operations using a particular.
1 A full binary tree A full binary tree is a binary tree in which all the leaves are on the same level and every non leaf node has two children. SHAPE.
Unsorted Lists CS 308 – Data Structures. What is a list? A list is a homogeneous collection of elements. Linear relationship between elements: (1) Each.
CS Data Structures Chapter 8 Lists Mehmet H Gunes
Chapter 4 ADT Sorted List.
1 Chapter 4 Stack and Queue ADT. 2 Stacks of Coins and Bills.
1 expanded by J. Goetz Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 Nell Dale Chapter 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
1 C++ Plus Data Structures Nell Dale Queues ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified.
Implementing a Stack as a Linked Structure CS 308 – Data Structures.
1 Chapter 6 Object-Oriented Software Development.
5 Linked Structures. 2 Definition of Stack Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and.
1 Chapter 6 Lists Plus. ADT Sorted List Operations Transformers n MakeEmpty n InsertItem n DeleteItem Observers n IsFull n LengthIs n RetrieveItem Iterators.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Doubly Linked Lists CS 308 – Data Structures. Node data info: the user's data next, back: the address of the next and previous node in the list.back.next.info.
1 Fall Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List.
Data Structures Using C++ 2E
Chapter 4 ADT Sorted List.
1 Nell Dale Chapter 6 Lists Plus Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus C++ Plus Data Structures.
Chapter 3 ADT Unsorted List. Lecture 7 List Definitions Linear relationship Each element except the first has a unique predecessor, and Each element.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus.
Chapter 4 ADT Sorted List. 2 Goals Describe the Abstract Data Type Sorted List from three perspectives Implement the following Sorted List operations.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
1 C++ Plus Data Structures Nell Dale Chapter 1 Software Engineering Principles Modified from the Slides made by Sylvia Sorkin, Community College of Baltimore.
Chapter 9 (modified) Abstract Data Types and Algorithms Nell Dale John Lewis.
1 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue Modified from the slides by Sylvia Sorkin, Community College of Baltimore County -
Programming Life Cycle Problem analysisunderstand the problem Requirements definition specify what program will do High- and low-level designhow it meets.
3 ADT Unsorted List. List Definitions Linear relationship Each element except the first has a unique predecessor, and each element except the last has.
1 Recall Definition of Stack l Logical (or ADT) level: A stack is an ordered group of homogeneous items (elements), in which the removal and addition of.
Queues CS 302 – Data Structures Sections 5.3 and 5.4.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
Stacks CS 302 – Data Structures Sections 5.1, 5.2, 6.1, 6.5.
Chapter 5 ADTs Stack and Queue. Stacks of Coins and Bills.
1 Chapter 13-1 Applied Arrays: Lists and Strings Dale/Weems.
What happens... l When a function is called that uses pass by value for a class object like our dynamically linked stack? StackType MakeEmpty Pop Push.
Chapter 6 Lists Plus. What is a Class Template? A class template allows the compiler to generate multiple versions of a class type by using type parameters.
1 Data Structures and Algorithms Stacks and Queues.
1 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures Modified from the slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Chapter 4 ADT Sorted List. Sorted Type Class Interface Diagram SortedType class IsFull GetLength ResetList DeleteItem PutItem MakeEmpty GetItem Private.
What is a List? A list is a homogeneous collection of elements, with a linear relationship between elements. Each list element (except the first) has a.
Chapter 6 Lists Plus Lecture 12. What is a Circular Linked List? A circular linked list is a list in which every node has a successor; the “last” element.
1 Chapter 13-2 Applied Arrays: Lists and Strings Dale/Weems.
CS 302 – Data Structures Sections 3.1, 3.2, 3.4 & 3.5
1 Nell Dale Lecture 3 ADTs Unsorted List and Sorted List Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by Reneta.
CSI 1340 Introduction to Computer Science II Chapter 3 ADTs Unsorted List and Sorted List.
1 Chapter 4 ADT Sorted List. 2 Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem.
CSI 1340 Introduction to Computer Science II Chapter 6 Lists Plus.
Chapter 5 (Part 1) ADT Stack 1 Fall Stacks TOP OF THE STACK.
1 C++ Plus Data Structures Abstract Data Types Stack and Queue Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus Modified by.
C++ Plus Data Structures ADTs Unsorted List and Sorted List
TA: Nouf Al-Harbi Data Structures LAB 3 TA: Nouf Al-Harbi
C++ Plus Data Structures
Chapter 5 ADTs Stack and Queue.
C++ Plus Data Structures
Unsorted Lists CS3240, L. Grewe.
Chapter 5 ADTs Stack and Queue Fall 2013 Yanjun Li CS2200.
Chapter 16 Linked Structures
C++ Plus Data Structures
CSI 1340 Introduction to Computer Science II
Yan Shi CS/SE 2630 Lecture Notes
Data Structures and Algorithms Memory allocation and Dynamic Array
TA: Nouf Al-Harbi Data Structures LAB 2 TA: Nouf Al-Harbi
Presentation transcript:

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

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 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 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 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 Detailed Program Specification l Tells what the program must do, but not how it does it. l Is written documentation about the program.

7 Detailed Program Specification Includes l Inputs l Outputs l Processing requirements l Assumptions

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 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 C++ Plus Data Structures Nell Dale Chapter 2 Data Design and Implementation

11 Abstract Data Type (ADT) l A data type whose properties (domain and operations) are specified independently of any particular implementation.

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 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 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 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 Pass-by-reference sends the location (memory address) of the actual parameter can change value of actual parameter CALLING BLOCK FUNCTION CALLED 16

17 Nell Dale Chapter 3 ADTs Unsorted List and Sorted List C++ Plus Data Structures

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 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 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 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 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 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 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 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. {. }

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

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 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 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 C++ Plus Data Structures Nell Dale Chapter 4 ADTs Stack and Queue

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 Class Interface Diagram StackType class StackType MakeEmpty Pop Push IsFull IsEmpty Private data: top [MAX_ITEMS-1]. [ 2 ] [ 1 ] items [ 0 ]

35 DYNAMIC ARRAY IMPLEMENTATION StackType ~StackType Push Pop. class StackType Private Data: top 2 maxStack 5 items items [0] items [1] items [2] items [3] items [4]

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 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 C++ Plus Data Structures Nell Dale Chapter 5 Linked Structures

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 class StackType StackType MakeEmpty Pop Push IsFull IsEmpty Private data: topPtr ~StackType ‘C’ ‘V’

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 Push( ) l Allocate space for new item l Put new item into the allocated space l Put the allocated space into the stack

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 Pop( ) l Set item to Info (top node) l Unlink the top node from the stack l Deallocate the old top node

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 class QueType QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’‘Z’ ‘T’

47 class UnsortedType MakeEmpty ~UnsortedType DeleteItem. InsertItem UnsortedType RetrieveItem GetNextItem ‘X’ ‘C’ ‘L’ Private data: length 3 listData currentPos

48 class SortedType MakeEmpty ~SortedType DeleteItem. InsertItem SortedType RetrieveItem GetNextItem ‘C’ ‘L’ ‘X’ Private data: length 3 listData currentPos

49 Nell Dale Chapter 6 Lists Plus C++ Plus Data Structures

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 ‘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 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 INT_MAX

53 Copy Structures

54 Pass by value makes a shallow copy StackType MyStack; // CLIENT CODE. MyFunction( MyStack ); // function call Private data: topPtr 7000 Private data: topPtr 7000 MyStack SomeStack shallow copy

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 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 Making a deep copy Private data: topPtr 7000 SomeStack Private data: topPtr 5000 MyStack deep copy

58 Object-Oriented Design: l Composition l Inheritance l …

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 class CountedQue QueType ~QueType Enqueue Dequeue. Private Data: qFront qRear ‘C’‘Z’ ‘T’ CountedQue LengthIs Enqueue Dequeue. Private Data: length 3

61 End