0% found this document useful (0 votes)
38 views

Abstract Data Types

The document defines several abstract data types including rational numbers, arrays, strings, lists, stacks, and queues. For each data type, it specifies value definitions and operator definitions such as makerational and add for rational numbers, extract and store for arrays, concat and substr for strings, empty and insert for lists, push and pop for stacks, and enqueue and dequeue for queues.

Uploaded by

Asada
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Abstract Data Types

The document defines several abstract data types including rational numbers, arrays, strings, lists, stacks, and queues. For each data type, it specifies value definitions and operator definitions such as makerational and add for rational numbers, extract and store for arrays, concat and substr for strings, empty and insert for lists, push and pop for stacks, and enqueue and dequeue for queues.

Uploaded by

Asada
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Abstract Data Types

Rational
/* value definition */
abstract typedef <integer, integer> RATIONAL;
condition RATIONAL[1] != 0;
/* operator definition */
abstract RATIONAL makerational(a,b)
int a, b;
precondition b != 0;
postcondition makerational[0] == a;
makerational[1] == b;
abstract RATIONAL add(a, b)
RATIONAL a, b;
postcondition add[0] == a[0]*b[1] + b[0]*a[1];
add[1] == a[1]*b[1];
abstract RATIONAL mult(a, b)
RATIONAL a, b;
postcondition mult[0] == a[0]*b[0];
mult[1] == a[1]*b[1];
abstract RATIONAL equal(a, b)
RATIONAL a, b;
postcondition equal == (a[0]*b[1] == b[0]*a[1]);
Array
/* value definition */
abstract typedef <<eltype, ub>> ARRTYPE(ub, eltype);

/* operator definition */
abstract eltype extract(a, i)
ARRTYPE(ub, eltype) a;
int i;
precondition 0 <= i < ub;
postcondition extract == ai;

abstract store(a, i, elt)


ARRTYPE(ub, eltype) a;
int i;
eltype elt;
precondition 0 <= i < ub;
postcondition a[i] == elt;
String
/* value definition */
abstract typedef <<char>> STRING;
/* operator definition */
abstract length (s)
STRING s;
postcondition length == len(s);
abstract STRING concat(s1, s2)
STRING s1, s2;
postcondition concat == s1+s2;
abstract STRING substr(s, i, j)
STRING s;
int i, j;
precondition 0 <= i < len(s);
0 <= j <= len(s) - i;
postcondition substr == sub(s, i, j);
abstract pos(s1, s2)
STRING s1, s2;
postcondition lastpos = len(s1) – len(s2)
((pos==-1) && (for (i=0; i<=lastpos; i++)
(s2<>sub(s1, i, len(s2)))));
List
abstract typedef <<eltype>> LIST(eltype);

abstract empty(l)
LIST(eltype) l;
postcondition: empty = (len(l) == 0);

abstract insert(l, elt) // Which type of insert is this? At head or at tail?


LIST(eltype) l;
eltypeelt;
postcondition: l = <elt> + l;

abstract eltype remove(l) // Which type of remove is this? From head or from tail?
LIST(eltype) l;
precondition: empty(l) == FALSE;
postcondition: x = first(l);
l = sub(l, 1, len(l) – 1);
remove = x;
Stack
abstract typedef <<eltype>> STACK(eltype);

abstract empty(s)
STACK(eltype) s;
postcondition: empty == (len(s) == 0);

abstract push(s, elt) // inserting at the head of the sequence (LIFO)


STACK(eltype) s;
eltypeelt;
postcondition: s = <elt> + s;

abstract eltype pop(s) // removing from the head of the sequence (LIFO)
STACK(eltype) s;
precondition: empty(s) == FALSE;
postcondition: pop = first(s);
s = sub(s, 1, len(s) – 1);
Queue
abstract typedef <<eltype>> QUEUE(eltype);

abstract empty(q)
QUEUE(eltype) q;
postcondition: empty == (len(q) == 0);

abstract enqueue(q, elt) // inserting at the end (tail) of the sequence (FIFO)
QUEUE(eltype) q;
eltypeelt;
postcondition: q = q + <elt>;

abstract eltype dequeue(q) // removing from the head of the sequence (FIFO)
QUEUE(eltype) q;
precondition: empty(q) == FALSE;
postcondition: remove = first(q);
q = sub(q, 1, len() – 1);

You might also like