Module I-Arrays, Strings, Dr.Ganga Holi, Prof, & Head, ISE Dept. Global Academy of Technology ept. of Information Science & Engineering Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 1 Contents 1. What is Data Structure? ............................................................................................................................ 1 2. Explain the classification of data structures. ............................................................................................. 1 3. What are the data structures operations? ................................................................................................... 1 4. Explain declaration, initialization of One dimensional and Two dimensional arrays. .............................. 2 5. Discuss the operations that can be performed on arrays. .......................................................................... 2 6. Describe the method to calculate the actual memory address of the array location. ................................ 3 7. What are Polynomials and Sparse matrices? Explain with example. ....................................................... 4 How to represent polynomial and Evaluate polynomials? .................................................................... 5 How to represent polynomial and add two polynomials? ..................................................................... 5 Write an algorithm to transpose sparse matrix. ..................................................................................... 8 8. What are strings? Explain the operations on strings. ................................................................................ 9 Q9. What do you mean by pattern matching? Where do we use this concept? .......................................... 11 Q10.Define user defined data types : Structures and Unions. ..................................................................... 13 Q11. What are pointers? How do we use pointers to different data types? ................................................ 15 Q12. Why do we need dynamic memory allocation techniques? Explain the functions available for allocating memory dynamically. ................................................................................................................. 15 14. Define string. Explain string handling functions: strcat, strcpy, strcmp, strncmp, strncat, strchr, strrchr, strtok, strstr, strspn, strcspn, strbrk ................................................................................................. 16 String handling functions ........................................................................................................................ 19 15. Write an algorithm for pattern/string matching. ................................................................................... 19 16. Write a program to search an element using linear and binary search. ................................................. 20 Linear Search algorithm/function ........................................................................................................... 20 Binary search .......................................................................................................................................... 20 17.Design, Develop and Implement a menu driven Program in C for the following Array operations .... 21 18. Write a program to sort the numbers using selection and bubble sort algorithms. ............................... 23 Questions on Module I ................................................................................................................................ 25 Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 1 1. What is Data Structure? Data can be organized in many different ways. The logical or mathematical model of a organization of data is called a Data Structure. 2. Explain the classification of data structures. Data structures are generally classified into primitive and non-primitive data structures. Basic data types such as integer, real, character and boolean are known as primitive data structures. These data types consist of characters that cannot be divided, and hence they are also called as simple datatypes. The simplest example of non-primitive data structure is the processing of complex numbers. Very few computers are capable of doing arithematic on complex numbers. Linked lists, stacks, queues, trees and graphs are examples of non-primitive data structures. Based on the structures and arrangement of data, non-primitive data structures are further classified into linear and non-linear. A data structure is said to be linear if its elements form a sequence or a linear list. In linear data structures, the data is arranged in a linear fashion although the way they are stored in memory need not be


data structures are further classified into linear and non-linear. A data structure is said to be linear if its elements form a sequence or a linear list. In linear data structures, the data is arranged in a linear fashion although the way they are stored in memory need not be sequential. Arrays, linked list, stacks and queues are the examples of linear data structures. Conversely, a data structure is said to be a non-linear if the data is not arranged in sequence. The insertion and deletion of data is therefore not possible in a linear fashion. Trees and graphs are examples of non-linear data structures. 3. What are the data structures operations? The data appearing in our data structures are processed by means of certain operations. In fact, the particular data structure that one chooses for a given situation depends largely on the frequency with which specific operations are performed. 1. Traversing: Accessing each record exactly once so that certain items in the record maybe processed. (This accessing and processing is sometimes called "visiting" the record). 2. Searching: Finding the location of the record with a given key value, or finding the locations of all records which satisfy one or more conditions. 3. Inserting: Adding a new record to the structure. 4. Deleting: Removing a record from the structure. The following two operations are used in special situations, 5. Sorting: Arranging the records in some logical order. 6. Merging: Combining the records of two different sorted files into a single sorted file. Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 2 4. Explain declaration, initialization of One dimensional and Two dimensional arrays. One Dimensional Array Declaration: Syntax: data_type array_name[array_size]; where, data_type can be int, float or char. array_name is the name of the array. array_size indicates number of elements in the array. For ex: int age[5]; Initialization: Syntax: data_type array_name[array_size]={v1,v2,v3}; where, v1, v2, v3 are the values For ex: int age[5]={2,4,34,25,18}; Two Dimensional Array Declaration: Syntax: data_type array_name[array_size][array_size]; where, first index shows the row number of the element and second index shows the coulmn number of the element. Initialisation: Syntax: data_type array_name[array_size][array_size]={V1,V2,V3,.....Vn}; where, V1, V2, V3,......,Vn are the values For ex: int matrix[2][3]={2,4,56,3,6}; 5. Discuss the operations that can be performed on arrays. (i)Traversal Let A be a collection of data elements stored in the computer's memory. To print the contents of each element of A or count the elements of A with a given property, each element of A will have to be accessed or processed at least once. This is called traversing. Algorithm for Array Traversal Let A be a linear array with lower bound LB upper bound UB. The following algorithm traverses A applying an operation PROCESS to each element of A. 1. Initialization counter Set counter = LB 2. Repeat steps 3 and 4 while counter <= UB 3. Visit element Apply PROCESS to arr[counter] 4. Increase counter Set counter = counter + 1 5. Exit (ii) Insertion Insertion refers to the


applying an operation PROCESS to each element of A. 1. Initialization counter Set counter = LB 2. Repeat steps 3 and 4 while counter <= UB 3. Visit element Apply PROCESS to arr[counter] 4. Increase counter Set counter = counter + 1 5. Exit (ii) Insertion Insertion refers to the operation of adding another element to the array. If an element is to be inserted at the end of the array, then the task is easily done provided, there is enough space to accommodate additional elements in the array. But if we need to insert in the middle of an array then half of the elements must be moved downwards to new locations to accommodate the new element and retain the order of other elements. Algorithm for Insertion Let A be a linear array--The function used is INSERT(A, N, K, ITEM). N is the number of items, K is the positive integer such that K<=N. The following algorithm inserts an element ITEM into the Kth position of array A. 1. Initialize Counter Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 3 Set J := N 2. Repeat Steps 3 and 4 while J >= K 3. Move Jth element downward Set[J+1] := A[J] 4. Decrease counter Set J := J-1 End of step 2 loop 5. Insert element Set A[K] := ITEM 6. Reset N Set N := N+1 7. Exit (iii) Deletion Deletion refers to the operation of removing an element from the array. Deleting the element from the end of an array is not a problem, but deleting from middle requires movement of each element upwards in order to fill up the void in the array. Algorithm for Deletion Let A be a linear array. The function used to delete from the array is DELETE(a, N, K, ITEM) where, N is the number of elements, K is the positive integer such that K<=N. The algorithm delets Kth element from the array. 1. Set ITEM := A[k] 2. Repeat for J - K to N-1 [Move J+1 element upward] Set A[J] := A[J+1] End of loop 3. Reset the number N of elements in A Set N := N-1 4. Exit 6. Describe the method to calculate the actual memory address of the array location. Address Calculation in One Dimension Array: Array of an element of an array say “A[ I ]” is calculated using the following formula: Address of A [ I ] = B + W * ( I – LB ) Where, B = Base address W = Storage Size of one element stored in the array (in byte) I = Subscript of element whose address is to be found LB = Lower limit / Lower Bound of subscript, if not specified assume 0 (zero) Example: Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2 bytes in the memory. Find the address of B[1700]. Solution: The given values are: B = 1020, LB =


Lower limit / Lower Bound of subscript, if not specified assume 0 (zero) Example: Given the base address of an array B[1300…..1900] as 1020 and size of each element is 2 bytes in the memory. Find the address of B[1700]. Solution: The given values are: B = 1020, LB = 1300, W = 2, I = 1700 Address of A [ I ] = B + W * ( I – LB ) = 1020 + 2 * (1700 – 1300) = 1020 + 2 * 400 = 1020 + 800 = 1820 [Ans] Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 4 Address Calculation in Two Dimensional Array: Address of A [I][j] = B + W *((i-LB)*(UB-LB+1)+ (j – LB )) Example: Hint: Formula for calculating address for multi dimensional arrays Li=upper bound-lower bound+1, where L is the number of elements in Column. For a given subscript Ki, the effective index Ei of Li is the number of indices preceding Ki in the index set and Ei can be calculated from Ei=Ki-lower bound Then the address LOC(K1, K2, K3…...Kn) of an ordinary element of C array can be obtained from the formula Base(C )+w[(((…(EnLn-1 +En-1 )Ln-2 + En-2 )Ln-3 + +E3)L2 + E2)L1 +E1) or from the formula Base( C )+ w[(…((E1L2 +E2 )L3 + E3 )L4 + ….+En-1)Ln+ En) While storing the elements of a 2-D array in memory, these are allocated contiguous memory locations. Therefore, a 2-D array must be linearized so as to enable their storage. There are two alternatives to achieve linearization: Row-Major and Column-Major. Address of an element of any array say “A[ I ][ J ]” is calculated in two forms as given: (1) Row Major System (2) Column Major System Row Major System: The address of a location in Row Major System is calculated using the following formula: Address of A [ I ][ J ] = B + W * [ N * ( I – Lr ) + ( J – Lc ) ] Column Major System: The address of a location in Column Major System is calculated using the following formula: Address of A [ I ][ J ] Column Major Wise = B + W * [( I – Lr ) + M * ( J – Lc )] Where, B = Base address I = Row subscript of element whose address is to be found J = Column subscript of element whose address is to be found W = Storage Size of one element stored in the array (in byte) Lr = Lower limit of row/start row index of matrix, if not given assume 0 (zero) Lc = Lower limit of column/start column index of matrix, if not given assume 0 (zero) M = Number of row of the given matrix N = Number of column of the given matrix 7. What are Polynomials and Sparse matrices? Explain with example. Polynomial is an expression consisting of variables and co-efficients which only employs operations


index of matrix, if not given assume 0 (zero) M = Number of row of the given matrix N = Number of column of the given matrix 7. What are Polynomials and Sparse matrices? Explain with example. Polynomial is an expression consisting of variables and co-efficients which only employs operations of addition, subtraction and multiplication. Ex: x^2-4x+7 is a single variable equation. x^3+xyz^2+x^2y+z+10 is a 3 variable polynomial equation. The largest exponent (power) of a polynomial is called its degree. Co-efficients that are zero are not displayed. Program to evaluate polynomial Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 5 How to represent polynomial and Evaluate polynomials? // Evaluation of polynomial equation #include<stdio.h> typedef struct { int coef,power; }term; term a[100]; int main() { int i, x, n,m, sum=0; i=0; printf("Enter n=" ); scanf("%d", &n); printf(" Enter Polynomial Coef and power="); for(i=0;i<n;i++) scanf("%d %d", &a[i].coef, & a[i].power); printf("Enter x=" ); scanf("%d", &x); for(i=0;i<n;i++) { sum=sum+a[i].coef*pow(x,a[i].power); } printf("Sum=%d\n",sum); } Program for Addition of two polynomials How to represent polynomial and add two polynomials? #include<stdio.h> typedef struct { int coef,power; }term; term a[100]; int startA, startB, finishA, finishB, avail, count=0; int compare(int a, int b) { if(a>b) return 1; else if(a<b) return -1; else return 0; } Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 6 void attach(int coef, int power) { printf("avail=%d\n",avail); if(avail>=100) { fprintf(stdout, "cannot add poly\n"); return ; } a[avail].coef=coef; a[avail].power=power; printf("added coef=%d ",a[avail].coef); avail++; count++; } void display() { int i=0, k; printf(" Ist polynomial=\n"); for(;i<=finishA;i++) printf("%d %d\n",a[i].coef,a[i].power); printf(" IIst polynomial=\n"); for(;i<=finishB;i++) printf("%d %d\n",a[i].coef,a[i].power); printf(" \n \n Result polynomial=\n"); k=i; for(k=i;k<avail;k++) printf("%d %d\n",a[k].coef,a[k].power); } void polyadd() { int t, coeff; avail=finishB+1; printf("startA=%d startB=%d\n", startA, startB); while(startA<=finishA && startB<=finishB) { t=compare(a[startA].power,a[startB].power); printf("t=%d \n",t); switch(t) { case -1 : attach(a[startB].coef, a[startB].power); startB++; break; case 0 : coeff=a[startA].coef+a[startB].coef; attach(coeff, a[startA].power); startA++; startB++; break; case 1 : attach(a[startA].coef, a[startA].power); startA++; break; } } for(;startA<=finishA;startA++) attach(a[startA].coef, a[startA].power); Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 7 for(;startB<=finishB;startB++) attach(a[startB].coef, a[startB].power); } int main() { int i, n,m; i=0; startA=0; printf("Enter n=" ); scanf("%d", &n); printf("Enter m=" ); scanf("%d", &m); printf(" Enter Ist Polynomial Coef and power="); for(i=0;i<n;i++) scanf("%d %d", &a[i].coef, & a[i].power); finishA=i-1; startB=i; printf(" Enter Ist Polynomial Coef and power="); for(;i<n+m;i++) scanf("%d %d", &a[i].coef, & a[i].power); finishB=i-1; avail=i; polyadd(); display(); return 0; } Operations on Sparse Matrix: (i) Create (ii) Transpose (iii) Add (iv) Multiply Program to create Sparse matrix #include<stdio.h> #define MAX_TERMS 100 typedef struct { int row; int col; int value; }term; term a[MAX_TERMS]; int main() { int i,j; printf("Enter size of sparse matrix:"); printf("Max rows=%d",a[0].row); printf("Max col=%d",a[0].col); printf("Total ele=%d \n",a[i].value); printf("Enter % elements \n",a[0].value); for(i=0;i<a[i].value;i++) scanf("%d%d%d",a[i].row,a[i].col,a[i].value); printf("Output of the matrix \n); k=1; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 8 for(i=0;i<a[0].row;i++) { for(j=o;j<a[0].col;j++) { if(i==a[k].row && j==a[k].col) { printf("%d\t",a[k].value); k++; } else printf("0 \t"); } } printf("\n"); } Transpose of a sparse Matrix Write an algorithm to transpose sparse matrix. #include<stdio.h> typedef struct


the matrix \n); k=1; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 8 for(i=0;i<a[0].row;i++) { for(j=o;j<a[0].col;j++) { if(i==a[k].row && j==a[k].col) { printf("%d\t",a[k].value); k++; } else printf("0 \t"); } } printf("\n"); } Transpose of a sparse Matrix Write an algorithm to transpose sparse matrix. #include<stdio.h> typedef struct { int row, col, value; }term; term a[20],b[20]; int main() { int i, n, j,current=0;; printf("Enter size of Matrix row & col and Total elemnts =\n" ); scanf("%d%d%d", &a[0].row, &a[0].col,&a[0].value); printf(" Enter sparse matrix elements\n"); for(i=1;i<=a[0].value;i++) { printf(" row col and value="); scanf("%d%d%d", &a[i].row, &a[i].col, &a[i].value); } printf(" Transpose of sparse matrix\n\n"); current=1; n=a[0].value; b[0].col=a[0].row; b[0].row=a[0].col; b[0].value=n; if(n>0) { for(i=0;i<a[0].col;i++) { for(j=1;j<=n;j++) { if(a[j].col==i) { b[current].row=a[j].col; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 9 b[current].col=a[j].row; b[current].value=a[j].value; current++; } } } } printf(" \n Input matrix\n\n"); for(i=0;i<=n;i++) printf("%d %d %d\n",a[i].row,a[i].col,a[i].value); printf(" \n Transpose of matrix\n\n"); for(i=0;i<=n;i++) printf("%d %d %d\n",b[i].row,b[i].col,b[i].value); return 0; } 8. What are strings? Explain the operations on strings. String : Strings are one-dimensional array characters. The length of a string is determined by a terminating null character. Initialization : Strings can be initialized in the following way : char string_name[string_size]; Example : char greeting[6]={‘H’,’E’,’L’,’L’,’O’}; #include<stdio.h> int main() { char greeting[6]={‘H’,’E’,’L’,’L’,’O’}; printf(“Greeting message : %s\n”,greeting); return 0; } Operations on string : 1. Strcpy(dest,src); This operation copies string source to destination string. int main() { char src[50],dest[50]; printf(“Enter source”); gets(src); printf(“Enter destination string”); gets(dest); strcpy(dest,src); printf(“Copied string is %s\n”,dest); return 0; } Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 10 Strcat(dest,char) : This operation concatenates string string vhar into the end of the string dest. int main() { char src[20],dest[20]; strcpy(src,”This is source”); strcpy(dest,”This is destination”); strcat(dest,src); printf(“Final destination string is %s\n”,dest); return (0); } Strlen(str) : This operation returns thr length of the entered string str. int main() { char str[50]; int len; printf(“Enter string”); gets(str); len=strlen(str); printf(“Length of %s is %d\n”,str,len); return (0); } Strcmp(s1,s2) : This returns 0 if s1 and s2 are same, less than 0 if s1<s2, greater than 0 if s1>s2. int main() { char s1[50],s2[50]; int ret; printf(“Enter string s1”); gets(s1); printf(“Enter string s2”); gets(s2); ret=strcmp(s1,s2); if(ret<0) { printf(“s1<s2”); } else if(ret>0) { printf(“s1>s2”); } else { printf(“s1=s2”); } Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 11 return (0); } Strchr(s1,s2) : This returns a pointer to the first occurrence of characterch in the string s1. int main() { const char s1=’Tea & Biscuits’; const char s2=’&’; char ret; ret=strchr(s1,ch); printf(“String after %cis %s”,ch,ret); return (0); } Strstr(s1,s2) : Returns a pointer to the first occurrence of string s2 in string s1. int main() { const char s1[50]=”Global Academy of Technology”; const char s2[50]=”Academy”; char ret; ret=strstr(s1,s2); printf(“The substring is %s\n”,ret); return (0); } Char strncpy(dest,src) This operation copies only ‘n’ characters from src string intp destination string and returns dest string. Char strncat(dest,char) : This concatenates dest and only n characters from src, returning in dest.


s1[50]=”Global Academy of Technology”; const char s2[50]=”Academy”; char ret; ret=strstr(s1,s2); printf(“The substring is %s\n”,ret); return (0); } Char strncpy(dest,src) This operation copies only ‘n’ characters from src string intp destination string and returns dest string. Char strncat(dest,char) : This concatenates dest and only n characters from src, returning in dest. Strcmp(s1,s2,n) This operation compares first ‘n’ characters of the string entered. Q9. What do you mean by pattern matching? Where do we use this concept? Pattern matching : A code to check if the given string is present in another string is pattern matching. For example : “Global is present in Global Academy of Technology” . If the string is present then its location is printed. Assume that we have two strings, string and pat, where pat is a pattern to be searched. If pattern is present in the string, then it returns its position else returns -1. it examines each character of the string until it finds the pattern or it reaches the end of the string. Example : #include<stdio.h> #include<string.h> int match(char[],char[]) int main() { char a[100],b[100]; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 12 int position; printf(“Enter text\n”); gets(a); printf(“Enter a string to find”); gets(b); position=match(a,b) if(position!=-1) { printf(“Found at location %d\n”,position+1); } else { printf(“Not found”); } return (0); } int match(char text[],char pattern[]) { int c,d,e,txt_length,pattern_length,position=-1; txt_length=strlen(txt); pattern_length=strlen(pattern); if(pattern_length>txt_length) { return -1; } for(c=0;c<=txt_length-pattern_length;c++) { position=e=c; for(d=0;d<pattern_length;d++) { if(pattern[d]==text[e]) { e++; } else { break; } } if(d==pattern_length) { return position; } } return -1; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 13 } Q10.Define user defined data types : Structures and Unions. Structures : A structure is a collection of data items of different data types. struct structure_name { member 1; member 2; member 3; member n; }; Ex : struct person { int age; float salary; char name[10]; }p; We can address data members of the structure using dot ( . ) notation. person.age=40 in first case and p.age =40 in second case. Another way of defining structure is as follows : typedef struct { char name[10]; int age; float salary; }human; This says that ‘Human’ is the name of the type defined by the structure definition and we may follow this definition with declarations of variables such as : human person 1,person2; We may use person1.age=40;person2.salary=10000; ex : To compare two structures we might call a function by passing a parameter if(humanequal(person1,person2)) printf(“Two persons are same\n”); else printf(“Two persons are not same”); int humanequal(human person1,human person2) { if(strcmp(person1.name,person2.name)) return 0; if(person1.age==person2.age) return 0; if(person1.salary==person2.salary) return 0; return 1; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 14 } We can also embed a structure within a structure. For example, associated with our human structure, we may wish to include the date of his or her birth. typedef struct{ int month; int day; int year; }date; typedef struct { int age; float salary; char name[50]; date dob; }human; We


We can also embed a structure within a structure. For example, associated with our human structure, we may wish to include the date of his or her birth. typedef struct{ int month; int day; int year; }date; typedef struct { int age; float salary; char name[50]; date dob; }human; We can assign data to a structure variable as follows : human person1, person 2; person1.age=40; person1.dob.month=10; person1.dob.year=1997; Unions : A union declaration is similar to a structure, but the fields of a union must share their memory space. This means that only one field of the union is “active” at any given time. typedef struct{ enum tagfield{female,male}gender; union{ int children; int beard; }u; }gendertype; typedef struct{ char name[10]; int age; float salary; date dob; gendertype genderinfo; }human; human person1; person1.genderinfo.gender=male; person1.genderinfo.u.children=4; Self Referential Structures : A self referential structure is one in which one or more of its components is a pointer to itself. Self referential structures usually require dynamic storage management routines (malloc,free) to explicitly obtain and release memory cinsider as an example. Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 15 typedef stuct{ char data; struct list *link; }list; There the structure will have two components, data and link, Data is a single character, while link is a pointer to a list structure. list l1, l2, l3; l1.link=l2.link; Q11. What are pointers? How do we use pointers to different data types? A pointer is a variable which contains the address in memory of another variable. Declaration : data_type *variable_name; Ex : int *a; Ex : #include<stdio.h> int main() { int var = 20; int *ip; ip=&var; printf(“Address of variable is %x\n”, &var); printf(“Address stored in ip variable is :%x”,ip); printf(“value of *ip variable : %d”, *ip); return 0; } Q12. Why do we need dynamic memory allocation techniques? Explain the functions available for allocating memory dynamically. Dynamic memory allocation : It refers to performing manual management for dynamic memory allocation using standard library functions such as malloc, realoc, calloc and free. The size of array initially declared can be sometimes insufficient or sometimes more than required but dynamic memory allocation allows a program to obtain more memory space, while running or to release space when not required. Functions: There are four standard library functions under “stdlib.h” for dynamic memory allocation. malloc() – It allocates requested size of bytes and returns a pointer first byte of allocated space. Syntax : ptr=(data_type *)malloc(bysize); Ex : (int*)malloc(100*sizeof(int)); calloc() – Allocates spaces for array elements, initializes to zero and then returns a pointer to memory. Syntax : ptr=(data_type*)calloc(n,element_size); Ex : ptr=(float*)calloc(25,sizeof(float)); realloc() – Changes the size of the previously allocated space according to the requirement. Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 16 Syntax : ptr=realloc(ptr,newsize); free() – It deallocates the previously allocated space. Syntax – free(ptr); Q13.What is sparse matrix? How to represent a sparse matrix? Sparse Matrix : If a matrix contains more zero entities then suc a matrix is called


Dr. Ganga Holi, ISE Dept., Global academy of Technology 16 Syntax : ptr=realloc(ptr,newsize); free() – It deallocates the previously allocated space. Syntax – free(ptr); Q13.What is sparse matrix? How to represent a sparse matrix? Sparse Matrix : If a matrix contains more zero entities then suc a matrix is called a sparse matrix. Ex: [ 0 5 0 4 0 0 2 0 0 0 1 0 0 5 0 0 1 0 5 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 ] The concept of sparse matrix is used in scientific or engineering applications. Representation : Array representation : 2D array having n-rows and 3-coloumns,where n is number of non-zero elements. a[i][0] – represents row value a[i][1] – represents column value a[i][2] – represents matrix value 1 2 5 1 4 4 2 1 2 2 5 1 3 2 5 3 5 1 4 5 1 5 6 1 Array of structures representation : Triple values represent row, column, value. typedef struct { int row;int col;int value; }term; term a[MAX_TERMS]; 14. Define string. Explain string handling functions: strcat, strcpy, strcmp, strncmp, strncat, strchr, strrchr, strtok, strstr, strspn, strcspn, strbrk In C programming, array of character are called strings. A string is terminated by null character /0. For example: Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 17 "c string tutorial" Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string. Declaration of strings Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. char s[5]; Strings can also be declared using pointer. char *p Initialization of strings In C, string can be initialized in different number of ways. char c[]="abcd"; OR, char c[5]="abcd"; OR, char c[]={'a','b','c','d','\0'}; OR; char c[5]={'a','b','c','d','\0'}; String can also be initialized using pointers char *c="abcd"; Reading words from user. char c[20]; scanf("%s",c); String variable c can only take a word. It is beacause when white space is encountered, the scanf() function terminates. Write a C program to illustrate how to read string from terminal. #include <stdio.h> int main() { char name[20]; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 18 printf("Enter name: "); scanf("%s",name); printf("Your name is %s.",name); return 0; } Output Enter name: Dennis Ritchie Your name is Dennis. Here, program will ignore Ritchie because, scanf() function takes only string before the white space. Reading a line of text C program to read line of text manually. #include <stdio.h> int main() { char name[30],ch; int i=0; printf("Enter name: "); while(ch!='\n') // terminates if user hit enter { ch=getchar(); name[i]=ch; i++; } name[i]='\0'; // inserting null character at end printf("Name: %s",name); return 0; } This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively. int main() { char name[30]; printf("Enter name: "); gets(name); //Function to read


name[i]=ch; i++; } name[i]='\0'; // inserting null character at end printf("Name: %s",name); return 0; } This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively. int main() { char name[30]; printf("Enter name: "); gets(name); //Function to read string from user. printf("Name: "); puts(name); //Function to display string. return 0; } Both, the above program has same output below: Output: Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 19 Enter name: Tom Hanks Name: Tom Hanks String handling functions C compiler provides a library function called string.h. The functions supported by the header file string.h are: int strlen(char* str) length of the char array char* strcat(char* dest, char* src) concatenate dest and src strings return result in dest char* strcat(char* dest, char* src, int n) concatenate dest and n characters from src strings return result in dest char* strcpy(char* dest, char* src) copy src into dest ; return dest char* strncpy(char* dest, char* src, int n) copy n characters from src string into dest ; return dest char* strcmp(char* str1, char* str2) compares two strings . returns <0 if str1<str2, returns 0 if str1=str2, returns >0 if str1>str2 char* strcmpi(char* str1, char* str2) or char* stricmp(char* str1, char* str2) compares two strings . returns <0 if str1<str2, returns 0 if str1=str2, returns >0 if str1>str2 it ignores case. char* strcmp(char* str1, char* str2) compare first n characters of two strings . returns <0 if str1<str2, returns 0 if str1=str2, returns >0 if str1>str2 char* strchr(char*s, int c) returns pointer to the first occurrence of c in s; return NULL if not present. char* strrchr(char*s, int c) returns pointer to the last occurrence of c in s; return NULL if not present. char* strstr(char* s, char* pat) Return pointer to start of pat in s char* strtok(char* s, char delimiters) Return a token from s, token is surrounded by delimiters char* strspn(char* s, char* spanset) Scan s for characters in spanset , return length of span char* strcspn(char* s, char* spanset) Scan s for characters not in spanset , return length of span strlwr() converts from upper case to lower case struper() converts from upper case to lower case strrev() reverses a string strset(char* s, char c) It sets all characters in string to character c strnset(char* s, char c) It sets first n characters in string to character c 15. Write an algorithm for pattern/string matching. Simple string matching algorithm using brute force method is given below. Match(text, pat) { L1=strlen(text); Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 20 L2=strlen(pat); for(i=0;i<(L1-L2);i++) { j=0; while( j! =L2 && text[i+j] ==pat[j]) { j++; } If(j==L2) { flag=1; break; } } If(flag==1) printf(“pattern found”); else printf(“Not found”); } 16. Write a program to search an element using linear and binary search. Linear Search algorithm/function Search(a, key) { For(i=0;i<n;i++) { if(a[i]==key) { flag=1; break; } } If(flag==1) printf(“ Found”); else printf(“not found”); }


{ j++; } If(j==L2) { flag=1; break; } } If(flag==1) printf(“pattern found”); else printf(“Not found”); } 16. Write a program to search an element using linear and binary search. Linear Search algorithm/function Search(a, key) { For(i=0;i<n;i++) { if(a[i]==key) { flag=1; break; } } If(flag==1) printf(“ Found”); else printf(“not found”); } Binary search Binsearch(a, key) { low=0; high=n-1; mid=(low+high)/2 while(low<high && a[mid]!=key) { If(key<a[mid]) high=mid-1; else low=mid+1; mid=(low+high)/2; } if(a[mid]==key) printf(“ found”); else Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 21 printf(“Not found”); } 17.Design, Develop and Implement a menu driven Program in C for the following Array operations I. Creating an Array of N Integer Elements II. Display of Array Elements with Suitable Headings III. Inserting an Element (ELEM) at a given valid Position (POS) IV. Deleting an Element at a given valid Position(POS) V. Exit. Support the program with functions for each of the above operations. */ #include<stdio.h> #include<conio.h> #define SIZE 15 void create(int array[], int n) { int i; printf(" Enter n elemnts\n"); for(i=1;i<=n;i++) { scanf("%d",&array[i]); } return ; } void display(int array[], int n) { int i; printf(" Elemnets of the array are\n"); for(i=1;i<=n;i++) { printf("%d\t",array[i]); } printf("\n"); } int insertAtPos(int array[], int n, int position, int value) { int i; for (i = n ; i >= position; i--) array[i+1] = array[i]; array[position] = value; n++; return n; } int deleteFromPos(int array[], int n, int position) { int i, v; v=array[position]; for (i = position; i < n; i++) array[i] = array[i+1]; Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 22 n--; return n; } int main() { int array[SIZE], n, choice, flag=0; int position, value; int count=0; char answer; while(1) { printf("1. Create\n"); printf("2. Display\n"); printf("3. Inserting Elemnt at given valid position\n"); printf("4. Deleting an Element at a given valid Position(POS)\n"); printf("5. Exit\n"); printf("Enter choice ="); scanf("%d", &choice); switch(choice) { case 1 : if(flag==0) { flag=1; printf("Enter no. of elements="); scanf("%d", &n); // if n is > SIZE. Reduce n=SIZE if(n>SIZE) n=SIZE; create(array,n); } else { printf("Array is already created.cannot create again "); } break; case 2 : display(array, n); break; case 3 : printf("Enter the location you wish to insert an element=\n"); scanf("%d", &position); if(position>=SIZE || position>n+1) { printf("IT is not valid Position"); break; } printf("Enter the value to insert=\n"); scanf("%d", &value); n=insertAtPos(array, n, position, value); break; case 4 : printf("Enter the location you wish to delete an element=\n"); scanf("%d", &position); if(position>n) { printf("Postion is beyond the array element\n"); break; } if(n==0) { Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 23 printf("Array is empty\n"); break; } n=deleteFromPos(array, n, position); break; case 5 : return 0; default : printf(" Please enter correct choice"); break; } } return 0; } 18. Write a program to sort the numbers using selection and bubble sort algorithms. Ans:1) SELECTION SORT: #include <stdio.h> int main() { int array[100], n, i,j, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( i =


correct choice"); break; } } return 0; } 18. Write a program to sort the numbers using selection and bubble sort algorithms. Ans:1) SELECTION SORT: #include <stdio.h> int main() { int array[100], n, i,j, position, swap; printf("Enter number of elements\n"); scanf("%d", &n); printf("Enter %d integers\n", n); for ( i = 0 ; i< n ; i++ ) scanf("%d", &array[i]); for ( i = 0 ; i < ( n - 1 ) ; i++ ) { position = i; for ( j = i + 1 ; j < n ; j++ ) { if ( array[position] > array[j] ) position = j; } if ( position != i ) { swap = array[i]; array[i] = array[position]; array[position] = swap; } } printf("Sorted list in ascending order:\n"); for ( i = 0 ; i< n ; i++ ) printf("%d\n", array[i]); return 0; } Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 24 2)BUBBLE SORT: #include <stdio.h> int main() { int data[100],i,n,step,temp; printf("Enter the number of elements to be sorted: "); scanf("%d",&n); for(i=0;i<n;++i) { printf("%d. Enter element: ",i+1); scanf("%d",&data[i]); } for(step=0;step<n-1;++step) { for(i=0;i<n-step-1;++i) { if(data[i]>data[i+1]) /* To sort in descending order, change > to < in this line. */ { temp=data[i]; data[i]=data[i+1]; data[i+1]=temp; } } } printf("In ascending order: "); for(i=0;i<n;++i) printf("%d ",data[i]); return 0; } Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 25 Questions on Module I 1. What is Data structure? 2. Explain the classification of data structure 3. What are Data structure operations? 4. Explain declaration, initialization of one dimensional and 2 dimensional Arrays. 5. Discuss the operations that can be performed on the arrays. 6. Describe the representation of multidimensional arrays. 7. Describe the method to calculate the actual memory address of the array location of 1D, 2D. 8. Describe the method to calculate the actual memory address of the array location for 2D, 4D arrays 9. What strings? Explain the operations on strings. 10. What do you mean by pattern matching? Where do we use this concept? 11. Discuss user defined data types: structures and unions. 12. Explain structure within structures. 13. What are pointers? How do we use pointers to different data types? 14. Why do we need dynamic memory allocation techniques? Explain the functions available for allocating memory dynamically. 15. What is sparse Matrix? How to represent Sparse matrix? 16. What is polynomial? How to represent polynomial and add two polynomials? 17. Write an algorithm to transpose sparse matrix. 18. Define string. Explain string handling functions: strcat, strcpy, strcmp, strncmp, strncat, strchr, strrchr, strtok, strstr, strspn, strcspn, strbrk 19. Write an algorithm for pattern/string matching. 20. Develop structure to represent the planets in the solar system. Each planet has fields for planet’s name, its distance from the sun(in miles), and the number of moons it has. Place items in each the fields for the planets: Earth and Venus. 21. Write a program to search an element using linear and binary search. 22. Write a program


system. Each planet has fields for planet’s name, its distance from the sun(in miles), and the number of moons it has. Place items in each the fields for the planets: Earth and Venus. 21. Write a program to search an element using linear and binary search. 22. Write a program to sort the numbers using selection and bubble sort algorithms. 23. Write a program/algorithm to transpose sparse matrix. 24. Let A be an NxN square matrix array. Write an algorithm/function/program to a. Find the number nonzero elements in A b. Find the sum of the elements above diagonal (hint I<J) c. Find the product of the diagonal elements 25. Suppose multidimensional arrays A & B are declared using A(-2,;2,2;22) and B(1:8,-5:5.-10:5) a. Find the length of each dimension and the number of elements in A and B Module I-Arrays, Strings, Dr. Ganga Holi, ISE Dept., Global academy of Technology 26 b. Consider the element B[3,3,3] in B. Find the effective indices E1, E2,E3 and the address of the element assuming Base(B)=400 and there are w=4 words per memory location(size of the data). Hint: Formula for calculating address Li=upper bound-lower bound+1, where L is the number of elements in Column. For a given subscript Ki, the effective index Ei of Li is the number of indices preceding Ki in the index set and Ei can be calculated from Ei=Ki-lower bound Then the address LOC(K1, K2, K3…...Kn) of an ordinary element of C array can be obtained from the formula Base(C )+w[(((…(EnLn-1 +En-1 )Ln-2 + En-2 )Ln-3 + +E3)L2 + E2)L1 +E1) or from the formula Base( C )+ w[(…((E1L2 +E2 )L3 + E3 )L4 + ….+En-1)Ln+ En)


SIR M VISVESVARAYA INSTITUTE OF TECHNOLOGY (Affiliated to VTU, Recognized by AICTE and Accredited by NBA, NAAC and an ISO 9001-2008 Certified Institution) Bengaluru – 562157 DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING DATA STRUCTURES LABORATORY CHOICE BASEDCREDITSYSTEM BCSL305 - III Semester B.E (Academic Year 2023-24) Compiled and Prepared by: Mrs. Dr. Savita Choudhary Associate Professor Dept. of CSE Under the Guidance of: Dr. Anitha T N Professor & Head Dept. of CSE Department Vision and Mission VISION To build a center for imparting quality technical education and carrying out research activity to meet the current and future challenges in the domain of Computer Science and Engineering. MISSION  The Computer Science and Engineering department strives for excellence in teaching, applying, promoting and imparting knowledge through comprehensive academic curricula.  Train students to effectively apply the knowledge to solve real-world problems, thus enhance their potential for life-long high-quality career and give them a competitive advantage in the ever-changing and fast paced computing.  Prepare students to demonstrate a sense of societal and ethical responsibilities in their professional endeavors.  Creating amongst students and faculty a collaborative environment open to the free exchange of ideas, which leads to research activity and fuels innovation thinking. PROGRAM OUTCOMES PO's PO Description PO1 Engineering knowledge: Apply the knowledge of mathematics, science, engineering fundamentals, and an engineering specialization to the solution of complex engineering problems. PO2 Problem analysis: Identify, formulate, review research literature, and analyze complex engineering problems reaching substantiated conclusions using first principles of mathematics, natural sciences, and engineering sciences. PO3 Design/development of solutions: Design solutions for complex engineering problems and design system components or processes that meet the specified needs with appropriate consideration for the public health and safety, and the cultural, societal, and environmental considerations. PO4 Conduct investigations of complex problems: Use research-based knowledge and research methods including design of experiments, analysis and interpretation of data, and synthesis of the information to provide valid conclusions. PO5 Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern engineering and IT tools including prediction and modeling to complex engineering activities with an understanding of the limitations. PO6 The engineer and society: Apply reasoning informed by the contextual knowledge to assess societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the professional engineering practice. PO7 Environment and sustainability: Understand the impact of the professional engineering solutions in societal and environmental contexts, and demonstrate the knowledge of, and need for sustainable development. PO8 Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of the engineering practice. PO9 Individual and team work: Function effectively as an individual, and as a member or leader in diverse teams, and in multidisciplinary settings. PO10 Communication: Communicate effectively on complex engineering activities with the engineering community and with society at large, such as, being able to comprehend and write effective reports and design documentation, make effective presentations, and give and receive clear instructions. PO11 Project management and finance: Demonstrate knowledge and understanding


multidisciplinary settings. PO10 Communication: Communicate effectively on complex engineering activities with the engineering community and with society at large, such as, being able to comprehend and write effective reports and design documentation, make effective presentations, and give and receive clear instructions. PO11 Project management and finance: Demonstrate knowledge and understanding of the engineering and management principles and apply these to one’s own work, as a member and leader in a team, to manage projects and in multidisciplinary environments. PO12 Life-long learning: Recognize the need for, and have the preparation and ability to engage in independent and life-long learning in the broadest context of technological change. PROGRAM SPECIFIC OUTCOMES PSO's PSO Description PSO1 An ability to design and analyze algorithms by applying theoretical concepts to build complex and computer- based systems in the domain of System Software, Computer Networks & Security, Web technologies, Data Science and Analytics. PSO2 Be able to develop various software solutions by applying the techniques of Data Base Management, Complex Mathematical Models, Software Engineering practices and Machine Learning with Artificial Intelligence. DATA STRUCTURES LABORATORY SEMESTER – III Course Code BCSL305 CIE Marks 50 Number of Contact Hours/Week 0:0:2 SEE Marks 50 Total Number of Lab Contact Hours 28 Exam Hours 03 Credits – 1 Course Learning Objectives: This laboratory course enables students to get practical experience in design, develop, implement, analyze and evaluation/testing of ● Dynamic memory management ● Linear data structures and their applications such as stacks, queues and lists ● Non-Linear data structures and their applications such as trees and graphs Descriptions (if any): ● Implement all the programs in “C ” Programming Language and Linux OS. Programs List: 1. Develop a Program in C for the following: a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent 7 days of a week. Each Element of the array is a structure having three fields. The first field is the name of the Day (A dynamically allocated String), The second field is the date of the Day (A integer), the third field is the description of the activity for a particular day (A dynamically allocated String). b) Write functions create(), read() and display(); to create the calendar, to read the data from the keyboard and to print weeks activity details report on screen. 2. Develop a Program in C for the following operations on Strings. a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP) b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR Support the program with functions for each of the above operations. Don't use Built-in functions. 3. Develop a menu driven Program in C for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX) a. Push an Element on to Stack b. Pop an Element from Stack c. Demonstrate how Stack can be


the above operations. Don't use Built-in functions. 3. Develop a menu driven Program in C for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX) a. Push an Element on to Stack b. Pop an Element from Stack c. Demonstrate how Stack can be used to check Palindrome d. Demonstrate Overflow and Underflow situations on Stack e. Display the status of Stack f. Exit Support the program with appropriate functions for each of the above operations 4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands. 5. Develop a Program in C for the following Stack Applications a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %, ^ b. Solving Tower of Hanoi problem with n disks 6. Develop a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX) a. Insert an Element on to Circular QUEUE b. Delete an Element from Circular QUEUE c. Demonstrate Overflow and Underflow situations on Circular QUEUE d. Display the status of Circular QUEUE e. Exit Support the program with appropriate functions for each of the above operations 7. Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo a. Create a SLL of N Students Data by using front insertion. b. Display the status of SLL and count the number of nodes in it c. Perform Insertion / Deletion at End of SLL d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack) e. Exit 8. Develop a menu driven Program in C for the following operations on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,Sal, PhNo a. Create a DLL of N Employees Data by using end insertion. b. Display the status of DLL and count the number of nodes in it c. Perform Insertion and Deletion at End of DLL d. Perform Insertion and Deletion at Front of DLL e. Demonstrate how this DLL can be used as Double Ended Queue. f. Exit 9. Develop a Program in C for the following operationson Singly Circular Linked List (SCLL) with header nodes a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3 b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in POLYSUM(x,y,z) Support the program with appropriate functions for each of the above operations 10. Develop a menu driven Program in C for the following operations on Binary Search Tree (BST) of Integers .Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2 a. Traverse the BST in Inorder, Preorder and Post Order b. Search the BST for a given element (KEY)


in C for the following operations on Binary Search Tree (BST) of Integers .Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2 a. Traverse the BST in Inorder, Preorder and Post Order b. Search the BST for a given element (KEY) and report the appropriate message c. Exit 11. Develop a Program in C for the following operations on Graph(G) of Cities a. Create a Graph of N cities using Adjacency Matrix. b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method 12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are Integers. Develop a Program in C that uses Hash function H: K →L as H(K)=K mod m (remainder method), and implement hashing technique to map a given key K to the address space L. Resolve the collision (if any) using linear probing. Laboratory Outcomes: The student should be able to: ● Analyze various linear and non-linear data structures ● Demonstrate the working nature of different types of data structures and their applications ● Use appropriate searching and sorting algorithms for the give scenario. ● Apply the appropriate data structure for solving real world problems Conduct of Practical Examination: ● Experiment distribution o For laboratories having only one part: Students are allowed to pick one experiment from the lot with equal opportunity. o For laboratories having PART A and PART B: Students are allowed to pick one experiment from PART A and one experiment from PART B, with equal opportunity. ● Change of experiment is allowed only once and marks allotted for procedure to be made zero of the changed part only. ● Marks Distribution (Need to change in accordance with university regulations) c) For laboratories having only one part – Procedure + Execution + Viva-Voce: 15+70+15 = 100 Marks d) For laboratories having PART A and PART B i. Part A – Procedure + Execution + Viva = 6 + 28 + 6 = 40 Marks ii. Part B – Procedure + Execution + Viva = 9 + 42 + 9 = 60 Marks DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 1 1. Develop a Program in C for the following: a) Declare a calendar as an array of 7 elements (A dynamically Created array) to represent 7 days of a week. Each Element of the array is a structure having three fields. The first field is the name of the Day (A dynamically allocated String), The second field is the date of the Day (A integer), the third field is the description of the activity for a particular day (A dynamically


Each Element of the array is a structure having three fields. The first field is the name of the Day (A dynamically allocated String), The second field is the date of the Day (A integer), the third field is the description of the activity for a particular day (A dynamically allocated String). b) Write functions create(), read() and display(); to create the calendar, to read the data from the keyboard and to print weeks activity details report on screen. #include <stdio.h> #define MAX_DESCRIPTION_LENGTH 100 #define MAX_DAY_NAME_LENGTH 20 struct Day { char dayName[MAX_DAY_NAME_LENGTH]; int date; char activityDescription[MAX_DESCRIPTION_LENGTH]; }; struct Day weekCalendar[7]; void read() { for (int i = 0; i < 7; ++i) { printf("Enter day name for day %d: ", i + 1); scanf("%s", weekCalendar[i].dayName); printf("Enter date for day %d: ", i + 1); scanf("%d", &weekCalendar[i].date); printf("Enter activity description for day %d: ", i + 1); scanf(" %[^\n]s", weekCalendar[i].activityDescription); DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 2 } } void display() { printf("\n--- Calendar ---\n"); for (int i = 0; i < 7; ++i) { printf("Day %d: %s, Date: %d, Activity: %s\n", i + 1, weekCalendar[i].dayName, weekCalendar[i].date, weekCalendar[i].activityDescription); } } int main() { printf("Enter details for each day of the week:\n"); read(); display(); return 0; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 3 2. Develop a Program in C for the following operations on Strings. a. Read a main String (STR), a Pattern String (PAT) and a Replace String (REP) b. Perform Pattern Matching Operation: Find and Replace all occurrences of PAT in STR with REP if PAT exists in STR. Report suitable messages in case PAT does not exist in STR Support the program with functions for each of the above operations. Don't use Built-in functions. #include<stdio.h> #include<conio.h> void read(char *s) { gets(s); } void strcopy(char *s1,char *s2) { int i; for(i=0;s2[i]!='\0';i++) s1[i]=s2[i]; s1[i]='\0'; } int matchnreplace(char *str,char *pat,char *rep) { char ans[100]; int i=0,m=0,c=0,j=0,flag=0,k; while(str[c]!='\0') { if (str[m]==pat[i]) { i++; m++; if(pat[i]=='\0') { flag=1; for(k=0;rep[k]!='\0';k++,j++) ans[j]=rep[k]; i=0; c=m; } } else { ans[j]=str[c]; j++;c++;m=c;i=0; } } ans[j]='\0'; strcopy(str,ans); return flag; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 4 int main() { char str[100],pat[20],rep[20]; int flag=0; clrscr(); printf("Enter the string"); read(str); printf("Enter the pattern"); read(pat); printf("Enter the string to be replaced"); read(rep); flag=matchnreplace(str,pat,rep); if(flag==1) printf("The string is found %s",str); else printf("The pattern is not found"); return 0; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 5 3. Develop a menu driven Program in C for the following operations on STACK of Integers (Array Implementation of Stack with maximum size MAX) a. Push an Element on to Stack b. Pop an Element from Stack c. Demonstrate how Stack can be used to check Palindrome d. Demonstrate Overflow and Underflow situations on Stack e. Display the status of Stack f. Exit Support the program with appropriate functions for each of the above


a. Push an Element on to Stack b. Pop an Element from Stack c. Demonstrate how Stack can be used to check Palindrome d. Demonstrate Overflow and Underflow situations on Stack e. Display the status of Stack f. Exit Support the program with appropriate functions for each of the above operations. #include<stdio.h> #define MAX 10 int top=-1; int overflow() { if (top==MAX-1) return 1; else return 0; } int underflow() { if (top==-1) return 1; else return 0; } void push(int *s,int ele) { if(!overflow()) s[++top]=ele; else printf("stack overflow\n"); } int pop(int *s) { if(!underflow()) return(s[top--]); else printf("stack underflow\n"); return(0); } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 6 void display(int *s) { int i; if(underflow()) { printf("stack underflow\n"); return; } printf("The stack contents are:\n"); for(i=0;i<=top;i++) printf("%d\t",s[i]); } int palin(char *pal,int *s) { int i; top=-1; for(i=0;pal[i]!='\0';i++) push(s,pal[i]-'0'); for(i=0;pal[i]!='\0';i++) if (pal[i]!=(pop(s)+'0')) return 0; return 1; } int main() { int stack[MAX],ch,i,j,ele,p; char str[10]; while(1) { printf("\nEnter your choice:\n1:PUSH\n2:POP\n3:CHECK PALINDROME\n4:DISPALY\n5.EXIT\n"); scanf("%d",&ch); switch(ch) { case 1: printf("\nEnter the element to be pushed: n"); scanf("%d",&ele); push(stack,ele); break; case 2: ele=pop(stack); if(ele!=0) printf("\nThe deleted element is: %d\n",ele); break; case 3:printf("\nEnter the string "); fflush(stdin); gets(str); DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 7 p=palin(str,stack); if(p) printf("\nThe given string is Palindrome"); else printf("\nThe given string is not a palindrome"); break; case 4:display(stack); break; default:exit(0); } } return 0; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 8 4. Develop a Program in C for converting an Infix Expression to Postfix Expression. Program should support for both parenthesized and free parenthesized expressions with the operators: +, -, *, /, % (Remainder), ^ (Power) and alphanumeric operands. #include<stdio.h> char stack[100]; int top=-1; void push(char x) { stack[++top]=x; } char pop() { char ret; ret=stack[top]; stack[top]='\0'; top--; return ret; } int inper(char ch) { switch(ch) { case '+': case '-': return 1; case '*': case '/': return 3; } } int stper(char ch) { switch(ch) { case '+': case '-': return 2; case '*': case '/': return 4; case '(': return 0; case '#': return -1; } } void convert(char *exprn,char *postfix) DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 9 { int n=0,i=0,j=0; char ch; ch=exprn[i++]; while(ch!='#') { switch(ch) { case '(': push(ch); break; case ')': while(stack[top]!='(') postfix[j++]=pop(); pop(); break; case '+': case '-': case '*': case '/': while(stper(stack[top])>=inper(ch)) postfix[j++]=pop(); push(ch); break; default: postfix[j++]=ch; break; } printf("\nSTACK=%s POSTFIX = %s",stack,postfix); ch=exprn[i++]; } while(stack[top]!='#') postfix[j++]=pop(); postfix[j]='\0'; } void main() { char exprn[20]="",postfix[20]=""; int i; printf("\n Enter the postfix expn\t"); scanf("%s",exprn); strcat(exprn,"#"); push('#'); convert(exprn,postfix); printf("\n After Evaluation %s",postfix); } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 10 5. Develop a Program in C for the following Stack Applications a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^ b. Solving Tower of Hanoi problem with n disks. 5 a)


BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 10 5. Develop a Program in C for the following Stack Applications a. Evaluation of Suffix expression with single digit operands and operators: +, -, *, /, %,^ b. Solving Tower of Hanoi problem with n disks. 5 a) #include<stdio.h> #include<ctype.h> #include<stdlib.h> #include<string.h> int stack[100]; int top=-1; char exprn[20]; void push(int ele) { stack[++top]=ele; } int pop() { return (stack[top--]); } void compute(int opr1,char ch,int opr2) { switch(ch) { case '+' : push(opr1 + opr2); break; case '-' : push(opr1 - opr2); break; case '*' : push(opr1 * opr2); break; case '/' : if(opr2!=0) push(opr1/opr2); else { printf("\nDivide by Zero Error"); exit(0); } break; } } int eval() { int n=0,i=0,flag=1; char ch; int opr1,opr2; ch=exprn[i++]; while(ch!='#') { DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 11 printf("\n%c",ch); switch(ch) { case '+': case '-': case '*': case '/': opr2=pop(); opr1=pop(); printf("\n%d %d %c",opr1,opr2,ch); compute(opr1,ch,opr2); break; default: push(ch-'0'); } ch = exprn[i++]; } return pop(); } void main() { printf("\n Enter the postfix expn\t"); scanf("%s",exprn); strcat(exprn,"#"); printf("\n After Evaluation %d",eval()); } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 12 5 b) #include<stdio.h> #define MAX 10 typedef struct { int n; int from; int to; int auxi; int ret; // which case to return }stack; int top = -1; stack st[MAX]; int empty() { return top==-1; } stack pop() { return st[top--]; } void push(int n,int beg,int to,int aux,int ret) { top++; st[top].n = n; st[top].from = beg; st[top].to = to; st[top].auxi = aux; st[top].ret = ret; } void hanoi(int n,int beg,int aux, int end) { stack t; int nmoves=0; int ret=1,temp,i; while(1) { switch(ret) { case 1:if (n==1) { printf("\nMove %d: Move Disk %d from %c to %c",++nmoves,n,beg,end); ret=5; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 13 } else ret=2; break; case 2: push(n,beg,end,aux,3); --n; temp=end; end=aux; aux=temp; ret=1; break; case 3: printf("\nMove %d: Move Disk %d from %c to %c",++nmoves,n,beg,end); case 4: push(n,beg,end,aux,5); --n; temp=beg; beg=aux; aux=temp; ret=1; break; case 5: if(empty()) return; t=pop(); n=t.n; beg=t.from; aux=t.auxi; end=t.to; ret=t.ret; break; } /*printf("\n Stack Content"); for(i=top;i>=0;i--) printf("\n%d %d %d %d",st[i].n,st[i].from,st[i].to,st[i].auxi);*/ } } int main() { int n; printf("\nEnter number of disks"); scanf("%d",&n); hanoi(n,'A','C','B'); return 0; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 14 6. Develop a menu driven Program in C for the following operations on Circular QUEUE of Characters (Array Implementation of Queue with maximum size MAX) a. Insert an Element on to Circular QUEUE b. Delete an Element from Circular QUEUE c. Demonstrate Overflow and Underflow situations on Circular QUEUE d. Display the status of Circular QUEUE e. Exit Support the program with appropriate functions for each of the above operations #include<stdio.h> #include<stdlib.h> #define MAX_SIZE 5 int Queue[MAX_SIZE]; int front = -1; int rear = -1; int count = 0; int Qfull() { return count == MAX_SIZE; } int Qempty()


d. Display the status of Circular QUEUE e. Exit Support the program with appropriate functions for each of the above operations #include<stdio.h> #include<stdlib.h> #define MAX_SIZE 5 int Queue[MAX_SIZE]; int front = -1; int rear = -1; int count = 0; int Qfull() { return count == MAX_SIZE; } int Qempty() { return count == 0; } void insert(int ele) { rear=(rear+1) % MAX_SIZE; Queue[rear]=ele; count++; if(front == -1) front++; } int deleteq() { int ret; ret=Queue[front]; front=(front+1) % MAX_SIZE; count--; return(ret); } void display() { int i,j; if(Qempty()) printf("\n Queue is empty\n"); DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 15 else { j=front; printf("\n Elements in the Queue\n"); for(i=0;i<count;i++) { printf(" %d(%d)",Queue[j],j); j=(j+1) % MAX_SIZE; } } } void main() { int n=4,i,ch,ele,flag=1; while(flag) { printf("\n Menu \n"); printf("\n1 Insert\n2 Delete\n3 Display\n4 Exit\n Enter the Choice"); scanf("%d",&ch); switch(ch) { case 1: if(Qfull()) { printf("\nQueue Full"); } else { printf("\n Enter the value for insert\t"); scanf("%d",&ele); insert(ele); } break; case 2: if (!Qempty()) { ele = deleteq(); printf("\n%d",ele); } else printf("\nQueue Empty"); break; case 3: display(); break; default: flag = 0; } } } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 16 7. Develop a menu driven Program in C for the following operations on Singly Linked List (SLL) of Student Data with the fields: USN, Name, Programme, Sem, PhNo a. Create a SLL of N Students Data by using front insertion. b. Display the status of SLL and count the number of nodes in it c. Perform Insertion / Deletion at End of SLL d. Perform Insertion / Deletion at Front of SLL(Demonstration of stack) e. Exit #include<stdio.h> #include<stdlib.h> struct student { char USN[10]; char Name[20]; char Branch[15]; char Sem[3]; char PhNo[10]; }; typedef struct student STU; struct node { STU info; struct node *link; }; typedef struct node* Node; STU GetData() { STU temp; printf("Enter the usn: "); fflush(stdin); gets(temp.USN); printf("\nEnter the Name:"); fflush(stdin); gets(temp.Name); printf("\nEnter branch:"); fflush(stdin); gets(temp.Branch); printf("\nEnter sem:"); fflush(stdin); gets(temp.Sem); printf("\nEnter the phone number:"); fflush(stdin); gets(temp.PhNo); return temp; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 17 void displayRec(STU ele) { printf("\n%s\t\t",ele.USN); printf("%s\t\t",ele.Name); printf("%s\t",ele.Branch); printf("%s\t",ele.Sem); printf("%s\n",ele.PhNo); } Node Create(STU ele) { Node temp; temp=(Node)malloc(sizeof(struct node)); temp->info=ele; temp->link=NULL; return temp; } Node Finsert(Node first,STU ele) { Node temp=Create(ele); if(first== NULL) first=temp; else { temp->link=first; first = temp; } return first; } Node Einsert(Node first,STU ele) { Node temp=Create(ele),cur; if(first== NULL) first=temp; else { //for(cur=first;cur->link!=NULL;cur=cur->link); cur=first; while(cur->link!=NULL) cur=cur->link; cur->link=temp; } return first; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 18 void display(Node first) { Node temp=first; if(temp == NULL) printf("\nList is empty\n"); else { printf("\nStudent Details in the list\n"); printf("USN\t\tName\t\tBranch\tSem\tPhno\n"); while(temp!=NULL) { displayRec(temp->info); temp=temp->link; } } } int Count(Node first) { Node temp=first; int cnt=0; if(temp == NULL) printf("\nList is empty\n"); else { while(temp!=NULL) { cnt++; temp=temp->link; } } return cnt; } Node Fdelete(Node first) { Node temp=first; if(first==


printf("\nList is empty\n"); else { printf("\nStudent Details in the list\n"); printf("USN\t\tName\t\tBranch\tSem\tPhno\n"); while(temp!=NULL) { displayRec(temp->info); temp=temp->link; } } } int Count(Node first) { Node temp=first; int cnt=0; if(temp == NULL) printf("\nList is empty\n"); else { while(temp!=NULL) { cnt++; temp=temp->link; } } return cnt; } Node Fdelete(Node first) { Node temp=first; if(first== NULL) printf("\nList is empty"); else { temp=first; printf("\nElement Deleted is\n"); printf("USN\t\tName\t\tBranch\tSem\tPhno\n"); displayRec(temp->info); first = first->link; free(temp); } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 19 return first; } Node Edelete(Node first) { Node temp=first,t; if(first== NULL) printf("\nList is empty"); else if(first->link==NULL) { printf("\nElement Deleted is\n"); printf("USN\t\tName\t\tBranch\tSem\tPhno\n"); displayRec(first->info); first=NULL; } else { //for(temp=first;temp->link->link!=NULL;temp=temp->link); while(temp->link->link!=NULL) temp=temp->link; printf("\nElement Deleted is\n"); printf("USN\t\tName\t\tBranch\tSem\tPhno\n"); displayRec(temp->link->info); t = temp->link; temp->link=NULL; free(t); } return first; } void main() { Node first=NULL; int flag=1,ch; STU ele; while(flag) { printf("\n Menu \n"); printf("\n1 Front Insert\n2 End Insert\n3 Front Delete\n4 End Delete\n5 Display\n6 No. of Nodes in the list\n"); printf("7 Exit\n Enter the Choice"); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter the Student Details \t"); ele=GetData(); first=Finsert(first,ele); break; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 20 case 2: printf("\n Enter the Student Details \t"); ele=GetData(); first=Einsert(first,ele); break; case 3: first=Fdelete(first); break; case 4: first=Edelete(first); break; case 5: display(first); break; case 6: printf("\n the Number of Node in list %d\t",Count(first)); break; default: flag = 0; } } } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 21 8. Develop a menu driven Program in C for the following operations on Doubly Linked List (DLL) of Employee Data with the fields: SSN, Name, Dept, Designation,Sal, PhNo a. Create a DLL of N Employees Data by using end insertion. b. Display the status of DLL and count the number of nodes in it c. Perform Insertion and Deletion at End of DLL d. Perform Insertion and Deletion at Front of DLL e. Demonstrate how this DLL can be used as Double Ended Queue. f. Exit #include<stdio.h> #include<stdlib.h> struct Employee { char SSN[10]; char Name[10]; char Branch[10]; char Des[10]; char sal[10]; char phone[10]; }; typedef struct Employee EMP; struct node { EMP info; struct node *lptr,*rptr; }; typedef struct node* Node; Node front=NULL,rear=NULL; EMP GetRec() { EMP temp; printf("Enter the SSN: "); fflush(stdin); gets(temp.SSN); printf("Enter the Name:"); fflush(stdin); gets(temp.Name); printf("Enter branch:"); fflush(stdin); gets(temp.Branch); printf("Enter the designation: "); fflush(stdin); gets(temp.Des); printf("Enter sal:"); fflush(stdin); DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 22 gets(temp.sal); printf("Enter the phone number:"); fflush(stdin); gets(temp.phone); return temp; } void DispRec(EMP temp) { printf("%s\t",temp.SSN); printf("%s\t",temp.Name); printf("%s\t",temp.Branch); printf("%s\t",temp.Des); printf("%s\t",temp.sal); printf("%s\n",temp.phone); } Node Create(EMP ele) { Node temp; temp=(Node)malloc(sizeof(struct node)); temp->info=ele; temp->lptr=NULL; temp->rptr=NULL; return temp; } void Finsert(EMP ele) { Node temp=Create(ele); if(front== NULL) { front=temp; rear=temp; } else { temp->rptr=front; front->lptr=temp; front = temp; } } void Einsert(EMP ele) { Node temp=Create(ele); if(rear == NULL) { front=temp; rear=temp; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 23 } else { rear->rptr=temp; temp->lptr=rear;


{ Node temp=Create(ele); if(front== NULL) { front=temp; rear=temp; } else { temp->rptr=front; front->lptr=temp; front = temp; } } void Einsert(EMP ele) { Node temp=Create(ele); if(rear == NULL) { front=temp; rear=temp; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 23 } else { rear->rptr=temp; temp->lptr=rear; rear=temp; } } void Fdelete() { Node temp=front; if(front == NULL) printf("\nList is empty"); else { temp=front; printf("\nDeleted Employee Records \n"); DispRec(temp->info); front = front->rptr; front->lptr= NULL; if(front == NULL) rear = NULL; free(temp); } } void Edelete() { Node temp=rear,t; if(rear== NULL) printf("\nList is empty"); else if(rear->lptr == NULL) { printf("\nDeleted Employee Records \n"); DispRec(rear->info); front=rear=NULL; free(temp); } else { rear = rear->lptr; rear->rptr=NULL; printf("\nDeleted Employee Records \n"); DispRec(temp->info); free(temp); } } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 24 void display(Node front) { Node temp=front; if(temp == NULL) printf("\nList is empty\n"); else { printf("\nEmployee Records list\n"); while(temp!=NULL) { DispRec(temp->info); temp=temp->rptr; } } } void main() { EMP ele; int flag=1,ch; while(flag) { printf("\n Menu Implementation of Double Ended Queue using DLL\n"); printf("\n1 Insert Front \n2 Insert rear \n3 Delete Front \n4 Delete Rear \n5 Display\n"); printf("6 Exit\n Enter the Choice"); scanf("%d",&ch); switch(ch) { case 1: printf("\n Enter the Employee Record to Insert Front of Queue\n"); ele = GetRec(); Finsert(ele); break; case 2: printf("\n Enter the Employee Record to Insert Front of Queue\n"); ele=GetRec(); Einsert(ele); break; case 3: Fdelete(); break; case 4: Edelete(); break; case 5: display(front); break; default: flag = 0; } } } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 25 9. Develop a Program in C for the following operationson Singly Circular Linked List (SCLL) with header nodes a. Represent and Evaluate a Polynomial P(x,y,z) = 6x2y2z-4yz5+3x3yz+2xy5z-2xyz3 b. Find the sum of two polynomials POLY1(x,y,z) and POLY2(x,y,z) and store the result in POLYSUM(x,y,z). Support the program with appropriate functions for each of the above operations #include <stdio.h> #include <stdlib.h> #include <math.h> struct term { int coeff; int pow_x; int pow_y; int pow_z; }; typedef struct term TERM; struct node { TERM info; struct node* next; }; typedef struct node* Node; Node Create(TERM t) { Node temp = (Node)malloc(sizeof(struct node)); temp->info=t; temp->next = temp; return temp; } Node MKHeader() { TERM x; Node temp; x.coeff=-1; x.pow_x=-1; x.pow_y=-1; x.pow_z=-1; temp=Create(x); return temp; } Node Insert(Node p,TERM t) { Node temp = Create(t),cur=p; while(cur->next!=p) cur=cur->next; cur->next=temp; temp->next=p; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 26 return p; } double Compute(Node temp,int x,int y,int z) { double ret; TERM t=temp->info; ret=t.coeff * pow(x, t.pow_x) * pow(y, t.pow_y) * pow(z,t.pow_z); return ret; } double Evaluate(Node p, int x, int y, int z) { Node po = p->next; double sum = 0; while (po!=p) { sum += Compute(po,x,y,z); po = po->next; } return sum; } void DispTerm(TERM t) { printf(" %dx^%dy^%dz^%d ", t.coeff, t.pow_x, t.pow_y, t.pow_z); } void PrintPoly(Node p) { Node po = p->next; while (po!=p) { DispTerm(po->info); if(po->next!=p) printf("+");


int z) { Node po = p->next; double sum = 0; while (po!=p) { sum += Compute(po,x,y,z); po = po->next; } return sum; } void DispTerm(TERM t) { printf(" %dx^%dy^%dz^%d ", t.coeff, t.pow_x, t.pow_y, t.pow_z); } void PrintPoly(Node p) { Node po = p->next; while (po!=p) { DispTerm(po->info); if(po->next!=p) printf("+"); po = po->next; } printf("\n"); } void ReadPoly(Node t1,int n) { TERM t; int i; for(i=1;i<=n;i++) { printf("Enter the value of coefficent and powers of x,y and z"); scanf("%d%d%d%d",&t.coeff,&t.pow_x,&t.pow_y,&t.pow_z); t1 = Insert(t1, t); } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 27 PrintPoly(t1); } int ComparePower(TERM m,TERM n) { if(m.pow_x==n.pow_x && m.pow_y==n.pow_y && m.pow_z==n.pow_z) return 1; else return 0; } TERM AddTerms(TERM m,TERM n) { TERM temp; temp.coeff=m.coeff+n.coeff; temp.pow_x = m.pow_x; temp.pow_y = m.pow_y; temp.pow_z = m.pow_z; return temp; } Node AddPoly(Node p1,Node p2) { Node Newlist=MKHeader(); Node t1=p1->next,t3; Node t2=p2->next,t4; TERM res; int i,flag; t3=t1; t4=t2; while(t1!=p1) { t2=p2->next; flag=1; while(t2!=p2 && flag) { if(ComparePower(t1->info,t2->info)) { res=AddTerms(t1->info,t2->info); Newlist=Insert(Newlist,res); flag=0; } t2=t2->next; } if (flag==1) Newlist=Insert(Newlist,t1->info); t1=t1->next; } while(t4!=p2) DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 28 { t3=Newlist->next; flag=1; while(t3!=Newlist && flag) { if(ComparePower(t3->info,t4->info)) flag=0; t3=t3->next; } if (flag==1) Newlist=Insert(Newlist,t4->info); t4=t4->next; } return Newlist; } int main() { int n,x,y,z,ch,i,coeff; Node polysum; Node poly1 = MKHeader(); Node poly2 = MKHeader(); while(1) { printf("\nMenu\n 1:Evaluate Polynomial \n 2:Add\n 3:Exit\n Enter your choice\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the terms in the polynomial"); scanf("%d",&n); ReadPoly(poly1,n); printf("Enter the values of x,y and z"); scanf("%d%d%d",&x,&y,&z); printf("%.2f\n", Evaluate(poly1, x, y, z)); break; case 2: printf("Enter the terms in the polynomial 2"); scanf("%d",&n); ReadPoly(poly2,n); polysum = AddPoly(poly1, poly2); PrintPoly(polysum); break; case 3: exit(0); } } return 0; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 29 10. Develop a menu driven Program in C for the following operations on Binary Search Tree (BST) of Integers . a. Create a BST of N Integers: 6, 9, 5, 2, 8, 15, 24, 14, 7, 8, 5, 2 b. Traverse the BST in Inorder, Preorder and Post Order c. Search the BST for a given element (KEY) and report the appropriate message d. Exit #include<stdio.h> struct node { struct node *left; int data; struct node *right; }; typedef struct node* Node; Node newNode(int item) { Node temp = (Node)malloc(sizeof(struct node)); temp->data = item; temp->left = temp->right = NULL; return temp; } Node insert(Node node, int key) { if (node == NULL) return newNode(key); if (key < node->data) node->left = insert(node->left, key); else if (key > node->data) node->right = insert(node->right, key); DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 30 return node; } int search(Node root, int key) { if (root == NULL) return -1; if(root->data == key) return 1; if (root->data < key) return search(root->right, key); return search(root->left, key); } void inorder(Node root) { if (root != NULL) { inorder(root->left); printf("%d \t", root->data); inorder(root->right); } } void preorder(Node root) {


node; } int search(Node root, int key) { if (root == NULL) return -1; if(root->data == key) return 1; if (root->data < key) return search(root->right, key); return search(root->left, key); } void inorder(Node root) { if (root != NULL) { inorder(root->left); printf("%d \t", root->data); inorder(root->right); } } void preorder(Node root) { if (root != NULL) { printf("%d \t", root->data); preorder(root->left); DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 31 preorder(root->right); } } void postorder(Node root) { if (root != NULL) { postorder(root->left); postorder(root->right); printf("%d \t", root->data); } } int main() { int n,i,ch,ch1,key,pos; Node root=NULL; printf("Enterthe no of nodes in the BST\n"); scanf("%d",&n); for(i=1;i<=n;i++) { printf("Enter the element to be iserted\n"); scanf("%d",&key); root=insert(root,key); } while(1) { printf("\nEnter the choice\n1: Insert Node\n2: Traversal\n3: Search for key\n4: Exit\n"); DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 32 scanf("%d",&ch); switch(ch) { case 1: printf("Enter the element to be iserted\n"); scanf("%d",&key); root=insert(root,key); break; case 2: printf("Enter your choice\n1: Preorder\n2: Inorder\n3: Postorder\n"); scanf("%d",&ch1); switch(ch1) { case 1: preorder(root); break; case 2: inorder(root); break; case 3: postorder(root); break; default: printf("\n Make Correct Choice"); } break; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 33 case 3: printf("Enter the key to be searched\n"); scanf("%d",&key); pos=search(root,key); if (pos==-1) printf("\n Key is not found\n"); else printf("\n Key is found\n"); break; case 4: exit(0); } } return 0; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 34 11. Develop a Program in C for the following operations on Graph(G) of Cities a. Create a Graph of N cities using Adjacency Matrix. b. Print all the nodes reachable from a given starting node in a digraph using DFS/BFS method #include<stdio.h> #include<conio.h> int count=0; void creategraph(int n,int a[10][10]) { int i,j; printf("enter the adjacency matrix\n"); for(i=1;i<=n;i++) for(j=1;j<=n;j++) scanf("%d",&a[i][j]); } void bfs(int a[10][10], int n, int source,int s[]) { int f,r,q[10],u,v,i; printf("enter the source vertex\n"); scanf("%d",&source); printf("the nodes reachable are\n"); for(i=1;i<=n;i++) s[i]=0; f=r=0; q[r]=source; s[source]=1; while(f<=r) { u=q[f++]; for(v=1;v<=n;v++) { if(a[u][v]==1 && s[v]==0) { printf("\n%d",v); s[v]=1; q[++r]=v; } } } } void dfs(int a[10][10],int n,int vis[],int v) { int i; count++; vis[v]=count; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 35 for(i=1;i<=n;i++) { if(vis[i]==0 && a[v][i]==1) dfs(a,n,vis,i); } } int main() { int a[20][20],n,source,s[10],i,j,vis[10]; int ch; while(1) { printf("\nEnter your choice\n1:Create graph\n2:Check Reachability\n3:Check Connectivity\n4.Exit\n"); scanf("%d",&ch); switch(ch) { case 1: printf("Enter the number of nodes:"); scanf("%d",&n); creategraph(n,a); break; case 2: bfs(a,n,source,s); break; case 3:for(i=1;i<=n;i++) vis[i]=0; dfs(a,n,vis,1); if(count==n) printf("the graph is connected\n"); else printf("the graph is not connected\n"); break; case 4: exit(0); } } return 0; } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 36 12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations with L as


and Engineering, Sir MVIT Page | 36 12. Given a File of N employee records with a set K of Keys (4-digit) which uniquely determine the records in file F. Assume that file F is maintained in memory by a Hash Table (HT) of m memory locations with L as the set of memory addresses (2-digit) of locations in HT. Let the keys in K and addresses in L are Integers. Develop a Program in C that uses Hash function H: K →L as H(K)=K mod m (remainder method), and implement hashing technique to map a given key K to the address space L. Resolve the collision (if any) using linear probing. #include<stdio.h> #include<stdlib.h> #definre max 100 int create(int num) { int key; key=num%100; return key; } void linear_prob(int a[max],int key,int num) { int flag=0,i,count=10; if(a[key]==-1) a[key]=num; else { for(i=10;i<max;i++) { if(a[i]!=-1) count++; } if(count==max) { printf(“\n hash table is full\n”); display(a); exit(1); } for(i=key+1;i<max;i++) if(a[i]==-1) { a[i]=num; flag=1;break; } for(i=10;i<key&&flag==0;i++) if(a[i]==-1) { a[i]=num; flag=1;break; DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 37 } } } void display(int a[max]) { int i; printf(“\n the hash table is…\n”); printf(“key/index\t employee id\n”); printf(“………………………………………..\n”); for(i=10;i<max;i++) printf(“\n %d %d\n”, i, a[i]); } int main() { int a[max],num,key,i; int ans; printf(“collision handling by linear probing\n”); for(i=10;i<max;i++) a[i]=-1; do { printf(“\n enter the four digit number:”); scanf(“%d”,&num); key=create(num); linear_pron(a,keyt,num); printf(“do you want to continue?\n”); scanf(“%d”,&ans); }while(ans==1); display(a); } DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 38 VIVA-VOICE QUESTIONS 1. What is a pointer on pointer? It’s a pointer variable which can hold the address of another pointer variable. It de-refers twice to point to the data held by the designated pointer variable. Eg:int x =5,*p=&x,**q=&p; Therefore ‘x’ can be accessed by **q. 2. Distinguish between malloc() & calloc() memory allocation. Both allocates memory from heap area/dynamic memory. By default calloc fills the allocated What is a constant pointer? A pointer which is not allowed to be altered to hold another address after it is holding one. 3. What is a static variable? A static local variables retains its value between the function call and the default value is 0. The following function will print 1 2 3 if called thrice. 4. What is a NULL pointer? A pointer pointing to nothing is called so. Eg: char *p=NULL; What is the purpose of extern storage specifier? Used to resolve the scope of global symbol. Eg: main(){ externint i; Printf(“%d”,i); } int i =20; 5. Explain the purpose of the function sprintf(). Prints the formatted output onto the character array. 6. What is the meaning of base address of the array? The starting address of the array is called as the base address of the array. 7. When should we use the register storage specifier? If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up


of the array is called as the base address of the array. 7. When should we use the register storage specifier? If a variable is used most frequently then it should be declared using register storage specifier, then possibly the compiler gives CPU register for its storage to speed up the look up of the variable. DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 39 8. What is a dangling pointer? A pointer initially holding valid address, but later the held address is released or freed. Then such a pointer is called as dangling pointer. 9. What is the purpose of the keyword typedef? It is used to alias the existing type. Also used to simplify the complex declaration of the type. 10. What is the difference between actual and formal parameters? The parameters sent to the function at calling end are called as actual parameters while at the receiving of the function definition called as formal parameters. 11. Can a program be compiled without main() function? Yes, it can be but cannot be executed, as the execution requires main() function definition. 12. What is the advantage of declaring void pointers? When we do not know what type of the memory address the pointer variable is going to hold, then we declare a void pointer for such. 13. Where an automatic variable is stored? Every local variable by default being an auto variable is stored in stack memory. 14. What is a nested structure? A structure containing an element of another structure as its member is referred so. 15. What is the difference between variable declaration and variable definition? Declaration associates type to the variable whereas definition gives the value to the variable. 16. What is a self-referential structure? A structure containing the same structure pointer variable as its element is called as self-referential structure. 17. Does a built-in header file contain built-in function definition? No, the header file only declares function. The definition is in library which is linked by the linker. 18. Explain modular programming. Dividing the program in to sub programs (modules/function) to achieve the given task is modular approach. More generic functions definition gives the ability to re-use the functions, such as built-in library functions. 19. What is a token? A C program consists of various tokens and a token is either a keyword, an identifier, a constant, a string literal, or a symbol. DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 40 20. What is a constant pointer? A pointer which is not allowed to be altered to hold another address after it is holding one. 21. Describe stack operation. Stack is a data structure that follows Last in First out strategy. StackOperations:- Push – Pushes (inserts) the element in the stack. The location is specified by the pointer. Pop – Pulls (removes) the element out of the stack. The location is specified by the pointer Swap: – the two top most elements


data structure that follows Last in First out strategy. StackOperations:- Push – Pushes (inserts) the element in the stack. The location is specified by the pointer. Pop – Pulls (removes) the element out of the stack. The location is specified by the pointer Swap: – the two top most elements of the stack can be swapped Peek: – Returns the top element on the stack but does not remove it from the stack Rotate:- the topmost (n) items can be moved on the stack in a rotating fashion A stack has a fixed location in the memory. When a data element is pushed in the stack, the pointer points to the current element. 22. Discuss how to implement queue using stack. A queue canbe implemented by using 2 stacks:- 1. An element is inserted in the queue by pushing it into stack1 2. An element is extracted from the queue by popping it from the stack2 3. If the stack 2 is empty then all elements currently in stack 1 are transferred to stack 2 but in thereverseorder 4. If the stack 2 is not empty just pop the value from stack 2. 23. What is data structure? Data structures refers to the way data is organized and manipulated. It seeks to find ways to make data access more efficient. When dealing with data structure, we not only focus on one piece of data, but rather different set of data and how they can relate to one another in an organized manner. 24. What is a queue? A queue is a data structures that can simulates a list or stream of data. In this structure, new elements are inserted at one end and existing elements are removed from the other end. 25. What are binary trees? A binary tree is one type of data structure that has two nodes, a left node and a right node. In programming, binary trees are actually an extension of the linked list structures. 26. Which data structures is applied when dealing with a recursive function? Recursion, which is basically a function that calls itself based on a terminating condition, makes use of the stack. Using LIFO, a call to a recursive function saves the return address so that it knows how to return to the calling function after the call terminates. 27. What is a stack? A stack is a data structure in which only the top element can be accessed. As data is stored in the stack, each data is pushed downward, leaving the most recently added data on top. DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 41 28. Explain Binary Search Tree A binary search tree stores data in such a way that they can be retrieved very efficiently. The left subtree contains nodes whose keys are less than the node’s key value, while the right subtree contains nodes whose keys are greater than or equal to the node’s key value. Moreover, both subtrees are


tree stores data in such a way that they can be retrieved very efficiently. The left subtree contains nodes whose keys are less than the node’s key value, while the right subtree contains nodes whose keys are greater than or equal to the node’s key value. Moreover, both subtrees are also binary search trees. 29. What are doubly linked lists? Doubly linked lists are a special type of linked list wherein traversal across the data elements can be done in both directions. This is made possible by having two links in every node, one that links to the next node and other one that links to the previous node. 30. What is Huffman’s algorithm? Huffman’s algorithm is associated in creating extended binary trees that has minimum weighted path lengths from the given weights. It makes use of a table that contains frequency of occurrence for each data element. 31. What is Fibonacci search? Fibonacci search is a search algorithm that applies to a sorted array. It makes use of a divide-and-conquer approach that can greatly reduce the time needed in order to reach the target element. 32. Briefly explain recursive algorithm. Recursive algorithm targets a problem by dividing it into smaller, manageable sub-problems. The output of one recursion after processing one sub-problem becomes the input to the next recursive process. 33. What is the minimum number of queues needed to implement the priority queue?Two. One queue is used for the actual storing of data, and the other one is used for storing the priorities. 34. Which data structure is used to perform recursion? The answer is Stack. Stack has the LIFO (Last In First Out) property; it remembers it’s ‘caller’. Therefore, it knows to whom it should return when the function has to return. On the other hand, recursion makes use of the system stack for storing the return addresses of the function calls. Every recursive function has its equivalent iterative (non-recursive) function. Even when such equivalent iterative procedures are written explicit, stack is to be used. 35. What are some of the applications for the tree data structure? 1-Manipulation of the arithmetic expressions. 2-Symbol table construction. 3- Syntax analysis. 34. How does dynamic memory allocation help in managing data? Aside from being able to store simple structured data types, dynamic memory allocation can combine separately allocated structured blocks to form composite structures that expand and contract as needed. DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 42 37. What is FIFO? FIFO is short for First-in, First-out, and is used to represent how data is accessed in a queue. Data has been inserted into the queue list the longest is the one that is removed first. 38. What is an ordered list? An ordered list is a list in which each node’s position in the list is determined by the value of its key component, so that the key values form an increasing sequence, as the list is traversed. 39. What is merge


is removed first. 38. What is an ordered list? An ordered list is a list in which each node’s position in the list is determined by the value of its key component, so that the key values form an increasing sequence, as the list is traversed. 39. What is merge sort? Merge sort takes a divide-and-conquer approach to sorting data. In a sequence of data, adjacent ones are merged and sorted to create bigger sorted lists. These sorted lists are then merged again to form an even bigger sorted list, which continuous until you have one single sorted list. 40. What is the difference between a PUSH and a POP? Pushing and popping applies to the way data is stored and retrieved in a stack. A push denotes data being added to it, meaning data is being “pushed” into the stack. On the other hand, a pop denotes data retrieval, and in particular refers to the topmost data being accessed. 41. What is a linear search? A linear search refers to the way a target key is being searched in a sequential data structure. Using this method, each element in the list is checked and compared against the target key, and is repeated until found or if the end of the list has been reached. 42. How does variable declaration affect memory allocation? The amount of memory to be allocated or reserved would depend on the data type of the variable being declared. For example, if a variable is declared to be of integer type, then 32 bits of memory storage will be reserved for that variable. 43. What is the advantage of the heap over a stack? Basically, the heap is more flexible than the stack. That’s because memory space for the heap can be dynamically allocated and de-allocated as needed. However, memory of the heap can at times be slower when compared to that stack. 44. What is a postfix expression? A postfix expression is an expression in which each operator follows its operands. The advantage of this form is that there is no need to group sub-expressions in parentheses or to consider operator precedence. 45. What is data structure? Data structures refers to the way data is organized and manipulated. It seeks to find ways to make data access more efficient. When dealing with data structure, we not only focus on one piece of data, but rather different set of data and how they can relate to one another in an organized manner. DATA STRUCTURES LABORATORY BCSL305 Department of Computer Science and Engineering, Sir MVIT Page | 43 46. Differentiate file structure from storage structure. Basically, the key difference is the memory area that is being accessed. When dealing with the structure that resides the main memory of the computer system, this is referred to as storage structure. When dealing with an auxiliary structure, we refer to it as file structures.w they can relate to one another in an organized manner. 47. What is a linked list?