Download presentation
Presentation is loading. Please wait.
Published byLogan Anthony Modified over 9 years ago
1
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Overview of Lecture Introduction to Data Structure Storage Containers Abstract Data Types (ADT) Classes Application Programmer Interface (API) Strings
2
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah A data structure is a systematic way of organizing and accessing data Programmer-defined data structures bundle data with operations that manipulate the data The structures, called containers have operations to access, insert, and remove items from the collection. What is a Data Structure?
3
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Storage Containers Containers such as vectors, lists or maps are storage structures that provide ways to access data and insert/delete items Vectors Lists Maps
4
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Storage Containers: Vectors A vector has all of the nice indexing features of an array along with the ability to dynamically grow to meet demand – aka “super array” Allows for direct access to its elements through the index We have the option to growing or shrinking vectors
5
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Storage Containers: Vectors Limitations to Vectors: Not an efficient storage structure for general insertion and deletion of items at an arbitrary position in the list Suppose an application wants to store elements in order and a vector is used: The insert operation requires shifting a block of elements to the right to make room for a new item The deletion operation requires shifting a bloc of elements to the right to make room for the new item Problem: High overhead of maintaining Vectors when collection of elements are large and the application calls for frequent insertion and deletion of items Solution: List containers
6
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Storage Containers: Lists Each element has a reference that identifies the next item in the list. Adding a new item involves breaking a link in the chain and creating two new links to connect the item Reference identifying next item on list The insertion occurs locally in the list and does not require the moving of the other elements to make room for the new element
7
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Storage Containers: Maps Arrays, vectors, and lists store elements by position: 0,1,2 etc., thus to access an element efficiently, we must know its position in the list – otherwise item-by-item search of list – not very effective for large list with say 100,000 elements – time consuming iterations of a loop to locate the position. Solution: Maps – which use tree structure to store data Data is stored by value and not position e.g. an airline reservation/ mail has a tracking number consisting of alphanumeric strings Map: structure where the index is represented by a value not position Maps use tree structures that maintain system files and directories and can parse expressions – very useful where trees hold a large number of items e.g.D29Z requires at most 3 iterations along a path from the root
8
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Abstract view of Data Structures To understand the design of a data structure, we use an abstract model that specifies the type of data stored operations that support the data. Abstract Data Types (ADT’s) are a model used to understand the design of a data structure. The main feature if an ADT is a simple and clear description of the operations in the data structure “abstract” implies that the data structure is given an implementation-independent view viewing a data structure as an ADT allows a programmer to focus on an idealized model of the data and its operations. Abstract Data Types (ADT)
9
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah ADT Operation Description An ADT provides simple and clear description of: the input to an operation. the action of the operation. its return type. Operation Name Operation Name: Action statement that specifies the input arguments type of operation on the elements of the date structure output value Preconditions Preconditions: Part of the description of an operation. A listing of the conditions that must apply in order for the operation to execute successfully. Postconditions Postconditions: Indicate changes to the object's data caused by the operation. Necessary because operations often alter the value of data. If the operation does not have a precondition or a postcondition, the ADT operation description disregards the condition
10
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah An ADT as a Class An ADT provides an abstract model of a data structure: The view is like an architect’s first sketch of a house – layout & # of rooms But becomes reality only when a(n): architect creates a structural blueprint contractor builds the walls and interior For an ADT, we need to create a: physical representation of the data computer model for the operations The object-oriented C++ language is ideal for this: The class construct provides a physical realization of an ADT The class declaration is the blueprint The writing of the implementation code actually builds the class
11
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah The time24 ADT Consider a data structure that uses integer values for hours and minutes to represent the time of day in a 24-hour clock Use time24 to identify the structure The ADT designer has the responsibility to specify the: number of operations action of each operation Like the architect who creates plans for a house – focus of ADT designer is on functionality and ease of use in a program
12
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah The time24 ADT: Operations e.g., the ADT could provide the operation called duration to measure the length of time between current value and some later time in the day The value is a time24 value that becomes the output parameter: Duration(t): Time t is an input argument. Measure the length of time from the current time to time t, and return the result as a time24 value Precondition: Time t must not be earlier than the current time readTime(): Input from the keyboard the hours and minutes for a time24 object, using the format hh:mm Postcondition: Set the hour and minute values of the time to hh and mm respectively. If either of the values is out of its designated range, the operation makes the appropriate adjustment e.g. 9:75 is stored as 10:15 writeTime(): Display on the screen the current time in the form hh:mm getHour(): Return the hour value for the current time getMinute(): Return the minute value for the current time See Exercise on page 13: “d_time24.h”
13
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah The C++ Class In object-oriented program design, building blocks of an application are values of primitive type and application Objects consist of data and operations on the data – specific instances of class type Class specifies the structure of an object - referred to as an object type Declaration of a class includes: Class Header consisting of reserved word class followed by the name of the class Declaration of a class includes: Class Body – beginning with ({ ), ending with (;) Format of Body includes: Private and Public sections Function Prototype: Specifies the parameters in an argument list within { } and indicates the return type of the output Function Prototype: return Type functionName ( ); Data values in the Class
14
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Classes: Private/Public Sections The public and private sections in a class declaration allow program statements outside the class different access to the class members.
15
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Classes: Public Section Public members of a class are the interface of the object to the program. Any statement in a program block that declares an object can access a public member of the object
16
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Classes: Private Section The private section typically contains the data values of the object and utility functions that support class implementation. Only member functions of the class may access elements in the private section. In addition, a class encapsulates information by bundling the data items and operations within an object
17
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Class: Constructor Constructor: a special member function of a class whose task it is to initialize the object The constructor always has the name of the class
18
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Application Programming Interface (API) Object-oriented programmers have developed a documentation format that collapses the key information from the ADT and the class declaration – this documentation is called application programming interface (API) API allows other programmers to use the public interface of the class without having to view the technical details of the class declaration or implementation The format includes: Function prototype for the constructor – provides programmer with the function name, arguments, and return type Other public member functions of the data structure The API includes: An action statement that describes what the operation does Preconditions that must apply before the operation can execute successfully, along with an exception that is thrown when the condition fails Postconditions indicate any changes to the current state of the object
19
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah API: Constructor CLASS classNameConstructors“.h” className( ); Initializes the attributes of the object Postconditions: the data members of the object have initial values
20
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah API: Operations CLASS classNameOperations“.h” returnType functionName(argument list); Description of the action of the function and any return value Preconditions:Necessary state of the object before executing the operation. Any exceptions that are thrown when an error is detected. Postconditions: State of the data items in the object after executing the operation ….
21
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Strings String: a sequence of characters that programs use to identify names, words, and sequences C++ provides two approaches to string handling. Older Method Older Method: C-style string - a character array that designates the end of the string by using the NULL character. Used by the C programming language and older C++ programs Modern Method Modern Method: String class – defines a string as an object and provides access to individuals characters and collections of characters within the string. The standard C++ library provides the string class, which is accessed using the directive: #include
22
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah Strings: Functions and Operations The string class provides: string handling functions that enable a programmer to search for characters within a string extraction of consecutive sequence of characters called substrings modification of a string by adding or removing characters
23
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah String: Functions and Operations int find_first_of(char c, int start = 0): c start Look for the first occurrence of c in the string beginning at index start. Return the index of the match if it occurs; otherwise return -1. start By default, start is 0 and the function searches the entire string. Searching for characters with a string: This function performs simple pattern matching that looks for a single character c in the string The index of the match is the return value. If no match occurs, the function returns -1
24
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah String Functions and Operations int find_last_of(char c): c Look for the last occurrence of c in the string. Return the index of the match if it occurs; otherwise return -1. Since the search seeks a match in the tail of the string, no starting index is provided. Useful in pattern matching especially in large strings See Example 1-4
25
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah String Functions and Operations string substr(int start = 0, int count = -1): start Copy count characters from the string beginning at index start and return the characters as a substring. If the tail of the string has fewer than count characters or count is -1, the copy stops at end-of-string. start By default, start is 0 and the function copies characters from the beginning of the string. Also by default, the function copies the tail of the string. Extraction of consecutive sequence The function substr() extracts from the string a consecutive sequence of characters called substrings The operation assumes a starting index and a count for the number of characters Useful in name searches on directories/ airline reservations etc See Example 1-5
26
Data Structures: CSCI 362 - Chapter 1 Data Structures: CSCI 362 - Chapter 1 lecture notes adapted from Data Structures with C++ using STL Dr. Nazli Mollah String Functions and Operations int find(const string& s, int start = 0): s s The search takes string s and index start and looks for a match of s as a substring. Return the index of the match if it occurs; otherwise return - 1. start By default, start is 0 and the function searches the entire string. The function find() locates a specific pattern in the string
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.