Government College University, Lahore. Today’s Agenda
Recap
ADTs Recap
I Pointers and Arrays
I Pointers and Objects I Dynamic Memory I new I delete I dangling pointer I memory leak Abstract Data Types
I Predefined data types and associated operations. For
Example, type int supports the basic arithmetic and relational operators, as well as a number of numerical functions. I We need more sophisticated data types, that are collection of related data items instead of individual data item. More sophisticated data types (items) (user defined) created using the predefined data types are called Abstract data type (ADTs). We specify ADTs by describing: I types of data items in ADT I structure: organization of data items I operations: describe the behavior of ADT Each operation must have some precondition and some postcondition. Data Structures
After these operations are precisely specified, the implementation
of the program may start. The implementation decides which data structure should be used to make execution most efficient in terms of time and space. Data structure is the area which discuss how the data is to be stored in memory. Data structure discuss the physical level details whereas an ADT is mathematical model of a data structure. ADT Set
A set is collection of distinct objects.
I data items: could be of any type I structure: No relation/ no ordering I operations: get number of objects, is empty, insert object, find, remove and make empty. ADT List
A sequence of zero or more elements a1 , a2 , . . . , an . These
elements are usually not sorted. I data items I structure: explicitly linearly ordered: ai precedes ai+1 and ai follows ai−1 I operations: insert element, delete element, print list, make empty, find, find kth etc. Choose data structure to implement I Array I Linked List ADT Sorted List
A sequence of zero or more elements such that a1 ≤ a2 , . . . ≤ an
(ascending) or a1 ≥ a2 , . . . ≥ an (descending). I data items I structure: implicit linear ordering: ai precedes ai+1 and ai follows ai−1 I operations: insert element, delete element, print list, make empty, find next larger, find previous smaller, iterate through objects between [aj , ak ] etc. Choose data structure to implement I Array I Linked List ADT Stack
A sequence of zero or more elements such that a1 , a2 , . . . , an .
I data items I structure: explicit linear ordering: LIFO structure. I operations: push object, pop object, make empty and show top. Choose data structure to implement I Array I Linked List ADT Queue
A sequence of zero or more elements such that a1 , a2 , . . . , an .
I data items I structure: explicit linear ordering: FIFO structure. I operations: Enque object, Dequeue object, make empty, show rear and show front . Choose data structure to implement I Array I Linked List ADT Tree
The Abstract Tree is defined for objects on which a hierarchical
ordering is placed. Convention has it that the objects within a tree are referred to as nodes. I data items I structure: explicit hierarchical ordering: I operations: Access the root node, Given a reference to a node, access the parent if it is not the root node, determine the depth of the node, Iterate through the ancestors back to the root node, Determine the number of children, Iterate through the children, Determine the number of descendants, and Iterate through all the descendants in a predictable manner (breadth-first traversal or depth-first traversal) Choose data structure to implement I Linked List ADT Polynomial An expression of more than two algebraic terms, especially the sum of several terms that contain different powers of the same variable(s). 4x 5 + 5x 2 − 8 The degree/order of polynomial is 5. A single variable polynomial can be generalized as: n X f (x) = ai x i i=0
which can be expanded as
an x n + an−1 x n−1 + . . . a2 x 2 + a1 x + a0
coefficients exponents ADT Polynomial
A polynomial object is a homogeneous ordered list of pairs
¡exponent,coefficient¿. I data items I structure: homogeneous ordered list of pairs I operations: Add & Subtract, Multiply, Differentiate, Integrate etc. How to implement it? I Array I Linked List