Programming and Data Structures Programming and Data Structures
Lecture 3 Lecture 3
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Dr Piotr Cybula <piotr.cybula@wmii.uni.lodz.pl>
Abstract Data Type Abstract Data Type
● formal description of data properties and operations on them without going into implementation details (ADT)
● ADT specification includes:
● determination of the nature and domain of the stored data
● assumptions about the relationship between the data, e.g. cardinality, ordering, relationships, etc.
● a set of possible operations specifying:
– names
– parameters
– result
– initial conditions
– final state
Abstract Data Type Abstract Data Type
● ADT capacity can be:
● bounded:
– fixed statically in the type definition (fixed)
– set dynamically for each instance of type (bounded)
● unbounded
● ADT implementation may use:
● continguous memory allocation, e.g. arrays:
– advantages: easy implementation, high operation efficiency
– low scalability, non-optimal memory consumption
● non-continguous memory allocation, e.g. linked structures:
– advantages: high scalability, optimal memory consumption
Stack Stack
● abstract data type assuming reversed order of data access
«Last In, First Out» (LIFO)
● basic operations:
● inserting onto the top the stack (push), only if the stack is not full (for a structure with a bounded size)
● taking from the top of the stack (pop), only if the stack is not empty
● checking what is on the top of the stack (top), only if the stack is not empty
● getting the current number of items (size)
● clearing (clear)
● checking if empty (empty)
Queue Queue
● abstract data type assuming chronological order of data access
«First In, First Out» (FIFO)
● basic operations:
● inserting into the queue (push, enqueue), only if the queue is not full (for a structure with a bounded size)
● taking from the queue (pop, dequeue), only if the queue is not empty
● checking what is at the beginning/end of the queue (front, back), only if the queue is not empty
● getting the current number of items (size)
● clearing (clear)
● checking if empty (empty)
ADT operation specification ADT operation specification
● intuitive operation name
● the order, types and meaning of operands
● conditions for performing the operation (preconditions):
● concerning the state of the structure, e.g. when it is not empty, when it is not full, when it already contains a given element, when the given
element is not found, etc.
● regarding arguments, e.g. range of values, interdependence between arguments, etc.
● optional exceptions reported when conditions are broken (explicit messages about the reasons for throwing exceptions)
● the final state of the structure after the operation (postconditions), in
particular how it differs from the state before the operation was performed
● optionally, the type and meaning of the operation result
Design by Contract (DbC) Design by Contract (DbC)
class Stack {
//internal structure
public: //public interface void push(int);
//pre: the stack is not full
//post: the argument value is on top, size is increased by 1 int pop();
//pre: the stack is not empty
//post: the top value is removed, size is decreased by 1 //return: the top value
void clear();
//pre: none
//post: the stack is empty };
Array implementation Array implementation
● fixed (static array):
● default constructors, destructor and assignment operator
● bounded (dynamic array):
● constructor parameterized by a structure size (memory allocation)
● destructor (memory deallocation)
● copy constructor (memory allocation)
● assignment operator (old memory deallocation and new memory allocation)
● unbounded (dynamic array):
● parameter-less constructor (optional memory allocation)
● destructor, copy constructor and assignment operator (as for bounded) insertion/removal (memory reallocation)