You are on page 1of 167

http://www.itstudentjunction.com/ignou-mca-lab-solutions/MCSL-025-java-labprograms-solved.html http://www.scribd.

com/fullscreen/34482554

Program to find Multiplication of two matrices USING C LANGUAGE #include<stdio.h> # define size 10 void main() { int r1,c1,r2,c2,i,j,k,x[size][size],y[size][size]; int m[size][size]; clrscr(); printf("Enter the order of 1st matrice\n"); scanf("%d%d",&r1,&c1); printf("Enter the order of 2nd matrice\n"); scanf("%d%d",&r2,&c2); if(c1==r2) { printf("enter the %d elements of first matrix\n",r1*c1); for (i=0;i<r1;i++) { for(j=0;j<c1;j++) scanf("%d",&x[i][j]); } printf("enter the %d elements of second matrix\n",r2*c2 );

for (i=0;i<r2;i++) { for(j=0;j<c2;j++) scanf("%d",&y[i][j]); } printf("The given first matrix is:\n"); for (i=0;i<r1;i++) { for(j=0;j<c1;j++) printf("%d\t",x[i][j]); printf ("\n"); } printf("The given second matrix is:\n"); for (i=0;i<r2;i++) { for(j=0;j<c2;j++) printf("%d\t",y[i][j]); printf ("\n"); } for (i=0;i<r1;i++) { for(j=0;j<c2;j++) { m[i][j]=0; for(k=0;k<r2;k++)

m[i][j]=m[i][j]+x[i][k]*y[k][j]; } } printf("The product is:\n"); for (i=0;i<r1;i++) { for (j=0;j<c2;j++) printf(" %d\t",m[i][j]); printf("\n"); } } else printf ("multiplication is not possible"); getch(); }

PROGRAM TO FIND TRANSPOSE OF A MATRIX IN C LANGUAGE #include<stdio.h> #include<conio.h> main () { int a[3][3], b[3][3];

readmatrix(a); transpose(a,b); printmatrix(b); getch(); }

readmatrix(m) int m[][3]; { int i, j; for(i=0;i<3;i++) for (j=0; j<3;j++) scanf("%d", &m[i][j]); }

transpose(a,b) int a[][3], b[][3]; { int i, j;

for(i=0;i<3;i++) for (j=0; j<3;j++) { b[j][i]=a[i][j]; } } printmatrix(m) int m[][3]; { int i,j; for(i=0;i<3;i++) { for (j=0; j<3;j++) printf("%d ", m[i][j]); printf("\n"); } }

PROGRAM TO ACCEPT 10 STRINGS AS INPUT & PRINT IN LEXICOGRAPHIC ORDER USING C LANGUAGE
#include <stdio.h>

#include <string.h> void main() { char a[4][25],temp[25]; int i,j; clrscr(); printf("Enter the names\n"); for (i=0;i<4;i++) gets(a[i]); for (i=0;i<3;i++) for (j=i+1;j<4;j++) { if (strcmp(a[i],a[j])>0) { strcpy(temp,a[i]); strcpy(a[i],a[j]); strcpy(a[j],temp); } } printf("Sorted strings are \n"); for (i=0;i<4;i++) puts (a[i]); getch(); }

PROGRAM TO TWO STRINGS S1,S2 & CHECK IF S2 IS SUB STRING OF S1 & ALSO THE POSITION OF THE SUB STRING IN S1
#include<stdio.h> #include <string.h> void main() { char st1[25],st2[25]; int cnt,i,j,k,c,len,m,sign; clrscr(); printf("Enter the first string\n"); gets(st1); printf("Enter the second string\n"); gets(st2); len=strlen(st1); for(i=0;i<len;i++) { c=0; if (st1[i]==st2[c]) { m=i; sign=0; cnt=0; while(st2[c]!='\0' && sign!=1)

{
if (st1[m]==st2[c])

{ m++;c++; cnt++; } else sign=1; } if (sign==0) { printf("The given string is present\n"); printf("The starting position %d & ending position %d\n",i+1,(i+cnt)); k=1; } } } if (k != 1) if (sign!=0) printf("The given string is not present\n"); getch(); }

PROGRAM TO TWO STRINGS S1,S2 & CHECK IF S2 IS SUB STRING OF S1 & ALSO THE POSITION OF THE SUB STRING IN S1
#include<stdio.h> #include <string.h>

void main() { char st1[25],st2[25]; int cnt,i,j,k,c,len,m,sign; clrscr(); printf("Enter the first string\n"); gets(st1); printf("Enter the second string\n"); gets(st2); len=strlen(st1); for(i=0;i<len;i++) { c=0; if (st1[i]==st2[c]) { m=i; sign=0; cnt=0; while(st2[c]!='\0' && sign!=1)

{
if (st1[m]==st2[c]) { m++;c++; cnt++; }

else sign=1; } if (sign==0) { printf("The given string is present\n"); printf("The starting position %d & ending position %d\n",i+1,(i+cnt)); k=1; } } } if (k != 1) if (sign!=0) printf("The given string is not present\n"); getch(); }

PROGRAM TO CONCATENATE TWO STRINGS S1 & S2 USING C LANGUAGE #include<stdio.h> #include<string.h> void main() { char str1[10],str2[10],str[20]; int i=0,j=0,k=0;

clrscr(); printf ("enter the two strings\n"); gets (str1); gets (str2); while (str1[i]!='\0') { str[k++]=str1[i++]; } while (str2[j]!='\0') { str[k++]=str2[j++]; } str[k]='\0'; printf("The concatenated string is:"); puts (str); getch(); }

ROGRAM TO FIND THE STUDENT INFORMATION & PRINT THE STUDENT INFORMATION & RANK SECURED IN ASCENDING ORDER - C LANGUAGE.
#include<stdio.h> #include<conio.h> #define SIZE 50 void main() {

int num,i,j; int temp=0,tempe=0; char tempn[50]; struct student { int eno ; char name[50]; int avg; } st[SIZE]; clrscr(); printf("Enter the number of students\n"); scanf("%d",&num); for(i=0;i<num;i++) { printf("Enter the name of the student\n"); scanf("%s",&st[i].name); printf("Enter the enrollment number\n"); scanf("%d",&st[i].eno); printf("Enter aggregate marks of enter students \n"); scanf("%d",&st[i].avg); } for(i=0;i<num-1;i++) for (j=i+1;j<num;j++) { temp=0;tempe=0;

if (st[i].avg<st[j].avg) { temp=st[i].avg; st[i].avg=st[j].avg; st[j].avg=temp; strcpy(tempn,st[i].name); strcpy(st[i].name,st[j].name); strcpy(st[j].name,tempn); tempe=st[i].eno; st[i].eno=st[j].eno; st[j].eno=tempe; } } for(i=0;i<num;i++) { printf("Enrollment number:%d\n Name:%s\n",st[i].eno,st[i].name); printf("Aggregate marks:%d\n Rank:%drank\n",st[i].avg,(i+1)); } getch(); }

PROGRAM TO MULTIPLY TWO SPARSE MATRICES USING C LANGUAGE #include<stdio.h> #include<conio.h> #include<alloc.h>

#define MAX1 3 #define MAX2 3 #define MAXSIZE 20 #define TRUE 1 #define FALSE 2 struct sparse { int *sp ; int row ; int *result ; }; void initsparse ( struct sparse * ) ; void create_array ( struct sparse * ) ; int count ( struct sparse ) ; void display ( struct sparse ) ; void create_tuple ( struct sparse*, struct sparse ) ; void display_tuple ( struct sparse ) ; void prodmat ( struct sparse *, struct sparse, struct sparse ) ; void searchina ( int *sp, int ii, int*p, int*flag ) ; void searchinb ( int *sp, int jj, int colofa, int*p, int*flag ) ; void display_result ( struct sparse ) ; void delsparse ( struct sparse * ) ; void main( )

{ struct sparse s[5] ; int i ; clrscr( ) ; for ( i = 0 ; i<= 3 ; i++ ) initsparse ( &s[i] ) ; create_array ( &s[0] ) ; create_tuple ( &s[1], s[0] ) ; display_tuple ( s[1] ) ; create_array ( &s[2] ) ; create_tuple ( &s[3], s[2] ) ; display_tuple ( s[3] ) ; prodmat ( &s[4], s[1], s[3] ) ; printf ( "\nResult of multiplication of two matrices: " ) ; display_result ( s[4] ) ; for ( i = 0 ; i<= 3 ; i++ ) delsparse ( &s[i] ) ; getch( ) ; } /* initialises elements of structure */ void initsparse ( struct sparse *p ) { p -> sp = NULL ;

p -> result = NULL ; } /* dynamically creates the matrix */ void create_array ( struct sparse *p ) { int n, i ; /* allocate memory */ p -> sp = ( int * ) malloc ( MAX1 * MAX2 * sizeof ( int ) ) ; /* add elements to the array */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { printf ( "Enter element no. %d: ", i ) ; scanf ( "%d", &n ) ; * ( p -> sp + i ) = n ; } } /* displays the contents of the matrix */ void display ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) {

/* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ; } } /* counts the number of non-zero elements */ int count ( struct sparse s ) { int cnt = 0, i ; for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { if ( * ( s.sp + i ) != 0 ) cnt++ ; } return cnt ; } /* creates an array that stores information about non-zero elements */ void create_tuple ( struct sparse *p, struct sparse s ) { int r = 0 , c = -1, l = -1, i ; /* get the total number of non-zero elements */ p -> row = count ( s ) + 1 ;

/* allocate memory */ p -> sp = ( int * ) malloc ( p -> row * 3 * sizeof ( int ) ) ; /* store information about total no. of rows, cols, and non-zero values */ * ( p -> sp + 0 ) = MAX1 ; * ( p -> sp + 1 ) = MAX2 ; * ( p -> sp + 2 ) = p -> row - 1 ; l=2; /* scan the array and store info. about non-zero values in the 3-tuple */ for ( i = 0 ; i< MAX1 * MAX2 ; i++ ) { c++ ; /* sets the row and column values */ if ( ( ( i % 3 ) == 0 ) && ( i != 0 ) ) { r++ ; c=0; } /* checks for non-zero element, row, column and non-zero value is assigned to the matrix */ if ( * ( s.sp + i ) != 0 ) { l++ ; * ( p -> sp + l ) = r ; l++ ;

* ( p -> sp + l ) = c ; l++ ; * ( p -> sp + l ) = * ( s.sp + i ) ; } } } /* displays the contents of the matrix */ void display_tuple ( struct sparse s ) { int i, j ; /* traverses the entire matrix */ printf ( "\nElements in a 3-tuple: " ) ; j = ( * ( s.sp + 2 ) * 3 ) + 3 ; for ( i = 0 ; i< j ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.sp + i ) ) ; } printf ( "\n" ) ; } /* performs multiplication of sparse matrices */

void prodmat ( struct sparse *p, struct sparse a, struct sparse b ) { int sum, k, position, posi, flaga, flagb, i , j ; k=1; p -> result = ( int * ) malloc ( MAXSIZE * 3 * sizeof ( int ) ) ; for ( i = 0 ; i< * ( a.sp + 0 * 3 + 0 ) ; i++ ) { for ( j = 0 ; j< * ( b.sp + 0 * 3 + 1 ) ; j++ ) { /* search if an element present at ith row */ searchina ( a.sp, i, &position, &flaga ) ; if ( flaga == TRUE ) { sum = 0 ; /* run loop till there are element at ith row in first 3-tuple */ while ( * ( a.sp + position * 3 + 0 ) == i ) { /* search if an element present at ith col. in second 3-tuple */ searchinb ( b.sp, j, * ( a.sp + position * 3 + 1 ), &posi, &flagb ) ; /* if found then multiply */ if ( flagb == TRUE ) sum = sum + * ( a.sp + position * 3 + 2 ) * * ( b.sp + posi * 3 + 2 ) ; position = position + 1 ;

} /* add result */ if ( sum != 0 ) { * ( p -> result + k * 3 + 0 ) = i ; * ( p -> result + k * 3 + 1 ) = j ; * ( p -> result + k * 3 + 2 ) = sum ; k=k+1; } } } } /* add total no. of rows, cols and non-zero values */ * ( p -> result + 0 * 3 + 0 ) = * ( a.sp + 0 * 3 + 0 ) ; * ( p -> result + 0 * 3 + 1 ) = * ( b.sp + 0 * 3 + 1 ) ; * ( p -> result + 0 * 3 + 2 ) = k - 1 ; } /* searches if an element present at iith row */ void searchina ( int *sp, int ii, int *p, int *flag ) { int j ; *flag = FALSE ; for ( j = 1 ; j<= * ( sp + 0 * 3 + 2 ) ; j++ )

{ if ( * ( sp + j * 3 + 0 ) == ii ) { *p = j ; *flag = TRUE ; return ; } } } /* searches if an element where col. of first 3-tuple is equal to row of second 3-tuple */ void searchinb ( int *sp, int jj, int colofa, int *p, int *flag ) { int j ; *flag = FALSE ; for ( j = 1 ; j<= * ( sp + 0 * 3 + 2 ) ; j++ ) { if ( * ( sp + j * 3 + 1 ) == jj && * ( sp + j * 3 + 0 ) == colofa ) { *p = j ; *flag = TRUE ; return ; } }

} /* displays the contents of the matrix */ void display_result ( struct sparse s ) { int i ; /* traverses the entire matrix */ for ( i = 0 ; i< ( * ( s.result + 0 + 2 ) + 1 ) * 3 ; i++ ) { /* positions the cursor to the new line for every new row */ if ( i % 3 == 0 ) printf ( "\n" ) ; printf ( "%d\t", * ( s.result + i ) ) ; } } /* deallocates memory */ void delsparse ( struct sparse *s ) { if ( s -> sp != NULL ) free ( s -> sp ) ; if ( s -> result != NULL ) free ( s -> result ) ; } PROGRAM TO TAKE PARAGRAPH AS INPUT AND OUTPUT NO, OF OCCURRENCES OF STRING IN PARAGRAPH USING C LANGUAGE

#include<stdio.h> #include <string.h> void main() { char st1[25],st2[25]; int cnt,i,j,k,c,len,m,sign; clrscr(); printf("Enter the first string\n"); gets(st1); printf("Enter the second string\n"); gets(st2); len=strlen(st1); for(i=0;i<len;i++) { c=0; if (st1[i]==st2[c]) { m=i; sign=0; cnt=0; while(st2[c]!='\0' && sign!=1)

{
if (st1[m]==st2[c]) { m++;c++;

cnt++; } else sign=1; } if (sign==0) { printf("The given string is present\n"); printf("The starting position %d & ending position %d\n",i+1,(i+cnt)); k=1; } } } if (k != 1) if (sign!=0) printf("The given string is not present\n"); getch(); }

Linkd list mising

PROGRAM TO REVERSE AN INPUT STRING USING C LANGUAGE


#include<stdio.h> #include<string.h> #define STACK_SIZE 20

void push(char item,int *top,char s[]) { if (*top==STACK_SIZE-1) { printf("\n stack overflow\n"); return; } s[++(*top)]=item; }

char pop(int *top,char s[]) { char item_deleted; if (*top==-1) { return 0; } item_deleted=s[(*top)--]; return item_deleted; }

int is_rev(char str[]) { int i; int top=-1;

char s[30] ; char stk_item=0;

for(i=0;i<strlen(str);i++) { push (str[i],&top,s); } printf("\n The reversed string is:"); for(i=0;i<strlen(str);i++) { stk_item= pop (&top,s); printf("%c",stk_item); } getch(); } void main() { char str[20]; clrscr(); printf("\n Enter the string to be reversed\n"); scanf("%s",str); is_rev(str); }

PROGRAM TO CONVERT A PREFIX EXPRESSION TO A POST FIX USING POINTERS USING C LANGUAGE
#include<stdio.h> #include<string.h> void push(char item[],int *top,char s[][20]) { *top=*top+1; strcpy(s[*top],item); }

void *pop(int *top,char s[][20]) { char *item; item=s[*top]; *top=*top-1; return item; }

void pre_post(char prefix[],char postfix[]) { char s[20][20]; int top,i; char symbol,temp[2]; char *op1,*op2;

top=-1; strrev(prefix); for(i=0;i<strlen(prefix);i++) { symbol=prefix[i]; temp[0]=symbol; temp[1]='\0'; switch (symbol) { case '+': case '-': case '*': case '/': case '^': op1=pop(&top,s); op2=pop(&top,s);

strcpy(postfix,op1); strcat(postfix,op2); strcat(postfix,temp); push(postfix,&top,s); break; default: push(temp,&top,s); }

void main() { char prefix[20]; char postfix[20]; printf("\n\n Enter the prefix expression \n\n"); scanf("%s",prefix); pre_post(prefix,postfix); printf("\n\n The postfix expression is %s \n\n",postfix); }

PROGRAM TO CREATE ADD REMOVE & DISPLAY ELEMENT FROM SINGLE LINKED LIST using c program or PROGRAM TO IMPLEMENT SINGLE LINKED LIST IN c language

#include<stdio.h> #include<stdlib.h> #include<string.h> struct info { char name[30]; int eno;

struct info *next; };

struct info *head=NULL,*temp,*disp; void addrecord(); void deleterecord(); void disrecord();

void main() { int ch; clrscr(); while (1) { printf("\n 1. To add records\n"); printf("\n 2. To delete a records\n"); printf("\n 3. To view the records\n"); printf("\n 4. To exit\n"); printf("\n Enter your choice\n"); scanf("%d",&ch); fflush(stdin); switch(ch) { case 1:addrecord(); break;

case 2:deleterecord(); break; case 3: disrecord(); break; case 4:exit(0); } } }

void addrecord() { struct info *add; char ans='y';

while (ans=='y') { add=(struct info*)malloc(sizeof(struct info)); printf("\n Enter the names:\n"); gets(add->name); fflush(stdin); printf("\n Enter the enrollment number:\n"); scanf("%d",&add->eno); fflush(stdin); if (head==NULL) {

head=add; add->next=NULL;

temp=add; } else { temp->next=add; add->next=NULL; temp=add; } printf("\n Would you like to enter another name(y\\n): \n"); ans = getchar(); fflush(stdin); }

} void deleterecord() { struct info *delete; int teno, present=0;

if (head==NULL) { printf("\n No records to delete\n"); return; } printf("\n Enter the enrollment number to be deleted \n"); scanf("%d",&teno); fflush(stdin);

for (delete=head;delete!=NULL;delete=delete->next) { if (delete->eno==teno) { if (head->eno==teno) { delete=head; head=head->next; free(delete); return; } else { temp->next=delete->next; free(delete); return;

} } temp=delete; }

if (present==0) printf("\nNo such enrollment number present\n"); } void disrecord() { if (head==NULL) { printf("\n No records to view\n"); return; } for (disp=head;disp!=NULL;disp=disp->next) { printf("\n\n Name : %s",disp->name);

printf("\n\n Number : %d",disp->eno); } }

PROGRAM TO CREATE ADD REMOVE & DISPLAY ELEMENT FROM DOUBLE LINKED LIST USING C LANGUAGE
#include<stdio.h>

#include<stdlib.h> struct info { char name[30]; int eno; struct info *next; struct info *prev; }; struct info *head=NULL,*temp,*disp;

void main() { void addrecord(); void deleterecord(); void disrecord(); int ch; clrscr(); while (1) { printf("\n 1. To add records\n"); printf("\n 2. To delete a records\n"); printf("\n 3. To view the records\n"); printf("\n 4. To exit\n"); printf("\n Enter your choice\n"); scanf("%d",&ch);

fflush(stdin); switch(ch) { case 1:addrecord(); break; case 2:deleterecord(); break; case 3: disrecord(); break; case 4:exit(0); } } }

void addrecord() { struct info *add; char ans='y';

while (ans=='y') { add=(struct info*)malloc(sizeof(struct info)); printf("\n Enter the names:\n"); gets(add->name); fflush(stdin);

printf("\n Enter the enrollment number:\n"); scanf("%d",&add->eno); fflush(stdin); if (head==NULL) { head=add; add->next=NULL; add->prev=NULL; temp=add; } else { temp->next=add; add->prev=temp; add->next=NULL; temp=add; } printf("\n Would you like to enter another name(y\\n): \n"); ans = getchar(); fflush(stdin); }

} void deleterecord() {

struct info *del; int teno;

if (head==NULL) { printf("\n No records to delete\n"); return; }

printf("\n Enter the enrollment number to be deleted \n"); scanf("%d",&teno); fflush(stdin);

del=(struct info*)malloc(sizeof (struct info)); del=head->next; if (head->eno==teno) { printf("\n Head data cannot be deleted\n"); return; } while(del) { if(del->eno==teno) { del->prev->next=del->next;

if (del->next!=NULL) { del->prev->next=del->next; del->next->prev=del->prev; } else { head->next=temp->next=NULL; temp=head; } return; } else { del=del->next; } }

printf("\nInvalid input\n"); } void disrecord() { if (head==NULL) { printf("\n No records to view\n");

return; } printf("\n From forward direction\n"); for (disp=head;disp!=NULL;disp=disp->next) { printf("\n\n Name : %s",disp->name);

printf("\n\n Number : %d",disp->eno); } printf("\n Press any key to continue\n"); getchar(); printf("\n From backward direction\n"); for (disp=temp;disp!=NULL;disp=disp->prev) { printf("\n\n Name : %s",disp->name);

printf("\n\n Number : %d",disp->eno); }

PROGRAM TO COUNT NUMBER OF NODES IN LINEAR LINKED LIST USING C PROGRAM

#include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h>

#include < ctype.h> struct linear_list { int info; struct linear_list *next; }*start,*newnode,*ptr; void main() { int item,num; int i; char ch; clrscr(); newnode=(struct linear_list*)malloc(sizeof(struct linear_list)); start=newnode; do { printf("\nEnter data: "); scanf("%d",&item); newnode->info=item; printf("\nDo you want to create another node:(y/n)"); fflush(stdin); scanf("%c",&ch);

if(tolower(ch)=='y') { newnode->next=(struct linear_list*)malloc(sizeof(struct linear_list)); newnode=newnode->next; } else { newnode->next=NULL; } }while(tolower(ch)!='n'); printf(\n Linked List is:\n); ptr=start; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } num=0; ptr=start; while(ptr!=NULL) {

ptr=ptr->next; num++; } printf("\nThe number of nodes in the linked list are: %d",num); getch(); } PROGRAM TO CREATE ADD REMOVE & DISPLAY ELEMENT FROM CIRCULAR LINKED LIST using c language OR PROGRAM TO IMPLEMENT CIRCULAR LINKED LIST - c Language
#include<stdio.h> #include<alloc.h> #include<conio.h> struct node { int data; struct node *next; }; struct node *head=NULL; struct node *tail=NULL; void main() { void addrecord(); void deleterecord(); void disrecord();

int ch; clrscr(); do { printf("\n 1. To add records\n"); printf("\n 2. To delete a records\n"); printf("\n 3. To view the records\n"); printf("\n 4. To exit\n"); printf("\n Enter your choice\n"); scanf("%d",&ch);

fflush(stdin); switch(ch) { case 1:addrecord(); break; case 2:deleterecord(); break; case 3: disrecord(); break; case 4:exit(0); } } while (ch!=4); }

void addrecord() { int new_data; char ans='y'; struct node *ptr,*prev,*temp; clrscr();

while (ans=='y') { temp=(struct node*)malloc(sizeof(struct node)); printf("\n Enter the new element:\n"); scanf("%d",&new_data); fflush(stdin); temp->data=new_data; temp->next=NULL; if (head==NULL) { head=tail=temp; temp->next=head;

} else { tail->next=temp; tail=temp;

} printf("\n Would you like to enter another data(y\\n): \n"); ans = getchar(); fflush(stdin); }

} void deleterecord() { struct node *ptr,*prev,*delnode; int elt;

printf("\n Enter the enrollment number to be deleted \n"); scanf("%d",&elt); fflush(stdin);

if (head==NULL) { printf("\n No elements in the list \n"); return; } else { if (head->data==elt) {

delnode=head; if (head==tail) head=tail=NULL; else { head=head->next; tail->next=head; } } else if (tail->data==elt) { for(ptr=head;(ptr!=tail);prev=ptr,ptr=ptr->next); delnode=tail; tail=prev; tail->next=head; } else { for(prev=ptr=head;(ptr->data!=elt)&&(ptr!=tail); prev=ptr,ptr=ptr->next); if(ptr->data==elt) { delnode=ptr; prev->next=ptr->next; printf("yes...");

} else { printf("Given element not found in the list"); getch(); return; } } } free(delnode); }

void disrecord() { struct node *ptr,*prev=NULL;

if (head==NULL) { printf("\n No records to view\n"); return; } printf("\n The elements in the circular list are\n"); for (ptr=head;prev!=tail;prev=ptr,ptr=ptr->next) printf("\n\n %d",ptr->data); printf(" NULL\n\n ");

getch(); }

PROGRAM TO ACCEPT 2 SINGLY LINKED LISTS & PRINT A SINGLY LINKED LIST USING C LANGUAGE #include<stdio.h> #include<stdlib.h> struct info { int num; struct info *next; };

struct node { int num1; struct node *next1; };

struct com { int num2; struct com *next2; };

struct info *temp,*disp,*head; struct node *temp1,*disp1,*head1; struct com *temp2,*disp2,*head2=NULL;

void addrecord(); void disrecord();

void main() { int ch; clrscr(); while (1) { printf("\n 1. To add records\n"); printf("\n 2. To view the records\n"); printf("\n 3. To exit\n"); printf("\n Enter your choice\n"); scanf("%d",&ch); fflush(stdin); switch(ch) { case 1:addrecord(); break; case 2:disrecord(); break;

case 3: exit(0);

} } }

void addrecord() { struct info *add; struct node *add1;

char ans='y'; char choice='y';

while (ans=='y') { add=(struct info*)malloc(sizeof(struct info)); printf("\n Enter the element of the first list:\n"); scanf("%d",&add->num); fflush(stdin); if (head==NULL|| head->num>=add->num) { add->next=head; head=add;

} else { temp=head; while (temp->next!=NULL && temp->next->num < add->num) { temp=temp->next; } add->next=temp->next; temp->next=add; } printf("\n Would you like to enter another name(y\\n): \n"); ans = getchar(); }

while (choice=='y') { add1=(struct node*)malloc(sizeof(struct node)); printf("\n Enter the element of the second list:\n"); scanf("%d",&add1->num1); fflush(stdin); if (head1==NULL|| head1->num1>=add1->num1) { add1->next1=head1; head1=add1;

} else { temp1=head1; while (temp1->next1!=NULL && temp1->next1->num1 < add1->num1) { temp1=temp1->next1; } add1->next1=temp1->next1; temp1->next1=add1; } printf("\n Would you like to enter another name(y\\n): \n"); choice = getchar(); fflush(stdin); } }

void disrecord() { struct com *add2; if (head==NULL) { printf("\n No records to view\n"); return; }

for (disp=head;disp!=NULL;disp=disp->next) { printf("\n\n Number : %d",disp->num); } for (disp1=head1;disp1!=NULL;disp1=disp1->next1) { printf("\n\n Number : %d",disp1->num1); } for (disp=head;disp!=NULL;disp=disp->next) { for (disp1=head1;disp1!=NULL;disp1=disp1->next1) { if (disp->num==disp1->num1) { add2=(struct com*)malloc(sizeof(struct com)); add2->num2=disp->num; printf("%d",add2->num2); if(head2==NULL) { head2= add2; add2->next2=NULL; temp2=add2; } else {

temp2->next2=add2; add2->next2=NULL; temp2=add2; } } } } printf("\n Sorted list is \n\n"); for (disp2=head2;disp2!=NULL;disp2=disp2->next2) { printf("\n\n Number : %d",disp2->num2); }

PROGRAM TO SORT LINKED LIST IN ASCENDING ORDER USING C LANGUAGE


#include<stdio.h> #include<stdlib.h> struct info { char name[30]; int eno; struct info *next; }; struct info *temp,*disp,*head;

void addrecord(); void disrecord();

void main() { int ch; clrscr(); while (1) { printf("\n 1. To add records\n"); printf("\n 2. To view the records\n"); printf("\n 3. To exit\n"); printf("\n Enter your choice\n"); scanf("%d",&ch); fflush(stdin); switch(ch) { case 1:addrecord(); break; case 2:disrecord(); break; case 3: exit(0);

} }

void addrecord() { struct info *add; char ans='y';

while (ans=='y') { add=(struct info*)malloc(sizeof(struct info)); printf("\n Enter the name:\n"); gets(add->name); fflush(stdin); printf("\n Enter the enrollment number:\n"); scanf("%d",&add->eno); fflush(stdin); if (head==NULL|| head->eno>=add->eno) { add->next=head; head=add; } else { temp=head;

while (temp->next!=NULL && temp->next->eno < add->eno) { temp=temp->next; } add->next=temp->next; temp->next=add; } printf("\n Would you like to enter another name(y\\n): \n"); ans = getchar(); fflush(stdin); } }

void disrecord() { if (head==NULL) { printf("\n No records to view\n"); return; } for (disp=head;disp!=NULL;disp=disp->next) { printf("\n\n Name : %s",disp->name);

printf("\n\n Number : %d",disp->eno); }

WRITE A PROGRAM TO IMPLEMENT STACK USING IN C PROGRAM TO IMPLEMENT STACK USING ARRAYS USING C LANGUAGE #include<stdio.h> struct stack { int info; struct stack *next; }; typedef struct stack node; class stlink { node *start; public: stlink() { start=NULL; } void display(void); void push(int); int pop(void); }; void stlink::push(int term) { node *p,*s; s=start; if(s==NULL||s!=NULL) { p=(node *)malloc(sizeof(node)); p->info=term; p->next=s; s=p; } start=s; return; } void stlink::display(void) { node *temp; if(start==NULL)

{ cout << endl<<"UNDERFLOEW"; } temp=start; while(temp!=NULL) { cout << endltemp=temp->next; } return; } int stlink::pop(void) { int term; if(start==NULL) { cout<<"UNDERFLOW"; return(-1); } else { node *p; term=start->info; p=start; free(start); start=p->next; return(term); } } int main() { stlink s1; int ch,temp; do { clrscr(); cout<<"1->Push\n"; cout<<"2->Display\n"; cout<<"3->Pop\n"; cout<<"4->Exit\n"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case'1': cout<<"Enter the term to push:"; cin>>temp;

s1.push(temp); break; case'2': cout << endl<<"Stack"; s1.display(); getch(); break; case'3': temp=s1.pop(); if(temp!=-1) cout<<"Popped term is " << temp; getch(); break; case'4': cout<<"Exiting"; getch(); break; default: cout<<"Invalid choice"; getch(); break; } }while(ch!=4); return(0); }

WRITE A PROGRAM TO IMPLEMENT STACK USING LINKED LIST USING C LANGUAGE #include<stdio.h> struct stack { int info; struct stack *next; }; typedef struct stack node; class stlink { node *start; public: stlink() { start=NULL; } void display(void);

void push(int); int pop(void); }; void stlink::push(int term) { node *p,*s; s=start; if(s==NULL||s!=NULL) { p=(node *)malloc(sizeof(node)); p->info=term; p->next=s; s=p; } start=s; return; } void stlink::display(void) { node *temp; if(start==NULL) { cout << endl<<"UNDERFLOEW"; } temp=start; while(temp!=NULL) { cout << endltemp=temp->next; } return; } int stlink::pop(void) { int term; if(start==NULL) { cout<<"UNDERFLOW"; return(-1); } else { node *p; term=start->info; p=start; free(start); start=p->next;

return(term); } } int main() { stlink s1; int ch,temp; do { clrscr(); cout<<"1->Push\n"; cout<<"2->Display\n"; cout<<"3->Pop\n"; cout<<"4->Exit\n"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case'1': cout<<"Enter the term to push:"; cin>>temp; s1.push(temp); break; case'2': cout << endl<<"Stack"; s1.display(); getch(); break; case'3': temp=s1.pop(); if(temp!=-1) cout<<"Popped term is " << temp; getch(); break; case'4': cout<<"Exiting"; getch(); break; default: cout<<"Invalid choice"; getch(); break; } }while(ch!=4); return(0); }

PROGRAM TO IMPLEMENT MULTIPLE STACK IN A SINGLE ARRAY USING C LANGUAGE OR Implement three stacks using a single array OR WRITE A PROGRAM TO IMPLEMENT MORE THAN ONE STACK IN SINGLE ARRAY #include <STDIO.H> #define MAX 10 int stack[MAX],topA=-1,topB=MAX; void pushA(int no) { if(topA==topB) { printf("\n OVERFLOW"); return; } stack[++(topA)]=no; } int popA() { if(topA==-1) { printf("\n UNDERFLOW"); return -999; } return stack[(topA)--]; }

void showA() { int i; if(topA==-1) { printf("\n stack Empty"); return; } for(i=topA;i>=0;i--) { printf("\n %d",stack[i]);

} } void pushB(int no) { if(topB-1==topA) { printf("\n OVERFLOW"); return; } stack[--(topB)]=no; } int popB() { if(topB==MAX) { printf("\n UNDERFLOW"); return -999; } return stack[(topB)--]; } void showB() { int i; if(topB==MAX) { printf("\n stack Empty"); return; } for(i=topB;i { printf("\n %d",stack[i]); } }

PROGRAM TO IMPLEMENT EXPRESSION TREE USING STACKS FIND ITS INORDER, PREORDER & POST ORDER TRAVERSALS USING C LANGUAGE

#include<STDIO.H> #include<CONIO.H> #include<MALLOC.H> typedef struct tree { char data; struct tree *left; struct tree *right; }*pos; pos stack[30]; int top=-1; pos newnode(char b) { pos temp; temp=(struct tree*)malloc(sizeof(struct tree)); temp->data=b; temp->left=NULL; temp->right=NULL; return(temp); } void push(pos temp) { stack[++top]=temp; } pos pop() { pos p; p=stack[top--]; return(p); } void inorder(pos t) { if(t!=NULL) { inorder(t->left); printf(%s,t->data); inorder(t->right); } } void preorder(pos t) { if(t!=NULL) { printf(%s,t->data); preorder(t->left);

inorder(t->right); } } void postorder(pos t) { if(t!=NULL) { postorder(t->left); postorder(t->right); printf(%s,t->data); } } void main() { char a[20];pos temp,t;int j,i; clrscr(); printf(\nEnter the postfix expression); gets(a); for(i=0;a[i]!=NULL;i++) { if(a[i]==* || a[i]==/ || a[i]==+ || a[i]==-') { temp=newnode(a[i]); temp->right=pop(); temp->left=pop(); push(temp); } else { temp=newnode(a[i]); push(temp); } } inorder(temp); printf(\n); preorder(temp); printf(\n); postorder(temp); getch(); } PROGRAM TO CONVERT INFIX NOTATION TO POSTFIX NOTATION USING STACKS IN C

#include<stdio.h> #include<conio.h> #include<string.h> #define MAX 20 char stack[MAX]; int top=-1; char pop(); void push(char item); int prcd(char symbol) { switch(symbol) { case '+': case '-':return 2; break; case '*': case '/':return 4; break; case '^': case ':return 6; break; case '(': case ')':

case '#':return 1; break; } } int isoperator(char symbol) { switch(symbol) { case '+': case '-': case '*': case '/': case '^': case ': case '(': case ')':return 1; break; default:return 0; } } void convertip(char infix[],char postfix[]) { int i,symbol,j=0;

stack[++top]='#'; for(i=0;i<strlen(infix);i++) { symbol=infix[i]; if(isoperator(symbol)==0) { postfix[j]=symbol; j++; } else{ if(symbol=='(')push(symbol); else if(symbol==')') { while(stack[top]!='(') { postfix[j]=pop(); j++; } pop();//pop out (. } else{ if(prcd(symbol)>prcd(stack[top])) push(symbol);

else{ while(prcd(symbol)<=prcd(stack[top])) { postfix[j]=pop(); j++; } push(symbol); }//end of else. }//end of else. }//end of else. }//end of for. while(stack[top]!='#') { postfix[j]=pop(); j++; } postfix[j]='\0';//null terminate string. } void main() { char infix[20],postfix[20]; clrscr(); printf("Enter the valid infix string:\n");

gets(infix); convertip(infix,postfix); printf("The corresponding postfix string is:\n"); puts(postfix); getch(); } void push(char item) { top++; stack[top]=item; } char pop() { char a; a=stack[top]; top--; return a; }

PROGRAM TO IMPLEMENT QUEUE USING ARRAYS USING C LANGUAGE #include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h>

#include < ctype.h> #define SIZE 5 void menu(); void display(); int underflow(); int overflow(); void enqueue(int); void dequeue(); int queue[SIZE]; int front=-1; int rear=-1; void main() { clrscr(); menu(); } void menu() { int choice,item; printf("MENU"); printf("\n1. Insert into the queue"); printf("\n2. Delete from queue"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); if(overflow()==0) { printf("\nEnter the item tobe inserted: "); scanf("%d",&item); enqueue(item); clrscr(); printf("\nAfter inserting queue is:\n"); } display(); getch(); clrscr();

menu(); break; case 2: clrscr(); if(underflow()==1) { dequeue(); if(underflow()==1) { printf("\nAfter deletion queue is:\n"); display(); } } getch(); clrscr(); menu(); break; case 3: clrscr(); if(underflow()==1) { printf("The queue is:\n"); display(); } getch(); clrscr(); menu(); break; case 4: exit(1); default: clrscr(); printf("Your choice is wrong\n\n"); menu();

int underflow() { if((front==-1)&&(rear==-1)) { printf("\nQueue is empty"); return(0); } else

{ } } return(1);

int overflow() { if(rear==SIZE-1) { printf("\nQueue is full\n"); return(1); } else { return(0); } } void enqueue(int item) { if((front==-1)&&(rear==-1)) { front=0; rear=0; } else { rear=rear+1; } queue[rear]=item; } void dequeue() { if(front==rear) { front=-1; rear=-1; } else { front=front+1; } } void display()

int i; for(i=front;i<=rear;i++) { printf("\nElement %d : %d",i+1,queue[i]); }

PROGRAM TO IMPLEMENT QUEUE USING Pointers in c

#include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h> struct linear_queue { int info; struct linear_queue *next; }*front,*rear,*newnode,*ptr; void menu(); void display(); int underflow(); void enqueue(int); void dequeue(); void main() { clrscr(); menu(); } void menu() { int choice,item; printf("MENU"); printf("\n1. Insert into the queue");

printf("\n2. Delete from queue"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); printf("\nEnter the item tobe inserted: "); scanf("%d",&item); enqueue(item); clrscr(); printf("\nAfter inserting queue is:\n"); display(); getch(); clrscr(); menu(); break; case 2: clrscr(); if(underflow()==1) { dequeue(); if(underflow()==1) { printf("\nAfter deletion queue is:\n"); display(); } } getch(); clrscr(); menu(); break; case 3: clrscr(); if(underflow()==1) { printf("The queue is:\n"); display(); } getch(); clrscr(); menu(); break; case 4:

exit(1); default: clrscr(); printf("Your choice is wrong\n\n"); menu(); } } int underflow() { if((front==NULL)&&(rear==NULL)) { printf("\nQueue is empty"); return(0); } else { return(1); } } void enqueue(int item) { newnode=(struct linear_queue*)malloc(sizeof(struct linear_queue)); newnode->info=item; if((front==NULL)&&(rear==NULL)) { front=newnode; rear=newnode; newnode->next=NULL; } else { rear->next=newnode; newnode->next=NULL; rear=newnode; } } void dequeue() { if(front==rear) { front=NULL; rear=NULL; }

else { front=front->next; } } void display() { int i; ptr=front; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } }

PROGRAM TO IMPLEMENT CIRCULAR QUEUE USING ARRAYS USING C LANGUAGE

#include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h> #define SIZE 5 void menu(); void display(); int underflow(); int overflow(); void enqueue(int); void dequeue(); int cqueue[SIZE];

int front=-1; int rear=-1; void main() { clrscr(); menu(); } void menu() { int choice,item; printf("MENU"); printf("\n1. Insert into the queue"); printf("\n2. Delete from queue"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); if(overflow()==0) { printf("\nEnter the item tobe inserted: "); scanf("%d",&item); enqueue(item); clrscr(); printf("\nAfter inserting queue is:\n"); } display(); getch(); clrscr(); menu(); break; case 2: clrscr(); if(underflow()==1) {

dequeue(); if(underflow()==1) { printf("\nAfter deletion queue is:\n"); display(); } } getch(); clrscr(); menu(); break; case 3: clrscr(); if(underflow()==1) { printf("The queue is:\n"); display(); } getch(); clrscr(); menu(); break; case 4: exit(1); default: clrscr(); printf("Your choice is wrong\n\n"); menu(); } } int underflow() { if((front==-1)&&(rear==-1)) { printf("\nQueue is empty"); return(0); } else {

return(1); } } int overflow() { if(((front==0)&&(rear==SIZE-1))||(front==rear+1)) { printf("\nQueue is full\n"); return(1); } else { return(0); } } void enqueue(int item) { if((front==-1)&&(rear==-1)) { front=0; rear=0; } else if(rear==SIZE-1) { rear=0; } else { rear=rear+1; } cqueue[rear]=item; } void dequeue() { if(front==rear) { front=-1; rear=-1;

} else if(front==SIZE-1) { front=0; } else { front=front+1; } } void display() { int i; if(front<=rear) { for(i=front;i<=rear;i++) { printf("\nElement %d : %d",i+1,cqueue[i]); } } else { for(i=front;i<=SIZE-1;i++) { printf("\nElement %d : %d",i+1,cqueue[i]); } for(i=0;i<=rear;i++) { printf("\nElement %d : %d",i+1,cqueue[i]); } } }

PROGRAM TO IMPLEMENT QUEUE USING ARRAYS USING C LANGUAGE


#include < stdio.h> #include < conio.h> #include < malloc.h> #include < process.h> #include < ctype.h>

struct linear_queue { int info; struct linear_queue *next; }*front,*rear,*newnode,*ptr; void menu(); void display(); int underflow(); void enqueue(int); void dequeue(); void main() { clrscr(); menu(); } void menu() { int choice,item; printf("MENU"); printf("\n1. Insert into the queue"); printf("\n2. Delete from queue"); printf("\n3. Display"); printf("\n4. Exit"); printf("\nEnter your choice: "); scanf("%d",&choice); switch(choice) { case 1: clrscr(); printf("\nEnter the item tobe inserted: "); scanf("%d",&item); enqueue(item); clrscr(); printf("\nAfter inserting queue is:\n"); display(); getch(); clrscr(); menu(); break; case 2: clrscr(); if(underflow()==1) { dequeue(); if(underflow()==1) { printf("\nAfter deletion queue is:\n"); display(); } } getch(); clrscr();

menu(); break; case 3: clrscr(); if(underflow()==1) { printf("The queue is:\n"); display(); } getch(); clrscr(); menu(); break; case 4: exit(1); default: clrscr(); printf("Your choice is wrong\n\n"); menu(); } } int underflow() { if((front==NULL)&&(rear==NULL)) { printf("\nQueue is empty"); return(0); } else { return(1); } } void enqueue(int item) { newnode=(struct linear_queue*)malloc(sizeof(struct linear_queue)); newnode->info=item; if((front==NULL)&&(rear==NULL)) { front=newnode; rear=newnode; newnode->next=NULL; } else { rear->next=newnode; newnode->next=NULL; rear=newnode; } } void dequeue() { if(front==rear)

{ front=NULL; rear=NULL; } else { front=front->next; } } void display() { int i; ptr=front; i=1; while(ptr!=NULL) { printf("\nNode %d : %d",i,ptr->info); ptr=ptr->next; i++; } }

PROGRAM TO IMPLEMENT BINARY TREE OR PROGRAM FOR THE CREATION OF BINARY TREE, PROVIDE INSERTION & DELETION USING C LANGUAGE

#include<stdio.h> #include<conio.h> #include<alloc.h> struct node { int data; struct node *left,*right; };

struct node *root; void insert(int x) { struct node *p,*previous,*current; p=(struct node *)malloc(sizeof(struct node)); if(p==NULL) { printf("\n Out of memory"); } p->data=x; p->left=NULL; p->right=NULL; if(root=NULL) { root=p; return; } previous=NULL; current=root; while(current!=NULL) { previous=current; if(p->data<current->data) current=current->left; else

current=current->right; } if(p->data<previous->data) previous->left=p; else previous->right=p; } void inorder(struct node *t) { if (t!=NULL) { inorder(t->left); printf("\n %5d",t->data); inorder (t->right); } } void del(int x) { int tright=0,tleft=0; struct node *ptr=root; struct node *parent=root; struct node *t1=root; struct node *temp=root; while(ptr!=NULL&& ptr->data!=x) {

parent=ptr; if (x<ptr->data) ptr=ptr->left; else ptr=ptr->right; } if (ptr==NULL) { printf("\n Delete element not found"); return ; } else if(t1->data==x && (t1->left ==NULL || t1->right==NULL)) if(t1->left==NULL) t1=t1->right; else t1=t1->left; else if (ptr->left==NULL) if (x<parent->data) parent->left=ptr->right; else parent->right=ptr->right; else if (ptr->right==NULL) if (x<parent->data) parent->left=ptr->left; else

parent->right=ptr->left; else { temp=ptr; parent=ptr; if((ptr->left)>=(ptr->right)) { ptr=ptr->left; while(ptr->right!=NULL) { tright=1; parent=ptr; ptr=ptr->right; } temp->data=ptr->data; if(tright) parent->right=ptr->left; else parent->left=ptr->left; } else { ptr=ptr->right; while (ptr->left!=NULL) {

tleft=1; parent=ptr; ptr=ptr->left; } temp->data=ptr->data; if(tleft) parent->left=ptr->right; else parent->right=ptr->right; } free(ptr); } }

void main() { int op,n,srchno; root=(struct node *)malloc(sizeof(struct node)); root->data=30; root->right=root->left=NULL; clrscr(); do { printf("\n 1.Insertion"); printf("\n 2.Deletion");

printf("\n 3.Inorder"); printf("\n 4.Quit"); printf("\n Enter your choice\n"); scanf("%d",&op);

switch (op) { case 1: printf("\n Enter the element to insert\n"); scanf("%d",&n); insert(n); break; case 2: printf("\n Enter the element to be deleted\n"); scanf("%d",&srchno); del(srchno); break; case 3: printf("\n The inorder elements are\n"); inorder(root); getch(); break; default: exit(0); } }while(op<4); getch();

PROGRAM FOR PRE-ORDER,POST-ORDER & IN-ORDER TRAVERSALS OF A BINARY TREE USING NON RECCUSIVE. #include<stdio.h> #include<conio.h> #include<alloc.h>

struct node { int data; struct node *left,*right; }; struct node *root;

void ins(struct node *n,int val,int opt) { struct node *t; t=(struct node *)malloc(sizeof(struct node)); t->data=val; t->right=t->left=NULL; if (opt==1) n->left=t; else n->right=t;

printf("\n %d is inserted",val); if (opt==1) { printf("\tat the left\n"); getch(); } else { printf("\tat the right\n"); getch(); } }

void inser(struct node *t,int x) { if (t->data >x) if (t->left==NULL) ins(t,x,1); else inser(t->left,x); else if (t->data < x) if (t->right==NULL) ins(t,x,2); else inser(t->right,x);

else printf("\n Element is already present in the list\n"); }

void inorder(struct node *p) { if (p!=NULL) { inorder(p->left); printf("\n %5d",p->data); inorder (p->right); } }

void preorder(struct node *p) { if (p!=NULL) { printf("\n %5d",p->data); preorder(p->left); preorder (p->right); } } void postorder(struct node *p) {

if (p!=NULL) { preorder(p->left); preorder (p->right); printf("\n %5d",p->data); } } void main() { int op,n; root=(struct node *)malloc(sizeof(struct node)); root->data=30; root->right=root->left=NULL; clrscr(); do { printf("\n 1.Insertion"); printf("\n 2.Preorder"); printf("\n 3.Inorder"); printf("\n 4.Postorder"); printf("\n 5.Quit"); printf("\n Enter your choice\n"); scanf("%d",&op);

switch (op)

{ case 1: printf("\n Enter the element to insert\n"); scanf("%d",&n); inser(root,n); break; case 2: printf("\n The preorder elements are\n"); preorder(root); getch(); break; case 3: printf("\n The inorder elements are\n"); inorder(root); getch(); break;

case 4: printf("\n The postorder elements are\n"); postorder(root); getch(); break; default: exit(0); } }while(op<5); getch(); }

PROGRAM TO COUNT NO, OF LEAVES OF BINARY TREE USING C LANGUAGE

#include <stdio.h> #include <stdlib.h> /* A binary tree node has data, pointer to left child and a pointer to right child */ struct node { int data; struct node* left; struct node* right; }; /* Function to get the count of leaf nodes in a binary tree*/ unsigned int getLeafCount(struct node* node) { if(node == NULL) return 0; if(node->left == NULL && node->right==NULL) return 1; else return getLeafCount(node->left)+ getLeafCount(node->right); } /* Helper function that allocates a new node with the given data and NULL left and right pointers. */ struct node* newNode(int data) { struct node* node = (struct node*) malloc(sizeof(struct node)); node->data = data; node->left = NULL; node->right = NULL; return(node); } /*Driver program to test above functions*/ int main()

{ /*create a tree*/ struct node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); /*get leaf count of the above created tree*/ printf("Leaf count of the tree is %d", getLeafCount(root)); getchar(); return 0; }

PROGRAM FOR IMPLEMENTATION OF B-TREE (INSERTION & DELETION) using / in c

#include<stdio.h> #include <conio.h> #define M 5 struct node{ int n; /* n < M No. of keys in node will always less than order of B tree */ int keys[M-1]; /*array of keys*/ struct node *p[M]; /* (n+1 pointers will be in use) */ }*root=NULL; enum KeyStatus { Duplicate,SearchFailure,Success,InsertIt,LessKeys }; void insert(int key); void display(struct node *root,int); void DelNode(int x); void search(int x); enum KeyStatus ins(struct node *r, int x, int* y, struct node** u); int searchPos(int x,int *key_arr, int n); enum KeyStatus del(struct node *r, int x);

int main() { int key; int choice; printf("Creation of B tree for node %d\n",M); while(1) { printf("1.Insert\n"); printf("2.Delete\n"); printf("3.Search\n"); printf("4.Display\n"); printf("5.Quit\n"); printf("Enter your choice : "); scanf("%d",&choice); switch(choice) { case 1: printf("Enter the key : "); scanf("%d",&key); insert(key); break; case 2: printf("Enter the key : "); scanf("%d",&key); DelNode(key); break; case 3: printf("Enter the key : "); scanf("%d",&key); search(key); break; case 4: printf("Btree is :\n"); display(root,0); break; case 5: exit(1); default: printf("Wrong choice\n");

break; }/*End of switch*/ }/*End of while*/ return 0; }/*End of main()*/ void insert(int key) { struct node *newnode; int upKey; enum KeyStatus value; value = ins(root, key, &upKey, &newnode); if (value == Duplicate) printf("Key already available\n"); if (value == InsertIt) { struct node *uproot = root; root=malloc(sizeof(struct node)); root->n = 1; root->keys[0] = upKey; root->p[0] = uproot; root->p[1] = newnode; }/*End of if */ }/*End of insert()*/ enum KeyStatus ins(struct node *ptr, int key, int *upKey,struct node **newnode) { struct node *newPtr, *lastPtr; int pos, i, n,splitPos; int newKey, lastKey; enum KeyStatus value; if (ptr == NULL) { *newnode = NULL; *upKey = key; return InsertIt; } n = ptr->n; pos = searchPos(key, ptr->keys, n);

if (pos < n && key == ptr->keys[pos]) return Duplicate; value = ins(ptr->p[pos], key, &newKey, &newPtr); if (value != InsertIt) return value; /*If keys in node is less than M-1 where M is order of B tree*/ if (n < M - 1) { pos = searchPos(newKey, ptr->keys, n); /*Shifting the key and pointer right for inserting the new key*/ for (i=n; i>pos; i--) { ptr->keys[i] = ptr->keys[i-1]; ptr->p[i+1] = ptr->p[i]; } /*Key is inserted at exact location*/ ptr->keys[pos] = newKey; ptr->p[pos+1] = newPtr; ++ptr->n; /*incrementing the number of keys in node*/ return Success; }/*End of if */ /*If keys in nodes are maximum and position of node to be inserted is last*/ if (pos == M - 1) { lastKey = newKey; lastPtr = newPtr; } else /*If keys in node are maximum and position of node to be inserted is not last*/ { lastKey = ptr->keys[M-2]; lastPtr = ptr->p[M-1]; for (i=M-2; i>pos; i--) { ptr->keys[i] = ptr->keys[i-1]; ptr->p[i+1] = ptr->p[i]; } ptr->keys[pos] = newKey; ptr->p[pos+1] = newPtr;

} splitPos = (M - 1)/2; (*upKey) = ptr->keys[splitPos]; (*newnode)=malloc(sizeof(struct node));/*Right node after split*/ ptr->n = splitPos; /*No. of keys for left splitted node*/ (*newnode)->n = M-1-splitPos;/*No. of keys for right splitted node*/ for (i=0; i < (*newnode)->n; i++) { (*newnode)->p[i] = ptr->p[i + splitPos + 1]; if(i < (*newnode)->n - 1) (*newnode)->keys[i] = ptr->keys[i + splitPos + 1]; else (*newnode)->keys[i] = lastKey; } (*newnode)->p[(*newnode)->n] = lastPtr; return InsertIt; }/*End of ins()*/ void display(struct node *ptr, int blanks) { if (ptr) { int i; for(i=1;i<=blanks;i++) printf(" "); for (i=0; i < ptr->n; i++) printf("%d ",ptr->keys[i]); printf("\n"); for (i=0; i <= ptr->n; i++) display(ptr->p[i], blanks+10); }/*End of if*/ }/*End of display()*/ void search(int key) { int pos, i, n; struct node *ptr = root; printf("Search path:\n"); while (ptr)

{ n = ptr->n; for (i=0; i < ptr->n; i++) printf(" %d",ptr->keys[i]); printf("\n"); pos = searchPos(key, ptr->keys, n); if (pos < n && key == ptr->keys[pos]) { printf("Key %d found in position %d of last dispalyed node\n",key,i); return; } ptr = ptr->p[pos]; } printf("Key %d is not available\n",key); }/*End of search()*/ int searchPos(int key, int *key_arr, int n) { int pos=0; while (pos < n && key > key_arr[pos]) pos++; return pos; }/*End of searchPos()*/ void DelNode(int key) { struct node *uproot; enum KeyStatus value; value = del(root,key); switch (value) { case SearchFailure: printf("Key %d is not available\n",key); break; case LessKeys: uproot = root; root = root->p[0]; free(uproot); break;

}/*End of switch*/ }/*End of delnode()*/ enum KeyStatus del(struct node *ptr, int key) { int pos, i, pivot, n ,min; int *key_arr; enum KeyStatus value; struct node **p,*lptr,*rptr; if (ptr == NULL) return SearchFailure; /*Assigns values of node*/ n=ptr->n; key_arr = ptr->keys; p = ptr->p; min = (M - 1)/2;/*Minimum number of keys*/ pos = searchPos(key, key_arr, n); if (p[0] == NULL) { if (pos == n || key < key_arr[pos]) return SearchFailure; /*Shift keys and pointers left*/ for (i=pos+1; i < n; i++) { key_arr[i-1] = key_arr[i]; p[i] = p[i+1]; } return --ptr->n >= (ptr==root ? 1 : min) ? Success : LessKeys; }/*End of if */ if (pos < n && key == key_arr[pos]) { struct node *qp = p[pos], *qp1; int nkey; while(1) { nkey = qp->n; qp1 = qp->p[nkey];

if (qp1 == NULL) break; qp = qp1; }/*End of while*/ key_arr[pos] = qp->keys[nkey-1]; qp->keys[nkey - 1] = key; }/*End of if */ value = del(p[pos], key); if (value != LessKeys) return value; if (pos > 0 && p[pos-1]->n > min) { pivot = pos - 1; /*pivot for left and right node*/ lptr = p[pivot]; rptr = p[pos]; /*Assigns values for right node*/ rptr->p[rptr->n + 1] = rptr->p[rptr->n]; for (i=rptr->n; i>0; i--) { rptr->keys[i] = rptr->keys[i-1]; rptr->p[i] = rptr->p[i-1]; } rptr->n++; rptr->keys[0] = key_arr[pivot]; rptr->p[0] = lptr->p[lptr->n]; key_arr[pivot] = lptr->keys[--lptr->n]; return Success; }/*End of if */ if (posn > min) { pivot = pos; /*pivot for left and right node*/ lptr = p[pivot]; rptr = p[pivot+1]; /*Assigns values for left node*/ lptr->keys[lptr->n] = key_arr[pivot]; lptr->p[lptr->n + 1] = rptr->p[0]; key_arr[pivot] = rptr->keys[0]; lptr->n++; rptr->n--;

for (i=0; i < rptr->n; i++) { rptr->keys[i] = rptr->keys[i+1]; rptr->p[i] = rptr->p[i+1]; }/*End of for*/ rptr->p[rptr->n] = rptr->p[rptr->n + 1]; return Success; }/*End of if */ if(pos == n) pivot = pos-1; else pivot = pos; lptr = p[pivot]; rptr = p[pivot+1]; /*merge right node with left node*/ lptr->keys[lptr->n] = key_arr[pivot]; lptr->p[lptr->n + 1] = rptr->p[0]; for (i=0; i < rptr->n; i++) { lptr->keys[lptr->n + 1 + i] = rptr->keys[i]; lptr->p[lptr->n + 2 + i] = rptr->p[i+1]; } lptr->n = lptr->n + rptr->n +1; free(rptr); /*Remove right node*/ for (i=pos+1; i < n; i++) { key_arr[i-1] = key_arr[i]; p[i] = p[i+1]; } return --ptr->n >= (ptr == root ? 1 : min) ? Success : LessKeys; }/*End of del()*/

PROGRAM FOR IMPLEMENTATION OF (M-WAY) MULTIWAY TREE USING C MISSING

WRITE A PROGRAM FOR IMPLEMENTATION OF AVL TREE USING C

#include <iostream.h> #include <stdlib.h> #include<constream.h> #define FALSE 0 #define TRUE 1 struct AVLNode { int data ; int balfact ; AVLNode *left ; AVLNode *right ; };

class avltree { private : AVLNode *root ; public : avltree( ) ; AVLNode* insert ( int data, int *h ) ; static AVLNode* buildtree ( AVLNode *root, int data, int *h ) ;

void display( AVLNode *root ) ; AVLNode* deldata ( AVLNode* root, int data, int *h ) ; static AVLNode* del ( AVLNode *node, AVLNode* root, int *h ) ; static AVLNode* balright ( AVLNode *root, int *h ) ; static AVLNode* balleft ( AVLNode* root, int *h ) ; void setroot ( AVLNode *avl ) ; ~avltree( ) ; static void deltree ( AVLNode *root ) ; }; avltree :: avltree( ) { root = NULL ; } AVLNode* avltree :: insert ( int data, int *h ) { root = buildtree ( root, data, h ) ; return root ; } AVLNode* avltree :: buildtree ( AVLNode *root, int data, int *h ) { AVLNode *node1, *node2 ;

if ( root == NULL )

{ root = new AVLNode ; root -> data = data ; root -> left = NULL ; root -> right = NULL ; root -> balfact = 0 ; *h = TRUE ; return ( root ) ; } if ( data < root -> data ) { root -> left = buildtree ( root -> left, data, h ) ;

// If left subtree is higher if ( *h ) { switch ( root -> balfact ) { case 1 : node1 = root -> left ; if ( node1 -> balfact == 1 ) { cout << "\nRight rotation." ;

root -> left = node1 -> right ; node1 -> right = root ; root -> balfact = 0 ; root = node1 ; } else { cout << "\nDouble rotation, left then right." ; node2 = node1 -> right ; node1 -> right = node2 -> left ; node2 -> left = node1 ; root -> left = node2 -> right ; node2 -> right = root ; if ( node2 -> balfact == 1 ) root -> balfact = -1 ; else root -> balfact = 0 ; if ( node2 -> balfact == -1 ) node1 -> balfact = 1 ; else node1 -> balfact = 0 ; root = node2 ; }

root -> balfact = 0 ; *h = FALSE ; break ;

case 0 : root -> balfact = 1 ; break ; case -1 : root -> balfact = 0 ; *h = FALSE ; } } }

if ( data > root -> data ) { root -> right = buildtree ( root -> right, data, h ) ;

if ( *h ) { switch ( root -> balfact ) { case 1 :

root -> balfact = 0 ; *h = FALSE ; break ; case 0 : root -> balfact = -1 ; break ; case -1 : node1 = root -> right ; if ( node1 -> balfact == -1 ) { cout << "\nLeft rotation." ; root -> right = node1 -> left ; node1 -> left = root ; root -> balfact = 0 ; root = node1 ; } else { cout << "\nDouble rotation, right then left." ; node2 = node1 -> left ; node1 -> left = node2 -> right ; node2 -> right = node1 ; root -> right = node2 -> left ;

node2 -> left = root ; if ( node2 -> balfact == -1 ) root -> balfact = 1 ; else root -> balfact = 0 ; if ( node2 -> balfact == 1 ) node1 -> balfact = -1 ; else node1 -> balfact = 0 ; root = node2 ; } root -> balfact = 0 ; *h = FALSE ; } } } return ( root ) ; } void avltree :: display ( AVLNode* root ) { if ( root != NULL ) { display ( root -> left ) ;

cout << root -> data << "\t" ; display ( root -> right ) ; } } AVLNode* avltree :: deldata ( AVLNode *root, int data, int *h ) { AVLNode *node ; if ( root -> data == 13 ) cout << root -> data ; if ( root == NULL ) { cout << "\nNo such data." ; return ( root ) ; } else { if ( data < root -> data ) { root -> left = deldata ( root -> left, data, h ) ; if ( *h ) root = balright ( root, h ) ; } else

{ if ( data > root -> data ) { root -> right = deldata ( root -> right, data, h ) ; if ( *h ) root = balleft ( root, h ) ; } else { node = root ; if ( node -> right == NULL ) { root = node -> left ; *h = TRUE ; delete ( node ) ; } else { if ( node -> left == NULL ) { root = node -> right ; *h = TRUE ; delete ( node ) ;

} else { node -> right = del ( node -> right, node, h ) ; if ( *h ) root = balleft ( root, h ) ; } } } } } return ( root ) ; } AVLNode* avltree :: del ( AVLNode *succ, AVLNode *node, int *h ) { AVLNode *temp = succ ;

if ( succ -> left != NULL ) { succ -> left = del ( succ -> left, node, h ) ; if ( *h ) succ = balright ( succ, h ) ; }

else { temp = succ ; node -> data = succ -> data ; succ = succ -> right ; delete ( temp ) ; *h = TRUE ; } return ( succ ) ; } AVLNode* avltree :: balright ( AVLNode *root, int *h ) { AVLNode *temp1, *temp2 ; switch ( root -> balfact ) { case 1 : root -> balfact = 0 ; break ; case 0 : root -> balfact = -1 ; *h = FALSE ; break ; case -1 :

temp1 = root -> right ; if ( temp1 -> balfact <= 0 ) { cout << "\nLeft rotation." ; root -> right = temp1 -> left ; temp1 -> left = root ; if ( temp1 -> balfact == 0 ) { root -> balfact = -1 ; temp1 -> balfact = 1 ; *h = FALSE ; } else { root -> balfact = temp1 -> balfact = 0 ; } root = temp1 ; } else { cout << "\nDouble rotation, right then left." ; temp2 = temp1 -> left ; temp1 -> left = temp2 -> right ;

temp2 -> right = temp1 ; root -> right = temp2 -> left ; temp2 -> left = root ; if ( temp2 -> balfact == -1 ) root -> balfact = 1 ; else root -> balfact = 0 ; if ( temp2 -> balfact == 1 ) temp1 -> balfact = -1 ; else temp1 -> balfact = 0 ; root = temp2 ; temp2 -> balfact = 0 ; } } return ( root ) ; } AVLNode* avltree :: balleft ( AVLNode *root, int *h ) { AVLNode *temp1, *temp2 ; switch ( root -> balfact ) { case -1 :

root -> balfact = 0 ; break ;

case 0 : root -> balfact = 1 ; *h = FALSE ; break ;

case 1 : temp1 = root -> left ; if ( temp1 -> balfact >= 0 ) { cout << "\nRight rotation." ; root -> left = temp1 -> right ; temp1 -> right = root ;

if ( temp1 -> balfact == 0 ) { root -> balfact = 1 ; temp1 -> balfact = -1 ; *h = FALSE ; } else

{ root -> balfact = temp1 -> balfact = 0 ; } root = temp1 ; } else { cout << "\nDouble rotation, left then right." ; temp2 = temp1 -> right ; temp1 -> right = temp2 -> left ; temp2 -> left = temp1 ; root -> left = temp2 -> right ; temp2 -> right = root ; if ( temp2 -> balfact == 1 ) root -> balfact = -1 ; else root -> balfact = 0 ; if ( temp2-> balfact == -1 ) temp1 -> balfact = 1 ; else temp1 -> balfact = 0 ; root = temp2 ; temp2 -> balfact = 0 ;

} } return ( root ) ; } void avltree :: setroot ( AVLNode *avl ) { root = avl ; } avltree :: ~avltree( ) { deltree ( root ) ; }

void avltree :: deltree ( AVLNode *root ) { if ( root != NULL ) { deltree ( root -> left ) ; deltree ( root -> right ) ; } delete ( root ) ; }

void main( ) { avltree at ; AVLNode *avl = NULL ; int h ; clrscr(); avl = at.insert ( 20, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 6, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 29, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 5, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 12, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 25, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 32, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 10, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 15, &h ) ;

at.setroot ( avl ) ; avl = at.insert ( 27, &h ) ; at.setroot ( avl ) ; avl = at.insert ( 13, &h ) ; at.setroot ( avl ) ; cout << endl << "AVL tree:\n" ; at.display ( avl ) ; avl = at.deldata ( avl, 20, &h ) ; at.setroot ( avl ) ; avl = at.deldata ( avl, 12, &h ) ; at.setroot ( avl ) ; cout << endl << "AVL tree after deletion of a node:\n" ; at.display ( avl ) ; getch(); }

PROGRAM TO ACCEPT 2 SINGLY LINKED LISTS & PRINT A SINGLY LINKED LIST USING C LANGUAGE #include<stdio.h> #include<stdlib.h> struct info {

int num; struct info *next; };

struct node { int num1; struct node *next1; };

struct com { int num2; struct com *next2; };

struct info *temp,*disp,*head; struct node *temp1,*disp1,*head1; struct com *temp2,*disp2,*head2=NULL;

void addrecord(); void disrecord();

void main() {

int ch; clrscr(); while (1) { printf("\n 1. To add records\n"); printf("\n 2. To view the records\n"); printf("\n 3. To exit\n"); printf("\n Enter your choice\n"); scanf("%d",&ch); fflush(stdin); switch(ch) { case 1:addrecord(); break; case 2:disrecord(); break; case 3: exit(0);

} } }

void addrecord() { struct info *add;

struct node *add1;

char ans='y'; char choice='y';

while (ans=='y') { add=(struct info*)malloc(sizeof(struct info)); printf("\n Enter the element of the first list:\n"); scanf("%d",&add->num); fflush(stdin); if (head==NULL|| head->num>=add->num) { add->next=head; head=add; } else { temp=head; while (temp->next!=NULL && temp->next->num < add->num) { temp=temp->next; } add->next=temp->next;

temp->next=add; } printf("\n Would you like to enter another name(y\\n): \n"); ans = getchar(); }

while (choice=='y') { add1=(struct node*)malloc(sizeof(struct node)); printf("\n Enter the element of the second list:\n"); scanf("%d",&add1->num1); fflush(stdin); if (head1==NULL|| head1->num1>=add1->num1) { add1->next1=head1; head1=add1; } else { temp1=head1; while (temp1->next1!=NULL && temp1->next1->num1 < add1->num1) { temp1=temp1->next1; } add1->next1=temp1->next1;

temp1->next1=add1; } printf("\n Would you like to enter another name(y\\n): \n"); choice = getchar(); fflush(stdin); } }

void disrecord() { struct com *add2; if (head==NULL) { printf("\n No records to view\n"); return; } for (disp=head;disp!=NULL;disp=disp->next) { printf("\n\n Number : %d",disp->num); } for (disp1=head1;disp1!=NULL;disp1=disp1->next1) { printf("\n\n Number : %d",disp1->num1); } for (disp=head;disp!=NULL;disp=disp->next)

{ for (disp1=head1;disp1!=NULL;disp1=disp1->next1) { if (disp->num==disp1->num1) { add2=(struct com*)malloc(sizeof(struct com)); add2->num2=disp->num; printf("%d",add2->num2); if(head2==NULL) { head2= add2; add2->next2=NULL; temp2=add2; } else { temp2->next2=add2; add2->next2=NULL; temp2=add2; } } } } printf("\n Sorted list is \n\n"); for (disp2=head2;disp2!=NULL;disp2=disp2->next2)

{ printf("\n\n Number : %d",disp2->num2); }

PROGRAM FOR IMPLEMENTATION OF RED-BLACK TREE USING C

This program will be posted soon.......... PROGRAM TO IMPLEMENT BREADTH FIRST SEARCH IN C #include <stdio.h> #include <conio.h> #include <alloc.h> #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; }; int visited[MAX] ; int q[8] ; int front, rear ; void bfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ; void addqueue ( int ) ; int deletequeue( ) ; int isempty( ) ; void del ( struct node * ) ;

void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ) ; v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ) ; v2 -> next = v3 = getnode_write ( 5 ) ; v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[6] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 4 ) ;

arr[7] = v1 ; v1 -> next = v2 = getnode_write ( 5 ) ; v2 -> next = v3 = getnode_write ( 6 ) ; v3 -> next = v4 = getnode_write ( 7 ) ; v4 -> next = NULL ; front = rear = -1 ; bfs ( 1, arr ) ; for ( i = 0 ; i < MAX ; i++ ) del ( arr[i] ) ; getch( ) ; } void bfs ( int v, struct node **p ) { struct node *u ; visited[v - 1] = TRUE ; printf ( "%d\t", v ) ; addqueue ( v ) ; while ( isempty( ) == FALSE ) { v = deletequeue( ) ; u=*(p+v-1); while ( u != NULL ) { if ( visited [ u -> data - 1 ] == FALSE ) { addqueue ( u -> data ) ; visited [ u -> data - 1 ] = TRUE ; printf ( "%d\t", u -> data ) ; } u = u -> next ; } } } struct node * getnode_write ( int val ) { struct node *newnode ; newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ; newnode -> data = val ;

return newnode ; } void addqueue ( int vertex ) { if ( rear == MAX - 1 ) { printf ( "\nQueue Overflow." ) ; exit( ) ; } rear++ ; q[rear] = vertex ; if ( front == -1 ) front = 0 ; } int deletequeue( ) { int data ; if ( front == -1 ) { printf ( "\nQueue Underflow." ) ; exit( ) ; } data = q[front] ; if ( front == rear ) front = rear = -1 ; else front++ ; return data ; } int isempty( ) { if ( front == -1 ) return TRUE ; return FALSE ; } void del ( struct node *n )

{ struct node *temp ; while ( n != NULL ) { temp = n -> next ; free ( n ) ; n = temp ; } }
LINEAR SEARCH PROGRAM USING POINTERS IN C LANGUAGE
#include<stdio.h> void main() { int *a[100],i,no,*srchno; clrscr(); printf("\n Enter the number of elements\n"); scanf("%d",&no); printf("\n Enter %d numbers\n",no); for(i=0;i<no;++i) scanf("%d",&a[i]); printf("Enter the search number\n"); scanf("%d",&srchno); for(i=0;i<no;++i) if(srchno==a[i]) { printf("\n search number is present"); exit(0);

} printf("\n Search number is not present"); }

PROGRAM TO IMPLEMENT BINARY SEARCH USING POINTERS IN C LANGUAGE


#include<stdio.h> void main() { int *a[100],i,no,*srchno,top,bottom,mid,j,*temp; clrscr(); printf("\n Enter the number of elements\n"); scanf("%d",&no); printf("\n Enter %d numbers\n",no); for(i=0;i<no;++i) scanf("%d",&a[i]); printf("Enter the search number\n"); scanf("%d",&srchno); for(i=0;i<no-1;++i) for(j=i+1;j<no;++j) if(a[i]>a[j]) { temp=a[i]; a[i]=a[j]; a[j]=temp;

} printf("\n Sorted array in ascending order\n"); for(i=0;i<no;++i) printf("%5d",a[i]); bottom=0; top=no-1; while(top!=bottom+1) { mid=(bottom+top)/2; if (a[mid]<=srchno) bottom=mid; else top=mid; } if(a[bottom]==srchno) printf("\n search number is present"); else printf("\n Search number is not present"); }

ROGRAM TO IMPLEMENT LINEAR SEARCH USING ARRAYS using c programming language #include<stdio.h> int main(){ int a[10],i,n,m,c=0; printf("Enter the size of an array");

scanf("%d",&n); printf("\nEnter the elements of the array"); for(i=0;i<=n-1;i++){ scanf("%d",&a[i]); } printf("\nThe elements of an array are"); for(i=0;i<=n-1;i++){ printf(" %d",a[i]); } printf("\nEnter the number to be search"); scanf("%d",&m); for(i=0;i<=n-1;i++){ if(a[i]==m){ c=1; break; } } if(c==0) printf("\nThe number is not in the list"); else printf("\nThe number is found"); return 0; }

PROGRAM TO IMPLEMENT BINARY SEARCH USING ARRAYS IN C language #include<stdio.h> #include<conio.h> void main() { clrscr(); int a[100],n,i,j,item,temp; printf("Enter the no.of elements in array "); scanf(" "); cout<<"\n Enter the elements \n"; for(i=1;i<=n;i++) { scanf('%d",a[i]); } for(i=1;i<=n-1;i++)

{ for(j=1;j<=n-i;j++) { if (a[j]>a[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp;} } } cout<<"\nSorted array: ";for(i=1;i<=n;i++) { printf("%d"a[i]); } printf(" \nEnter the element to be searched \n"); scanf("%d", item); int beg,end,mid,loc=-1; beg=1; end=n; mid=int(beg+end)/2; while((beg<=end) && (loc==-1)) { if (a[mid]==item) { loc=mid; } else if (item<a[mid]) { end=mid-1; } else{ beg=mid+1; } mid=int(beg+end)/2; } if (loc==-1) printf("search unsuccessfull"); else printf('search successfull"); printf("search location =%d", loc); }

PROGRAM TO IMPLEMENT HASHING USING OPEN ADDRESSING USING C

#include<STDIO.H> void main() { int a[10]={1,2,3,4,5,6,7,8,9,10}; int n,value; int temp, hash; clrscr(); printf(\nEnter the value of n(table size):); scanf(%d,&n); do { printf(\nEnter the hash value); scanf(%d,&value); hash=value%n; if(a[hash]==0) { a[hash]=value; printf(\na[%d]the value %d is stored,hash,value); } else { for(hash++;hash<n;hash++) { if(a[hash]==0) { printf("Space is allocated give other value"); a[hash]=value; printf("\n a[%d]the value %d is stored",hash,value); goto menu; } } hash=0; for(hash;hash<n;hash++) { if(a[hash]==0) { printf("Space is allocated give other value"); a[hash]=value; printf("\n a[%d]the value %d is stored",hash,value); goto menu; } } printf(ERROR); printf("\nEnter '0' and press 'Enter key' twice to exit");

} menu: printf("\n Do u want enter more"); scanf("%d",&temp); } while(temp==1); getch(); }

PROGRAM TO IMPLEMENT DIJKSTRA'S ALGORITHM USING PRIORITY QUEUES USING C

#include<stdio.h> #include<conio.h> #define infinity 999 #define max 10 int G[max][max],Q[max]; int n,path[max],p[max]; int dest,scr,y,z; void display(int,int); void main() { void buildgraph(); void dijkstra(int,int); void insert(int,int); void insertq(int); int front,rear; clrscr(); printf(\nProgram for the shortest path algorithm using priority queue); printf(\nEnter the number of the vertices:); scanf(%d,&n); buildgraph(); printf(\nEnter the source);

scanf(%d,&scr); printf(\nEnter the destination); scanf(%d,&dest); dijkstra(scr,dest); for(y=1;y<=max;y++) p[y]=infinity; printf(\nThe shortest path is:\n\t); display(dest,scr); printf(%d,dest); getch(); } void display(int dest,int scr) { int z=1; while(dest>scr) { int a; a=path[dest]; if(a!=scr) { p[z]=a; } else { p[z]=a; } ++z; dest=a; } for(y=max;y>0;y) { if(p[y]!=infinity) { printf(%d,p[y]); } } } void buildgraph() { int i,j,v1,v2;

for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { printf(\nEnter the edge of v%d to v%d:,i,j); scanf(%d,&G[i][j]); } printf(\n); } } void insertq(int index) { Q[index]=1; } void insert(int index,int vertex) { path[index]=vertex; } void dijkstra(int scr,int dest) { int small,dist[10],current,start,new1; int temp,i,a[10]; void insertq(int); for(i=0;i<=n;i++) { a[i]=0; dist[i]=infinity; } Q[scr]=1; dist[scr]=0; current=scr; while(current!=dest) { small=infinity; start=dist[current]; for(i=1;i<=n;i++) { if(Q[i]==0) { new1=start+G[current][i];

if(new1<dist[i]) { dist[i]=new1; insert(i,current); } if(dist[i]<small) { small=dist[i]; temp=i; } } } current=temp; insertq(current); } //printf(\nThe minimum cost is:%d,new1); }

PROGRAM TO IMPLEMENT KNAPSACK PROBLEM USING BACKTRACKING IN C Mising PROGRAM TO ADD & DELETE NODES FROM ADJACENCY LIST IN C Mising

Minimal spanning tree using PRIMS algm

#include<stdio.h> #define INF 1000 char vertex[10]; int wght[10][10]; int span_wght[10][10]; int source; struct Sort { int v1,v2; int weight;

}que[20]; int n,ed,f,r; int cycle(int s,int d) { int j,k; if(source==d) return 1; for(j=0;j<n;j++) if(span_wght[d][j]!=INF && s!=j) { if(cycle(d,j)) return 1; } return 0; } void build_tree() { int i,j,w,k,count=0; for(count=0;count<n;f++) { i=que[f].v1; j=que[f].v2; w=que[f].weight; span_wght[i][j]=span_wght[j][i]=w; source=i; k=cycle(i,j); if(k) span_wght[i][j]=span_wght[j][i]=INF; else count++; } } void swap(int *i,int *j) { int t; t=*i; *i=*j; *j=t; } void main() { int i,j,k=0,temp;

int sum=0; clrscr(); printf("\n\n\tKRUSKAL'S ALGORITHM TO FIND SPANNING TREE\n\n"); printf("\n\tEnter the No. of Nodes : "); scanf("%d",&n); for(i=0;i<n;i++) { printf("\n\tEnter %d value : ",i+1); fflush(stdin); scanf("%c",&vertex[i]); for(j=0;j<n;j++) { wght[i][j]=INF; span_wght[i][j]=INF; } } printf("\n\nGetting Weight\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) { printf("\nEnter 0 if path Doesn't exist between %c to %c : ",vertex[i],vertex[j]); scanf("%d",&ed); if(ed>=1) { wght[i][j]=wght[j][i]=ed; que[r].v1=i; que[r].v2=j; que[r].weight=wght[i][j]; if(r) { for(k=0;k<r;k++) if(que[k].weight>que[r].weight) { swap(&que[k].weight,&que[r].weight); swap(&que[k].v1,&que[r].v1); swap(&que[k].v2,&que[r].v2); } } r++; } } clrscr();

printf("\n\tORIGINAL GRAPH WEIGHT MATRIX\n\n"); printf("\n\tweight matrix\n\n\t"); for(i=0;i<n;i++,printf("\n\t")) for(j=0;j<n;j++,printf("\t")) printf("%d",wght[i][j]); build_tree(); printf("\n\n\t\tMINIMUM SPANNING TREE\n\n"); printf("\n\t\tLIST OF EDGES\n\n"); for(i=0;i<n;i++) for(j=i+1;j<n;j++) if(span_wght[i][j]!=INF) { printf("\n\t\t%c ------ %c = %d ",vertex[i],vertex[j],span_wght[i][j]); sum+=span_wght[i][j]; } printf("\n\n\t\tTotal Weight : %d ",sum); getch(); }

PROGRAM TO IMPLEMENT BREADTH FIRST SEARCH IN C #include <stdio.h> #include <conio.h> #include <alloc.h> #define TRUE 1 #define FALSE 0 #define MAX 8 struct node { int data ; struct node *next ; }; int visited[MAX] ; int q[8] ; int front, rear ; void bfs ( int, struct node ** ) ; struct node * getnode_write ( int ) ;

void addqueue ( int ) ; int deletequeue( ) ; int isempty( ) ; void del ( struct node * ) ; void main( ) { struct node *arr[MAX] ; struct node *v1, *v2, *v3, *v4 ; int i ; clrscr( ) ; v1 = getnode_write ( 2 ) ; arr[0] = v1 ; v1 -> next = v2 = getnode_write ( 3 ) ; v2 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[1] = v1 ; v1 -> next = v2 = getnode_write ( 4 ) ; v2 -> next = v3 = getnode_write ( 5 ) ; v3 -> next = NULL ; v1 = getnode_write ( 1 ) ; arr[2] = v1 ; v1 -> next = v2 = getnode_write ( 6 ) ; v2 -> next = v3 = getnode_write ( 7 ) ; v3 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[3] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 2 ) ; arr[4] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ; arr[5] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 3 ) ;

arr[6] = v1 ; v1 -> next = v2 = getnode_write ( 8 ) ; v2 -> next = NULL ; v1 = getnode_write ( 4 ) ; arr[7] = v1 ; v1 -> next = v2 = getnode_write ( 5 ) ; v2 -> next = v3 = getnode_write ( 6 ) ; v3 -> next = v4 = getnode_write ( 7 ) ; v4 -> next = NULL ; front = rear = -1 ; bfs ( 1, arr ) ; for ( i = 0 ; i < MAX ; i++ ) del ( arr[i] ) ; getch( ) ; } void bfs ( int v, struct node **p ) { struct node *u ; visited[v - 1] = TRUE ; printf ( "%d\t", v ) ; addqueue ( v ) ; while ( isempty( ) == FALSE ) { v = deletequeue( ) ; u=*(p+v-1); while ( u != NULL ) { if ( visited [ u -> data - 1 ] == FALSE ) { addqueue ( u -> data ) ; visited [ u -> data - 1 ] = TRUE ; printf ( "%d\t", u -> data ) ; } u = u -> next ; } } }

struct node * getnode_write ( int val ) { struct node *newnode ; newnode = ( struct node * ) malloc ( sizeof ( struct node ) ) ; newnode -> data = val ; return newnode ; } void addqueue ( int vertex ) { if ( rear == MAX - 1 ) { printf ( "\nQueue Overflow." ) ; exit( ) ; } rear++ ; q[rear] = vertex ; if ( front == -1 ) front = 0 ; } int deletequeue( ) { int data ; if ( front == -1 ) { printf ( "\nQueue Underflow." ) ; exit( ) ; } data = q[front] ; if ( front == rear ) front = rear = -1 ; else front++ ; return data ; } int isempty( ) { if ( front == -1 )

return TRUE ; return FALSE ; } void del ( struct node *n ) { struct node *temp ; while ( n != NULL ) { temp = n -> next ; free ( n ) ; n = temp ; } }

#include<stdio.h> #include<conio.h> void bubble(int a[],int n) { int i,j,t; for(i=n-2;i>=0;i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }

}//end for 1. }//end function.

void main() { int a[100],n,i; clrscr(); printf("\n\n Enter integer value for total no.s of elements to be sorted: "); scanf("%d",&n); for( i=0;i<=n-1;i++) { printf("\n\n Enter integer value for element no.%d : ",i+1); scanf("%d",&a[i]); } bubble(a,n); printf("\n\n Finally sorted array is: "); for( i=0;i<=n-1;i++) printf("%3d",a[i]); } //end program.

PROGRAM TO IMPLEMENT MERGE SORT USING ARRAYS #include<stdio.h> #define MAX 20 void mergesort(int *,int); void main() { int x[MAX],n,j,i; char ans; clrscr(); { printf("\nEnter The Length Of The Array\t: "); scanf("%d",&n); for(i=0;i< n;i++) {

printf("Enter Element %d\t: ",i+1); scanf("%d",&x[i]); } mergesort(x,n); printf("\n\t Sorted Array :\t\t\t \n\t "); for(i=0;i< n;i++) printf("%d\t",x[i]); } printf(" "); getch(); } void mergesort(int x[],int n) { int sub[MAX]; int i,j,k,list1,list2,u1,u2,size=1; while(size< n) { list1=0; k=0; while((list1+size)< n) { list2=list1+size; u1=list2-1; u2=((list2+size-1)< n)?(list2+size-1):(n-1); for(i=list1,j=list2;i< =u1 &&amp;amp;amp; j< =u2;k++) if(x[i]< =x[j]) sub[k]=x[i++]; else sub[k]=x[j++]; for(;i< =u1;k++) sub[k]=x[i++]; for(;j< =u2;k++) sub[k]=x[j++]; list1=u2+1; } for(i=list1;k< n;i++) sub[k++] = x[i]; for(i=0;i< n;i++) x[i] =sub[i]; size *= 2;

} }
SELECTION SORT PROGRAM USING ARRAYS

#include <stdio.h> #define MAXSIZE 500 void selection(int elements[], int maxsize); int elements[MAXSIZE],maxsize; int main() { int i; printf(\nHow many elements you want to sort: ); scanf(%d,&maxsize); printf(\nEnter the values one by one: ); for (i = 0; i < maxsize; i++) { printf (\nEnter element %i :,i); scanf(%d,&elements[i]); } printf(\nArray before sorting:\n); for (i = 0; i < maxsize; i++) printf([%i], ,elements[i]); printf (\n); selection(elements, maxsize); printf(\nArray after sorting:\n); for (i = 0; i < maxsize; i++) printf([%i], , elements[i]); } void selection(int elements[], int array_size) { int i, j, k; int min, temp; for (i = 0; i < maxsize-1; i++) { min = i; for (j = i+1; j < maxsize; j++) { if (elements[j] < elements[min]) min = j; } temp = elements[i]; elements[i] = elements[min]; elements[min] = temp; } }

PROGRAM TO IMPLEMENT INSERTION SORT USING ARRAYS

#include<stdio.h> #include<conio.h> void bubble(int a[],int n) { int i,j,t; for(i=n-2;i>=0;i--) { for(j=0;j<=i;j++) { if(a[j]>a[j+1]) { t=a[j]; a[j]=a[j+1]; a[j+1]=t; } }

}//end for 1. }//end function.

void main() { int a[100],n,i; clrscr(); printf("\n\n Enter integer value for total no.s of elements to be sorted: "); scanf("%d",&n); for( i=0;i<=n-1;i++) { printf("\n\n Enter integer value for element no.%d : ",i+1); scanf("%d",&a[i]); } bubble(a,n);

printf("\n\n Finally sorted array is: "); for( i=0;i<=n-1;i++) printf("%3d",a[i]); } //end program.

PROGRAM TO IMPLEMENT TOPOLOGICAL SORT USING ARRAYS USING C

#include<stdio.h> #defineMAX20 int n,adj[MAX][MAX]; int front=-1,rear=-1,queue[MAX]; void main() { int i,j=0,k; int topsort[MAX],indeg[MAX]; create_graph(); printf(The adjacency matrix is:\n); display(); for(i=1;i<+n;i++) { indeg[i]=indegree(i); if(indeg[i]==0) insert_queue(i); } while(front<=rear) { k=delete_queue(); topsort[j++]=k; for(i=1;i<=n;i++) { if(adj[k][i]==1) { adj[k][i]=0; indeg[i]=indeg[i]-1; if(indeg[i]==0) insert_queue(i);

} } } printf("Nodes after topological sorting are:\n"); for(i=0;i<=n;i++) printf("%d",topsort[i]); printf("\n"); } create_graph() { int i,max_edges,origin,destin; printf("\n Enter number of vertices:"); scamf("%d",&n); max_edges=n*(n-1); for(i=1;i<=max_edges;i++) { printf("\n Enter edge %d (00 to quit):",i); scanf("%d%d",&origin,&destin); if((origin==0)&&(destin==0)) { printf("Invalid edge!!\n"); i; } else adj[origin][destin]=1; }return; } display() { int i,j; for(i=0;i<=n;i++) { for(j=1;jrear) { printf(Queue Underflow); return; } else { del_item=queue[front];

front=front+1; return del_item; } } int indegree(int node) { int i,in_deg=0; for(i=1;i<=n;i++) if(adj[i][node]==1) in_deg++; returnin_deg; }

PROGRAM TO IMPLEMENT HEAP SORT USING ARRAYS USING C #include <stdio.h> #include <conio.h> void main() { int i,v,p,l[100],n,k,t,j; clrscr(); printf("enter the number"); scanf("%d",&n); for(i=1;i<=n;i++) { scanf("%d",&l[i]); } printf("\nentered list are as follows"); for(i=1;i<=n;i++) { printf("\t%d",l[i]); } /*creation of heap*/ for(k=2;k<=n;k++) { i=k; t=l[k];

j=i/2; while((i>1)&&(t > l[j])) { l[i]=l[j]; i=j; j=i/2; if(j<1) j=1; } l[i]=t; }

printf("\nHEAP");

for(i=1;i<=n;i++) { printf("\t%d",l[i]); } printf("\n"); //heapsort for(k=n;k>=2;--k) { t=l[1]; l[1]=l[k]; l[k]=t; i=1; v=l[1]; j=2; if((j+1) if(l[j+1]>l[j]) j++; while((j<=(k-1))&&(l[j]>v)) { l[i]=l[j]; i=j; j=2*i; if((j+1)

if(l[j+1]>l[j]) j++; else if(j>n) j=n; l[i]=v; } for(p=1;p<=n;p++) { printf("\t%d",l[p]); } printf("\n"); } printf("\nthe sorted list "); for(i=1;i<=n;i++) { printf("\t%d",l[i]); } getch(); }

PROGRAM TO IMPLEMENT HEAP SORT USING POINTERS IN C

#include<stdio.h> int *x[100],no,i; void buildheap(); void sort();

void main() { clrscr(); printf("\n Enter the number of elements\n");

scanf("%d",&no); printf("\n Enter %d numbers\n",no); for(i=1;i<=no;++i) scanf("%d",&x[i]); buildheap(); sort(); printf("\n Sorted elements are:\n"); for(i=1;i<=no;++i) printf("%5d",x[i]); getch(); }

void buildheap() { int j,k,*temp; for(k=2;k<no;++k) { i=k; temp=x[k]; j=i/2; while((i>1)&&(temp>x[j])) { x[i]=x[j]; i=j; j=i/2;

if(j<1)j=1; } x[i]=temp; } }

void sort() { int *temp,*value,j,k; for(k=no;k>=2;--k) { temp=x[1]; x[1]=x[k]; x[k]=temp; i=1; value=x[1]; j=2; if ((j+1)<k) if(x[j+1]>x[j]) j++; while((j<=(k-1))&&(x[j]>value)) { x[i]=x[j]; i=j; j=2*i;

if ((j+1)<k) if(x[j+1]>x[j]) j++; else if(j>no) j=no; x[i]=value; } }

PROGRAM TO IMPLEMENT BUBBLE SORT USING POINTERS IN C


#include<stdio.h> int *a[100],i,j,item; void main() { void sort(),display(); int i; clrscr(); printf("\n Enter the number of elements in the first array\n"); scanf("%d",&item); printf("\n Enter %d numbers\n",item); for(i=0;i<item;++i) scanf("%d",&a[i]); sort(); display();

void sort() { int swap=1,*temp; for(i=0;i<item && swap==1;++i) { swap=0; for(j=0;j<item-(i+1);++j) if (a[j]>a[j+1])

{ temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; swap=1; } } }

void display()

{ printf("\n Sorted elements are:\n"); for(i=0;i<item;++i) printf("%d\n",a[i]); getch(); }

You might also like