Lecture 1 Data Structures Shafay Shamail September 05, 2006

Download Report

Transcript Lecture 1 Data Structures Shafay Shamail September 05, 2006

Lecture 1
Data Structures
Shafay Shamail
September 05, 2006
Introduction
•
•
•
•
Course outline
Rules and regulations
Course contents
Grading
• Rules
• Conventions
• Good Programming Practices
Data Types
• Simple (basic)
– char, int, float, double
• Modifiers
– signed, unsigned
• Qualifiers
– static, const, volatile, void
• Structured (derived)
– Arrays, structures, unions, classes
• Advanced (composite)
– List, queues, stacks, trees, graphs
Abstract Data Type (ADT)
• Abstraction
– Separating data from implementation
– Generalization
• ADT
– A collection of related data items together with
basic operations between them and
operations to be performed on them
Arrays
• Definition
– Collection of items of same type in contiguous
memory
• ADT
– Collection of data items/elements
• A fixed size sequence (ordered set) of elements,
all of the same type
– Basic operations
• Direct access to each element in the array so that
values can be retrived from or stored in this
element
Structures
• Definition
– Collection of items of same or different types
in contiguous memory
• ADT
– Collection of data elements
• A fixed-size sequence (ordered set) of elements,
not necessarily of the same type. The elements
are called members or fields.
– Basic operations
• Direct access to each member of the structure so
that values can be retrieved or stored in this
member
Strings
• Definition
– Collection of characters in contiguous
memory
– A special form of array
• ADT
– Collection of data elements
• A finite sequence of characters drawn from some
given character set
– Basic operations
• Input, output, compare, copy, insert, replace,
length, concatenate, find, delete
Sequences
• S = <s0, s1, s2, …, sn-1>
• S is of length n
• len(S) = n
• first(S) s0
• last(S) = Sn-1
• len(nilseq) = 0
• first(nilseq) not defined
• last(nilseq) = not defined
Sequences …
• abstract typedef <<tp>> stp1;
– An ADT of a sequence spt1 of arbitrary length
consisting of elements of same type tp
• abstract typedef <tp0, tp1, …, tpn-1> stp2;
– ADT stp2, whose values are sequences of fixed
length, and of elements of specific type
• abstract typedef <<tp, n>> stp3;
– ADT stp3, a sequence of fixed length n, all of same
type tp elements
Sequences …
• abstract typedef <<int>> seq1;
– Sequence of integers of any length
• abstract typedef <int, char, float> seq2;
– Sequence of length 3, consisting of int, char, float
• abstract typedef <<int, 10>> seq3;
– Sequence of 10 integers
• abstract typedef << , 2>> seq4;
– Arbitrary sequence of length 2 (pair)
Sequences …
• Two sequences are equal if each element of the first is equal to the
corresponding element of the second
• A subsequence is a contiguous portion of a sequence
– If S is a sequence then sub(S, i, j) consists of j elements starting at
location i within S
• Concatenation U = S+T
– All elements of S followed by all elements of T
• Place (S, i, x)
– Insert x after position i, or at first position if i=-1, and shift all remaining
elements to right by one position
– place (S, i, x) = sub(S, 0, i-1) + <x> + sub(S, i+1, len(S)-(i+1))
• Delete
– S-<x> = Sequence S without all occurrences of S
– delete(S, i) = delete element at position I
= sub(S, 0, i) + sub(S, i+1, len(S)-(i+1))
ADT 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[1] == a[1]*b[1];
add[0] == a[0]*b[1] + b[0]*a[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]);
ADT 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)))));
ADT 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] == elti;