Download presentation
Presentation is loading. Please wait.
Published byImogen Fleming Modified over 8 years ago
1
Lecture 1 Data Structures Shafay Shamail September 05, 2006
2
Introduction Course outline Rules and regulations Course contents Grading
3
Rules Conventions Good Programming Practices
4
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
5
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
6
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
7
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
8
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
9
Sequences S = S is of length n len(S) = n first(S) s 0 last(S) = S n-1 len(nilseq) = 0 first(nilseq) not defined last(nilseq) = not defined
10
Sequences … abstract typedef > stp1; –An ADT of a sequence spt1 of arbitrary length consisting of elements of same type tp abstract typedef stp2; –ADT stp2, whose values are sequences of fixed length, and of elements of specific type abstract typedef > stp3; –ADT stp3, a sequence of fixed length n, all of same type tp elements
11
Sequences … abstract typedef > seq1; –Sequence of integers of any length abstract typedef seq2; –Sequence of length 3, consisting of int, char, float abstract typedef > seq3; –Sequence of 10 integers abstract typedef > seq4; –Arbitrary sequence of length 2 (pair)
12
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) + + sub(S, i+1, len(S)-(i+1)) Delete –S- = 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))
13
ADT RATIONAL /* value definition */ abstract typedef RATIONAL; condition RATIONAL[1] != 0; /* operator definition */ abstract RATIONAL makerational(a,b) Int a,b; preconditionb != 0; postconditionmakerational[0] == a; makerational[1] == b; abstract RATIONAL add(a, b) RATIONAL a, b; postconditionadd[1] == a[1]*b[1]; add[0] == a[0]*b[1] + b[0]*a[1]; abstract RATIONAL mult(a, b) RATIONAL a, b; postconditionmult[0] == a[0]*b[0]; mult[1] == a[1]*b[1]; abstract RATIONAL equal(a, b) RATIONAL a, b; postconditionequal == (a[0]*b[1] == b[0]*a[1]);
14
ADT STRING /* value definition */ abstract typedef > STRING; /* operator definition */ abstract length (s) STRING s; postconditionlength == len(s); abstract STRING concat(s1, s2) STRING s1, s2; postconditionconcat == s1+s2; abstract STRING substr(s, i, j) STRING s; int i, j; precondition0 <= i < len(s); 0 <= j <= len(s) - i; postconditionsubstr == 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)))));
15
ADT ARRAY /* value definition */ abstract typedef > ARRTYPE(ub, eltype); /* operator definition */ abstract eltype extract(a, i) ARRTYPE(ub, eltype) a; int i; precondition0 <= i < ub; postconditionextract == a i ; abstract store(a, i, elt) ARRTYPE(ub, eltype) a; int i; eltype elt; precondition0 <= i < ub; postconditiona[i] == elt i ;
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.