1 / 29

Introduction to Data Structures

Introduction to Data Structures. CMPE231 Spring 2012 Assoc. Prof. Alexander Chefranov. 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

drew
Download Presentation

Introduction to Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Data Structures CMPE231 Spring 2012 Assoc. Prof. Alexander Chefranov

  2. 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

  3. Binary and Decimal Numbers • Binary number system • Ones complement notation • Twos complement notation • Binary coded decimal

  4. Real Numbers • Floating-point notation • Mantissa • Base • Exponent

  5. Data Type • Integer • Real number • Character string • Memory, Address, Value (Contents) Int x,y; Float a,b;

  6. Character Strings • Byte • First byte size • Null terminating strings

  7. Abstract Data Types /*value definition*/ Abstract typedef <integer,integer> RATIONAL; Condition Rational[1]!=0; /*operator definition*/ Abstract RATIONAL makerational(a,b) Inta,b; Precondition b!=0; Postconditionmakerational[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; Postconditionmult[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]);

  8. Sequences as Value Definitions • S=<s0,s1,…,sn-1> • Abstract typedef <<tp>> stp1; • Abstract typedef <tp0,tp1,…,tpn> stp2; • Abstract typedef <<tp,n>> stp3;

  9. 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; Postconditionconcat==s1+s2; Abstract STRING substr(s1,i,j) STRING s1; IntI,j; Precondition 0<=i<len(s1); 0<=j<len(s2); Postconditionsubstr==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)))))

  10. 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

  11. 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 }

  12. 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 }

  13. 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);

  14. 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; Eltypeelt; Precondition 0<=i<ub; Postcondition a[i]==elt;

  15. 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); }

  16. 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

  17. 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

  18. Character Strings in C #define STRSIZE 80 Char string[STRSIZE]; IntStrlen(char string[]){ int I; for(i=0;string[i]!=‘\0’;i++); return I; } Intstrpos(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; }

  19. Character Strings in C (cont) Void strcat(char s1[], char s2[]){ intI,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[]){ intk,n; for(k=I,m=0;m<j;s2[m++]=s1[k++]; s2[m]=‘\0’; }

  20. 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

  21. Multi-Dimensional Arrays Int b[3][2][4]; Base(ar)+esize*(i1*r2*..*rn+i2*r3*..*rn+..+i(n-1)*rn+in)

  22. Structures in C

  23. Unions

  24. Structure Parameters Intwritename(structnametype *name){ intcount,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; }

  25. 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

  26. 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

  27. Source file1.c 1 intx,y,z; 2 void func1(){ 3 inta,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

  28. file2.c 11 #include <stdio.h> 12 extern intx,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}

  29. Dynamic Memory Allocation Void *calloc(size_tnobj, 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.

More Related