• Nie Znaleziono Wyników

Programming and Data Structures Programming and Data Structures

N/A
N/A
Protected

Academic year: 2021

Share "Programming and Data Structures Programming and Data Structures"

Copied!
8
0
0

Pełen tekst

(1)

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>

(2)

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

(3)

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

(4)

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)

(5)

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)

(6)

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

(7)

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 };

(8)

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)

Cytaty

Powiązane dokumenty

● automatically created if (and only if) there are no explicit constructors, but without any initialisation of the fields (risky when there are any dynamically allocated fields in

● the public method changing the state of an object (modifier) is to check the compliance of such a change with the rules described by invariants. ● if the rules are exceeded,

(3) set a dedicated pointer (killer) to the last node using the address in the next pointer of the node pointed to by tmp (or the head pointer if tmp is empty, or the tail pointer

● during instantiation of a template with a specific type, the compiler replaces each instance of a parametric type with the specified type and compiles the data structure

(1) set the pred pointer on the node preceding the deleted node, and succ on the node to be removed (if there is no node we are looking for, throw an exception or abort the

(1) create two auxiliary pointers: set pred to an empty address, and succ on to the address of the first node (head) and pass the auxiliary pointers towards the end of the list

(1) by comparing values, go from root to children and set the pred pointer on the parent node to be removed, and succ on the node to be removed (if there is no node we are looking

O ile bowiem literackość innej literatury (w tych rozważaniach pleonazm wydaje.. się nieunikniony) można zakwestionować z różnych pozycji, w różnych teo- riach (także Ingardena)