Sei sulla pagina 1di 38

Data Structures and Algorithms Using C

Data Structures and Algorithms Lab Using C

Problems & Solutions

Note: The following programs are Compiled and Executed in Borland C++ ver.5.02. Page 1

Data Structures and Algorithms Using C


1. Write a program to determine the Factorial and Binomial Co-efficient of a number. #include<stdio.h> #include<conio.h> float factorial(int); void main() { int x,r; float b; clrscr(); printf("\n INTEGER NUMBER = "); scanf("%d",&x); printf("\n R VALUE = "); scanf("%d",&r); clrscr(); printf("\n FACTORIAL OF A NUMBER"); printf("\n ---------------------"); printf("\n AN INTEGER NUMBER = %2d",x); printf("\n FACTORIAL OF %2d IS = %5.1f",x,factorial(x)); printf("\n\n BINOMIAL CO-EFFICIENT"); printf("\n ---------------------"); b = factorial(x)/(factorial(r)*factorial(x-r)); printf("\n BINOMIAL CO-EFFICIENT OF %1dC%1d IS = %5.2f",x,r,b); getch(); }//main end float factorial(int n) { if (n<=1) return(1.0); else return(n*factorial(n-1)); }

Page 2

Data Structures and Algorithms Using C


2. Write a program to Add two Polynomials, A and B. #include<stdio.h> #include<conio.h> struct pol{ float coef; int exp; }; void newterm(float,int); struct pol terms1[10],terms2[10],terms3[20]; int af,al,bf,bl,cf,cl,i,n,n1,p,q,d,k=1; float c; void main() { clrscr(); printf("\nNo.of terms in polynomial A : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nThe Coefficient Part : "); fflush(stdin); scanf("%f",&terms1[i].coef); printf("\nThe Exponent Part : "); fflush(stdin); scanf("%d",&terms1[i].exp); } printf("\n\nNo.of term in polynomial B : "); scanf("%d",&n1); for(i=1;i<=n1;i++) { printf("\nThe Coefficient Part : "); fflush(stdin); scanf("%f",&terms2[i].coef); printf("\nThe Exponent Part : "); fflush(stdin); scanf("%d",&terms2[i].exp); } Page 3

Data Structures and Algorithms Using C


p=1,q=1,cf=1; al=n,bl=n1; while((p<=al) && (q<=bl)) { d=terms1[p].exp-terms2[q].exp; if (d==0) { c=terms1[p].coef+terms2[q].coef; if (c!=0) newterm(c,terms1[p].exp); p++;q++; } if (d<0) { newterm(terms2[q].coef,terms2[q].exp); q++; } if (d>0) { newterm(terms1[p].coef,terms1[p].exp); p++; } }/*end while*/ while (p<=al) { newterm(terms1[p].coef,terms1[p].exp); p++; } while (q<=bl) { newterm(terms2[q].coef,terms2[q].exp); q++; } cl=k-1; clrscr(); printf("\n POLYNOMIAL ADDITION"); printf("\n -------------------"); printf("\n Polynomial A"); printf("\n 2X^3 + 3X^2 - 2X^1 + 2\n"); Page 4

Data Structures and Algorithms Using C


printf("\n Polynomial B"); printf("\n 6X^8 + 5X^2 + 2X^1 - 7\n"); printf("\n Added Polynomial"); printf("\n ----------------\n"); while(cf<=cl) { if (terms3[cf].coef<0) printf("(%2.1f)",terms3[cf].coef); else printf("%2.1f",terms3[cf].coef); if (terms3[cf].exp!=0) printf("X^%1d+",terms3[cf].exp); if (cf!=cf) printf("+"); cf++; } getch(); }/*main end*/ void newterm(float c,int e) { if (k>9999) { printf("\n TOO MANY TERMS IN POLYNOMIALS"); //exit(0); } terms3[k].coef=c; terms3[k].exp=e; k++; return; }

Page 5

Data Structures and Algorithms Using C


3. Create a Stack and Add elements into the Stack. Delete elements from the same Stack. #include<stdio.h> #include<conio.h> int stack[50],item; int ch,n=0,top,i; void main() { void create(); void add(); void display(); void delete1(); do { clrscr(); printf("\nSTACK OPERATIONS"); printf("\n----------------"); printf("\n Your Choice(1-Create 2-Add 3-Delete 4-Display 5-Exit:"); scanf("%d",&ch); if (ch==1) { printf("\n Maximum Size of the Stack : "); scanf("%d",&n); } switch(ch) { case 1:create();getch();break; case 2:add();getch();break; case 3:delete1();getch();break; case 4:display();getch();break; case 5:break; default :printf("\n INVALID CHOICE. TRYAGAIN..."); getch(); } } while(ch!=5); }/*main end*/ void create() Page 6

Data Structures and Algorithms Using C


{ printf("\n CREATING A STACK"); printf("\n ----------------"); top=0; printf("\n Stack Creation Over"); return; } void add() { printf("\n ADDING ELEMENT INTO A STACK"); printf("\n ---------------------------"); printf("\n Item that has to be inserted : "); scanf("%d",&item); if (top == n) { printf("\n STACK FULL CONDITION"); getch(); return; } top++; stack[top]=item; printf("\n Item %d has been inserted into Stack",item); printf("\n Items of the Stack are :\n"); for(i=1;i<=top;i++) printf("\t %4d",stack[i]); return; } void delete1() { printf("\n DELETING ELEMENT FROM A STACK"); printf("\n -----------------------------"); if(top==0) { printf("\n STACK EMPTY CONDITION"); getch();return; } item=stack[top]; top--; Page 7

Data Structures and Algorithms Using C


printf("\n Item %d has been deleted from Stack",item); printf("\n Items of the Stack are :\n"); for(i=1;i<=top;i++) printf("\t %4d",stack[i]); return; } void display() { printf("\n DISPLAYING ALL ELEMENTS OF STACK"); printf("\n --------------------------------\n"); for(i=1;i<=top;i++) printf("\t %4d",stack[i]); return; }

Page 8

Data Structures and Algorithms Using C


4. Create a Queue and Add elements into the Queue. Delete elements from the same Queue. #include<stdio.h> #include<conio.h> int queue[50],item; int ch,n=0,front,rear,i; void main() { void create(); void add(); void delete1(); void display(); do { clrscr(); printf("\nQUEUE OPERATIONS"); printf("\n----------------"); printf("\n Your Choice(1-Create 2-Add 3-Delete 4-Display 5-Exit:"); scanf("%d",&ch); if (ch==1) { printf("\n Maximum Size of the Queue : "); scanf("%d",&n); } switch(ch) { case 1:create();getch();break; case 2:add();getch();break; case 3:delete1();getch();break; case 4:display();getch();break; case 5:break; default :printf("\n INVALID CHOICE. TRYAGAIN..."); getch(); } }while(ch!=5); }/*main end*/ void create() Page 9

Data Structures and Algorithms Using C


{ printf("\n CREATING A QUEUE"); printf("\n ----------------"); front=0;rear=0; printf("\n Queue Creation Over"); return; } void add() { printf("\n ADDING ELEMENT INTO A QUEUE"); printf("\n ---------------------------"); printf("\n Item that has to be inserted : "); scanf("%d",&item); if (rear == n) { printf("\n QUEUE FULL CONDITION"); getch(); return; } rear++; queue[rear]=item; printf("\n Item %d has been inserted into Queue",item); printf("\n Items of the Queue are :\n"); for(i=front+1;i<=rear;i++) printf("\t %4d",queue[i]); return; } void delete1() { printf("\n DELETING ELEMENT FROM A QUEUE"); printf("\n -----------------------------"); if(front==rear) { printf("\n QUEUE EMPTY CONDITION"); getch(); return; } Page 10

Data Structures and Algorithms Using C


front++; item=queue[front]; front++; printf("\n Item %d has been deleted from Queue",item); printf("\n Items of the Queue are :\n"); for(i=front;i<=rear;i++) printf("\t %4d",queue[i]); return; } void display() { printf("\n DISPLAYING ALL ELEMENTS OF QUEUE"); printf("\n --------------------------------\n"); for(i=front+1;i<=rear;i++) printf("\t %4d",queue[i]); return; }

Page 11

Data Structures and Algorithms Using C


5. To Traverse a Binary Tree in a. In-order form b. Pre-order form c. Post-order form #include<stdio.h> #include<conio.h> #include<stdlib.h> struct treerec { struct treerec *lchild; char data; struct treerec *rchild; }; struct treerec *item; struct treerec *rdata[50]; main() { int i,n; clrscr(); printf("\n No. of Numbers : "); scanf("%d",&n); printf("\n Enter Data of the Tree : \n"); for(i=1;i<=n;i++) { rdata[i]=(struct treerec *)malloc(sizeof(struct treerec)); scanf("\r%c",&rdata[i]->data); rdata[i]->lchild=NULL; rdata[i]->rchild=NULL; } for(i=1;i<=n;i++) { rdata[i]->lchild = rdata[2*i]; rdata[i]->rchild = rdata[2*i+1]; } item=rdata[1]; Page 12

Data Structures and Algorithms Using C


clrscr(); printf("\n TREE TRAVERSALS"); printf("\n ---------------"); printf("\n GIVEN DATA IN TREE"); printf("\n ------------------\n\t"); for(i=1;i<=n;i++) printf("%c",rdata[i]->data); printf("\n\n"); printf("\n INORDER TREE TRAVERSAL"); printf("\n ----------------------\n\t"); inorder(item); printf("\n PREORDER TREE TRAVERSAL"); printf("\n -----------------------\n\t"); preorder(item); printf("\n POSTORDER TREE TRAVERSAL"); printf("\n ------------------------\n\t"); postorder(item); getch(); }/*main end*/ inorder(cnode) struct treerec *cnode; { if (cnode!=NULL) { inorder(cnode->lchild); if (cnode->data!='-') printf("%c",cnode->data); inorder(cnode->rchild); } return(1); } preorder(cnode) struct treerec *cnode; { if (cnode!=NULL) { if (cnode->data!='-') printf("%c",cnode->data); preorder(cnode->lchild); preorder(cnode->rchild); } return(1); Page 13

Data Structures and Algorithms Using C


} postorder(cnode) struct treerec *cnode; { if (cnode!=NULL) { postorder(cnode->lchild); postorder(cnode->rchild); if (cnode->data!='-') printf("%c",cnode->data); } return(1); }

Page 14

Data Structures and Algorithms Using C


6. Create a Singly Linked List. Insert elements into the Singly Linked List and Delete elements from the same Singly Linked List. #include<stdio.h> #include<conio.h> #include<stdlib.h> struct node { int no; struct node *next; }; struct node *head,*ptr; int i,ch; void main() { void create(); void add(); void display(); void delete1(); do { clrscr(); printf("\n LINKED LIST OPERATIONS"); printf("\n ----------------------"); printf("\n\t 1 - CREATION"); printf("\n\t 2 - ADDITION"); printf("\n\t 3 - DELETION"); printf("\n\t 4 - EXIT"); printf("\n\n Enter your Choice : "); scanf("%d",&ch); switch(ch) { case 1 : { create();display(); getch(); break; } Page 15

Data Structures and Algorithms Using C


case 2 : { add();display(); getch(); break; } case 3 : { delete1();display(); getch(); break; } case 4 : break; default: printf("\n\t Invalid Choice. TryAgain."); getch(); }/*end switch*/ }while(ch!=4); }/*main end*/ void create() { head=NULL; printf("\n A Number (Type 999 to quit) : "); scanf("%d",&i); while(i!=999) { ptr=(struct node*)malloc(sizeof(struct node)); ptr->no=i; ptr->next=head; head=ptr; printf("\n A Number (Type 999 to quit) : "); scanf("%d",&i); } return; }/*end create*/

Page 16

Data Structures and Algorithms Using C


void add() { struct node *pnode; int ch; ptr=(struct node *)malloc(sizeof(struct node)); printf("\n A Number (Type 999 to quit) : "); scanf("%d",&ptr->no); printf("\nPlace ahead of (Press 0 if New Item is Last s: "); scanf("%d",&ch); if (ch==0) { ptr->next=head; head=ptr; } else { pnode=head; while ((pnode->no!=ch) && (pnode)) pnode=pnode->next; ptr->next=pnode->next; pnode->next=ptr; } return; }/*end add*/ void display() { getch(); clrscr(); printf("\n\t Contents of Linked List"); printf("\n\t -----------------------\n"); ptr=head; while(ptr) { printf("\n\t Number = %d\n",ptr->no); ptr=ptr->next; } return; }/*end display*/ void delete1() Page 17

Data Structures and Algorithms Using C


{ int i; char flag,flag1; struct node *pnode; printf("\n The Element to be Deleted : "); scanf("%d",&i); ptr=head; if (ptr->no==i) { head=ptr->next; flag='y'; } else { pnode=ptr; ptr=ptr->next; while((ptr->no!=i) && (ptr)) { pnode=ptr; ptr=ptr->next; } if(ptr) pnode->next=ptr->next; flag1='y'; } if ((flag=='n') || (flag1=='n')) printf("\nKey %2d is not found in the Linked List",i); return; }

Page 18

Data Structures and Algorithms Using C


7. Sort a given set of numbers using Bubble Sort technique. #include<stdio.h> #include<conio.h> void main() { int i,j,n,pass; char change; float temp,x[20]; clrscr(); printf("\n No. of numbers : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n Enter a number : "); scanf("%f",&x[i]); } clrscr(); printf("\n\n\t\t SORTING OF NUMBERS"); printf("\n\t\t ------------------"); printf("\n\t\t Given List"); printf("\n\t\t ----------\n\n\t\t"); for(i=1;i<=n;i++) printf("%6.2f",x[i]); printf("\n\n\t\t AFTER SUCCESSIVE PASSES"); printf("\n\t\t -----------------------\n"); change='t'; pass=1; while((pass<=(n-1)) && (change=='t')) { printf("\t\t"); for(j=1;j<=(n-pass);j++) { if (x[j]>x[j+1]) { temp=x[j]; x[j]=x[j+1]; x[j+1]=temp; change='t'; Page 19

Data Structures and Algorithms Using C


}/*end if*/ }/*end for*/ for(i=1;i<=n;i++) printf("%6.2f",x[i]); printf("\n"); pass++; }/*end while*/ printf("\n\n\t\t SORTED LIST"); printf("\n\t\t -----------\n\t\t"); for(i=1;i<=n;i++) printf("%6.2f",x[i]); getch(); }/*main end*/

Page 20

Data Structures and Algorithms Using C


8. Write a program to search for a particular number in a given set of numbers using Binary Search technique. #include<stdio.h> #include<conio.h> void main() { int f[20],i,j,n,k,l,u,m,temp; char done; clrscr(); printf("\n Enter the no. of numbers : "); fflush(stdin); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\nEnter %2d number = ",i); fflush(stdin); scanf("%d",&f[i]); } printf("\n Enter tthe key to be searched : "); fflush(stdin); scanf("%d",&k); clrscr(); printf("\n\n BINARY SEARCH"); printf("\n -------------\n"); printf("\n Given List"); printf("\n ----------\n\t"); for(i=1;i<=n;i++) printf("%6d",f[i]); for(i=1;i<=n-1;i++) { for(j=i+1;j<=n;j++) { if(f[i]>f[j]) { temp=f[i]; f[i]=f[j]; f[j]=temp; }/*end if*/ }/*end for j*/ }/*end for i*/ Page 21

Data Structures and Algorithms Using C


printf("\n\n SORTED LIST"); printf("\n -----------\n\t"); for(i=1;i<=n;i++) printf("%6d",f[i]); printf("\n\n Key to be Searched : %d",k); l=1;u=n; done='f'; while ((l<=u) && (done=='f')) { m=(l+u)/2; if (k>f[m]) l=m+1; if (k<f[m]) u=m-1; if (k==f[m]) { i=m; done='t'; }/*end if*/ }/*end while*/ if (done=='f') printf("\n KEY %d IS NOT FOUND IN THE FILE",k); else printf("\n KEY %d IS FOUND IN POSITION %d",k,i); getch(); }/*main end*/

Page 22

Data Structures and Algorithms Using C


9. Given a String, a. Find the length of the String b. Concatenate with another String #include<stdio.h> #include<string.h> #include<conio.h> void main() { char str1[35],str2[35],str3[70]; int i,j; for(i=0;i<=70;i++) str3[i]=' '; printf("\n Enter the Text1 : "); gets(str1); printf("\n Enter the Text2 : "); gets(str2); clrscr(); printf("\n\t STRING MANIPULATION"); printf("\n\t -------------------"); printf("\n\t GIVEN TEXT1"); printf("\n\t -----------"); printf("\n\t %s\n",str1); printf("\n\t Number of Characters = %d\n",strlen(str1)); printf("\n\t GIVEN TEXT2"); printf("\n\t -----------"); printf("\n\t %s\n",str2); for(i=0;str1[i]!='\0';i++) str3[i]=str1[i]; for(j=0;str2[j]!='\0';j++) { str3[i]=str2[j]; i++; } str3[i]='\0'; printf("\n\t CONCATENATED STRING"); printf("\n\t -------------------"); printf("\n\t %s",str3); getch(); }/*main end*/ Page 23

Data Structures and Algorithms Using C


10. Write a program to delete a portion of a given String.

#include<stdio.h> #include<conio.h> void main() { char str[55],str1[55]; int i,j,s,e; printf("\n\n Enter the Text : "); gets(str); printf("\n Starting - Ending position of text deleted"); scanf("%d%d",&s,&e); clrscr(); printf("\n STRING MANIPULATION"); printf("\n -------------------\n"); printf("\n\n Given Text"); printf("\n ----------"); printf("\n %s\n",str); printf("\n\n Starting-Ending Position of Text to be Deleted(S-E):%d-%d",s,e); printf("\n\n String After Deletion"); printf("\n ---------------------\n "); j=0; for(i=0;str[i]!='\0';i++) if ((i<(s-1))|| (i>(e-1))) { str1[j]=str[i]; j++; } str1[j]='\0'; for(i=0;str1[i]!='\0';i++) printf("%c",str1[i]); getch(); }/*main end*/

Page 24

Data Structures and Algorithms Using C


11. Count the number of characters in a given String and reverse the String. #include<stdio.h> #include<conio.h> #include<ctype.h> void main() { char x,str[55]; int i,charcnt=0,wordcnt=0,chcnt=0; printf("\n Enter the Text : "); gets(str); printf("\n Enter the Character : "); scanf("%c",&x); for(i=0;str[i]!='\0';i++) { charcnt++; if ((str[i]==x)||(str[i]==toupper(x))||(toupper(str[i])==x)) chcnt++; if (str[i]==' ') wordcnt++; } clrscr(); printf("\n\t STRING MANIPULATION"); printf("\n\t -------------------\n"); printf("\n\t GIVEN TEXT"); printf("\n\t ----------"); printf("\n\t %s\n",str); printf("\n\t OCCURENCES OF CHARACTER %c = %d",x,chcnt); printf("\n\n\t NO. OF WORDS = %d\n\n",wordcnt+1); printf("\n\t REVERSED TEXT"); printf("\n\t -------------\n\n\t"); for(i=charcnt;i>=0;i--) printf("%c",str[i]); getch(); }/*main end*/

Page 25

Data Structures and Algorithms Using C


12. Given an Employee number, Employee name, Department and Basic pay, prepare a pay slip for that Employee using Files. #include<stdio.h> #include<conio.h> struct emp { int eno; char ename[30]; char dept[15]; float basic; }; main() { struct emp employee; FILE *fp; float da,hra,cca,pf,lic,gp,np; int n,i; fp=fopen("emp.dat","w"); clrscr(); printf("\n Enter the no. of Employees : "); scanf("%d",&n); for(i=1;i<=n;i++) { printf("\n Employee No. = "); scanf("%d",&employee.eno); fflush(stdin); printf("\n Employee Name = "); gets(employee.ename); fflush(stdin); printf("\n Department Name = "); gets(employee.dept); printf("\n Basic Pay = "); scanf("%f",&employee.basic); fwrite(&employee,sizeof(struct emp),1,fp); } fclose(fp); fp=fopen("emp.dat","r"); printf("%s",employee.ename); fread(&employee,sizeof(struct emp),1,fp); Page 26

Data Structures and Algorithms Using C


while(!feof(fp)) { printf("\n%s",employee.ename); clrscr(); da = 0.08 * employee.basic; hra = 0.05 * employee.basic; cca = 0.03 * employee.basic; pf = 0.06 * employee.basic; lic = 0.10 * employee.basic; gp = employee.basic + hra + da + cca; np = gp - (pf + lic); printf("\n PAYSLIP"); printf("\n -------\n\n"); printf("\n Employee No. : %3d",employee.eno); printf("\n Employee Name : %s",employee.ename); printf("\n Department : %s",employee.dept); printf("\n Basic Pay :(Rs.)%8.2f\n\n",employee.basic); printf("\n Allowances"); printf("\n HRA :(Rs.)%6.2f",hra); printf("\n DA :(Rs.)%6.2f",da); printf("\n CCA :(Rs.)%6.2f\n\n",cca); printf("\n Gross Pay :(Rs.)%6.2f\n\n",gp); printf("\n Deductions"); printf("\n LIC :(Rs.)%6.2f\n\n",lic); printf("\n Net Pay :(Rs.)%6.2f",np); getch(); fread(&employee,sizeof(struct emp),1,fp); } fclose(fp); }/*main end*/

Page 27

Data Structures and Algorithms Using C


13. Determine the Permutations of a given String. #include<stdio.h> #include<conio.h> #include<string.h> void main() { void prem(char a[50],int n,int k); int n,k=0; char a[50]; clrscr(); printf("Enter the String : "); gets(a); n=strlen(a); clrscr(); printf("\n OUTPUT"); printf("\n ------\n\n"); printf("\n Generation of Permutations"); printf("\n --------------------------\n\n"); printf("\n Given String : %s\n",a); printf("\n\n Permutations of Given String are \n\n"); perm(a,k,n); getch(); }/*main end*/ void perm(ar,k1,n1) int k1,n1; char ar[50]; { int i; char j,b[10]; if(k1==n1) { printf("\t%s",ar); } strcpy(b,ar); for(i=k1;i<n1;i++) { j=ar[k1]; ar[k1]=ar[i]; ar[i]=j; perm(ar,k1+1,n1); strcpy(ar,b); } } Page 28

Data Structures and Algorithms Using C


14. Write a program to generate the Fibonacci Series. #include<stdio.h> #include<math.h> #include<conio.h> void main() { int j,i0,i1,i2,n; clrscr(); printf("\n\n"); printf("\n The no. of Terms that have to be Generated is : "); scanf("%d",&n); i0=-1; i1=1; clrscr(); printf("\n\n Generation of Fibonacci Sequence"); printf("\n --------------------------------\n\n"); printf("\n The Fibonacci Sequence for the %d Values is found to be :",n); printf("\n\n\n"); for(j=1;j<=n;j++) { i2=i0+i1; printf(" %d ",i2); i0=i1; i1=i2; } getch(); }/*main end*/

Page 29

Data Structures and Algorithms Using C


15. Determine the Sparse Transpose Matrix of a given Matrix. #include<stdio.h> #include<math.h> #include<conio.h> main() { struct sparse { int row; int col; int val; }; struct sparse a[50],b[50]; int i,j,m; clrscr(); printf("\n Enter the No. of Rows, Columns, Values of Sparse Matrix :\n"); scanf("%d%d%d",&a[0].row,&a[0].col,&a[0].val); b[0].row=a[0].col; b[0].col=a[0].row; b[0].val=a[0].val; printf("\n Please enter the corresponding Rows, Columns and Values :\n"); for(i=1;i<=a[0].val;i++) { scanf("\n%d%d%d",&a[i].row,&a[i].col,&a[i].val); } m=1; for(i=1;i<=a[0].col;i++) { for(j=1;j<=a[0].val;j++) { if(i==a[j].col) { b[m].row=a[j].col; b[m].col=a[j].row; b[m].val=a[j].val; m=m+1; } } } Page 30

Data Structures and Algorithms Using C

clrscr(); printf("\n SPARSE MATRIX TRANSPOSE"); printf("\n -----------------------\n"); printf("\n Given Matrix A :"); printf("\n ----------------\n\n"); for(i=0;i<=a[0].val;++i) { printf("\n\t A[%d] %d %d %d",i,a[i].row,a[i].col,a[i].val); } printf("\n"); printf("\n The Transpose of the Given Matrix is :\n"); printf("\n --------------------------------------\n\n"); for(i=0;i<=b[0].val;++i) { printf("\n\t B[%d] %d %d %d",i,b[i].row,b[i].col,b[i].val); } getch(); return; }/*main end*/

Page 31

Data Structures and Algorithms Using C


16. Write a program to convert an Infix Expression to Postfix Expression. #include<stdio.h> #include<string.h> #include<conio.h> void display_status(char stack[], int top, char ch, char exp[], int k); /*** Main Program ***/ void main() { void change_screen(); /*** Variable Declaration Part ***/ char infix[100], stack[100], exp[100]; int i, top=0, k=0,len; /*** Printing Heading and Initialization Process ***/ gotoxy(26,2); printf("Infix to Postfix Conversion"); gotoxy(15,4); printf("Infix Expression : "); fflush(stdin);scanf("%s",&infix); len=strlen(infix); infix[len]=')'; stack[0]='('; infix[len+1]='\0'; /*** Process Converting Infix to Postfix ***/ gotoxy(10,6); printf("Symbol Scanned"); gotoxy(30,6); printf("Stack Content"); gotoxy(50,6); printf("Expression"); gotoxy(10,7); for(i=1;i<=50;i++) printf("%c", 205); for(i=0; infix[i]!='\0';i++) { switch(infix[i]) { case'^':if(stack[top]=='^') exp[k++]=stack[top--]; stack[++top]=infix[i]; break; case'*': Page 32

Data Structures and Algorithms Using C

case'/':if(stack[top]=='/'||stack[top]=='*' ||stack[top]=='^') exp[k++]=stack[top--]; stack[++top]=infix[i]; break; case'+': case'-': if(stack[top]=='/'||stack[top]=='*'|| stack[top]=='^'||stack[top]=='+'||stack[top]=='-') exp[k++]=stack[top--]; stack[++top]=infix[i]; break; case'(':stack[++top]=infix[i]; break; case')':while(stack[top]!='(') exp[k++]=stack[top--]; if(stack[top]=='(') top--; break; default:if(infix[i]!=')'||infix[i]!=' ') exp[k++]=infix[i]; break; } display_status(stack,top,infix[i],exp,k); } exp[k]='\0'; /*** Displaying the Result ***/ gotoxy(15, wherey()+2); textcolor(15); printf("Postfix Expression : %s",exp); getch(); }/*main end*/ void change_screen() { int i; getch(); clrscr(); gotoxy(10,6); printf("Symbol Scanned"); gotoxy(30,6); printf("Stack Context"); gotoxy(50,6); printf("Expression"); gotoxy(10,7); Page 33

Data Structures and Algorithms Using C


for(i=1;i<=50;i++) printf("%c",205); } void display_status(char stack[],int top,char ch,char exp[],int k) { int r,i; r=wherey(); r++; if(r>22) { change_screen(); r=wherey(); r++; } gotoxy(18,r); printf("%c",ch); gotoxy(36,r); for(i=0;i<=top;i++) printf("%c",stack[i]); gotoxy(54,r); for(i=0;i<k;i++) printf("%c",exp[i]); }

Page 34

Data Structures and Algorithms Using C


17. Write a program that implements Depth First Search technique. #include<stdio.h> #include<conio.h> int adj[100][100],n; int visit[100]; void dfs(); void main() { int i,j,var; printf("\n\n DEPTH FIRST SEARCH"); printf("\n ------------------"); printf("\n Enter the no. of vertices in the graph : "); scanf("%d",&n); fflush(stdin); printf("\n Enter the values of Adjacency Matrix : \n"); for(i=0;i<n;i++) { visit[i]=0; for(j=0;j<n;j++) { scanf("%d",&adj[i][j]); fflush(stdin); } } printf("\n Enter the no. of DFS has to be done : "); scanf("%d",&var); fflush(stdin); clrscr(); printf("\n DEPTH FIRST SEARCH"); printf("\n ------------------\n\n"); printf("\n The Number of Vertices %d\n",n); printf("\n The Given Adjacency Matrix \n"); for(i=0;i<n;i++) { printf("\n"); for(j=0;j<n;j++) { Page 35

Data Structures and Algorithms Using C


printf("\t %d",adj[i][j]); } } printf("\n\n"); printf("\n The Vertex for which DFS has to be done %d \n",var); printf("\n The Vertices Obtained after DFS \n"); dfs(var-1); printf("\n"); getch(); return; }/*main end*/ /*recursive procedure*/ void dfs(v) int v; { int j; visit[v]=1; printf("\t %d",v+1); for(j=0;j<n;j++) { if (adj[v][j]==1) { if (visit[j]==0) { dfs(j); } } } return; }

Page 36

Data Structures and Algorithms Using C


18. Determine the Connected Components of an Adjacency Matrix. #include<stdio.h> #include<conio.h> int adjacent[100][100],m; int view[100];

void main() { void component(); void trap(); int i,j; printf("\n"); clrscr(); printf("\n\t\t\t CONNECTED COMPONENTS\n"); printf("\n ENTER THE NO OF VERTICES IN THE GRAPH "); scanf("%d",&m); fflush(stdin); printf("\n\n THE NUMBER OF VERTICES ARE FOUND TO BE %d \n",m); printf("\n ENTER THE VALUES OF THE ADJACENCY MATRIX\n"); for(i=0;i<m;i++) { view[i]=0; for(j=0;j<m;++j) { scanf("%d",&adjacent[i][j]); fflush(stdin); } } clrscr(); printf("\n\t\t\t CONNECTED COMPONENTS"); printf("\n\t\t\t ####################"); printf("\n"); printf("\n\n\t\t THE NUMBER OF VERTICES IS : %d \n",m); printf("\n\t\t THE GIVEN ADJACENCY MATRIX \n"); for(i=0;i<m;i++) { printf("\n\t\t"); Page 37

Data Structures and Algorithms Using C


for(j=0;j<m;j++) printf("\t %d",adjacent[i][j]); } printf("\n\n"); component(); getch(); }/*main end*/ /* Recursive Procedure */ void trap(r) int r; { int j; view[r]=1; printf(" %d",r+1); for(j=0;j<m;++j) { if(adjacent[r][j]==1) if (view[j]==0) trap(j); } printf("\n"); return; } void component() { int i; for(i=0;i<m;i++) if(view[i]==0) { printf("\t\t CONNECTED COMPONENTS FOR %d ARE : ",i+1); trap(i); } printf("\n"); return; }

Page 38

Potrebbero piacerti anche