• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer
projectsgeek

ProjectsGeek

Download Mini projects with Source Code, Java projects with Source Codes

  • Home
  • Java Projects
  • C++ Projects
  • VB Projects
  • PHP projects
  • .Net Projects
  • NodeJs Projects
  • Android Projects
    • Project Ideas
      • Final Year Project Ideas
      • JSP Projects
  • Assignment Codes
    • Fundamentals of Programming Language
    • Software Design Laboratory
    • Data Structure and Files Lab
    • Computer Graphics Lab
    • Object Oriented Programming Lab
    • Assembly Codes
  • School Projects
  • Forum

C Codes

Boundary and Flood Fill Algorithms in C++ Language

April 6, 2011 by ProjectsGeek 2 Comments

Boundary and Flood Fill Algorithms

Write a program for Boundary and Flood Fill Algorithms in C++ Language . Boundary and Flood Fill Algorithms in C++ should accept the coordinates for polygon.

  • INPUT
  • FLOOD FILL
  • BOUNDARY FILL
  • EXIT

After selecting Boundary and Flood fill algorithm option , It must fill polygon by both algorithms.

Boundary and Flood Fill Algorithms Code

 #include  
 #include  
 #include  
 #include  
 #define max 100000  
 class fill  
 {  
   int edge,a[50],sx,sy,midx,midy;  
   public:  
   void input()  
   {  
     cout<<"\nENTER NO OF EDGES:";  
     cin>>edge;  
     int j=0;  
     for(int i=0;i<edge;i++) <br="">     {  
       cout<<"\nENTER THE X"<<i+1<<"coordinate:"; <br="">       cin>>a[j++];  
       cout<<"\nENTER THE Y"<<i+1<<"coordinate:"; <br="">       cin>>a[j++];  
     }  
   }  
   void b_fill(int x,int y,int clr)  
   {  
     int j=0;  
     midx=getmaxx()/2;  
     midy=getmaxy()/2;  
     setcolor(WHITE);  
     line(0,midy,2*midx,midy);  
     line(midx,0,midx,2*midy);  
     setcolor(RED);  
     for(int i=0;i<edge;i++) <br="">     {  
       if(edge-i!=1)  
         line(a[j]+midx,midy-a[j+1],a[j+2]+midx,midy-a[j+3]);  
       else  
         line(a[j]+midx,midy-a[j+1],a[0]+midx,midy-a[1]);  
       j=j+2;  
     }  
     getch();  
     int st[max][2],top=-1;  
     x=midx+x;  
     y=midy-y;  
     st[++top][0]=x;  
     st[top][1]=y;  
     while(top!=-1)  
     {  
       x=st[top][0];  
       y=st[top][1];  
       top--;  
       if((getpixel(x+1,y)!=clr)&&(getpixel(x+1,y)!=RED))  
       {  
         putpixel(x+1,y,clr);  
         st[++top][0]=x+1;  
         st[top][1]=y;  
       }  
       if((getpixel(x-1,y)!=clr)&&(getpixel(x-1,y)!=RED))  
       {  
         putpixel(x-1,y,clr);  
         st[++top][0]=x-1;  
         st[top][1]=y;  
       }  
       if((getpixel(x,y+1)!=clr)&&(getpixel(x,y+1)!=RED))  
       {  
         putpixel(x,y+1,clr);  
         st[++top][0]=x;  
         st[top][1]=y+1;  
       }  
       if((getpixel(x,y-1)!=clr)&&(getpixel(x,y-1)!=RED))  
       {  
         putpixel(x,y-1,clr);  
         st[++top][0]=x;  
         st[top][1]=y-1;  
       }  
      }  
   }  
   void f_fill(int x,int y,int clr)  
   {  
     int j=0;  
     midx=getmaxx()/2;  
     midy=getmaxy()/2;  
     setcolor(WHITE);  
     line(0,midy,2*midx,midy);  
     line(midx,0,midx,2*midy);  
     setcolor(RED);  
     for(int i=0;i<edge;i++) <br="">     {  
       if(edge-i!=1)  
         line(a[j]+midx,midy-a[j+1],a[j+2]+midx,midy-a[j+3]);  
       else  
         line(a[j]+midx,midy-a[j+1],a[0]+midx,midy-a[1]);  
       j=j+2;  
     }  
     getch();  
     int st[max][2],top=-1;  
     x=midx+x;  
     y=midy-y;  
     st[++top][0]=x;  
     st[top][1]=y;  
     putpixel(x,y,col);  
     while(top!=-1)  
     {  
       x=st[top][0];  
       y=st[top][1];  
       top--;  
       if((getpixel(x+1,y)!=clr)&&(getpixel(x+1,y)==7))  
       {  
         putpixel(x+1,y,clr);  
         st[++top][0]=x+1;  
         st[top][1]=y;  
       }  
       if((getpixel(x-1,y)!=clr)&&(getpixel(x-1,y)==7))  
       {  
         putpixel(x-1,y,clr);  
         st[++top][0]=x-1;  
         st[top][1]=y;  
       }  
       if((getpixel(x,y+1)!=clr)&&(getpixel(x,y+1)==7))  
       {  
         putpixel(x,y+1,clr);  
         st[++top][0]=x;  
         st[top][1]=y+1;  
       }  
       if((getpixel(x,y-1)!=clr)&&(getpixel(x,y-1)==7))  
       {  
         putpixel(x,y-1,clr);  
         st[++top][0]=x;  
         st[top][1]=y-1;  
       }  
      }  
   }  
 };  
 void menu()  
 {  
   clrscr();  
   cout<<"\n\t##### MAIN MENU #####";  
   cout<<"\n\t1.INPUT";  
   cout<<"\n\t2.FLOOD FILL";  
   cout<<"\n\t3.BOUNDARY FILL";  
   cout<<"\n\t4.EXIT";  
   cout<<"\n\tENTER UR CHOICE:";  
 }  
 void main()  
 {  
   int gd=DETECT,gm;  
     initgraph(&gd,&gm,"c:\\tc\\bgi");  
   int ch,x,y,clr;  
   fill f;  
   do  
   {  
     menu();  
     cin>>ch;  
     clrscr();  
     switch(ch)  
     {  
       case 1:  
         f.input();  
         break;  
       case 2:  
         cout<<"\n#### FLOOD FILL ####";  
         cout<<"\nENTER THE COLOR CODE TO FILL:";  
         cin>>clr;  
         cout<<"\nENTER THE X-COORDINATE OF SEED POINT:";  
         cin>>x;  
         cout<<"\nENTER THE Y-COORDINATE OF SEED POINT:";  
         cin>>y;  
         f.f_fill(x,y,clr);  
         getch();  
         break;  
       case 3:  
         cout<<"\n#### BOUNDARY FILL ####";  
         cout<<"\nENTER THE COLOR CODE TO FILL:";  
         cin>>clr;  
         cout<<"\nENTER THE X-COORDINATE OF SEED POINT:";  
         cin>>x;  
         cout<<"\nENTER THE Y-COORDINATE OF SEED POINT:";  
         cin>>y;  
         f.b_fill(x,y,clr);  
         getch();  
         break;  
       case 4:  
         break;  
     }  
   }while(ch!=4);  
 }

 

 

Other Projects to Try:

  1. Graphics Editor code Using C++ Language
  2. Transformations computer graphics using C++ Language
  3. Computer graphics codes in C++ Language
  4. Paging Algorithms using C language OS problem
  5. Bressenham and DDA Line Drawing algorithms

Filed Under: C Assignments, Computer Graphics Tagged With: Computer Graphics

Hash table code in C Language

April 5, 2011 by ProjectsGeek 4 Comments

Hash table code in C

Implementation of Hash table using array and handle collisions using Linear probing with replacement and Chaining without replacement.

Hash table code in C Language Code

 #include  
 #include  
 #define MAX 8  
 void createwor(int [][2]);  
 void insertwor(int [][2]);  
 void deletwor(int [][2]);  
 void displaywor(int [][2]);  
 void createwr(int [][2]);  
 void insertwr(int [][2]);  
 void deletwr(int [][2]);  
 void displaywr(int [][2]);  
 void searchwr(int [][2]);  
 void searchwor(int [][2]);  
 void main()  
 {  
 int ch,ch1,i,j;  
 int a[MAX][2],b[MAX][2];  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 b[i][j]=-1;  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 a[i][j]=-1;  
 do  
 {  
 clrscr();  
 printf("\n\t\tHASH TABLE\n\t\t1.CREATE HASH TABLE\n\t\t2.INSERT\n\t\t3.DELETE\n\t\t4.DISPLAY\n\t\t5.SEARCH\n\t\t6.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 do  
 {  
 clrscr();  
 printf("CREATE HASH TABLE\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 createwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 createwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 2:  
 do  
 {  
 clrscr();  
 printf("\n\t\tINSERT\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 insertwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 insertwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 3:  
 do  
 {  
 clrscr();  
 printf("\n\t\tDELETE\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 deletwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 deletwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 4:  
 do  
 {  
 clrscr();  
 printf("\n\t\tDISPLAY\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 displaywr(a) ;  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 displaywor(b) ;  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 5:  
 do  
 {  
 clrscr();  
 printf("\n\t\tSEARCH\n\t\t1.WITH REPLACEMENT\n\t\t2.WITHOUT REPLACEMENT\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("WITH REPLACEMENT");  
 searchwr(a);  
 break;  
 case 2:  
 printf("WITHOUT REPLACEMENT");  
 searchwor(b);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 6:  
 break;  
 }  
 }while(ch!=6);  
 getch();  
 }  
 void createwor(int b[][2])  
 {  
 int i,j;  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 b[i][j]=-1;  
 do  
 {  
 insertwor(b);  
 printf("DO U WANT TO ENTER MORE");  
 }while(getche()=='y');  
 }  
 void insertwor(int b[][2])  
 {  
 int r,s,t,a;  
 printf("\nENTER DATA");  
 scanf("%d",&a);  
 s=a%5;  
 if(b[s][0]==-1)  
 b[s][0]=a;  
 else  
 {  
 //printf("COLLISION OCCURS");  
 //getch();  
 while(1)  
 {  
 printf("\nTRUE COLLISION OCCURS");  
 getch();  
 if(b[s][1]==-1)  
 {  
 t=s;  
 while(b[s][0]!=-1)  
 { //s++;  
 s=(s+1)%MAX;  
 if(s==t)  
 {  
 printf("OVERFLOW");  
 break;  
 }  
 }  
 if(s!=t)  
 {  
 b[s][0]=a;  
 b[t][1]=s;  
 }  
 break;  
 }  
 else  
 s=b[s][1];  
 }  
 }  
 }  
 void deletwr(int b[][2])  
 {  
 int r,s,t,a;  
 printf("ENTER DATA WHICH U WANT TO DELETE");  
 scanf("%d",&a);  
 t=s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 t=s;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 while(1)  
 {  
 if(b[b[t][1]][0]!=a)  
 b[t][0]=b[b[t][1]][0];  
 r=b[t][1];  
 if(b[r][1]==-1)  
 {  
 b[t][1]=b[r][1];  
 break;  
 }  
 t=r;  
 }  
 b[r][0]=-1;  
 }  
 }  
 void displaywor(int b[][2])  
 {  
 int i,r,s,t,a;  
 printf("\nINDEX\tDATA\tCHAIN");  
 for(i=0;i<max;i++) <br=""> {  
 if(b[i][0]!=-1)  
 {  
 printf("\n%d",i);  
 printf("\t%d",b[i][0]);  
 //if(b[i][1]!=-1)  
 printf("\t%d",b[i][1]);  
 }  
 }  
 getch();  
 }  
 void createwr(int b[][2])  
 {  
 int i,j;  
 for(i=0;i<max;i++) <br=""> for(j=0;j<2;j++)  
 b[i][j]=-1;  
 do  
 {  
 insertwr(b);  
 printf("DO U WANT TO ENTER MORE");  
 }while(getche()=='y');  
 }  
 void insertwr(int b[][2])  
 {  
 int r,s,t,a,i;  
 printf("\nENTER DATA");  
 scanf("%d",&a);  
 s=a%5;  
 if(b[s][0]==-1)  
 b[s][0]=a;  
 else if(s==(b[s][0]%5))  
 {  
 //printf("TRUE COLLISION OCCURS");  
 //getch();  
 while(1)  
 {  
 printf("\nTRUE COLLISION OCCURS");  
 getch();  
 if(b[s][1]==-1)  
 {  
 t=s;  
 while(b[s][0]!=-1)  
 {  
 s=(s+1)%MAX;  
 if(t==s)  
 {  
 printf("OVERFLOW");  
 getch();  
 break;  
 }  
 }  
 if(t!=s)  
 {  
 b[s][0]=a;  
 b[t][1]=s;  
 }  
 break;  
 }  
 else  
 s=b[s][1];  
 }  
 }  
 else  
 {  
 printf("FALSE COLLISION OCCURS");  
 getch();  
 r=i=b[s][0]%5;  
 while(1)  
 {  
 if(b[i][1]==s)  
 {  
 b[i][1]=b[s][1];  
 break;  
 }  
 i=b[i][1];  
 }  
 t=i;  
 while(b[i][1]!=-1)  
 i=b[i][1];  
 t=i;  
 i=(i+1)%MAX;  
 while(b[i][0]!=-1)  
 {  
 i=(i+1)%MAX;  
 if(r==i)  
 {  
 printf("OVERFLOW");  
 getch();  
 break;  
 }  
 }  
 if(r!=i)  
 {  
 b[t][1]=i;  
 b[i][0]=b[s][0];  
 //b[i][1]=-1;  
 b[s][0]=a;  
 b[s][1]=-1;  
 //b[r][1]=i;  
 }  
 }  
 }  
 void displaywr(int b[][2])  
 {  
 int i,r,s,t,a;  
 printf("\nINDEX\tDATA\tCHAIN");  
 for(i=0;i<max;i++) <br=""> {  
 if(b[i][0]!=-1)  
 {  
 printf("\n%d",i);  
 printf("\t%d",b[i][0]);  
 //if(b[i][1]!=-1)  
 printf("\t%d",b[i][1]);  
 }  
 }  
 getch();  
 }  
 void searchwr(int b[][2])  
 {  
 int r,s,a;  
 printf("ENTER DATA WHICH U WANT TO SEARCH");  
 scanf("%d",&a);  
 s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 printf("\nINDEX IS %d:",s);  
 printf("\nDATA IS %d:",b[s][0]);  
 printf("\nINDEX WHICH IS CHAINED FROM IT:%d",b[s][1]);  
 }  
 getch();  
 }  
 void searchwor(int b[][2])  
 {  
 int r,s,a;  
 printf("ENTER DATA WHICH U WANT TO SEARCH");  
 scanf("%d",&a);  
 s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 printf("\nINDEX IS %d:",s);  
 printf("\nDATA IS %d:",b[s][0]);  
 printf("\nINDEX WHICH IS CHAINED FROM IT:%d",b[s][1]);  
 }  
 getch();  
 }  
 void deletwor(int b[][2])  
 {  
 int r,s,t,a,k;  
 printf("ENTER DATA WHICH U WANT TO DELETE");  
 scanf("%d",&a);  
 t=s=a%5;  
 while(1)  
 {  
 if(b[s][0]==a)  
 break;  
 t=s;  
 s=b[s][1];  
 if(s==-1)  
 break;  
 }  
 if(s==-1)  
 printf("NOT FOUND");  
 else  
 {  
 r=t;  
 while(1)  
 {  
 if(b[b[r][1]][0]!=a&&b[b[r][1]][0]%5!=b[r][1])  
 {  
 b[t][0]=b[b[r][1]][0];  
 t=r;  
 }  
 else if(b[b[t][1]][1]==-1)  
 {  
 b[t][1]=-1;  
 break;  
 }  
 r=b[r][1];  
 if(b[r][1]==-1)  
 {  
 b[t][1]=b[r][1];  
 break;  
 }  
 if(b[b[t][1]][0]!=a&&b[b[t][1]][0]%5!=b[t][1])  
 {  
 t=r;  
 k=r;  
 }  
 }  
 b[r][0]=b[k][1]=-1;  
 }  
 }  

 

Other Projects to Try:

  1. Sparse Matrix Operations Code using C Langauge
  2. Matrix operations in c language
  3. Breadth First and Depth First Search C Language
  4. Krushkals algorithm Code in C Language
  5. How to Implement Hash Table using C language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

Primitive operations on Sequential file in C language

April 5, 2011 by ProjectsGeek Leave a Comment

Primitive operations on Sequential file in C language

Implement all primitive operations on Sequential file in C

Primitive operations on Sequential file in C language

 
 #include  
 #include  
 #include  
 typedef struct stud  
 {  
 int roll;  
 char name[20];  
 struct add  
 {  
 int hno;  
 char city[10];  
 }add;  
 char clas[4];  
 int mark;  
 }stud;  
 stud alloc(stud );  
 void insert(char [20]);  
 void create();  
 void display();  
 void searchnr(char [20]);  
 void searchcr(char [20]);  
 void modify();  
 void delet();  
 void main()  
 {  
 int ch,ch1;  
 char filename[20];  
 do  
 {  
 clrscr();  
 printf("\n\t\tFILE OPERATION\n\t\t1.CREATE\n\t\t2.INSERT\n\t\t3.DISPLAY\n\t\t4.SEARCH\n\t\t5.MODIFY\n\t\t6.DELETE\n\t\t7.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 printf("\n\t\tCREATE");  
 create();  
 break;  
 case 2:  
 printf("\n\t\tINSERT");  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 insert(filename);  
 break;  
 case 3:  
 printf("\n\t\tDISPLAY");  
 display();  
 break;  
 case 4:  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 do  
 {  
 clrscr();  
 printf("\n\t\tSEARCH\n\t\t1.BY NAME N ROLL NO\n\t\t2.BY ROLL NO N CLASS\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch1);  
 switch(ch1)  
 {  
 case 1:  
 printf("SEARCH BY NAME N ROLL NO");  
 searchnr(filename);  
 break;  
 case 2:  
 printf("SEARCH BY CLASS N ROLL NO");  
 searchcr(filename);  
 break;  
 case 3:  
 break;  
 }  
 }while(ch1!=3);  
 break;  
 case 5:  
 printf("\n\t\tMODIFY");  
 modify();  
 break;  
 case 6:  
 printf("\n\t\tDELETE");  
 delet();  
 break;  
 case 7:  
 break;  
 }  
 }while(ch!=7);  
 }  
 void create()  
 {  
 FILE *fp;  
 char filename[20];  
 printf("\nENTER THE FILE NAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 if(fp==NULL)  
 {  
 fclose(fp);  
 fopen(filename,"r");  
 }  
 else  
 {  
 printf("\nFILE IS ALREADY PRESENT,DO U WANT TO OVERWRITE PRESS 'y'");  
 if(getche()=='y')  
 {  
 fclose(fp);  
 fopen(filename,"w");  
 }  
 else  
 {  
 fclose(fp);  
 fopen(filename,"a");  
 }  
 }  
 fclose(fp);  
 do  
 {  
 insert(filename);  
 printf("DO U WANT TO CONTINUE:y/n");  
 }while(getche()=='y');  
 }  
 void insert(char filename[20])  
 {  
 FILE *fp;  
 char cl[10];  
 int i=0,r;  
 stud st,st1;  
 fp=fopen(filename,"r");  
 while(1)  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&st.roll);  
 if(st.roll<0&&st.roll>60)  
 printf("\nINVALID ROLL NO");  
 else  
 break;  
 }  
 while(1)  
 {  
 printf("\nENTER CLASS:fe/se/te/be only");  
 scanf("%s",st.clas);  
 if(strcmp(st.clas,"fe")!=0&&strcmp(st.clas,"se")!=0&&strcmp(st.clas,"te")!=0&&strcmp(st.clas,"be")!=0)  
 printf("\nINVALID CLASS");  
 else  
 break;  
 }  
 if(fp==NULL)  
 {  
 printf("\nTHIS IS FIRST ENTRY IN THIS FILE");  
 }  
 else  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st1.roll,&st1.mark,&st1.add.hno,st1.add.city,st1.name,st1.clas);  
 if(st.roll==st1.roll&&(strcmp(st.clas,st1.clas))==0)  
 {  
 printf("\nALREADY PRESENT");  
 getch();  
 i++;  
 break;  
 }  
 }  
 fclose(fp);  
 fp=fopen(filename,"a");  
 if(i==0)  
 {  
 st=alloc(st);  
 fprintf(fp,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 fclose(fp);  
 }  
 void display()  
 {  
 FILE *fp;  
 char filename[20];  
 int i=0;  
 stud st;  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 clrscr();  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 printf("\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.clas);  
 i++;  
 getch();  
 if(i%2==0)  
 clrscr();  
 }  
 fclose(fp);  
 }  
 void searchnr(char filename[])  
 {  
 FILE *fp;  
 char nam[10];  
 int r,i=0;  
 stud st;  
 fp=fopen(filename,"r");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&r);  
 printf("\nENTER NAME:");  
 scanf("%s",nam);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==r&&strcmp(st.name,nam)==0)  
 {  
 i=1;  
 printf("\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.clas);  
 getch();  
 }  
 }  
 }  
 if(i==0)  
 printf("\nNOT FOUND");  
 fclose(fp);  
 getch();  
 }  
 void searchcr(char filename[20])  
 {  
 FILE *fp;  
 char cl[10];  
 int r,i=0;  
 stud st;  
 fp=fopen(filename,"r");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&r);  
 printf("\nENTER CLASS:");  
 scanf("%s",cl);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==r&&strcmp(st.clas,cl)==0)  
 {  
 i=1;  
 printf("\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.name);  
 getch();  
 break;  
 }  
 }  
 }  
 if(i==0)  
 printf("\nNOT FOUND");  
 fclose(fp);  
 getch();  
 }  
 void modify()  
 {  
 FILE *fp,*fp1;  
 char filename[20],filename1[20],cl[10];  
 int r,i=0,rol,ch;  
 stud st;  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 strcpy(filename1,"t");  
 strcat(filename1,filename);  
 fp1=fopen(filename1,"w");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&rol);  
 printf("\nCLASS:");  
 scanf("%s",cl);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==rol&&strcmp(st.clas,cl)==0)  
 {  
 printf("\nOLD DAT IS:\nROLL NO:%d",st.roll);  
 printf("\nMARKS:%d",st.mark);  
 printf("\nHOUSE NO:%d",st.add.hno);  
 printf("\nCITY:%s",st.add.city);  
 printf("\nNAME:%s",st.name);  
 printf("\nCLASS:%s",st.clas);  
 getch();  
 do  
 {  
 clrscr();  
 printf("\nWHICH ENTRY U WANT TO MODIFY\n\t\t1.MARKS\n\t\t2.HOUSE NO\n\t\t3.CITY\n\t\t4.NAME\n\t\t5.EXIT");  
 printf("\nENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 while(1)  
 {  
 printf("\nENTER NEW MARKS:");  
 scanf("%d",&st.mark);  
 if(st.mark<0&&st.mark>100)  
 printf("\nINVALID MARKS");  
 else  
 break;  
 getch();  
 }  
 break;  
 case 2:  
 while(1)  
 {  
 printf("\nENTER NEW HNO:");  
 scanf("%d",&st.add.hno);  
 if(st.add.hno<0&&st.add.hno>32000)  
 printf("\nINVALID h.NO");  
 else  
 break;  
 getch();  
 }  
 break;  
 case 3:  
 printf("\nENTER CITY:");  
 scanf("%s",st.add.city);  
 break;  
 case 4:  
 printf("\nENTER NAME:");  
 scanf("%s",st.name);  
 break;  
 case 5:  
 break;  
 }  
 }while(ch!=5);  
 i=1;  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 break;  
 }  
 else  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 }  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 fclose(fp);  
 fclose(fp1);  
 if(i==0)  
 {  
 printf("\nNOT FOUND");  
 remove(filename1);  
 }  
 else  
 {  
 remove(filename);  
 rename(filename1,filename);  
 }  
 getch();  
 }  
 void delet()  
 {  
 FILE *fp,*fp1;  
 char filename[20],cl[10],filename1[20];  
 int r,i=0;  
 stud st;  
 printf("\nENTER FILENAME:");  
 flushall();  
 gets(filename);  
 fp=fopen(filename,"r");  
 strcpy(filename1,"t");  
 strcat(filename1,filename);  
 fp1=fopen(filename1,"w");  
 if(fp==NULL)  
 printf("\nFILE NOT FOUND");  
 else  
 {  
 printf("\nENTER ROLL NO:");  
 scanf("%d",&r);  
 printf("\nENTER CLASS:");  
 scanf("%s",cl);  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 if(st.roll==r&&strcmp(st.clas,cl)==0)  
 {  
 i=1;  
 break;  
 }  
 else  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 }  
 while(!feof(fp))  
 {  
 fscanf(fp,"%d%d%d%s%s%s",&st.roll,&st.mark,&st.add.hno,st.add.city,st.name,st.clas);  
 fprintf(fp1,"\n%d\n%d\n%d\n%s\n%s\n%s",st.roll,st.mark,st.add.hno,st.add.city,st.name,st.clas);  
 }  
 fclose(fp);  
 fclose(fp1);  
 if(i==0)  
 {  
 printf("\nNOT FOUND");  
 remove("filename1");  
 }  
 else  
 {  
 remove(filename);  
 rename(filename1,filename);  
 printf("DATA IS DELETED");  
 }  
 getch();  
 }  
 stud alloc(stud st)  
 {  
 while(1)  
 {  
 printf("\nENTER MARKS:");  
 scanf("%d",&st.mark);  
 if(st.mark<0&&st.mark>100)  
 printf("\nINVALID ROLL NO");  
 else  
 break;  
 }  
 while(1)  
 {  
 printf("\nENTER HNO:");  
 scanf("%d",&st.add.hno);  
 if(st.add.hno<0&&st.add.hno>32000)  
 printf("\nINVALID ROLL NO");  
 else  
 break;  
 }  
 printf("\nENTER CITY:");  
 scanf("%s",st.add.city);  
 printf("\nENTER NAME:");  
 scanf("%s",st.name);  
 getch();  
 return st;  
 }

 

 

Other Projects to Try:

  1. string operations such as Copy, Length, Reversing, Palindrome, Concatenation
  2. Hash table code in C Language
  3. Queue Operations using C Language
  4. Stack Operations using C Language
  5. Matrix Operations in C Language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

Hoffmans algorithm in C Language

April 5, 2011 by ProjectsGeek Leave a Comment

Hoffmans algorithm in C

Implement Huffmans algorithm in C Language.

Hoffmans algorithm in C Language Code

 

 
/*Hoffmans algorithm in C Language*/
 #include  
 #include  
 #include  
 #define MAX 26  
 typedef struct str  
 {  
 int freq;  
 char alpha;  
 }str;  
 typedef struct huff  
 {  
 struct str info;  
 struct huff *lc,*rc;  
 }huff;  
 typedef struct sll  
 {  
 int flag;  
 union data  
 {  
 struct huff * addr;  
 struct str info;  
 }data;  
 struct sll *link;  
 }sll;  
 sll *insert(sll *,huff*,sll *);  
 void insertsort(str *,int );  
 sll * createsll(str *);  
 sll * input(str*);  
 huff * huffman(sll *,huff *);  
 void itpost(huff *);  
 huff *freepa(huff *);  
 void leveldis(huff *f);  
 void prefix(huff *,int [],int );  
 void main()  
 {  
 int ch,a[25],i;  
 str *s;  
 sll *f=NULL;  
 huff *pa=NULL;  
 s=(str *)calloc(1,26*sizeof(str));  
 do  
 {  
 clrscr();  
 printf("\n\t\tHUFFMAN TREE CREATION\n\t\t1.INPUT\n\t\t2.HUFFMAN TREE\n\t\t3.DISPLAY\n\t\t4.PREFIX CODE\n\t\t5.LEVEL VISE DISPLAY\n\t\t6.EXIT");  
 printf("\n\t\tENTER UR CHOICE");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 printf("INPUT");  
 f=input(s);  
 break;  
 case 2:  
 if(pa!=NULL)  
 pa=freepa(pa);  
 printf("HUFFMAN TREE");  
 pa=huffman(f,pa);  
 printf("HUFFMAN TREE IS CREATED");  
 break;  
 case 3:  
 if(pa==NULL)  
 printf("MAKE HUFFMAN TREE FIRST");  
 else  
 {  
 printf("DISPLAY");  
 itpost(pa);  
 }  
 break;  
 case 4:  
 if(pa==NULL)  
 printf("MAKE HUFFMAN TREE FIRST");  
 else  
 {  
 printf("PREFIX CODE");  
 i=-1;  
 prefix(pa,a,i);  
 getch();  
 }  
 break;  
 case 5:  
 if(pa==NULL)  
 printf("MAKE HUFFMAN TREE FIRST");  
 else  
 {  
 printf("LEVEL VISE DISPLAY");  
 leveldis(pa);  
 }  
 break;  
 }  
 getch();  
 }while(ch!=6);  
 }  
 sll* input(str *s)  
 {  
 sll *f=NULL;  
 char st[100];  
 int k=0,j,i;  
 for(i=0;i<max;i++) <br=""> (s+i)->freq=0;  
 flushall();  
 gets(st);  
 for(i=0;st[i]!=NULL;i++)  
 {  
 if((64<st[i]&&st[i]<91)||(96<st[i]&&st[i]<123)) <br=""> {  
 for(j=0;j<k;j++) <br=""> {  
 if(st[i]==(s+j)->alpha)  
 break;  
 }  
 if(j==k||i==0)  
 {  
 (s+k)->alpha=st[i];  
 (s+k)->freq++;  
 k++;  
 }  
 else  
 {  
 (s+j)->freq++;  
 }  
 }  
 }  
 insertsort(s,k);  
 f=createsll(s);  
 return f;  
 }  
 sll * createsll(str *s)  
 {  
 sll *nw,*t,*f=NULL;  
 int i;  
 for(i=0;(s+i)->freq!=0;i++)  
 {  
 nw=(sll *)malloc(sizeof(sll));  
 nw->flag=0;  
 nw->link=NULL;  
 nw->data.info.freq=(s+i)->freq;  
 nw->data.info.alpha=(s+i)->alpha;  
 nw->link=NULL;  
 if(f==NULL)  
 f=nw;  
 else  
 {  
 t=f;  
 while(t->link!=NULL)  
 t=t->link;  
 t->link=nw;  
 }  
 }  
 return f;  
 }  
 void insertsort(str *a,int n)  
 {  
 int i,j;  
 str temp;  
 for(i=1;i<n;i++) <br=""> {  
 temp.freq=(a+i)->freq;  
 temp.alpha=(a+i)->alpha;  
 for(j=i-1;j>=0&&(a+j)->freq>temp.freq;j--)  
 {  
 (a+j+1)->freq=(a+j)->freq;  
 (a+j+1)->alpha=(a+j)->alpha;  
 }  
 (a+j+1)->freq=temp.freq;  
 (a+j+1)->alpha=temp.alpha;  
 }  
 }  
 huff * huffman(sll *f,huff *pa)  
 {  
 huff *q=NULL,*nw,*lch,*rch,*p;  
 sll *t,*s;  
 while(f!=NULL)  
 {  
 if(f->flag==0)  
 {  
 nw=(huff *)malloc(sizeof(huff));  
 nw->info=f->data.info;  
 nw->lc=nw->rc=NULL;  
 lch=nw;  
 }  
 else  
 lch=f->data.addr;  
 t=f;  
 f=f->link;  
 free(t);  
 if(f!=NULL)  
 {  
 if(f->flag==0)  
 {  
 nw=(huff *)malloc(sizeof(huff));  
 nw->info=f->data.info;  
 nw->lc=nw->rc=NULL;  
 rch=nw;  
 }  
 else  
 rch=f->data.addr;  
 t=f;  
 f=f->link;  
 nw=(huff *)malloc(sizeof(huff));  
 nw->info.freq=lch->info.freq+rch->info.freq;  
 nw->lc=lch;  
 nw->rc=rch;  
 p=nw;  
 p->info.alpha='\0';  
 t->flag=1;  
 t->data.addr=p;  
 t->link=NULL;  
 f=insert(f,p,t);  
 }  
 else  
 p=lch;  
 }  
 q=p;  
 free(t);  
 return(q);  
 }  
 sll *insert(sll *f,huff*p,sll *t)  
 {  
 sll *s;  
 s=f;  
 if(f!=NULL)  
 {  
 if(((p->info.freqdata.info.freq)&&f->flag==0)||((p->info.freqdata.addr->info.freq)&&f->flag==1))  
 {  
 t->link=f;  
 f=t;  
 }  
 else  
 {  
 while(s->link!=NULL)  
 {  
 if(s->link->data.info.freq>t->data.addr->info.freq&&s->link->flag==0)  
 {  
 t->link=s->link;  
 break;  
 }  
 else if(s->link->data.addr->info.freq>t->data.addr->info.freq&&s->link->flag==1)  
 {  
 t->link=s->link;  
 break;  
 }  
 s=s->link;  
 }  
 }  
 }  
 s->link=t;  
 if(s->link==NULL)  
 t->link=NULL;  
 return f;  
 }  
 /*void itpost(huff *root)  
 {  
 huff *t;  
 stack *top,*nw;  
 top=NULL;  
 t=root;  
 while(t!=NULL||top!=NULL)  
 {  
 if(t!=NULL)  
 {  
 nw=(stack*)malloc(sizeof(stack));  
 nw->ad=t;  
 nw->next=top;  
 nw->flag=0;  
 top=nw;  
 t=t->lc;  
 }  
 else  
 {  
 t=top->ad;  
 if(top->flag==0)  
 {  
 top->flag=1;  
 t=t->rc;  
 }  
 else  
 {  
 if(t->lc==NULL&&t->rc==NULL)  
 printf("%c",t->info.alpha);  
 else  
 printf("%d",t->info.freq);  
 nw=top;  
 top=top->next;  
 free(nw);  
 t=NULL;  
 }  
 }  
 }  
 getch();  
 } */  
 huff *freepa(huff *t)  
 {  
 huff *q,*n;  
 if(t!=NULL)  
 {  
 q=t->lc;  
 n=t->rc;  
 if(q->lc==NULL&&q->rc==NULL)  
 q=freepa(q);  
 if(n->lc==NULL&&n->rc==NULL)  
 n=freepa(n);  
 }  
 return q;  
 }  
 void leveldis(huff *f)  
 {  
 huff *queue[MAX],*t;  
 int front=-1,rear=-1,i=-1,pl=0,cl=1;  
 t=f;  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t;  
 while(cl!=0||pl!=0)  
 {  
 if(pl==0)  
 {  
 i++;  
 printf("\nELEMENT IN %d LEVEL:",i);  
 pl=cl;  
 cl=0;  
 }  
 t=queue[front];  
 if(front==rear)  
 front=rear=-1;  
 else  
 front=(front+1)%MAX;  
 if(t->info.alpha!='\0')  
 printf("\t%c",t->info.alpha);  
 else  
 printf("\t%d",t->info.freq);  
 if(t->lc!=NULL)  
 {  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t->lc;  
 cl++;  
 }  
 if(t->rc!=NULL)  
 {  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t->rc;  
 cl++;  
 }  
 pl--;  
 }  
 getch();  
 }  
 void prefix(huff *f,int a[],int i)  
 {  
 huff *t;  
 int m;  
 t=f;  
 i++;  
 if(t!=NULL)  
 {  
 if(t->lc==NULL&&t->rc==NULL)  
 {  
 printf("\nPREFIX OF %c:",t->info.alpha);  
 for(m=0;m<i;m++) <br=""> printf("\t%d",a[m]);  
 printf("\tFREQUENCY:%d",t->info.freq);  
 }  
 if(t->lc!=NULL)  
 {  
 a[i]=0;  
 prefix(t->lc,a,i);  
 }  
 if(t->rc!=NULL)  
 {  
 a[i]=1;  
 prefix(t->rc,a,i);  
 }  
 }  
 }

 

 

Other Projects to Try:

  1. Expression Tree using C Language
  2. Breadth First and Depth First Search C Language
  3. Stack Operations using C Language
  4. Dijkstra Algorithm in C Language
  5. BFS AND DFS Algorithm using C Language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

Dijkstra Algorithm in C Language

April 5, 2011 by ProjectsGeek Leave a Comment

Dijkstra Algorithm in C Language

Represent a given graph using adjacency matrix and find the shortest path using Dijkstra’s algorithm. Use the map of the area around the college as the graph. Identify the prominent land marks as nodes and find minimum distance to various land marks from the college as the source.

Dijkstra Algorithm in C Language Code

 

#include  
 #include  
 #include  
 #include  
 #define MAX 20  
 #define INFINITE 32767  
 int *mat(int *,int,char[][20] );  
 int *freemat(int *);  
 void shortpath(int *,int,int,int,char[][20]);  
 int check(char[],char[][20]);  
 void main()  
 {  
 int ch,*visited,s,n,i,n1,*adj,d;  
 char str[20][20],str1[20];  
 adj=NULL;  
 do  
 {  
 clrscr();  
 printf("\n\t\tGRAPH\n\t\t1.ADJANCY MATRIX\n\t\t2.SHORTEST PATH\n\t\t\n\t\t3.EXIT");  
 printf("\n\t\tENTER UR CHOICE:");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 1:  
 if(adj!=NULL)  
 adj=freemat(adj);  
 printf("\n\t\tADJANCY MATRIX");  
 printf("ENTER NO OF NODES");  
 scanf("%d",&n1);  
 for(i=0;i<n1;i++) <br=""> {  
 printf("ENTER PLACE NO %d",i+1);  
 flushall();  
 gets(str[i]);  
 }  
 adj=mat(adj,n1,str);  
 break;  
 case 2:  
 if(adj==NULL)  
 printf("ENTER ADJENCY MATRIX FIRST");  
 else  
 {  
 while(1)  
 {  
 printf("\n\t\tENTER SOURCE");  
 flushall();  
 gets(str1);  
 s=check(str1,str);  
 if(s<n1) <br=""> break;  
 }  
 while(1)  
 {  
 printf("\n\t\tENTER DESTINATION");  
 flushall();  
 gets(str1);  
 d=check(str1,str);  
 if(d<n1) <br=""> break;  
 }  
 printf("SHORTEST PATH IS:");  
 shortpath(adj,s,d,n1,str);  
 }  
 getch();  
 break;  
 case 3:  
 break;  
 }  
 }while(ch!=3);  
 }  
 int * mat(int *adj,int n,char str[][20])  
 {  
 int s,d,w,i;  
 char so[20],de[20];  
 adj=(int *)calloc(n,n*2);  
 while(1)  
 {  
 printf("IS THERE ANY EDGES:y/n");  
 if(getche()=='n')  
 break;  
 printf("ENTER SOURCE");  
 flushall();  
 gets(so);  
 for(i=0;i<n;i++) <br=""> {  
 if(strcmp(so,str[i])==0)  
 break;  
 }  
 if(i==n)  
 printf("INVALID SOURCE");  
 else  
 {  
 s=i;  
 printf("ENTER DESTINATION");  
 flushall();  
 gets(de);  
 for(i=0;i<n;i++) <br=""> {  
 if(strcmp(de,str[i])==0)  
 break;  
 }  
 if(i==n)  
 printf("INVALID DESTINATION");  
 else  
 {  
 d=i;  
 if(*(adj+s*n+d)!=0)  
 printf("ALREADY PRESENT");  
 else  
 {  
 printf("ENTER DISTANCE B/W %s & %s",str[s],str[d]);  
 scanf("%d",&w);  
 *(adj+s*n+d)=w;  
 *(adj+d*n+s)=w;  
 }  
 }  
 }  
 }  
 return adj;  
 }  
 int *freemat(int *adj)  
 {  
 free(adj);  
 return NULL;  
 }  
 void shortpath(int *adj,int s,int d,int n,char str[][20])  
 {  
 int *visited,mincost,minver,i,j,k,m=0,*cost,*dest,path[20];  
 cost=(int *)malloc(2*n);  
 dest=(int *)malloc(2*n);  
 visited=(int *)calloc(1,2*n);  
 for(i=0;i<n;i++) <br=""> *(cost+i)=INFINITE;  
 j=s;  
 *(visited+s)=1;  
 while(*(visited+d)!=1)  
 {  
 mincost=INFINITE;  
 for(i=0;i<n;i++) <br=""> {  
 if(*(visited+i)!=1)  
 {  
 if(*(adj+s*n+i)!=0)  
 {  
 if(*(cost+i)>m+(*(adj+s*n+i)))  
 {  
 *(cost+i)=m+(*(adj+s*n+i));  
 *(dest+i)=s;  
 }  
 }  
 if(mincost>=*(cost+i))  
 {  
 mincost=*(cost+i);  
 minver=i;  
 }  
 }  
 }  
 m=mincost;  
 s=minver;  
 *(visited+s)=1;  
 }  
 printf("%d",*(cost+d));  
 getch();  
 i=d;  
 k=0;  
 path[k]=d;  
 k++;  
 while(i!=j)  
 {  
 path[k++]=*(dest+i);  
 i=*(dest+i);  
 }  
 k--;  
 printf("PATH IS");  
 while(k>0)  
 {  
 i=path[k];  
 printf("\n%s\t%s\t%d",str[i],str[path[k-1]],*(adj+i*n+path[k-1]));  
 k--;  
 }  
 getch();  
 free(visited);  
 free(cost);  
 free(dest);  
 }  
 int check(char str[],char str1[][20])  
 {  
 int s=0;  
 while(1)  
 {  
 flushall();  
 if(strcmp(str,str1[s])==0)  
 break;  
 s++;  
 }  
 return s;  
 }

 

Other Projects to Try:

  1. Breadth First and Depth First Search C Language
  2. Stack Operations using C Language
  3. string operations such as Copy, Length, Reversing, Palindrome, Concatenation
  4. Hoffmans algorithm in C Language
  5. BFS AND DFS Algorithm using C Language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

Breadth First and Depth First Search C Language

April 5, 2011 by ProjectsGeek Leave a Comment

Breadth First and Depth First Search C Language Graphs

Write a Breadth First and Depth First Search program to Represent a given graph using adjacency list and perform DFS and BFS .Use the map of the area around the college as the graph.

 Breadth First and Depth First Search Code

 #include  
 #include  
 #include  
 #include  
 #define MAX 20  
 typedef struct graph  
 {  
 char vertex[MAX];  
 struct graph *link;  
 }graph;  
 void breadth(graph *,int ,int );  
 graph *list(int);  
 void depth(graph *,int ,int *,int);  
 graph *freelist(graph *,int);  
 void displaylist(graph *,int);  
 void main()  
 {  
 int ch,*visited,s,n,i,n1,j;  
 graph *g=NULL;  
 char str2[MAX],*str;  
 do  
 {  
 clrscr();  
 printf("\n\t\tGRAPH\n\t\t2.ADJANCY LIST\n\t\t3.DISPLAY OF ADJ MAT\n\t\t4.DISPLAY OF ADJ LIST\n\t\t5.DST OF ADJ LIST\n\t\t6.BST OF ADJ LIST\n\t\t9.EXIT");  
 printf("\n\t\tENTER UR CHOICE:");  
 scanf("%d",&ch);  
 switch(ch)  
 {  
 case 2:  
 if(g!=NULL)  
 g=freelist(g,n);  
 printf("ENTER NO OF NODES");  
 scanf("%d",&n);  
 printf("\n\t\tADJANCY LIST");  
 g=list(n);  
 break;  
 case 4:  
 printf("DISPLAY OF ADJ LIST");  
 break;  
 case 5:  
 if(g==NULL)  
 printf("ENTER ADJENCY LIST FIRST");  
 else  
 {  
 printf("\n\t\tDST OF ADJ LIST");  
 while(1)  
 {  
 printf("\n\t\tENTER SOURCE");  
 j=0;  
 flushall();  
 gets(str2);  
 while(1)  
 {  
 if(strcmp(str2,(g+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(j<n) <br=""> break;  
 }  
 visited=(int *)calloc(1,sizeof(int)*n);  
 depth(g,j,visited,n);  
 printf("ISOLATED VERTEX");  
 for(i=0;i<n;i++) <br=""> if(*(visited+i)==0)  
 printf("\n\t%s",(g+i)->vertex);  
 free(visited);  
 }  
 getch();  
 break;  
 case 6:  
 if(g==NULL)  
 printf("ENTER ADJENCY LIST FIRST");  
 else  
 {  
 printf("\n\t\tBST OF ADJ LIST");  
 while(1)  
 {  
 printf("\n\t\tENTER SOURCE");  
 j=0;  
 flushall();  
 gets(str2);  
 while(1)  
 {  
 if(strcmp(str2,(g+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(j<n) <br=""> break;  
 }  
 breadth(g,j,n);  
 }  
 getch();  
 break;  
 case 9:  
 break;  
 }  
 }while(ch!=9);  
 }  
 void depth(graph *f,int i,int *visited,int n)  
 {  
 graph *t;  
 int j=0;  
 printf("\t%s",(f+i)->vertex);  
 *(visited+i)=1;  
 t=(f+i)->link;  
 while(t!=NULL)  
 {  
 j=0;  
 while(1)  
 {  
 if(strcmp(t->vertex,(f+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(*(visited+j)!=1)  
 depth(f,j,visited,n);  
 t=t->link;  
 }  
 }  
 graph *freelist(graph *g,int n)  
 {  
 int i;  
 graph *q,*p,*k;  
 for(i=0;i<n;i++) <br=""> {  
 k=(g+i);  
 //PRINTF("\n%d",k->vertex);  
 q=k->link;  
 while(q!=NULL)  
 {  
 //printf("\t%d",q->link);  
 p=q; //(remove at d tym of display)  
 q=q->link;  
 free(p);//(remove at d tym of display)  
 }  
 }  
 p=g;  
 free(p);  
 return NULL;  
 }  
 void breadth(graph *f,int i,int n)  
 {  
 graph *queue[MAX],*q,*t;  
 int *visited,front=0,rear=0,j=0;  
 visited=(int *)calloc(1,sizeof(int)*n);  
 queue[rear]=(f+i);  
 *(visited+i)=1;  
 //q=f+i;  
 while(front!=-1)  
 {  
 q=queue[front];  
 j=0;  
 while(1)  
 {  
 if(strcmp(q->vertex,(f+j)->vertex)==0)  
 break;  
 j++;  
 }  
 q=f+j;  
 if(front==rear)  
 front=rear=-1;  
 else  
 front=(front+1)%MAX;  
 printf("\t%s",q->vertex);  
 t=q->link;  
 while(t!=NULL)  
 {  
 j=0;  
 while(1)  
 {  
 if(strcmp(t->vertex,(f+j)->vertex)==0)  
 break;  
 j++;  
 }  
 if(*(visited+j)==0)  
 {  
 if(front==-1)  
 front++;  
 rear=(rear+1)%MAX;  
 queue[rear]=t;  
 *(visited+j)=1;  
 }  
 t=t->link;  
 }  
 }  
 printf("ISOLATED VERTEX");  
 for(j=0;j<n;j++) <br=""> if(*(visited+j)==0)  
 printf("\n\t%s",(f+j)->vertex);  
 getch();  
 free(visited);  
 }  
 graph * list(int n)  
 {  
 graph *g=NULL,*p,*q,*nw;  
 char so[MAX],de[MAX];  
 int s,i,d;  
 g=(graph *)malloc(sizeof(graph)*n);  
 for(i=0;i<n;i++) <br=""> {  
 printf("ENTER DATA");  
 flushall();  
 gets(so);  
 strcpy((g+i)->vertex,so);  
 (g+i)->link=NULL;  
 }  
 while(1)  
 {  
 printf("\n\t\tIS THERE ANY EDGES:y/n");  
 if(getche()=='n')  
 break;  
 printf("ENTER SOURCE & DESTINATION");  
 flushall();  
 gets(so);  
 flushall();  
 gets(de);  
 for(s=0;s<n;s++) <br=""> if(strcmp((g+s)->vertex,so)==0)  
 break;  
 for(d=0;d<n;d++) <br=""> if(strcmp((g+d)->vertex,de)==0)  
 break;  
 if(s==n||strcmp(so,de)==0||d==n)  
 printf("INVALID");  
 else  
 {  
 p=(g+s);  
 q=p->link;  
 while(q!=NULL)  
 {  
 if(strcmp(q->vertex,de)==0)  
 {  
 printf("ALREADY PRESENT");  
 break;  
 }  
 p=q;  
 q=q->link;  
 }  
 if(q==NULL)  
 {  
 nw=(graph *)malloc(sizeof(graph));  
 nw->link=NULL;  
 strcpy(nw->vertex,de);  
 p->link=nw;  
 p=g+d;  
 while(p->link!=NULL)  
 p=p->link;  
 nw=(graph *)malloc(sizeof(graph));  
 nw->link=NULL;  
 strcpy(nw->vertex,so);  
 p->link=nw;  
 }  
 }  
 }  
 return g;  
 }

 

 

Other Projects to Try:

  1. BFS AND DFS Algorithm using C Language
  2. Prims algorithm Code in C Language
  3. Queue Operations using C Language
  4. Hash table code in C Language
  5. Hoffmans algorithm in C Language

Filed Under: C Assignments, Datastructure and Files Tagged With: Datastructure Assignments

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Go to Next Page »

Primary Sidebar

Tags

.Net Projects Download Android Project Ideas Android Projects Angular 2 Assembly Codes C # Projects C & C++ Projects C++ Projects Class Diagrams Computer Graphics Database Project Data Mining Projects DataScience Projects Datastructure Assignments Download Visual Basic Projects Electronics project Hadoop Projects Installation Guides Internet of Things Project IOS Projects Java Java Interview Questions Java Projects JavaScript JavaScript Projects java tutorial JSON JSP Projects Mechanical Projects Mongodb Networking Projects Node JS Projects OS Problems php Projects Placement Papers Project Ideas Python Projects seminar and presentation Struts

Search this Website


Footer

Download Java Project
Download Visual Basic Projects
Download .Net Projects
Download VB Projects
Download C++ Projects
Download NodeJs Projects
Download School Projects
Download School Projects
Ask Questions - Forum
Latest Projects Ideas
Assembly Codes
Datastructure Assignments
Computer Graphics Lab
Operating system Lab
australia-and-India-flag
  • Home
  • About me
  • Contact Form
  • Submit Your Work
  • Site Map
  • Privacy Policy