8/13/2019 Introduction to Data Structures 17022012
1/29
Introduction to Data Structures
CMPE231 Spring 2012
Assoc. Prof. Alexander Chefranov
1
8/13/2019 Introduction to Data Structures 17022012
2/29
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
8/13/2019 Introduction to Data Structures 17022012
3/29
Binary and Decimal Numbers
Binary number system
Ones complement notation
Twos complement notation Binary coded decimal
3
8/13/2019 Introduction to Data Structures 17022012
4/29
Real Numbers
Floating-point notation
Mantissa
Base Exponent
4
8/13/2019 Introduction to Data Structures 17022012
5/29
Data Type
Integer
Real number
Character string Memory, Address, Value (Contents)
Int x,y;
Float a,b;
5
8/13/2019 Introduction to Data Structures 17022012
6/29
Character Strings
Byte
First byte size
Null terminating strings
6
8/13/2019 Introduction to Data Structures 17022012
7/29
Abstract Data Types
/*value definition*/
Abstract typedef 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
8/13/2019 Introduction to Data Structures 17022012
8/29
Sequences as Value Definitions
S=
Abstract typedef stp1;
Abstract typedef stp2; Abstract typedef stp3;
8
8/13/2019 Introduction to Data Structures 17022012
9/29
ADT for Varying-length Character
Strings Abstract typedef 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
8/13/2019 Introduction to Data Structures 17022012
10/29
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
8/13/2019 Introduction to Data Structures 17022012
11/29
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
8/13/2019 Introduction to Data Structures 17022012
12/29
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
8/13/2019 Introduction to Data Structures 17022012
13/29
Arrays in C
One-dimensional array
Int a[100];
Basic operations: extracting and storingLower bound, Upper bound
#define NUMELTS 100
Int a[NUMELTS];For(int i=0;i
8/13/2019 Introduction to Data Structures 17022012
14/29
The Array as an ADT
Abstract typedef ARRTYPE(ub,eltype);
Condition type(ub)==int;
Abstract eltype extract(a,i) /* written a[i] */
ARRTYPE(ub, Eltype) a;
Int I;Precondition 0
8/13/2019 Introduction to Data Structures 17022012
15/29
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
8/13/2019 Introduction to Data Structures 17022012
16/29
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 contiguousset of memory locations each of which holds anaddress of an element
16
8/13/2019 Introduction to Data Structures 17022012
17/29
Arrays as Parameters
Float avg(float a[], int size){
int I;
Float sum;
sum=0;
for(i=0;i
8/13/2019 Introduction to Data Structures 17022012
18/29
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
8/13/2019 Introduction to Data Structures 17022012
19/29
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
8/13/2019 Introduction to Data Structures 17022012
20/29
Two-Dimensional Arrays
Int a[3][5];
Range r1 of the 1stdimension (rows) is 3
Range r2 of the 2
nd
dimension (columns) is 5A[1][3]=5;
Base(ar)+(i1*r2+i2)*esize
5
20
8/13/2019 Introduction to Data Structures 17022012
21/29
Multi-Dimensional Arrays
Int b[3][2][4];
Base(ar)+esize*(i1*r2*..*rn+i2*r3*..*rn+..+i(n-
1)*rn+in)0 2
0 1
0 3
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
8/13/2019 Introduction to Data Structures 17022012
22/29
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
8/13/2019 Introduction to Data Structures 17022012
23/29
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
8/13/2019 Introduction to Data Structures 17022012
24/29
Structure Parameters
Int writename(struct nametype *name){
int count,I;
count=0; printf(\n);
for(i=0;(ifirst[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;(ilast[i+)!=\0);i++){printf(%c,name->last[i]); count++;
}
return count;
}
24
8/13/2019 Introduction to Data Structures 17022012
25/29
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 itscontaining source file
25
8/13/2019 Introduction to Data Structures 17022012
26/29
Allocation of Storage and Scope of
VariablesFile1 #define MAXSTUDENTS ..
int grades[MAXSTUDENTS];
End of file1
File2 extern int grades[];
Float average(){
..
}
End file2
Static variables
Register variablesUninitialized external and static variables are initialized to 0, whereas
uninitialized automatic and register variables have undefined values
26
8/13/2019 Introduction to Data Structures 17022012
27/29
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
8/13/2019 Introduction to Data Structures 17022012
28/29
file2.c
11 #include
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
8/13/2019 Introduction to Data Structures 17022012
29/29
Dynamic Memory Allocation
Void *calloc(size_t nobj, size_t size)
Returns a pointer to space for an array of nobjobjects, each of size size, or NULL if the
request cannot be satisfied. The space isinitialized to zero bytes
Void *malloc(size_t size)
Returns a pointer to space for an object of sizesize, or NULL if the request cannot besatisfied. The space is uninitialized.
29