Академический Документы
Профессиональный Документы
Культура Документы
S.NO. 1. 2. 3. 4 5 6 7. 8.
NAME OF PROGRAM
C program for addition of two matrices addition of matrix in c C program to find largest element from array C program to find the image of matrix C program to print reverse of an array | reverse array in c C program to print spiral matrix Find the sum of diagonal elements of a matrix Find the transpose of a matrix Perform addition of two sparse matrix | sparse matrix addition
PAGE NO.
4 TO 7
SIG
8 TO 9 10 TO 13 14 TO 15
16 TO 18 19 TO 20
21 TO 22 23 TO 26
9.
27 TO 30
10.
31 TO 35
11.
36 TO 41
12.
42 TO 44
13.
C program to find the length of given linked list | length of link list in c
45 TO 47
131150049
Page 1
Compare two linked lists and check whetherthey are same or not Implement Queue Using Linked List Join two doubly circular linked list and print it. join doubly linked list Join two linked list and print it join link list C program to perform string insertion and deletion over queue Insert And Delete Operation Over Queue Insertion And Deletion Operation Over Circular Queue Insertion And Deletion Operation Over Multiple Queue | multiple queue in data structure
48 TO 51
52 TO 55 56 TO 58
59 TO 61 62 TO 63
64 TO 65 66 TO 67
68 TO 70
22. 23.
C program to sort n values using insertion sort method C program to sort n values using quick sort method by using a Non-Recursive Function
71 TO 72
73 TO 75
24.
C program for sorting array using merge sort method merge sort program in c
76 TO 78
25.
79 TO 82
26.
83 TO 85
27. 28.
Create a stack and perform push and pop operations using linked list Perform push and pop operations over multiple stack
86 TO 87
88 TO 90
131150049
Page 2
Perform push and pop operations over stack in c | push pop stack Perform push and pop operations over stack using string
C program to check binary tree | binary tree in c
91 TO 93
94 TO 95
C program to check Left Skewed Binary tree C program to check right skewed binary tree Count Leaf Nodes And Non-leaf Nodes Of Binary Tree
131150049
Page 3
Program:1 /* C program for addition of two matrices addition of matrix in c*/ #include<stdio.h> #include<conio.h>3 void main() { int m1[10][10],m2[10][10],m3[10][10]; int r,c,i,j; clrscr(); printf("\nEnter r & c : "); scanf("%d %d",&r,&c); printf("\nEnter first matrix of size %d X %d\n",r,c); for(i=0;i < r ; i ++) { for(j=0;j < c;j++) { scanf("%d",&m1[i][j]); } } printf("\nEnter second matrix of size %d X %d\n",r,c); for(i=0;i < r ; i ++) { for(j=0;j < c;j++)
131150049
Page 4
{ scanf("%d",&m2[i][j]); } } for(i=0;i < r;i++) { for(j=0;j < c;j++) { m3[i][j] = m1[i][j] + m2[i][j]; } } printf("\nFirst matrix is\n"); for(i=0;i< r;i++) { for(j=0;j < c;j++) { printf("%4d",m1[i][j]); } printf("\n"); } printf("\nSecond matrix is\n"); for(i=0;i < r;i++) {
131150049
Page 5
for(j=0;j < c;j++) { printf("%4d",m2[i][j]); } printf("\n"); } printf("\nThe addition is \n"); for(i=0;i < r;i++) { for(j=0;j < c;j++) { printf("%4d",m3[i][j]); } printf("\n"); } }
Program Input:
131150049
Page 6
Program Output:
131150049
Page 7
Program:2 /*C program to find largest element from array*/ #include<stdio.h> #include<conio.h> void main() { int a[100],max,i,n; clrscr(); printf("Enter the size of array\n"); scanf("%d",&n); printf("Enter the elements of array\n"); for(i=0;i < n;i++) { scanf("%d",&a[i]); } max=a[0]; for(i=0;i < n;i++) { if(a[i]>max) { max=a[i]; } } printf("The max of the given array is %d",max); getch(); }
Program Input:
131150049
Page 8
Program Output:
131150049
Page 9
Program:3 /*C program to find the image of matrix*/ #include<stdio.h> #include<conio.h> void main() { int m1[10][10],m2[10][10],m3[10][10]; int r,c,i,j,k; clrscr(); printf("\nEnter r & c : "); scanf("%d %d",&r,&c); printf("\nEnter matrix of size %d X %d\n",r,c); for(i=0;i < r ; i ++) { for(j=0;j < c;j++) { scanf("%d",&m1[i][j]); } } for(i=0;i < r;i++) { for(j=0,k=c-1;j < c;j++,k--) {
131150049
Page 10
m2[i][k] = m1[i][j]; } } for ( j=0;j < c;j++) { for(i=0,k=r-1;i < r; i++,k--) { m3[k][j] = m1[i][j]; } } printf("\nThe original matrix is\n"); for(i=0;i < r;i++) { for(j=0;j < c;j++) { printf("%4d",m1[i][j]); } printf("\n"); } printf("\nThe Y axis image is \n"); for(i=0;i < r;i++) { for(j=0;j < c;j++)
131150049
Page 11
{ printf("%4d",m2[i][j]); } printf("\n"); } printf("\nThe x axis image is \n"); for(i=0;i < r;i++) { for(j=0;j < c;j++) { printf("%4d",m3[i][j]); } printf("\n"); } } Program Input:
131150049
Page 12
Program Output:
131150049
Page 13
Program:4 /*C program to print reverse of an array | reverse array in c */ #include<stdio.h> #include<conio.h> void main() { int a[10],i,n; clrscr(); printf("Enter the size of array\n"); scanf("%d",&n); printf("Enter the %d elements\n",n); for(i=0;i << n;i++) { scanf("%d",&a[i]); } printf("The %d elements are \n",n); for(i=0;i < n;i++) { printf("%5d",a[i]); } printf("\n"); printf("The %d elements in reverse order are\n",n); for(i=n-1;i>=0;i--) { printf("%5d",a[i]); } getch(); } Program Input:
131150049
Page 14
Program Output:
131150049
Page 15
Program:5 /*C program to print spiral matrix */ #include<stdio.h> #include<conio.h> void main() { int m[20][20],i,j; int lc,hc,lr,hr,r,c,cnt; clrscr(); printf("\nEnter r & c :"); scanf("%d %d",&r,&c); cnt = 1; lr = 0; lc = 0; hr = r - 1; hc = c - 1; while ( lr <=hr && lc <= hc ) { i = lr; for(j=lc;j <= hc;j++) { m[i][j] = cnt++; j = hc; for(i=lr+1;i<=hr;i++) { m[i][j] = cnt++;
131150049
Page 16
if( lr != hr ) { i = hr; for(j=hc-1;j>=lc;j--) m[i][j] = cnt++; } if ( lc != hc ) { j = lc; for(i=hr-1;i>lr;i--) m[i][j] = cnt++; } } } lr++;lc++; hr--;hc--; } printf("\nSpirally filled matrix is\n"); for(i=0;i < r;i++) { for(j=0;j < c;j++) { printf("%4d",m[i][j]); printf("\n");
131150049
Page 17
} } } Program Input:
Program Output:
131150049
Page 18
Program:6 /*Find the sum of diagonal elements of a matrix*/ #include<stdio.h> #include<conio.h> void main() { int a[10][10],i,j,sum=0,r,c; clrscr(); printf("\n Enter the number of rows and column "); scanf("%d%d",&r,&c); printf("\nEnter the %dX%d matrix",r,c); for(i=0;i < r;i++) { for(j=0;j < c;j++) { scanf("%d",&a[i][j]); } } for(i=0;i < r;i++) { for(j=0;j < c;j++) { if(i==j) { sum+=a[i][j]; } } } printf("\nThe sum of diagonal elements is %d",sum); } Program Input:
131150049
Page 19
Program Output:
131150049
Page 20
Program:7 /*Find the transpose of a matrix*/ #include<stdio.h> #include<conio.h> void main() { int m1[10][10],m2[10][10]; int r,c,i,j; clrscr(); printf("\nEnter r & c : "); scanf("%d %d",&r,&c); printf("\nEnter first matrix of size %d X %d\n",r,c); for(i=0;i< r ; i ++) { for(j=0;j < c;j++) { scanf("%d",&m1[i][j]); } } for(i=0;i < r ; i ++) { for(j=0;j < c;j++) { m2[j][i] = m1[i][j]; } } printf("\nthe original matrix is\n"); for(i=0;i < r;i++) { for(j=0;j < c;j++) { printf("%4d",m1[i][j]); printf("\n"); } } printf("\nThe resultane matrix is\n"); for(i=0;i< c;i++) { for(j=0;j < r;j++) { printf("%4d",m2[i][j]); }
131150049
Page 21
Program Output:
131150049
Page 22
Program:8 /*Perform addition of two sparse matrix | sparse matrix addition */ #include<stdio.h> #include<conio.h> read_sp_mat(sp2); add_sp_mat(sp1,sp2,sp3); void main() { int sp1[10][3],sp2[10][3],sp3[10][3]; clrscr(); printf("\nEnter first sparse matrix"); read_sp_mat(sp1); printf("\nEnter second sparse matrix"); read_sp_mat(sp2); add_sp_mat(sp1,sp2,sp3); printf("\nFirst sparse matrix is"); print_sp_mat(sp1); printf("\nSecond sparse matrix is"); print_sp_mat(sp2); printf("\nThird sparse matrix is"); print_sp_mat(sp3); } int read_sp_mat(int sp[10][3]) { int r,c,i,j,k,t; printf("\nEnter r and c : "); scanf("%d %d",&r,&c); printf("\nEnter the data \n"); k=1; for(i=0;i < r;i++) { for(j=0;jc;j++) { scanf("%d",&t); if( t != 0 ) { sp[k][0] = i; sp[k][1] = j; sp[k][2] = t; k++; }
131150049
Page 23
} } sp[0][0] = r; sp[0][1] = c; sp[0][2] = k-1; return; } int print_sp_mat(int sp[10][3]) { int r,c,i,j,tot_val,k; r = sp[0][0]; c = sp[0][1]; tot_val = sp[0][2]; for(i=0;ir;i++) { printf("\n"); for(j=0;jc;j++) { for(k=1;k<=tot_val;k++) { if( sp[k][0] == i && sp[k][1] == j ) break; } if( k > tot_val) printf("%4d",0); else printf("%4d",sp[k][2]); } } return; } int add_sp_mat(sp1,sp2,sp3) int sp1[10][3],sp2[10][3],sp3[10][3]; { int r,c,i,j,k1,k2,k3,tot1,tot2; if( sp1[0][0] != sp2[0][0] || sp1[0][1] != sp2[0][1] ) { printf("Invalid matrix size "); exit(0); } tot1 = sp1[0][2];
131150049
Page 24
tot2 = sp2[0][2]; k1 = k2 = k3 = 1; while ( k1 <= tot1 && k2 <= tot2) { if ( sp1[k1][0] < sp2[k2][0] ) { sp3[k3][0] = sp1[k1][0]; sp3[k3][1] = sp1[k1][1]; sp3[k3][2] = sp1[k1][2]; k3++;k1++; } else if ( sp1[k1][0] > sp2[k2][0] ) { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp2[k2][2]; k3++;k2++; } else if ( sp1[k1][0] == sp2[k2][0] ) { if ( sp1[k1][1] < sp2[k2][1] ) { sp3[k3][0] = sp1[k1][0]; sp3[k3][1] = sp1[k1][1]; sp3[k3][2] = sp1[k1][2]; k3++;k1++; } else if ( sp1[k1][1] > sp2[k2][1] ) { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp2[k2][2]; k3++;k2++; } else { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp1[k1][2] + sp2[k2][2]; k3++;k2++;k1++;
131150049
Page 25
} } while ( k1 <=tot1 ) { sp3[k3][0] = sp1[k1][0]; sp3[k3][1] = sp1[k1][1]; sp3[k3][2] = sp1[k1][2]; k3++;k1++; } while ( k2 <= tot2 ) { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp2[k2][2]; k3++;k2++; } sp3[0][0] = sp1[0][0]; sp3[0][1] = sp1[0][1]; sp3[0][2] = k3-1; return; } Program Input:
Program Output:
131150049
Page 26
Program:9 /*Convert an infix expression to postfix expression */ #include<stdio.h> #include<conio.h> #define max 50 void main() { int sp1[10][3],sp2[10][3],sp3[10][3]; clrscr(); printf("\nEnter first sparse matrix"); read_sp_mat(sp1); printf("\nEnter second sparse matrix"); read_sp_mat(sp2); add_sp_mat(sp1,sp2,sp3); printf("\nFirst sparse matrix is"); print_sp_mat(sp1); printf("\nSecond sparse matrix is"); print_sp_mat(sp2); printf("\nThird sparse matrix is"); print_sp_mat(sp3); } int read_sp_mat(int sp[10][3]) { int r,c,i,j,k,t; printf("\nEnter r and c : "); scanf("%d %d",&r,&c); printf("\nEnter the data \n"); k=1; for(i=0;i < r;i++) { for(j=0;jc;j++) { scanf("%d",&t); if( t != 0 ) { sp[k][0] = i; sp[k][1] = j; sp[k][2] = t; k++; }
131150049
Page 27
} } sp[0][0] = r; sp[0][1] = c; sp[0][2] = k-1; return; } int print_sp_mat(int sp[10][3]) { int r,c,i,j,tot_val,k; r = sp[0][0]; c = sp[0][1]; tot_val = sp[0][2]; for(i=0;ir;i++) { printf("\n"); for(j=0;jc;j++) { for(k=1;k<=tot_val;k++) { if( sp[k][0] == i && sp[k][1] == j ) break; } if( k > tot_val) printf("%4d",0); else printf("%4d",sp[k][2]); } } return; } int add_sp_mat(sp1,sp2,sp3) int sp1[10][3],sp2[10][3],sp3[10][3]; { int r,c,i,j,k1,k2,k3,tot1,tot2; if( sp1[0][0] != sp2[0][0] || sp1[0][1] != sp2[0][1] ) { printf("Invalid matrix size "); exit(0); } tot1 = sp1[0][2];
131150049
Page 28
tot2 = sp2[0][2]; k1 = k2 = k3 = 1; while ( k1 <= tot1 && k2 <= tot2) { if ( sp1[k1][0] < sp2[k2][0] ) { sp3[k3][0] = sp1[k1][0]; sp3[k3][1] = sp1[k1][1]; sp3[k3][2] = sp1[k1][2]; k3++;k1++; } else if ( sp1[k1][0] > sp2[k2][0] ) { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp2[k2][2]; k3++;k2++; } else if ( sp1[k1][0] == sp2[k2][0] ) { if ( sp1[k1][1] < sp2[k2][1] ) { sp3[k3][0] = sp1[k1][0]; sp3[k3][1] = sp1[k1][1]; sp3[k3][2] = sp1[k1][2]; k3++;k1++; } else if ( sp1[k1][1] > sp2[k2][1] ) { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp2[k2][2]; k3++;k2++; } else { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp1[k1][2] + sp2[k2][2]; k3++;k2++;k1++;
131150049
Page 29
} } while ( k1 <=tot1 ) { sp3[k3][0] = sp1[k1][0]; sp3[k3][1] = sp1[k1][1]; sp3[k3][2] = sp1[k1][2]; k3++;k1++; } while ( k2 <= tot2 ) { sp3[k3][0] = sp2[k2][0]; sp3[k3][1] = sp2[k2][1]; sp3[k3][2] = sp2[k2][2]; k3++;k2++; } sp3[0][0] = sp1[0][0]; sp3[0][1] = sp1[0][1]; sp3[0][2] = k3-1; return; } } Program Input:
Program Output:
131150049
Page 30
Program:10 /*C program to create graph using adjacency matrix method */ #include<stdio.h> #include<conio.h> void main() { int option; clrscr(); do { printf("\n A Program to represent a Graph by using an "); printf("Adjacency Matrix method \n "); printf("\n 1. Directed Graph "); printf("\n 2. Un-Directed Graph "); printf("\n 3. Exit "); printf("\n\n Select a proper option : "); scanf("%d", &option); switch(option) { case 1 : dir_graph(); break; case 2 : undir_graph(); break; case 3 : exit(0); }
131150049
Page 31
} while(1); } int dir_graph() { int adj_mat[50][50]; int n; int in_deg, out_deg, i, j; printf("\n How Many Vertices ? : "); scanf("%d", &n); read_graph (adj_mat, n); printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree "); for (i = 1; i <= n ; i++ ) { in_deg = out_deg = 0; for ( j = 1 ; j <= n ; j++ ) { if ( adj_mat[j][i] == 1 ) in_deg++; } for ( j = 1 ; j <= n ; j++ ) if (adj_mat[i][j] == 1 ) out_deg++; printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg);
131150049
Page 32
} return; } int undir_graph() { int adj_mat[50][50]; int deg, i, j, n; printf("\n How Many Vertices ? : "); scanf("%d", &n); read_graph(adj_mat, n); printf("\n Vertex \t Degree "); for ( i = 1 ; i <= n ; i++ ) { deg = 0; for ( j = 1 ; j <= n ; j++ ) if ( adj_mat[i][j] == 1) deg++; printf("\n\n %5d \t\t %d\n\n", i, deg); } return; } int read_graph ( int adj_mat[50][50], int n ) { int i, j;
131150049
Page 33
char reply;
for ( i = 1 ; i <= n ; i++ ) { for ( j = 1 ; j <= n ; j++ ) { if ( i == j ) { adj_mat[i][j] = 0; continue; } printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j); flushall(); scanf("%c", &reply); if ( reply == 'y' || reply == 'Y' ) adj_mat[i][j] = 1; else adj_mat[i][j] = 0; } } return; }
131150049
Page 34
Program Output:
131150049
Page 35
Program:11 /*C program to create graph using adjacency list*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #define new_node (struct node*)malloc(sizeof(struct node)) struct node { int vertex; struct node *next; }; void main() { int option; clrscr(); do { printf("\n A Program to represent a Graph by using an Adjacency List \n "); printf("\n 1. Directed Graph "); printf("\n 2. Un-Directed Graph "); printf("\n 3. Exit "); printf("\n\n Select a proper option : "); scanf("%d", &option); switch(option) {
131150049
Page 36
case 1 : dir_graph(); break; case 2 : undir_graph(); break; case 3 : exit(0); } } while(1); } int dir_graph() { struct node *adj_list[10], *p; int n; int in_deg, out_deg, i, j; printf("\n How Many Vertices ? : "); scanf("%d", &n); for( i = 1 ; i <= n ; i++ ) adj_list[i] = NULL; read_graph (adj_list, n); printf("\n Vertex \t In_Degree \t Out_Degree \t Total_Degree "); for (i = 1; i <= n ; i++ ) { in_deg = out_deg = 0; p = adj_list[i];
131150049
Page 37
while( p != NULL ) { out_deg++; p = p -> next; } for ( j = 1 ; j <= n ; j++ ) { p = adj_list[j]; while( p != NULL ) { if ( p -> vertex == i ) in_deg++; p = p -> next; } } printf("\n\n %5d\t\t\t%d\t\t%d\t\t%d\n\n",i,in_deg,out_deg,in_deg+out_deg); } return; } int undir_graph() { struct node *adj_list[10], *p; int deg, i, j, n; printf("\n How Many Vertices ? : ");
131150049
Page 38
scanf("%d", &n); for ( i = 1 ; i <= n ; i++ ) adj_list[i] = NULL; read_graph(adj_list, n); printf("\n Vertex \t Degree ");
for ( i = 1 ; i <= n ; i++ ) { deg = 0; p = adj_list[i]; while( p != NULL ) { deg++; p = p -> next; } printf("\n\n %5d \t\t %d\n\n", i, deg); } return; } int read_graph ( struct node *adj_list[10], int n ) { int i, j; char reply; struct node *p, *c;
131150049
Page 39
for ( i = 1 ; i <= n ; i++ ) { for ( j = 1 ; j <= n ; j++ ) { if ( i == j ) continue; printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j); flushall(); scanf("%c", &reply); if ( reply == 'y' || reply == 'Y' ) { c = new_node; c -> vertex = j; c -> next = NULL; if ( adj_list[i] == NULL ) adj_list[i] = c; else { p = adj_list[i]; while ( p -> next != NULL ) p = p -> next; p -> next = c; } }
131150049
Page 40
131150049
Page 41
Program:12 /*C program to create graph using depth first search method */ #include<stdio.h> #include<conio.h> void main() { int option, i, j, n; int adj_mat[50][50]; clrscr(); printf("\n A Program to implement Depth First Search (DFS) &"); printf("\n Breadth First Search (BFS) methods \n "); printf("\n How Many Vertices ? : "); scanf("%d", &n); read_graph(adj_mat, n); printf("\n\n DFS Output is \n\n"); dfs( adj_mat, n ); printf("\n"); printf("\n BFS Output is \n\n "); bfs( adj_mat, n ); } int read_graph ( int adj_mat[50][50], int n ) { int i, j; char reply; for ( i = 1 ; i <= n ; i++ ) { for ( j = 1 ; j <= n ; j++ ) { if ( i == j ) { adj_mat[i][j] = 0; continue; } printf("\n Vertices %d & %d are Adjacent ? (Y/N) :",i,j); flushall(); scanf("%c", &reply); if ( reply == 'y' || reply == 'Y' ) adj_mat[i][j] = 1; else adj_mat[i][j] = 0; } }
131150049
Page 42
return; } int dfs ( int adj_mat[50][50], int n ) { int visited[20]; int stk[20]; int top, i, j, v; top = -1; for ( i = 1 ; i <= n ; i++ ) visited[i] = 0; top++; stk[top] = 1; while ( top != -1 ) { v = stk[top]; top--; if ( visited[v] == 0 ) { printf("\t%d", v); visited[v] = 1; for( i = n ; i >= 1 ; i-- ) { if ( adj_mat[v][i] == 1 && visited[i] == 0 ) { top++; stk[top] = i; } } } } return; } int bfs (int adj_mat[50][50], int n ) { int visited[20]; int q[20], f, r, i, j, v; f = r = 1; for ( i = 1 ; i <= n ; i++ ) visited[i] = 0; r++; q[r] = 1; while ( f != r )
131150049
Page 43
{ f++; v = q[f]; if ( visited[v] == 0 ) { printf("\t%d", v); visited[v] = 1; for ( i = 1 ; i <= n ; i++ ) { if ( adj_mat[v][i] == 1 && visited[i] == 0 ) { r++; q[r] = i; } } } } return; } Program Output:
131150049
Page 44
Program:13 /*C program to find the length of given linked list | length of link list in c */ #include<stdio.h> #include<conio.h> #include<alloc.h> #define newnode (struct node*) malloc(sizeof(struct node)) struct node { int data; struct node *next; }; struct node *create_list(); void main() { struct node *f; int len; f = NULL; clrscr(); f = create_list(); len = find_len(f); printf("\n length = %d",len); } struct node *create_list() { struct node *f,*c,*p;
131150049
Page 45
int tdata; f = NULL; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); while( tdata != 0 ) { c = newnode; if( c == NULL) { printf("\n Insuf. mem. "); exit(0); } c->data = tdata; c->next = NULL; if( f== NULL) f = c; else p->next = c; p = c; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); } return(f); }
131150049
Page 46
int find_len(struct node *f) { int len=0; struct node *t; if( f == NULL) return(0); t = f; while( t != NULL ) { len++; t = t->next; } return(len); } Program Input:
Program Output:
131150049
Page 47
Program:14 /*Compare two linked lists and check whether they are same or not */ #include<stdio.h> #include<conio.h> #include<alloc.h> #define newnode (struct node*) malloc(sizeof(struct node)) struct node { int data; struct node *next; }; struct node *create_list(); void main() { struct node *f1,*f2; int len,reply; f1 = f2 = NULL; clrscr(); printf("\nEnter first list\n"); f1 = create_list(); printf("\nEnter second list\n"); f2 = create_list(); reply = compare(f1,f2);
131150049
Page 48
if( reply == 1 ) printf("Both lists are same"); else printf("Both lists are not same"); } struct node *create_list() { struct node *f,*c,*p; int tdata; f = NULL; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); while( tdata != 0 ) { c = newnode; if( c == NULL) { printf("\n Insuf. mem. "); exit(0); } c->data = tdata; c->next = NULL; if( f== NULL)
131150049
Page 49
f = c; else p->next = c; p = c; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); } return(f); } int compare(struct node *f1, struct node *f2) { int reply; struct node *t1,*t2; if( f1 == NULL && f2 == NULL) return(1); t1 = f1; t2 = f2; while( t1 != NULL && t2 != NULL) { if( t1->data != t2->data ) break; t1 = t1->next; t2 = t2->next;
131150049
Page 50
Program Output:
131150049
Page 51
Program:15 /*Implement Queue Using Linked List */ #include<stdio.h> #include<conio.h> #include<alloc.h> struct queue { int data; struct queue *next; }; struct queue *addq(struct queue *front); struct queue *delq(struct queue *front); void main() { struct queue *front; int reply,option,data; clrscr(); front=NULL; do { printf("\n1.addq"); printf("\n2.delq"); printf("\n3.exit"); printf("\nSelect proper option");
131150049
Page 52
scanf("%d",&option); switch(option) { case 1 : front=addq(front); break; case 2 : front=delq(front); case 3 : exit(0); } } while(1); } struct queue *addq(struct queue *front) { struct queue *c,*r; c=(struct queue*)malloc(sizeof(struct queue)); if(c==NULL) { printf("Insufficient memory"); return(front); { printf("\nEnter data");
131150049
Page 53
scanf("%d",&c->data); c->next=NULL; if(front==NULL) { front=c; } else { r=front; while(r->next!=NULL) { r=r->next; } } return(front); } struct queue *delq(struct queue *front) { struct queue *c; if(front==NULL) { printf("Queue is empty"); return(front);
131150049
Page 54
Program Output:
131150049
Page 55
Program:16 /*Join two doubly circular linked list and print it. join doubly linked list*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #define newnode (struct node*) malloc(sizeof(struct node)) struct node { int data; struct node *next; struct node *prev; }; struct node *create_list(); struct node *join_list(struct node *f1, struct node *f2); void main() { struct node *f1,*f2,*f3; int len,reply; f1 = f2 = NULL; clrscr(); printf("\nEnter first list\n"); f1 = create_list(); printf("\nEnter second list\n"); f2 = create_list(); printf("\n First list is\n"); print_list(f1); printf("\n Second list is\n"); print_list(f2); f3 = join_list(f1,f2); printf("\nThe resultant list is \n"); print_list(f3); } struct node *create_list() { struct node *f,*c,*p; int tdata; f = NULL; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); while( tdata != 0 ) { c = newnode;
131150049
Page 56
if( c == NULL) { printf("\n Insuf. mem. "); exit(0); } c->data = tdata; c->next = NULL; c->prev = NULL; if( f== NULL) f = c; else { p->next = c; c->prev = p; } p = c; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); } f->prev = c; c->next= f; return(f); } void print_list( struct node *f) { struct node *t; if( f == NULL) { printf("List is empty"); return; } printf("%4d",f->data); t = f->next; while ( t != f) { printf("%4d",t->data); t = t->next; } return; } struct node *join_list(struct node *f1, struct node *f2) {
131150049
Page 57
int reply; struct node *t1,*t2; if( f1 == NULL && f2 == NULL) return(NULL); else if( f1 != NULL && f2 == NULL) return(f1); else if( f1 == NULL && f2 != NULL) return(f2); else { t1 = f1->prev; t2 = f2->prev; t1->next = f2; f2->prev = t1; f1->prev = t2; t2->next = f1; return(f1); } } Program Input:
Program Output:
131150049
Page 58
Program:17 /*Join two linked list and print it join link list*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #define newnode (struct node*) malloc(sizeof(struct node)) struct node { int data; struct node *next; }; struct node *create_list(); struct node *join_list(struct node *f1, struct node *f2); void main() { struct node *f1,*f2,*f3; int len,reply; 1 = f2 = NULL; clrscr(); printf("\nEnter first list\n"); f1 = create_list(); printf("\nEnter second list\n"); f2 = create_list(); printf("\n First list is\n"); print_list(f1); printf("\n Second list is\n"); print_list(f2); f3 = join_list(f1,f2); printf("\nThe resultant list is \n"); print_list(f3); } struct node *create_list() { struct node *f,*c,*p; int tdata; f = NULL; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); while( tdata != 0 ) { c = newnode; if( c == NULL)
131150049
Page 59
{ printf("\n Insuf. mem. "); exit(0); } c->data = tdata; c->next = NULL; if( f== NULL) f = c; else p->next = c; p = c; printf("\n Enter data ( use 0 to exit ) : "); scanf("%d",&tdata); } return(f); } void print_list( struct node *f) { struct node *t; if( f == NULL) { printf("List is empty"); return; } t = f; while ( t != NULL) { printf("%4d",t->data); t = t->next; } return; } struct node *join_list(struct node *f1, struct node *f2) { int reply; struct node *t1,*t2; if( f1 == NULL && f2 == NULL) return(NULL); else if( f1 != NULL && f2 == NULL) return(f1); else
131150049
Page 60
if( f1 == NULL && f2 != NULL) return(f2); else { t1 = f1; while ( t1->next != NULL ) { t1 = t1->next; } t1->next = f2; return(f1); } } Program Input:
Program Output:
131150049
Page 61
Program:18 /*C program to perform string insertion and deletion over queue */ #include<stdio.h> #include<conio.h> #include<string.h> void main() { char queue[max][80], data[80]; int front, rear, reply, option; clrscr(); front = rear = -1; do { printf("\n C Language program to implement the basic operations on String Queue \n"); printf("\n 1. Insert String in a Queue"); printf("\n 2. Delete String from a Queue"); printf("\n 3. Exit"); printf("\n Select proper option ( 1 / 2 / 3 ) : "); scanf("%d", &option); switch(option) { case 1 : printf("\n Enter the String to be insert in a Queue : "); flushall(); gets(data); reply = insq(queue, &rear, data); if( reply == -1 ) printf("\n Queue is Full \n"); else printf("\n Entered String is Inserted in a Queue \n"); break; case 2 : reply = delq(queue, &front, &rear, data); if( reply == -1 ) printf("\n Queue is Empty \n"); else printf("\n Deleted String from Queue is : %s", data); printf("\n"); break; case 3 : exit(0); }
131150049
Page 62
} while(1); } int insq(char queue[max][80], int *rear, char data[80]) { if(*rear == max -1) return(-1); else { *rear = *rear + 1; strcpy(queue[*rear], data); return(1); } } int delq(char queue[max][80], int *front, int *rear, char data[80]) { if(*front == *rear) return(-1); else { (*front)++; strcpy(data, queue[*front]); return(1); } } Program Output:
131150049
Page 63
Program:19 /*Insert And Delete Operation Over Queue*/ #include<stdio.h> #include<conio.h> #define max 3 void main() { int queue[max],data; int front,rear,reply,option; clrscr(); front = rear = -1; do { printf("\n 1. insert queue"); printf("\n 2. delete queue"); printf("\n 3. exit"); printf("\nSelect proper option :"); scanf("%d",&option); switch(option) { case 1 : printf("\nEnter data : "); scanf("%d",&data); reply = insertq(queue,&rear,&data); if ( reply == - 1) printf("Queue is full"); else printf("Inserted data in queue"); break; case 2 : reply = delq(queue,&front,&rear,&data); if ( reply == -1 ) printf("Queue is empty "); else printf("Deleted data is : %d", data); break; case 3 : exit(0); } } while(1); } int insertq ( int queue[max], int *rear , int *data)
131150049
Page 64
{ if ( *rear == max -1 ) return(-1); else { *rear = *rear + 1; queue[*rear] = *data; return(1); } } int delq( int queue[max], int *front, int *rear , int *data) { if ( *front == *rear) return(-1); else { (*front)++; *data = queue[*front]; return(1); } } Program Output:
131150049
Page 65
Program:20 /*Insertion And Deletion Operation Over Circular Queue*/ #include<stdio.h> #include<conio.h> # define max 10 void main() { int cqueue[max], data; int front, rear, reply, option; clrscr(); front = rear = 0; do { printf("\n C Language program to implement basic operations on a Circular Queue \n"); printf("\n 1. Insert number in a Circular Queue"); printf("\n 2 .Delete number from Circular Queue"); printf("\n 3. Exit \n"); printf("\n Select proper option ( 1 / 2 / 3 ) : "); scanf("%d", &option); switch(option) { case 1 : printf("\n Enter number: "); scanf("%d", &data); reply = insq(cqueue, &front, &rear, &data); if(reply == -1) printf("\n Circular Queue is Full \n"); else printf("\n Number %d is inserted in a Circular Queue \n", data); break; case 2 : reply = delq(cqueue, &front, &rear,&data); if(reply == -1) printf("\n Circular Queue is Empty \n"); else printf("\n %d is deleted from Circular Queue \n", data); printf("\n"); break; case 3 : exit(0); } }
131150049
Page 66
while(1); } int insq(int cqueue[max], int *front, int *rear, int *data) { if((*rear + 1) % max == *front) return(-1); else { *rear = (*rear + 1) % max; cqueue[*rear] = *data; return(1); } } int delq(int cqueue[max], int *front, int *rear, int *data) { if(*front == *rear) return(-1); else { *front = (*front + 1) % max; *data = cqueue[*front]; return(1); } } Program Output:
131150049
Page 67
Program:21 /*Insertion And Deletion Operation Over Multiple Queue | multiple queue in data structure*/ #include<stdio.h> #include<conio.h> # define max 20 void main() { int queue[max], data; int bott[10], limit[10], f[10], r[10]; int i, n, qno, size, option, reply; clrscr(); printf("\n C Language program to implement the Multiple Queues \n"); printf("\n How Many Queues ? : "); scanf("%d", &n); size = max / n; bott[0] = -1; for(i = 1; i < n; i++) bott[i] = bott[i-1] + size; for(i = 0; i < n; i++) limit[i] = bott[i] + size; for(i = 0; i < n; i++) f[i] = r[i] = bott[i]; do { printf("\n C Language program to implement the Multiple Queues \n"); printf("\n 1. Insert in a Queue"); printf("\n 2. Delete from a Queue"); printf("\n 3. Exit \n"); printf("\n Select proper option ( 1 / 2 / 3 ) : "); scanf("%d", &option); switch(option) { case 1 : printf("\n Enter a Logical Queue Number (0 to %d) : ", n-1); scanf("%d", &qno); printf("\n Enter Data : "); scanf("%d", &data); reply = insq(queue, qno, r, limit, &data); if( reply == -1) printf("\n Queue %d is Full \n", qno); else
131150049
Page 68
printf("\n %d is inserted in a Queue No. %d \n", data, qno); break; case 2 : printf("\n Enter a Logical Queue Number (0 to %d) : ", n-1); scanf("%d", &qno); reply = delq(queue, qno, f, r, &data); if( reply == -1) printf("\n Queue %d is Empty \n", qno); else printf("\n %d is deleted from Queue No. %d \n", data, qno); break; case 3 : exit(0); } } while(1); } int insq( int queue[max], int qno, int rear[], int limit[], int *data ) { if( rear[qno] == limit[qno] ) return(-1); else { rear[qno]++; queue[ rear[qno] ] = *data; return(1); } } int delq( int queue[max], int qno, int front[], int rear[], int *data) { if( front[qno] == rear[qno] ) return(-1); else { front[qno]++; *data = queue[ front[qno] ]; return(1); } }
131150049
Page 69
Program Input:
131150049
Page 70
Program:22 /*C program to sort n values using insertion sort method */ #include<stdio.h> #include<conio.h> void main() { int a[50],n; clrscr(); printf("\nEnter n:"); scanf("%d",&n); insert_sort(a,n); printf("\n%d values after sorting are ",n); print_data(a,n); } int insert_sort(int a[],int n) { int bottom,i,j,t; bottom=-1; for(j=0;j < n;j++) { printf("\nData :"); scanf("%d",&t); i=bottom; while(a[i]>t && i!=-1) { a[i+1]=a[i]; i--; } a[i+1]=t; bottom++; } return; } int print_data(int a[],int n) { int i; for(i=0;i < n;i++) { printf("%4d",a[i]); } return; }
131150049
Page 71
Program Input:
Program Output:
131150049
Page 72
Program:23 /*C program to sort n values using quick sort method by using a NonRecursive Function */ #include<stdio.h> #include<conio.h> struct stack { int low, high; }; void main() { int a[50]; int n; clrscr(); printf("\nEnter n: "); scanf("%d",&n); read_data(a,n); a[n]=9999; printf("\nBefore sort :"); print_data(a,n); qck_srt(a,n); printf("\nAfter sort :"); print_data(a,n); } int read_data(int a[],int max) { int i; printf("\nEnter %d values \n",max); for(i=0; i < max; i++) { scanf("%d",&a[i]); } return; } int print_data(int a[],int max) { int i; for(i=0; i < max; i++) { printf("%4d",a[i]); } return;
131150049
Page 73
} int qck_srt(int a[], int n) { struct stack s[50]; int top; int i, j, k, l, h; int t; top = -1; top++; s[top].low = 0; s[top].high = n - 1; while( top != -1 ) { l = s[top].low; h = s[top].high; top--; if(l >= h ) { continue; } k = a[l]; i = l; j = h + 1; while( i < j ) { while( i < h && a[i] <= k ) { i++; } while( j > l && a[j] >= k ) { j--; } if( i < j ) { t = a[i]; a[i] = a[j]; a[j] = t; } } if(l != j) {
131150049
Page 74
t = a[l]; a[l] = a[j]; a[j] = t; } top++; s[top].low = j + 1; s[top].high = h; top++; s[top].low = l; s[top].high = j - 1; } return; } Program Input:
Program Output:
131150049
Page 75
Program:24 /*C program for sorting array using merge sort method merge sort program in c */ #include<stdio.h> #include<conio.h> void main() { int a1[20],a2[20],a3[40]; int max1,max2,max3; clrscr(); printf("\nEnter max1: "); scanf("%d",&max1); read_data(a1,max1); printf("\nEnter max2: "); scanf("%d",&max2); read_data(a2,max2); max3=merge_sort(a1,a2,a3,max1,max2); printf("\nFirst array is \n"); print_data(a1,max1); printf("\nSecond array is \n"); print_data(a2,max2); printf("\nThird array is \n"); print_data(a3,max3); } int read_data(int a[],int max) { int i; printf("\nEnter %d sorted values \n",max); for(i=0;i < max;i++) { scanf("%d",&a[i]); } return; } int print_data(int a[],int max) { int i; for(i=0;i < max;i++) { printf("%4d",a[i]); } return;
131150049
Page 76
} int merge_sort(a1,a2,a3,max1,max2) int a1[],a2[],a3[]; int max1,max2; { int i,j,k; i=j=k=0; while(i < max1 && j < max2) { if (a1[i] < a2 [j]) { a3[k++]=a1[i++]; } else { a3[k++]=a2[j++]; } } while (i < max1) { a3[k++]=a1[i++]; } while(j < max2) { a3[k++]=a2[j++]; } return(k); } Program Input:
131150049
Page 77
Program Output:
131150049
Page 78
Program:25 /*Sort Numeric Array Using Heap Sort | heap sort program in c */ #include<stdio.h> #include<conio.h> void main() { int a[50]; int n; clrscr(); printf("\nEnter n: "); scanf("%d",&n); read_data(a,n); printf("\nBefore sort : \n"); print_data(a,n); heap_sort(a,n); printf("\nAfter sort : \n"); print_data(a,n); } int read_data(int a[],int max) { int i; printf("\nEnter %d values \n",max); for(i=1;i<=max;i++) { scanf("%d",&a[i]);
131150049
Page 79
} return; } int print_data(int a[],int max) { int i; for(i=1;i<=max;i++) { printf("%10d",a[i]); } return; } int heap_sort(int a[],int n) { int i,j,t; for(i=n/2;i>=1;i--) { adjust(a,i,n); } for(i=n-1;i>=1;i--) { printf("\n"); print_data(a,n); t=a[i+1];
131150049
Page 80
a[i+1]=a[1]; a[1]=t; adjust(a,1,i); } return; } int adjust(int a[],int cur_pos,int max) { int cur_rec,j; cur_rec=a[cur_pos]; j=cur_pos * 2; while(j<=max) { if(j < max) { if(a[j] < a[j+1]) { j=j+1; } } if(a[j] > cur_rec) { a[j/2]=a[j]; j=j*2;
131150049
Page 81
Program Input:
Program Output:
131150049
Page 82
Program:26 /*Sort structure type array using bubble sort | bubble sort in c */ #include<stdio.h> #include<conio.h> struct stud { int roll; char name[15]; float per; }; void main() { struct stud a[50], t; int i, j, n; clrscr(); printf("\n C Language Program to Sort Struct type Array by using a Bubble Sort method "); printf("\n To sort the Student Records in Dercreasing Order of % (Percentage) \n"); printf("\n Enter How Many Records [ i.e. Size of Array (n) ] : "); scanf("%d", &n); read_data(a, n); printf("\n %d Records Before Sorting are \n", n); print_data(a, n); bbl_sort(a, n); printf("\n %d Values After Sorting are \n", n); print_data(a, n); } int read_data( struct stud a[], int n ) { int i; float t; printf("\n Enter %d Records \n", n); for(i = 0; i < n; i++) { printf("\n Roll No. : "); scanf("%d", &a[i].roll); printf("\n Name : "); flushall(); gets(a[i].name); printf("\n Percentage (%) : "); scanf("%f", &t);
131150049
Page 83
a[i].per = t; } return; } int print_data( struct stud a[], int n ) { int i; float t; printf("\n Roll No. \t Name \t Percentage (%) \n"); for(i = 0; i < n; i++) { printf("\n \t %d \t %s \t %.2f", a[i].roll, a[i].name, a[i].per); } return; } int bbl_sort( struct stud a[], int n ) { int i,j, k; struct stud t; for(k = n - 1; k >= 1; k--) { for(i = 0,j = 1; j <= k; i++,j++) { if( a[i].per > a[j].per) { t = a[i]; a[i] = a[j]; a[j] = t; } } } return; }
131150049
Page 84
Program Input:
Program Output:
131150049
Page 85
Program:27 /*Create a stack and perform push and pop operations using linked list*/ #include<stdio.h> #include<conio.h> #include<alloc.h> struct stack { int data; struct stack *next; }; struct stack *pop(struct stack *top); struct stack *push(struct stack *top); void main() { struct stack *top; int reply,option,data; clrscr(); top = NULL; do { printf("\n 1. push"); printf("\n 2. pop"); printf("\n 3. exit"); printf("\nSelect proper option : " ); scanf("%d",&option); switch(option) { case 1 : top = push(top); break; case 2 : top = pop(top); break; case 3 : exit(0); } } while(1); } struct stack *push(struct stack *top) { struct stack *c; c = (struct stack*)malloc(sizeof(struct stack));
131150049
Page 86
if( c == NULL) { printf("Insuff. mem"); return(top); } printf("\nEnter data : "); scanf("%d",&c->data); c->next = NULL; if( top == NULL) top = c; else { c->next = top; top = c; } return(top); } struct stack *pop(struct stack *top) { struct stack *c; if( top == NULL) { printf("Stack is empty"); return(top); } printf("\npopped data : %d",top->data); c = top; top = top->next; free(c); return(top); } Program Input:
131150049
Page 87
Program:28 /*Perform push and pop operations over multiple stack */ #include<stdio.h> #include<conio.h> # define max 20 void main() { int stack[max], data, n, size, sno; int top[10], bott[10], limit[10]; int i, option, reply; clrscr(); printf("\n C Language program to implement the Multiple Stacks \n"); printf("\n How Many Stacks ? : "); scanf("%d", &n); size = max / n; bott[0] = -1; for(i = 1; i < n; i++) bott[i] = bott[i-1] + size; for(i = 0; i < n-1; i++) limit[i] = bott[i] + size; for(i = 0; i < n; i++) top[i] = bott[i]; do { printf("\n C Language program to implement the Multiple Stacks \n"); printf("\n 1. Push"); printf("\n 2. Pop"); printf("\n 3. Exit \n"); printf("\n Select proper option ( 1 / 2 / 3 ) : "); scanf("%d", &option); switch(option) { case 1 : printf("\n Enter a Logical Stack Number (0 to %d) : ", n-1); scanf("%d", &sno); printf("\n Enter a Value: "); scanf("%d", &data); reply = push(stack, sno, top, limit, &data); if( reply == -1) printf("\n Stack %d is Full", sno); else printf("\n %d is Pushed in Stack No. %d \n", data, sno);
131150049
Page 88
break; case 2 : printf("\n Enter a Logical Stack Number (0 to %d) : ", n-1); scanf("%d", &sno); reply = pop( stack, sno, top, bott, &data); if( reply == -1) printf("\n Stack %d is Empty \n", sno); else printf("\n %d is popped from Stack No. %d \n", data, sno); break; case 3 : exit(0); } } while(1); } int push( int stack[max], int sno, int top[], int limit[], int *data ) { if( top[sno] == limit[sno] ) return(-1); else { top[sno]++; stack[ top[sno] ] = *data; return(1); } } int pop( int stack[max], int sno, int top[], int bott[], int *data) { if( top[sno] == bott[sno] ) return(-1); else { *data = stack[ top[sno] ]; top[sno]--; return(1); } }
131150049
Page 89
Program Input:
131150049
Page 90
Program:29 /*Perform push and pop operations over stack in c | push pop stack*/ #include<stdio.h> #include<conio.h> #define max 5 void main() { int stack[max],data; int top,option,reply; top = -1; clrscr(); do { printf("\n 1. push"); printf("\n 2. pop"); printf("\n 3. exit"); printf("\nSelect proper option : "); scanf("%d",&option); switch(option) { case 1 : printf("\n Enter a value : "); scanf("%d",&data); reply = push(stack,&top,&data); if( reply == -1 )
131150049
Page 91
printf("\nStack is full"); else printf("\n Pushed value"); break; case 2 : reply = pop ( stack,&top,&data); if( reply == - 1) printf("\nStack is empty"); else printf("\n Popped value is %d",data); break; case 3 : exit(0); } } while(1); } int push( int stack[max],int *top, int *data) { if( *top == max -1 ) return(-1); else { *top = *top + 1; stack[*top] = *data;
131150049
Page 92
return(1); } } int pop( int stack[max], int *top, int *data) { if( *top == -1 ) return(-1); else { *data = stack[*top]; *top = *top - 1; return(1); } } Program Output:
131150049
Page 93
Program:30 /*Perform push and pop operations over stack using string*/ #include<stdio.h> #include<conio.h> # define max 5 void main() { char stack[max][80], data[80]; int top, option, reply; top = -1; clrscr(); do { printf("\n C Language to implement basic stack operations for String based Stack \n"); printf("\n 1. Push"); printf("\n 2. Pop"); printf("\n 3. Exit"); printf("\n Select proper option ( 1 / 2 / 3 ) : "); scanf("%d", &option); switch(option) { case 1 : printf("\n Enter a String : "); flushall(); gets(data); reply = push(stack, &top, data); if(reply == -1) printf("\n Stack is Full \n"); else printf("\n Entered String is Pushed in Stack \n"); break; case 2 : reply = pop(stack, &top, data); if(reply == -1) printf("\n Stack is Empty \n"); else printf("\n Popped String is : %s", data); printf("\n"); break; case 3 : exit(0); }
131150049
Page 94
} while(1); } int push(char stack[max][80], int *top, char data[80]) { if(*top == max -1) return(-1); else { *top = *top + 1; strcpy(stack[*top], data); return(1); } } int pop(char stack[max][80], int *top, char data[80]) { if(*top == -1) return(-1); else { strcpy(data, stack[*top]); *top = *top - 1; return(1); } } Program Output:
131150049
Page 95
Program:31 /*C program to check binary tree | binary tree in c*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #define new_node (struct node*)malloc(sizeof (struct node)) struct node { int data; struct node *lc; struct node *rc; }; struct node *create_bin_pre_rec(); void main() { struct node *r; int reply; clrscr(); printf("\nCreate a binary tree \n"); r = create_bin_pre_rec(); printf("\n The binary tree is \n"); print_bin_pre_rec(r); reply = check_complete(r); if( reply == 1 ) printf("\n Complete tree "); else printf("\n Not complete tree "); } struct node *create_bin_pre_rec() { struct node *c; int data; printf("\nData : "); scanf("%d",&data); if( data == -1) return(NULL); else { c = new_node; c->data = data; c->lc = create_bin_pre_rec(); c->rc = create_bin_pre_rec();
131150049
Page 96
return(c); } } int print_bin_pre_rec(struct node *t) { if( t != NULL) { printf("%4d",t->data); print_bin_pre_rec(t->lc); print_bin_pre_rec(t->rc); } return; } int check_complete(struct node *t) { int reply; if( t == NULL) return(1); else if( t->lc == NULL && t->rc == NULL) return(1); else if( t->lc != NULL && t->rc == NULL) return(0); else if( t->lc == NULL && t->rc != NULL) return(0); else { reply = check_complete(t->lc); if ( reply == 1 ) reply = check_complete(t->rc); return(reply); } }
131150049
Page 97
Program Input:
Program Output:
131150049
Page 98
Program:32 /*C program to check Left Skewed Binary tree*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #define new_node (struct node*)malloc(sizeof (struct node)) struct node { int data; struct node *lc; struct node *rc; }; struct node *create_bin_pre_rec(); void main() { struct node *r; int reply; clrscr(); printf("\nCreate a binary tree \n"); r = create_bin_pre_rec(); printf("\n The binary tree is \n"); print_bin_pre_rec(r); reply = check_left_skewed(r); if( reply == 1 ) printf("\n Left skewed binary tree "); else printf("\n Not left skewed binary tree "); } struct node *create_bin_pre_rec() { struct node *c; int data; printf("\nData : "); scanf("%d",&data); if( data == -1) return(NULL); else { c = new_node; c->data = data; c->lc = create_bin_pre_rec(); c->rc = create_bin_pre_rec();
131150049
Page 99
return(c); } } int print_bin_pre_rec(struct node *t) { if( t != NULL) { printf("%4d",t->data); print_bin_pre_rec(t->lc); print_bin_pre_rec(t->rc); } return; } int check_left_skewed(struct node *t) { int reply; if( t == NULL) return(1); else if( t->rc != NULL) return(0); else { reply = check_left_skewed(t->lc); return(reply); } } Program Input:
131150049
Page 100
Program Output:
131150049
Page 101
Program:33 /*C program to check right skewed binary tree*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #define new_node (struct node*)malloc(sizeof (struct node)) struct node { int data; struct node *lc; struct node *rc; }; struct node *create_bin_pre_rec(); void main() { struct node *r; int reply; clrscr(); printf("\nCreate a binary tree \n"); r = create_bin_pre_rec(); printf("\n The binary tree is \n"); print_bin_pre_rec(r); reply = check_right_skewed(r); if( reply == 1 ) printf("\n Right skewed binary tree "); else printf("\n Not right skewed binary tree "); } struct node *create_bin_pre_rec() { struct node *c; int data; printf("\nData : "); scanf("%d",&data); if( data == -1) return(NULL); else { c = new_node; c->data = data; c->lc = create_bin_pre_rec(); c->rc = create_bin_pre_rec();
131150049
Page 102
return(c); } } int print_bin_pre_rec(struct node *t) { if( t != NULL) { printf("%4d",t->data); print_bin_pre_rec(t->lc); print_bin_pre_rec(t->rc); } return; } int check_right_skewed(struct node *t) { int reply; if( t == NULL) return(1); else if( t->lc != NULL) return(0); else { reply = check_right_skewed(t->rc); return(reply); } } Program Input:
Program Output:
131150049
Page 103
Program:34 /*Count Leaf Nodes And Non-leaf Nodes Of Binary Tree*/ #include<stdio.h> #include<conio.h> #include<alloc.h> #define new_node (struct node*)malloc(sizeof (struct node)) struct node { int data; struct node *lchild; struct node *rchild; }; struct node *create_bin_rec(); void print_bin_pre_rec(struct node *t); void cnt_nodes(struct node *t, int *l, int *nl); void main() { struct node *root; int leaf,nonleaf; clrscr(); printf("\nCreate a binary tree \n"); root = create_bin_rec(); printf("\n Binary tree is "); print_bin_pre_rec(root); leaf = nonleaf = 0; cnt_nodes(root,&leaf,&nonleaf); printf("\n Total leaf nodes are : %d ",leaf); printf("\n Total non leaf nodes are : %d ",nonleaf); } struct node *create_bin_rec() { int data; struct node *t; printf("\nData ( -1 to exit ) : "); scanf("%d",&data); if( data == -1) return(NULL); else { t = new_node; t->data = data; t->lchild =create_bin_rec();
131150049
Page 104
t->rchild =create_bin_rec(); return(t); } } void print_bin_pre_rec(struct node *t) { if( t != NULL) { printf("%4d",t->data); print_bin_pre_rec(t->lchild); print_bin_pre_rec(t->rchild); } } void cnt_nodes(struct node *t, int *l, int *nl) { if( t != NULL) { if( t->lchild == NULL && t->rchild == NULL) (*l)++; else (*nl)++; cnt_nodes(t->lchild,l,nl); cnt_nodes(t->rchild,l,nl); } } Program Input:
131150049
Page 105
Program Output:
131150049
Page 106