Introduction to Data Structures

advertisement
Introduction to Data Structures
CMPE231 Spring 2012
Assoc. Prof. Alexander Chefranov
1
Information and Meaning
• Concept of information similar to the concept
of point, line, and plane in geometry is not
defined formally
• It is possible to talk about the length of a line
• We can measure quantities of information
• The basic unit of information is the bit
2
Binary and Decimal Numbers
•
•
•
•
Binary number system
Ones complement notation
Twos complement notation
Binary coded decimal
3
Real Numbers
•
•
•
•
Floating-point notation
Mantissa
Base
Exponent
4
Data Type
• Integer
• Real number
• Character string
• Memory, Address, Value (Contents)
Int x,y;
Float a,b;
5
Character Strings
• Byte
• First byte size
• Null terminating strings
6
Abstract Data Types
/*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) /*written a+b */
RATIONAL a,b;
Postcondition add[1]==a[0]*b[0];
Add[0]==a[0]*b[1]+a[1]*b[0];
Abstract RATIONAL mult(a,b) /*written a*b */
RATIONAL a,b;
Postcondition mult[0]==a[0]*b[0]; mult[1]==a[1]*b[1];
Abstract equal(a,b)
/*written a==b */
RATIONAL a,b;
Postcondition equal==(a[0]*b[1]==a[1]*b[0]);
7
Sequences as Value Definitions
•
•
•
•
S=<s0,s1,…,sn-1>
Abstract typedef <<tp>> stp1;
Abstract typedef <tp0,tp1,…,tpn> stp2;
Abstract typedef <<tp,n>> stp3;
8
ADT for Varying-length Character
Strings
• Abstract typedef <<char>> STRING;
Abstract length(s)
STRING s;
Postcondition length==lens(s);
Abstract STRING concat(s1,s2)
STRING s1,s2;
Postcondition concat==s1+s2;
Abstract STRING substr(s1,i,j)
STRING s1;
Int I,j;
Precondition 0<=i<len(s1); 0<=j<len(s2);
Postcondition substr==sub(s1,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)))))
|| (pos>=0)&&(pos<=lastpos)&&(s2==sub(s1,pos,len(s2))&&(for(i=1;i<pos;i++)(s2<>sub(s1,I,len(s2)))))
9
Data Types in C
• Int, float, char, double
• Short int, long int, unsigned
• Pointers
Int *pi;
Float *pf;
Char *pc;
Pi=(int *)pf;
Int x;
Pi=&x; x=*pi;
*(pi+1)
*pi+1
10
Parameters
By value, by reference
1 x=5;
2 printf(“%d\n”,x);
3 funct(x);
4 printf(“%d\n”,x);
..
5 void funct(int y){
6 ++y;
7 printf(“%d\n”,y);
8}
11
Parameters (cont)
1 x=5;
2 printf(“%d\n”,x);
3 funct(&x);
4 printf(“%d\n”,x);
5 void funct(int *py){
6 ++(*py);
7 printf(“%d\n”,*py);
8}
12
Arrays in C
• One-dimensional array
Int a[100];
Basic operations: extracting and storing
Lower bound, Upper bound
#define NUMELTS 100
Int a[NUMELTS];
For(int i=0;i<NUMELTS;a[i++]=0);
13
The Array as an ADT
Abstract typedef <<eltype,ub>> ARRTYPE(ub,eltype);
Condition type(ub)==int;
Abstract eltype extract(a,i) /* written a[i] */
ARRTYPE(ub, Eltype) a;
Int I;
Precondition 0<=i<ub;
Postcondition extract==ai;
Abstract store(a,I,elt) /* written a[i]=elt */
ARRTYPE(ub,eltype) a;
Int I;
Eltype elt;
Precondition 0<=i<ub;
Postcondition a[i]==elt;
14
Using One-Dimensional Arrays
#define NUMELTS 100
Void main(){
Int num[NUMELTS];
Int I;
Int total;
Float avg;
Float diff;
Total=0;
For(i=0;i<NUMELTS;i++){
scanf(“%d”,num[i]);
total+=num[i];
}
Avg=(float)total/NUMELTS;
Printf(“\nnumber difference”);
For(i=0;i<NUMELTS;i++){
diff=num[i]-avg;
printf(“\n %d %f”, num[i], diff);
}
Printf(“\n average= %f”,avg);
}
15
Implementing One-Dimensional Arrays
Int b[100];
Base address
B[i] in base(b)+i*esize
In C an array variable is a pointer variable
Int *b does not reserve 100 elements
B+I; *(b+i)
Varying-sized element array: reserve a contiguous
set of memory locations each of which holds an
address of an element
16
Arrays as Parameters
Float avg(float a[], int size){
int I;
Float sum;
sum=0;
for(i=0;i<size;i++) sum+=a[i];
return (sum/size);
}
#define ARANGE 100
Float a[ARANGE];
..
Avg(a, ARANGE);
Array is passed by reference saving space and time
17
Character Strings in C
#define STRSIZE 80
Char string[STRSIZE];
Int Strlen(char string[]){
int I;
for(i=0;string[i]!=‘\0’;i++);
return I;
}
Int strpos(char s1[],char s2[]){
int len1, len2, I,j1,j2;
len1=strlen(s1);
len2=strlen(s2);
for(i=0;i+len2<=len1;i++)
for(j1=i,j2=0;j2<=len2&&s1[j1]==s2[j2];j1++,j2++)
if(j2==len2) return I;
return -1;
}
18
Character Strings in C (cont)
Void strcat(char s1[], char s2[]){
int I,j;
for(i=0;s1[i]!=‘\0’;i++);
for(j=0;s2[j]!=‘\0’;s1[i++]=s2[j++];
}
Void substr(char s1[],int I, int j, char s2[]){
int k,n;
for(k=I,m=0;m<j;s2[m++]=s1[k++];
s2[m]=‘\0’;
}
19
Two-Dimensional Arrays
Int a[3][5];
Range r1 of the 1st dimension (rows) is 3
Range r2 of the 2nd dimension (columns) is 5
A[1][3]=5;
Base(ar)+(i1*r2+i2)*esize
5
20
Multi-Dimensional Arrays
Int b[3][2][4];
Base(ar)+esize*(i1*r2*..*rn+i2*r3*..*rn+..+i(n1)*rn+in)
02
01
03
B[0][0][0]
B[0][0][1]
B[0][0][2]
B[0][0][3]
B[0][1][0]
B[0][1][1]
B[0][1][2]
B[0][1][3]
21
Structures in C
Struct{
char first[10];
char midinit;
char last[20];
} sname, ename;
Typedef struct{
char first[10];
char midinit;
char last[20];
} NAMETYPE;
NAMETYPE sname, ename;
struct nametype{
char first[10];
char midinit;
char last[20];
}
Struct nametype sname, ename;
Printf(“%s”, sname.first);
Ename.midinit=‘N’;
For(i=0;i<20;i++)
sname.last[i]=ename.last[i];
22
Unions
#define LIFE 1
#define AUTO 2
#define HOME 3
Struct addr{
char street[50];
char city[10];
char state[3];
char zip[6];
}
Struct date{
int month;
int day;
int year;
}
Struct policy{
int polnumber;
Char name[30];
struct addr address;
int amount;
Float premium;
Int kind; /*LIFE,AUTO,HOME*/
Union{
struct{
char beneficiary[30];
struct day birthday;
} life;
struct{
int autodeduct;
Char license[10];
char state[3];
char model[15];
int year} auto;
Struct{
int homededuct;
int yearbuilt;
} home;
}policyinfo;
}
Struct policy p;
If(p.kind==LIFE)
printf(“\n%s %2d %2d %4d”,
p.policyinfo.life.beneficiary,
p.policyinfo.birthday.month,
p.policyinfo.life.birthday.day, p.
policyinfo.life.birthday.year);
Struct policy a[100];
For(i=0;i<100;i++)
if(a[i].kind==LIFE)
..
23
Structure Parameters
Int writename(struct nametype *name){
int count,I;
count=0; printf(“\n”);
for(i=0;(i<10)&&(name->first[i]!=‘\0’);i++){
printf(“%c”,name->first[i]);
count++;
}
printf(“%c”,’ ‘); count++;
if(name->midinit!=‘ ‘){
printf(“%c%s”, name->midinit,”. “); count+=3;
}
for(i=0;(i<20)&&(name->last[i])!=‘\0’);i++){
printf(“%c”,name->last[i]); count++;
}
return count;
}
24
Allocation of Storage and Scope of
Variables
Automatic variables (declared within a function)
Can be referenced only throughout entire
block unless the variable identifier is
redeclared within an internal block
External variables (declared outside of any
function) The scope lasts from the point at
which it is declared until the end of its
containing source file
25
Allocation of Storage and Scope of
Variables
File1 #define MAXSTUDENTS ..
int grades[MAXSTUDENTS];
End of file1
File2 extern int grades[];
Float average(){
..
}
End file2
Static variables
Register variables
Uninitialized external and static variables are initialized to 0, whereas
uninitialized automatic and register variables have undefined values
26
Source file1.c
1 int x,y,z;
2 void func1(){
3 int a,b;
4 x=1; y=2;z=3;a=1;b=2;
5 printf(“%d %d %d %d %d\n”,x,y,z,a,b);
6}
7 void func2(){
8 int a=5;
9 printf(“%d %d %d %d\n”,x,y,z,a);
10}
End of file1.c
27
file2.c
11 #include <stdio.h>
12 extern int x,y,z;
13 void main(){
14 func1();
15 printf(“%d %d %d\n”,x,y,z);
16 func2();
17 func3();
18 func3();
19 func4();
20 printf(“%d %d %d\n”,x,y,z);
21}
22 Void func3(){
23 static int b;
24 y++; b++
25 printf(“%d %d %d %d\n”,x,y,z,b);
26}
27 void func4(){
28 int x=10,y=20,z=30;
29 printf(“%d %d %d\n”,x,y,z);
30}
28
Dynamic Memory Allocation
Void *calloc(size_t nobj, size_t size)
Returns a pointer to space for an array of nobj
objects, each of size size, or NULL if the
request cannot be satisfied. The space is
initialized to zero bytes
Void *malloc(size_t size)
Returns a pointer to space for an object of size
size, or NULL if the request cannot be
satisfied. The space is uninitialized.
29
Download