Data Structures Analysis of Algorithm Course Code : ITC303 (SE, Sem-III) Core Concepts Sr. No. Major Concepts in Data Structures Algorithm Prerequisite Concepts 1 Linear Data Structures and Operations C Basics, 2 Non-linear Data Structures and Operations Arrays , 3 Searching / sorting techniques Pointers, 4 Hashing techniques Functions, Structures, 5 Applications of Data Structures Dynamic Allocation Arrays in C • An array can be used to store and process a fixed number of data elements that all have the same type. • Array elements are stored at consecutive memory locations. • The first element in the array is numbered 0, so the last element is 1 less than the size of the array. • An array is also known as a subscripted variable. • For exampleint a=11;int b=21;int c=33;int d=40;int e=74; could be represented asint X[5]={11,-21,33,-40,74}; • Individual elements in the array can be referred using subscript asint value=X[2]; • #include<stdio.h> • i=0; • #include<conio.h> • while(name[i]!='\0') //we dont know length of name entered but know how it ends • void main() • { • char name[20]; • { • putch(name[i]); • int i, x[]={ 22,44,77,55,99}; //initialized • printf("\t%d its ascii value\t",name[i]);// corresponding ascii value numerical observe • clrscr(); • i++; • for(i=0;i<5;i++) • } • printf("\t%d",x[i]); • getch(); • printf("Observe garbage value after fifth element\n") ; • } • for(i=0;i<15;i++) // fifthe onwards garbage values no error • printf("\t%d",x[i]); • printf("Please enter your name\n"); • gets(name); • puts(name); output output #include<stdio.h> #include<conio.h> void main() { static int var=5; printf("%d",var--); if(var) main(); } Output#include<stdio.h> • #include<conio.h> • #include<string.h> • int main() •{ • char*c="GATECSIT2017"; • char*p=c; • printf("%d",(int)strlen(c+2[p]-6[p]1)); • return 0; •} output #include<stdio.h> #include<conio.h> void main() { int i=-1,j=-1,k=0,l=2,m; clrscr(); m=i++&&j++&&k++||l++; printf("%d%d%d%d%d",i,j,k,l,m); getch(); } • • • • • • • • • • • #include<stdio.h> #include<conio.h> void main() { int i,j,k,n=4; for(i=0;i<=n;i++) for(j=0;j<=n;j++) for(k=0;k<=n;k++) printf("bye"); } How many times ? What will be output? #include<conio.h> int main() { int a[]={2,4,6,8,10}; int i,sum=0, *b=a+4; clrscr(); printf("%d",*b); for(i=0;i<5;i++) sum=sum+(*b-i)-*(b-i); printf("%d\n",sum); return 0; } #include<stdio.h> #include<conio.h> void main() { int var[3]={5,10,15,20,25}; printf("%d",var[4]); } • Error Operations that can be performed on arrays Sr. No. Operation Description 1 Traversing to access each data item exactly once so that it can be processed 2 Searching to find out the location of the data item if it exists in the given collection of data items 3 Insertion to add a new data item in the given collection of data items 4 Deletion to delete an existing data item from the given collection of data items 5 Sorting to arrange the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data 6 Merging to combine the data items of two sorted files into single file in the sorted form Traversing an array • Let X be an 1D array initialized as, Index X Accessing an element 0 11 1 -21 2 33 3 -40 4 74 X[0] X[1] X[2] X[3] X[4] • Here we will start from beginning and will go till last element and during this process we will access value of each element exactly once asX[0]=11, X[1]=-21, X[2]=33, X[3]=-40, X[4]=74 Searching in an array • Let X be an 1D array initialized as, Index X Accessing an element 0 11 1 -21 2 33 3 -40 4 74 X[0] X[1] X[2] X[3] X[4] • Suppose item to be searched is 20, then comparison will start from 0th index until the element is found or array is finished. Insertion in an array • Let X be an 1D array initialized as, Index X Accessing an element 0 11 1 -21 2 33 3 -40 4 74 X[0] X[1] X[2] X[3] X[4] • New element to be inserted is 100 and location for insertion is 2. So shift the elements from 4th location to 2nd location downwards by 1 place. And then insert 100 at 2nd location. Index 0 1 2 3 4 5 X 11 -21 100 33 -40 74 Insert new element Shift the elements to the right by 1 position Deletion in an array • Let X be an 1D array initialized as, Index X Accessing an element 0 11 1 -21 2 100 3 33 4 -40 X[0] X[1] X[2] X[3] 5 74 X[4] X[5] • The element to be deleted is -21 which is at 1st location. So shift the elements from 2nd to last location upwards by 1 place and decrement the count for number of elements by 1. Index 0 1 2 3 4 X 11 100 33 -40 74 Shift the elements to the left by 1 position Sorting an array • Let X be an 1D array initialized as, Index 0 1 2 3 4 X 11 -21 33 -40 74 Accessing an element X[0] X[1] X[2] X[3] X[4] • After arranging the elements in increasing order by using a sorting technique, array will look like, Index 0 1 2 3 4 X -40 -21 11 33 74 Accessing an element X[0] X[1] X[2] X[3] X[4] Merging two sorted arrays • Let X be an 1D array initialized as, Index X Accessing an element 0 -40 1 -21 2 11 3 33 4 74 X[0] X[1] X[2] X[3] X[4] • Let Y be an 1D array initialized as, Index Y Accessing an element 0 20 Y[0] 1 35 Y[1] 2 45 Y[2] 3 90 Y[3] Merging in arrays • After merging merged array Z is as below: Index 0 1 2 3 4 5 6 7 8 Z Accessing an element -40 -21 11 20 33 35 45 74 90 Z[0] Z[1] Z[2] Z[3] Z[4] Z[5] Z[6] Z[7] Z[8] Pointers in C • A pointer is a variable that contains the address of a variable. • It is possible to goto that address and retrieve the data stored in it. • Need to tell the compiler during declaration time, about the pointer variable & what type of memory it points to. • For example, int *ptr; Pointer Notation • Consider the declaration, int i=3; • To print the address through C program: #include<stdio.h> main() { int i = 3 ; printf("\nAddress of i = %u\n",&i); printf("\nValue of i = %d",i); } Address of i = 65524 Value of i = 3 • “&” used in this statement is C’s “address of” operator. • “*” is used for “value at address” or “indirection” operator. • This address can be collected in a variable as, j=&i • To make variable “j” capable of holding an address of another variable, “j” must be declared as, int *j; • The memory map will look like Example #include<stdio.h> main( ) { int i = 3 ; int *j ; j = &i ; printf ("\n%u %u %u",&i,j,&j) ; printf ("\n%u %d %d %d",j,i,*(&i),*j) ; } What will be the output of the C program? Example1 #include<stdio.h> int main() { char temp; char arr[10] = {1, 2, 3, 4, 5, 6, 9, 8}; temp = (arr + 1)[2]; printf("%d\n", temp); return 0; } Example2 #include<stdio.h> int main() { int arr[2] = {1, 2, 3, 4, 5}; printf("%d", arr[3]); return 0; } Example3 #include<stdio.h> int main() { int rows = 3, cols = 4, i, j, k; int a[3][4] = {1, 2, 3, 5, 7}; i = j = k = 00; for(i=0; i<rows; i++) for(j=0; j<cols; j++) if(a[k][j]<k) k = a[i][j]; printf("%d\n", k); return 0; } Example4 #include<stdio.h> int main() { int arr[ ]={1.2, 2.4, 3.6, 4.8, 5}; int j, *ptr = arr; for(j = 0;j<5;j++) { printf("%d ", *arr); ++ptr; } } Example5 #include<stdio.h> int main() { void swap(); int x = 5, y = 10; swap(&x, &y); printf("x = %d y = %d",x,y); return 0; } void swap(int *a, int *b) { *a ^= *b, *b ^= *a, *a ^= *b; } Example6 #include<stdio.h> #include<stdio.h> int main(){ int i = 3; int *j; j = &i; j++; printf("%d ",*j); return 0; } Functions • A function is combined of a block of code that can be called or used anywhere in the program by calling the name. • Any C program contains at least one function i.e. main(), with which the program execution begins. • Once created, a function can be called within any other function or itself. • For example, #include<stdio.h> myfunc(); //function prototype main() { myfunc(); } myfunc() //function definition { printf*”Hello World”); } Function arguments #include<stdio.h> myfunc(int count); //function prototype main() { myfunc(5); } myfunc(int count) //function definition { int i; for(i=0; i<count; i++) printf(“\nHello World”); } Function return values #include<stdio.h> int myfunc() //function definition { int i=4,j=5,k; k=i*j; return k; } main() { int n; n=myfunc(); //function call printf(“%d”, n); } output • int fun2(int n) • { • static int i=0; • #include<stdio.h> • • • • • • • • • • #include<conio.h> int fun1(int n) { static int i=0; if(n>0) { ++i; fun1(n-1); } return(i); • } • if(n>0) • { • ++i; • i=i+fun1(n); • fun2(n-1); • } • return(i); • } • void main() • { • int x; • x=fun2(5); • printf("%d\t",x) ; • } • • • • • • • • • • • • • #include<stdio.h> #include<conio.h> int r() { static int num=7; return num--; } int main() { for(r();r();r()) printf("%d",r()); return 0; } Recursive Function #include<stdio.h> int rec(int); main() { int a, fact ; printf("\nEnter any number "); scanf ("%d", &a); fact = rec(a); printf ("Factorial value = %d", fact); } int rec (int x) { int f; if (x == 1) return (1); else f = x * rec (x - 1) ; return (f) ; } How recursion works for calculating factorial fact(5) 120 5*fact(4) 5*24 4*fact(3) 4*6 3*fact(2) 3*2 2*fact(1) 2*1 1