Data Structures Lab INDEX Exp. No Topic Page no 1 Implementation of Polynomials and Sparse matrices using arrays. 2 Implementation of Stack , Queues, Priority Queues, DEQUEUE and Circular Queues using arrays 3 Application problems using stacks: Conversion of expression from one notation to another notation 4 Implementation of various linked list operations. 5 Representation of polynomials using linked list, addition and multiplication of polynomials. 6 Implementation of binary trees using linked lists and arrays - creations, insertion, deletion and traversal. 7 Implementation of sorting algorithms – bubble, insertion, selection, quick, merge sort and heap sort. 8 Implementation of searching algorithms – linear search, binary search. 9 Implementation of BFS and DFS for each graph representations. 10 Implementation of hash table using your own mapping functions and observe collisions and overflow resolving schemes. Data Structures Lab Experiment No: 1 Implementation of Polynomials and Sparse matrices using arrays. 1.a POLYNOMIAL ADDITION PROBLEM DEFINITION: Write a program to read two polynomials and store them in an array. Calculate the sum of the two polynomials and display the first polynomial, second polynomial and the resultant polynomial. ALGORITHM: Algorithm for main( ) Step 1: Start Step 2: Read coefficient &exponent of 2 polynomials Step 3: Display the 2 polynomials. Step 4: Call function polyadd(af,al,bf,bl,free) Step 5: Stop Algorithm for polyadd(af,al,bf,bl,free) Step 1: Declare all the variables. Step 2: Repeat steps 3 to 8 till all terms are checked. i.e. (p<=al)&&(q<=bl) Step 3: Compare the exponents using function compare(term[p].exp,term[q].exp) Step 4: Check ( term[p].exp = term[q].exp ) Step 4.1: sum = term[p].coeff + term[q].coeff Step 4.2: check (sum!=0) free =newterm(sum,term[p].exp,free), p++, q++ Step 4.3: beak Step 5: Else check term[p].exp > term[q].exp Step 5.1: free =newterm(term[p].coeff,term[p].exp,free), p++ Step 5.2: break Step 6: Else check term[p].exp < term[q].exp Step 6.1: free = newterm(term[q].coeff,term[q].exp,free), q++ Step 6.2 : break Step 7: Repeat step 7 till p<=al Step 7.1: free = newterm(term[p].coeff,term[p].exp,free), p++ Step 8 : Repeat step 8 till q<=bl Step 8.1: free = newterm(term[q].coeff,term[q].exp,free) Data Structures Lab Step 9: Print the resultant polynomial. Algorithm for compare(int a,int b) Step 1: Check a = b, if true return ‘=’ Step 2: Check a > b, if true return ‘>’ Step 3: Check a < b, if true return ‘<’ Algorithm for newterm(int a, int b,int fsize ) Step 1: check ( fsize >= max ) Step 1.1: print space insufficient and exit from pgm. Step 2: else Step 2.1: term[fsize].coeff = a; term[fsize].exp = b; fsize++; return (fsize); PROGRAM : #include<stdio.h> #include<stdlib.h> #define MAX 30 struct poly { int coeff; int exp; }term[MAX]; void polyadd(int af,int al,int bf,int bl,int free); char compare(int a, int b); int newterm(int a, int b,int fsize); void main() { int i,j,num1,num2,free; int af,al,bf,bl; printf("Enter the number of terms of the first polynomial \ n"); scanf("%d",&num1); printf("Enter the coefficents and exponents of the first polynomial \ n"); for(i=0; i<num1; i++) { scanf("%d",&term[i].coeff); scanf("%d",&term[i].exp); } printf("Enter the number of terms of the second poly nomial \ n"); scanf("%d",&num2); free = (num1+num2); printf("Enter the coefficents and exponents of the second polynomial \ n"); Data Structures Lab for(i=num1; i<free; i++) { scanf("%d",&term[i].coeff); scanf("%d",&term[i].exp); } printf("Entered polynomials are: \ n"); i=0; while(i<num1) { printf("%dx^%d ",term[i].coeff,term[i].exp); i++; if(i==num1) break; printf("+ "); } printf(" \ n"); i=num1; while(i<free) { printf("%dx^%d ",term[i].coeff,term[i].exp); i++; if(i==free) break; printf("+ "); } printf(" \ n"); af=0;al=num1 - 1;bf=num1;bl=free - 1; polyadd(af,al,bf,bl,free); } void polyadd(int af,int al,int bf,int bl,int free) { int p,i,q,e,sum=0,free1; free1 = free; // copying the initial value of free into free1 p = af; / / copying first position of fir st polynomial to p, so that original position is not lost q = bf; / / copying first position of second polynomial to q, so that ori ginal position is not lost while((p<=al)&&(q<=bl)) // p and q used for traversing in the structure { switch(compare(term[p].exp,term[q].exp)) // comparing both terms { case '=': { sum = term[p].coeff + term[q].coeff; if(sum!=0) // If sum of two coefficients is zero than no need for displaying it { free =newterm(sum,term[p].exp,free);/ / can pass either p or q because both same p++; // for next element q++; // for next element Data Structures Lab } break; } case '>': // term[p].exp > term[q].exp { free =newterm(term[p].coeff,term[p].exp,free); p++; break; } case '<': // term[p].exp < term[q].exp { free = newterm(term[q].coeff,term[q].exp,free); q++; break; } } } while(p<=al) { free = newterm(term[p].coeff,term[p].exp,free); p++; } while(q<=bl) { free = newterm(term[q].coeff,term[q].exp,free); q ++; } printf("Resultant Polynomial is: \ n"); i=free1; // orignal value of free while(i<free) { printf("%dx^%d ",term[i].coeff,term[i].exp); i++; if(i==free) break; printf("+ "); } printf(" \ n"); } char compare(int a,int b) { if(a==b) return '='; else if(a>b) return '>'; else Data Structures Lab return '<'; } int newterm(int a, int b,int fsize ) { if (fsize >= MAX) { printf("Space is insufficent \ n"); exit(0); } else { term[fsize].coeff = a; term[fsize].exp = b; fsize++; return fsize; } } OUTPUT: Enter the number of terms of the first polynomial 3 Enter the coeffic i ents and exponents of the first polynomial 2 1000 5 3 1 0 Enter the number of terms of the second polynomial 4 Enter the coeffic i ents and exponents of the second polynomial 1 4 10 3 3 2 1 0 Entered polynomials are: Data Structures Lab 2x^1000 + 5x^3 + 1x^0 1x^4 + 10x^3 + 3x^2 + 1x^0 Resultant Polynomial is: 2x^1000 + 1x^4 + 15x^3 + 3x^2 + 2x^0 CONCLUSION: The algorithm was developed and the program was coded. The program run successfully. Data Structures Lab 1.b SPARSE MATRICES PROBLEM DEFINITION: Write a program to en ter two matrices in normal form . Write a function to convert two matrices to tuple form and display it. Also find the transpose of the two matrices represented in tuple form and display it. Find the sum of the two matrices in tuple form and display the sum in tuple form. ALGORITHM: Algorithm for main( ) Step 1: Start Step 2: Read number of rows and columns of matrix A and B Step 3: Read elements of matrix A and B Step 4: Display the 2 matixes. Step 5: Call function sparse(row,col) Step 6: Call function add( ) Step 7: Stop Algorithm for sparse(row,col) Step 1: Declare all the variables. Step 2: Assign sparsea[0][0] = row sparsea[0][1] = col, count A =0, n=1 Step 3: In a loop check if elements of matrix A is non - zero. Step 3.1: sparsea[n][0] = i sparsea[n][1] = j sparsea[n][2] = a[i][j] n++ count A ++ Step 4: Assign sparsea[0][2] = count A Step 5: Print the sparse matrix A. Step 6: Assign sparseb[0][0] = row sparseb[0][1] = col, count B =0, n=1 Step 7: In a loop check if elements of matrix B is non - zero. Step 7.1: sparseb[n][0 ] = i sparseb[n][1] = j sparseb[n][2] = b[i][j] n++ count B ++ Step 8: Assign sparseb[0][2] = count B Data Structures Lab Step 9: Print the sparse matrix B. Step 10: Call function transpose(count A ). Step 10: Call function transpose(count B ). Algorithm for transpose(count) Step 1 : Declare all variables. Step 2: Assign at[0][0] = sparsea[0][1] at[0][1] = sparsea[0][0] at[0][2] = sparsea[0][2] Step 3: In ‘i’ loop, repeat step 3.1 till ( i<sparsea[0][1]) Step 3.1: In ‘j’ loop, repeat step 3.1.1 till (j<=sparsea[0][2]) else step 3 Step 3.1.1: Check (sparsea[j][1]==i), if true go to ste p 3.1.2 else step 3.1 Step 3.1.2: Assign at[k][0] = sparsea[j][1] at[k][1] = sparsea[j][0] at[k][2] = sparsea[j][2] k++ Step 4: Print transpose of sparse matrix A Step 5: Assign bt[0][0] = sparseb[0][1] bt[0][1] = sparseb[0][0] bt[0][2] = sparseb[0][2] Step 6: In ‘i’ loop, repeat step 6.1 till ( i<sparseb[0][1]) Step 6.1: In ‘j’ loop, repeat step 6.1.1 till (j<=sparseb[0][2]) else step 6 Step 6.1.1: Check (sparseb[j][1]==i), if true go to step 6.1.2 else step 6.1 Step 6.1.2: Assign bt[k][0] = sparseb[j][1] bt[k][1] = sparseb[j][0] bt[k][2] = sparseb[j][2] k++ Step 7: Print transpose of sparse matrix B Algorithm for add() Step 1 : Declare variables. Step 2: Assign sum[0][0] = sparsea[0][0] sum[0][1] = sparsea[0][1] Step 3: Repeat step 3 till ((i<=sparsea[0][2])&&(j<=sparseb[0][2])) Step 3.1: check (sparsea[i][0] == sparseb[j][0]) if true step 3.2 else step 3.5 Step 3.2: check (sparsea[i][1] == sparseb[j][1]) if true step 3.3 else step 3.1 Step 3.3 : temp = sparsea[i][2] + sparseb[j][2] Check (temp!= 0) if true step 3.4 else step 3.2 Step 3.4: sum[s][0] = sparsea[i][0] sum[s][1] = sparsea[i][1] sum[s][2] = temp Data Structures Lab s++ i++ j++ Step 3.5: check (sparsea[i][1]<sparseb[j][1]) Step 3.5:1 sum[s][0] = sparsea[i][0] sum[s][1] = sparsea[i][1] sum[s][2] = sparsea[i][2] s++ i++ Step 3.6: check (sparsea[i][1]>sparseb[j][1]) Step 3.6:1 sum[s][0] = sparseb[j][0] sum[s][1] = sparseb[j][1] sum[s][2] = sparseb[j][2] s++ j++ Step 3.7: check (sparsea[i][0]<sparseb[j][0]) Step 3.7:1 sum[s][0] = sparseb[i][0] sum[s][1] = sparseb[i][1] sum[s][2] = sparseb[i][2] s++ i++ Step 3.8: check (sparse a[i][0]>sparseb[j][0]) Step 3.8:1 sum[s][0] = sparseb[j][0] sum[s][1] = sparseb[j][1] sum[s][2] = sparseb[j][2] s++ j++ Step 4: Repeat step 4.1 till (i<=sparsea[0][2]) Step 4.1: sum[s][0] = sparsea[i][0] sum[s][1] = sparsea[i][1] sum[s][2] = sparsea[i][2] s++ i++ Step 5: Repeat step 5.1 till (j<=sparseb[0][2]) Step 5.1: sum[s][0] = sparseb[j][0] sum[s][1] = sparseb[j][1] sum[s][2] = sparseb[j][2] s++ j++ Step 6: Assign sum[0][2] = s - 1 Step 7 : Print sparse matrix sum Data Structures Lab PROGRAM : # include <stdio.h> #include<conio.h> # define max 20 int a[max][max],at[max][max]; int b[max][max],bt[max][max]; int sum[20][3]; int sparsea[20][3]; int sparseb[20][3]; void sparse( int,int ); void transpose( int ); void add(); void main() { int i,j,row,col; clrscr(); printf("Enter the number of rows of the matri x \ n"); scanf("%d",&row); printf("Enter the number of columns of the matrix \ n"); scanf("%d",&col); printf("Enter the elements of Matrix A \ n"); for(i=0; i<row; i++) { for(j=0; j<col; j++) { scanf("%d",&a[i][j]); } } printf("Entered Matrix A: \ n"); for(i=0; i<row; i++) { for(j=0; j<col; j++) { printf("%d ",a[i][j]); } printf(" \ n"); } printf("Enter the elements of Matrix B \ n"); for(i=0; i<row; i++) { for(j=0; j<col; j++) { Data Structures Lab scanf("%d",&b[i][j]); } } printf("Entered Matrix B: \ n"); for(i=0; i<row; i++) { for(j=0; j<col; j++) { printf("%d ",b[i][j]); } printf(" \ n"); } sparse(row,col); add(); getch(); } void sparse(int row,int col) { int i,j,count A , countB, n; sparsea[0][0] = row; sparsea[0][1] = col; count A =0; // counter used to find number of non - zero elements n=1; // row index of sparse matrix for(i=0; i<row; i++) { for(j=0; j<col; j++) { if(a[i][j]!=0) // checks if element is a non - zero element { sparsea[n][0] = i; sparsea[n][1] = j; sparsea[n][2] = a[i][j]; n++; count A ++; } } } sparsea[0][2] = count A ; printf("Sparse Matrix A: \ n"); for(i=0; i<count A +1; i++) { for(j=0; j<3; j++) { printf("%d ",sparsea[i][j]); } printf(" \ n"); Data Structures Lab } sparseb[0][0] = row; sparseb[0][1] = col; count B =0; // counter used to find number of non - zero elements n=1; // row index of sparse matrix for(i=0; i<row; i++) { for(j=0; j<col; j++) { if(b[i][j]!=0) // checks if element is a non - zero element { sparseb[n][0] = i; sparseb[n][1] = j; sparseb[n][2] = b[i][j]; n++; cou nt B ++; } } } sparseb[0][2] = coun B t; printf("Sparse Matrix B: \ n"); for(i=0; i<count B +1; i++) { for(j=0; j<3; j++) { printf("%d ",sparseb[i][j]); } printf(" \ n"); } transpose(count A ); transpose(countB); } void transpose(int count) { int i,j,k=1; at[0][0] = sparsea[0][1]; at[0][1] = sparsea[0][0]; at[0][2] = sparsea[0][2]; for(i=0; i<sparsea[0][1]; i++) { for(j=0; j<=sparsea[0][2]; j++) { if(sparsea[j][1]==i) { at[k][0] = sparsea[j][ 1]; at[k][1] = sparsea[j][0]; Data Structures Lab at[k][2] = sparsea[j][2]; k++; } } } printf("Transposed Matrix A: \ n"); for(i=0; i<count+1; i++) { for(j=0; j<3; j++) { printf("%d ",at[i][j]); } printf(" \ n"); } k=1; bt[0][0] = sparseb[0][1]; bt[0][1] = sparseb[0][0]; bt[0][2] = sparseb[0][2]; for(i=0; i<spars eb[0][1]; i++) { for(j=0; j<=sparseb[0][2]; j++) { if(sparseb[j][1]==i) { bt[k][0] = sparseb[j][1]; bt[k][1] = sparseb[j][0]; bt[k][2] = sparseb[j][2]; k++; } } } printf("Transposed Matrix B: \ n"); for(i=0; i<count+1; i++) { for(j=0; j<3; j++) { printf("%d ",bt[i][j]); } printf(" \ n"); } } void add() { int i=1,j=1,s=1,temp=0,non=0; // row index (s) sum[0][0] = sparsea[0][0]; sum[0][1] = sparsea[0][1]; Data Structures Lab while((i<=sparsea[0][2])&&(j<=sparseb[0][2])) { if(sparsea[i] [0] == sparseb[j][0]) { if(sparsea[i][1] == sparseb[j][1]) { temp = sparsea[i][2] + sparseb[j][2]; if(temp!=0) { sum[s][0] = sparsea[i][0]; sum[s][1] = sparsea[i][ 1]; sum[s][2] = temp; s++; i++; j++; } } } else if(sparsea[i][1]<sparseb[j][1]) { sum[s][0] = sparsea[i][0]; sum[s][1] = sparsea[i][1]; sum[s][2] = sparsea[i][2]; s++; i++; } else if(sparsea[i][1]>sparseb[j][1]) { sum[s][0] = sparseb[j][0]; sum[s][1] = sparseb[j][1]; sum[s][2] = sparseb[j][2]; s++; j++; } else if(sparsea[i][0]<sparseb[j][0]) { sum[s][0] = sparsea[i][0]; sum[s][1] = sparsea[i][1]; sum[s][2] = sparsea[i][2]; s++; i++; } el se if(sparsea[i][0]>sparseb[j][0 ]) { sum[s][0] = sparseb[j][0]; Data Structures Lab sum[s][1] = sparseb[j][1]; sum[s][2] = sparseb[j][2]; s++; j++; } } while(i<=sparsea[0][2]) { sum[s][0] = sparsea[i][0]; sum[s][1] = sparsea[i][1]; sum[s][2] = sparsea[i][2]; s++; i++; } while(j<=sparseb[0][2]) { sum[s][0] = sparseb[j][0]; sum[s][1] = sparseb[j][1]; sum[s][2] = sparseb[j][2]; s++; j++; } sum[0][2] = s - 1; non = s - 1; printf("Sum of two Sparse Matrix is: \ n"); for(i=0; i<=non; i++) { for(j=0; j<3; j++) { printf("%d ",sum[i][j]); } printf(" \ n" ); } } OUTPUT: Enter the number of rows of the matrix 3 Enter the number of columns of the matrix 2 Enter the elements of Matrix A 1 0 Data Structures Lab 0 2 0 0 Entered Matrix A: 1 0 0 2 0 0 Enter the elements of Matrix B 2 0 0 0 3 0 Entered Matrix B: 2 0 0 0 3 0 Sparse Matrix A: 3 2 2 0 0 1 1 1 2 Sparse Matrix B: 3 2 2 0 0 2 2 0 3 Transposed Matrix A: 2 3 2 0 0 1 1 1 2 Transposed Matrix B: 2 3 2 0 0 2 0 2 3 Sum of two Sparse Matrix is: 2 3 3 0 0 3 1 1 2 0 2 3 CONCLUSION: The algorithm was developed and the program was coded. The program runs successfully. Data Structures Lab Experiment No: 2 Implementation of Stack, Queues, Priority Queues, DEQUEUE and Circular Queues using arrays 2.a STACK PROBLEM DEFINITION: Write a program to implement the operations in stack using array. ALGORITHM: Algorithm for main( ) Step 1: Start Step 2: Read choice for push, pop, display and exit. Step 2.1: If push, call push( ) Step 2.2: If pop, call pop( ) Step 2.3: If display, call display( ) Step 2.4: If exit, call exit(0) Step 3: Stop Algorithm for push() Step 1: Declare all the variables. Step 2: Read item to be pushed. Step 3: Check (top= size - 1) Step 3.1: Print Stack Overflow Step 4: else Step 4.1: top++ stack[top] = item Print inserted item Algorithm for pop() Step 1: Declare all the variables. Step 2: Check (top= - 1) Step 2.1: Print Stack Underflow Step 3: else Step 3.1: item = stack[top] Data Structures Lab Print deleted item top -- Algorithm for display() Step 1: Declare all the variables. Step 2: Check (top= - 1) Step 2.1: Print No elements in Stack Step 3: else Step 3.1: Print all item PROGRAM : # include <stdio.h> # include <stdlib.h> #include<conio.h> # define size 5 int stack[size],top= - 1; void push(); void pop(); void display(); void main() { int act; clrscr(); do { printf("Select an action to Continue \ n"); printf("1.Push \ t2.pop \ t3.Display \ t4.Exit \ n"); scanf("%d",&act); switch(act) { case 1: { printf("You have chosen Push Operation \ n"); push(); break; } case 2: { printf("You have cho sen Pop Operation \ n"); pop(); break; } case 3: Data Structures Lab { printf("You have chosen Display Operation \ n"); display(); break; } case 4: { exit(0); } default: { printf("Wrong choice!! \ n"); printf("Try again \ n"); break; } } } while(act!=4); getch(); } void push() { int item; printf("Enter item to be pushed into the stack \ n"); scanf("%d",&item); if(top==size - 1) printf("Error:Stack Overflow \ n"); else { top++; stack[top] = item; printf("%d pushed Successfully \ n",item); } } void pop() { int item; if(top== - 1) printf("Error:Stack Underflow \ n"); else { item = stack[top]; printf("%d is deleted Successfully \ n",item); top -- ; }