CSCE , SPRING 2016 INSTRUCTOR: DR. NANCY M. AMATO 1
SYLLABUS, COURSE ORGANIZATION & ASSIGNMENTS Course Webpage: Syllabus: Assignments and Activites: https//parasol.tamu.edu/~amato/assignments.php:https//parasol.tamu.edu/~amato/assignments.php Culture Programming Homework In Class Activities (ICAs) and In Lab Activities (ILAs) 2
CH 1-4 : INTRODUCTION ACKNOWLEDGEMENT: THESE SLIDES INCLUDE INPUT FROM SLIDES PROVIDED WITH ``DATA STRUCTURES AND ALGORITHMS IN C++’’, GOODRICH, TAMASSIA AND MOUNT (WILEY 2004) AND SLIDES FROM JORY DENNY 3
OBJECT ORIENTED DESIGN (CH 2) Object Oriented Design and Principles Abstract Data Types Encapsulation Inheritance Polymorphism Exceptions 4
OBJECT ORIENTED DESIGN AND PRINCIPLES (CH 2.1) Design Goals: Robustness : Capability to handle any type of inputs Adaptability : Can be used in various environments with minimal or no changes Reusability : Same code can be used as a component in various applications over time Design Principles: Abstraction: Abstract the complicated details in form of fundamental parts and operations Encapsulation: Coupling data with methods. Modularity: Component based design Inheritance: Hierarchical and “is-a type-of” relationship Polymorphism: Ability to take different form 5
ABSTRACT DATA TYPE (ADT) An abstract data type (ADT) is an abstraction of a data structure An ADT specifies: Data stored Operations on the data Error conditions associated with operations Mathematical model only with no details about the implementation : ADT specifies what each operation does not how it does it. Example: ADT modeling a simple stock trading system The data stored are buy/sell orders The operations supported are order buy(stock, shares, price) order sell(stock, shares, price) void cancel(order) Error conditions: Buy/sell a nonexistent stock Cancel a nonexistent order 6
ENCAPSULATION Bundling of data and associated methods as a type. Hiding the details and direct access to the underlying data. Class: Construct or the definition of encapsulated data and associated methods. User-defined types. class Person { private: string name; public: string GetName() { return name; } void SetName( string _n) { name = _n; } } Object: Instance of an object Person A; // A is an object of class Person or in other words type of A is Person 7
INHERITANCE Hierarchical organization of classes. Base class: The class from which another class is inherited. Derived class: The class that inherits Abstract classes: Base class with one or more virtual member functions Virtual member functions are abstract functions with no details. 8 Shape CircleTriangleSquare
POLYMORPHISM Different types of a variable Subtyping : Type of the variable is determined at runtime based on the instance. Eg: Shape* newShape = new Circle(); newShape->draw(); //Will call draw() function of Circle class and not of the Shape class. Parametric: Generics (templates) where code is written without any specific type. Eg: template T sum ( T a, T b) {} // Can be used as sum (3,4) and sum (4.5, 7.8) 9
EXCEPTIONS (CH 2.4) Attempting the execution of an operation of ADT may sometimes cause an error condition, called an exception Exceptions are said to be “thrown” by an operation that cannot be executed Example: removing an element from an empty container 10
ARRAYS AND LINKED LISTS (CH 3) Arrays (Ch 3.1) Singly Linked List (Ch 3.2) Doubly Linked (Ch 3.3) 11
ARRAYS Data structure containing a collection of same type Contiguous allocation in memory Limitation: The length or capacity of the array is fixed. Easy Random Access through index of the element (position of the element with respect to the first element in the array. Eg. A[3] indicates fourth element if the first element is stored in index 0. Insertions and deletion at arbitrary location would include rearrangement of the elements following the location. Eg. Removing an element at index 6 would involve moving elements at indices 7,8,9 to 6,7,8. 12
SINGLY LINKED LIST A singly linked list is a concrete data structure consisting of a sequence of nodes Each node stores element link to the next node No contiguous allocation of memory Accessing an element at an arbitrary location includes traversing the list from the first element. next elem node ABCD 13
DOUBLY LINKED LIST A doubly linked list provides a natural implementation of the Node List ADT Nodes implement Position and store: element link to the previous node link to the next node Special trailer and header nodes No contiguous allocation of memory Access at arbitrary position requires traversal of the list Easy insertion and deletion of element If the pointer to the element is known trailer header nodes/positions elements prevnext elem node 14
EXERCISE: ARRAYS, SINGLY LINKED LIST, DOUBLY LINKED LIST 15 Describe how you would reach/access the third element in a data structure if it were a Array Singly Linked List Doubly Linked List Would your answer change if we wanted to reach/access the last element in the data structure? If so, how?
EXERCISE: COMPLETE THE TABLE WITH THE COMPLEXITY OF THESE OPERATIONS ArraySingly Linked ListDoubly Linked List Access first element Access last element Access middle element 16
EXERCISE: COMPLETE THE TABLE WITH THE COMPLEXITY OF THESE OPERATIONS ArraySingly Linked ListDoubly Linked List Access first element Access last element Access middle element 17