• 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

Datastructure Assignments

Database Operations without pointers source code

June 26, 2011 by ProjectsGeek Leave a Comment

Database Operations without pointers

Objective

Program for Operations on a database without using pointers. This program in c/c++ will simulate database operations such as create record, insert, delete, search, print, sort and modify records using simple menu interface which is provided to interact with the program.
Program developed using c/c++ starts with menu options where user can provide data such as employee details. User needs to provide number of employees we want to insert into the database. After entering number of employees we want to insert user can provide data inform of employee name, employee id, salary for particular employee. As soon as entered is pressed record will be inserted into database.
For more c programs check here.
Database Operations without pointers
 #include  
 #include  
 #include  
 typedef struct employee  
 { int code;  
 char name[20];  
 int salary;  
 }employee;  
 void insert(employee st[],int position,int n);  
 void Delete(employee st[],int position,int n);  
 int search(employee st[],int code,int n);  
 void print(employee st[],int n);  
 void read(employee st[],int n);  
 void sort(employee st[],int n);  
 void modify(employee st[],int n);  
 void main()  
 { employee st[30];  
 int n,i,op,position,code;  
 clrscr();  
 do{  
 flushall();  
 printf("\n1)create\n2)insert\n3)delete\n4)search\n5)print\n6)Sort\n7)Modify");  
 printf("\n8)Quit");  
 printf("\nEnter Your Choice:");  
 scanf("%d",&op);  
 switch(op)  
 { case 1: printf("\nEnter No. of employees:");  
 scanf("%d",&n);  
 read(st,n);  
 break;  
 case 2: printf("\n enter the position(no of records=%d):",n);  
 scanf("%d",&position);  
 if(position<=n+1)  
 {  
 insert(st,position,n);  
 n++;  
 print(st,n);  
 }  
 else  
 printf("\n can not insert");  
 break;  
 case 3:printf("\n enter the code : ");  
 scanf("%d",&code);  
 position=search(st,code,n);  
 if(position != -1 )  
 {  
 Delete(st,position,n);  
 n--;  
 print(st,n);  
 }  
 else  
 printf("\n can not delete ");  
 break;  
 case 4: printf("\nenter code:");  
 scanf("%d",&code);  
 position=search(st,code,n);  
 if(position==-1)  
 printf("\nnot found");  
 else  
 { printf("\n found at location=%d",position+1);  
 printf("\n %s\t%d\t%d",st[position].name,st[position].code,st[position].salary);  
 }  
 break;  
 case 5: print(st,n);  
 break;  
 case 6: sort(st,n);print(st,n);break;  
 case 7: modify(st,n);print(st,n);break;  
 }  
 }while(op!=8);  
 }  
 void insert( employee st[],int position,int n)  
 { int i;  
 printf("\n enter data(name code salary): ");  
 for(i=n-1;i>=position-1;i--) /*index is 1 less than position*/  
 st[i+1]=st[i];  
 scanf("%s%d%d",st[position-1].name,&st[position-1].code,&st[position-1].salary);  
 }  
 void Delete(employee st[],int position,int n)  
 { int i;  
 for(i=position+1;i st[j+1].code)  
 {  
 temp=st[j];  
 st[j]=st[j+1];  
 st[j+1]=temp;  
 }  
 }  

Output

1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter Your Choice:1

Enter No. of employees:2

enter data(name code salary): John 1 2000
Jenny 2 3000

1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter Your Choice:2

enter the position(no of records=2):3

enter data(name code salary): Merry 3 5000

John 1 2000
Jenny 2 3000
Merry 3 5000
1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter Your Choice:3

enter the code : 2

John 1 2000
Merry 3 5000
1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter Your Choice:4

enter code:3

found at location=2
Merry 3 5000
1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter Your Choice:5

John 1 2000
Merry 3 5000
1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter Your Choice:6

John 1 2000
Merry 3 5000
1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter Your Choice:7

Enter the code : 3

enter data(name code salary): Diya 5 4000

John 1 2000
Diya 5 4000
1)create
2)insert
3)delete
4)search
5)print
6)Sort
7)Modify
8)Quit
Enter the code : 8

 

 

 

Other Projects to Try:

  1. Mobile User Information Project
  2. Hash table code in C Language
  3. String Operations with Pointers
  4. Matrix Operations with Pointers
  5. Sparse Matrix Operations Code using C Langauge

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Quick sort using recursion

June 26, 2011 by ProjectsGeek Leave a Comment

Implement Quick sort using recursion

 

In this post we will see how to implement quick sort using recursion in c/c++. Quick sort is simple and efficient algorithm for sorting items in array or elements in array. Its is very common algorithm used for sorting and asked in interviews as well.

Quick sort using recursion

Features of Quick sort

  • Worst-case performance‎: ‎O(n2)
  • Average performance‎: ‎O(n log n)
  • Best-case performance‎: ‎O(n log n)
Quick sort using recursion work is such as way that we need to select a pivot element. Now reorder the elements which are less than pivot should come before pivot. Similarly elements which are greater than pivot will come after selected pivot element. We need to repeat this process recursively on the element array.
Beyond this below implementation we have many different types of quick sorts available which are more efficient is specific scenarios.

Quick sort source code

#include
#include
void quick_sort(int [],int,int);
int partition(int [],int,int);
void main()
{
int a[30],n,i,op;
clrscr();
do
{
printf("\n1)Quick Sort\n2)Quit");
printf("\nEnter your choice : ");
scanf("%d",&op);
if(op==1)
{
printf("\nEnter no of elements :");
scanf("%d",&n);
printf("\nEnter array elements :");
for(i=0;i<=u);do
{ j--;
}while(a[j]>v);

if(i)>;i++)>

Quick sort using recursion Output

1)Quick Sort
2)Quit
Enter your choice : 1

Enter no of elements :6

Enter array elements :60 33 70 84 21 50

Sorted array is :21 33 50 60 70 84
1)Quick Sort
2)Quit
Enter your choice : 2

);>)>)>;i++)>

Other Projects to Try:

  1. Matrix Operations with Pointers
  2. Mobile User Information Project
  3. Set operations – Union, Intersection, Difference, Symmetric Difference using C
  4. Sparse Matrix Operations Code using C Langauge
  5. Factorial of a number using recursion in C

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Mobile User Information Project

June 26, 2011 by ProjectsGeek Leave a Comment

Program for Mobile user information

Program details:

1. Display the data in descending order of Mobile No.(insertion sort).
2. Display data in ascending order of name (selection sort).
3. Display details of Mobileno specified by user (Binary search).
4. Display the number of passes and comparisons for different test cases(
(Worst, Average,Best case).

 

;i++)>;i++)>

 #include  
 #include  
 #include  
 #include  
 typedef struct mobile  
 { long mobileno;  
 char name[20];  
 float billamt;  
 }mobile;  
 int binsearch(mobile st[],long mobileno,int n);  
 /* Different cases for searching :  
 Best case - Data being searched is exactly at the centre  
 worst Case - Data being searched is not there.  
 Average Case - Data being searched is at a random place .  
 */  
 void print(mobile st[],int n);  
 void read(mobile st[],int n);  
 void insertionsort(mobile a[],int n); //sort in descending order on MobileNo  
 /* Different cases for insertin sort  
 Best case : when the data is already sorted in descending order on mobileno  
 worst case: when the data is already sorted in ascending order on mobileno.  
 Average case : when the data is in a random sequence  
 */  
 void selectionsort(mobile a[],int n);  
 /*Different cases for selection sort :  
 Only one case :  
 No. of passes = n-1  
 no. of comparisons= n(n-1)/2  
 */  
 void main()  
 { mobile st[30];  
 int n,i,op,position;  
 long mobileno;  
 clrscr();  
 do{  
 flushall();  
 printf("\n1)create\n2)print");  
 printf("\n3)Display the data in descending order of mobileno(insertion sort)");  
 printf("\n4)Display the data in ascending order of name(selection sort)");  
 printf("\n5)Display details for mobileno specified by user(binary search)");  
 printf("\n6)Quit");  
 printf("\nEnter Your Choice:");  
 scanf("%d",&op);  
 switch(op)  
 { case 1: printf("\nEnter No. of Users :");  
 scanf("%d",&n);  
 read(st,n);  
 break;  
 case 2: print(st,n);break;  
 case 3: insertionsort(st,n); print(st,n); break;  
 case 4: selectionsort(st,n); print(st,n); break;  
 case 5: printf("\nPlease ensure that data is sorted using option no.3");  
 printf("\nenter Mobile number : ");  
 scanf("%ld",&mobileno);  
 position=binsearch(st,mobileno,n);  
 if(position==-1)  
 printf("\nnot found");  
 else  
 { printf("\n found at location=%d",position+1);  
 printf("\n %s\t%ld\t%6.2f",st[position].name,st[position].mobileno,st[position].billamt);  
 }  
 break;  
 }  
 }while(op!=6);  
 }  
 int binsearch(mobile st[],long mobileno,int n)  
 { int i,j,k,comp=0;  
 i=0;  
 j=n-1;  
 while(i<=j)  
 {  
 k=(i+j)/2;  
 comp++;  
 if(mobileno==st[k].mobileno)  
 {  
 printf("\nNo. of comparisons = %d ",comp);  
 return(k);  
 }  
 else  
 if(mobileno > st[k].mobileno)  
 j=k-1;  
 else  
 i=k+1;  
 }  
 printf("\nNo. of comparisons = %d ",comp);  
 return(-1);  
 }  
 void print(mobile st[],int n)  
 { int i;  
 for(i=0;i=0 && a[j].mobileno < temp.mobileno;j--)  
 {  
 comp++;  
 a[j+1]=a[j];  
 }  
 a[j+1]=temp;  
 }  
 printf("\nPasses = %d\t Comparisons = %d",passes,comp);  
 }  
 void selectionsort(mobile a[],int n)  
 {  
 int i,j,k,passes=0,comp=0;  
 mobile temp;  
 for(i=0;i< 0 )  
 k=j;  
 }  
 temp=a[i];  
 a[i]=a[k];  
 a[k]=temp;  
 }  
 printf("\nPasses = %d\t Comparisons = %d",passes,comp);  
 }  

Output

1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:1

Enter No. of Users :3

enter data(name mobile No.(9 disgits max) Bill Amount ):
Shreya 989087256 2289
Shruti 976015736 1345
Shina 989076543 1100

1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:2

Shreya 989087256 2289.00
Shruti 976015736 1345.00
Shina 989076543 1100.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:3

Passes = 2 Comparisons = 1
Shreya 989087256 2289.00
Shina 989076543 1100.00
Shruti 976015736 1345.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:4

Passes = 2 Comparisons = 3
Shina 989076543 1100.00
Shreya 989087256 2289.00
Shruti 976015736 1345.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit

Enter Your Choice:5

Please ensure that data is sorted using option no.3
enter Mobile number : 976015736

No. of comparisons = 2
found at location=3
Shruti 976015736 1345.00
1)create
2)print
3)Display the data in descending order of mobileno(insertion sort)
4)Display the data in ascending order of name(selection sort)
5)Display details for mobileno specified by user(binary search)
6)Quit
Enter Your Choice:6

Other Projects to Try:

  1. Bubble Sort Code in C Language
  2. Database Operations without pointers source code
  3. Quick sort using recursion
  4. Matrix Operations with Pointers
  5. Operations on matrices like addition, multiplication, saddle point, magic square ,inverse & transpose

Filed Under: Fundamentals of Datastructure Tagged With: Datastructure Assignments

Matrix Operations with Pointers

June 26, 2011 by ProjectsGeek Leave a Comment

Program for Matrix operations with pointers

 

1) Create() : for creating and reading m x n elements in the given matrix using pointers.
2) Create1() : for reading m x n elements in the given matrix without using pointers.
3) print() : diaplays the given matrix using pointers
4) Print1() : displays the given matrix without using pointers.
3) Transpose() : Creates transpose of the given square matrix using pointers.
4) addmat() : adds two matrices and returns the resultant matrix using pointers.
5) multmat() : multipiles two matrices without using pointers.
6) saddle() : checks whether the given matrix has a saddle point without using pointers.

 

 

;j++)>;j++)>;i++)>;i++)>

#include  
 #include  
 /* Protype of functions */  
 int** create(int m ,int n); // Returns a 2D matrix.  
 void create1(int a[][10],int m , int n);  
 void print(int **a,int m ,int n);  
 void print1(int a[][10], int m , int n);  
 void transpose(int **a,int m ,int n);  
 /* Transpose will be calculated if m is equal to n.  
 Transpose will be strored in the same matrix a */  
 int** addmat(int **a,int m1,int n1 ,int **b,int m2,int n2);  
 /* Two matrices a and b will be added and the result  
 will be returned.  
 Matrix a is of the size m1 x n1 and the matrix b  
 is of the size m2 x n2 */  
 void multmat(int a[][10],int m1,int n1 ,int b[][10],int m2,int n2,int c[][10]);  
 /* Two matrices a and b will be multiplied and the result  
 will be stored in c .  
 Matrix a is of the size m1 x n1 and the matrix b  
 is of the size m2 x n2 */  
 int saddle(int a[][10],int m,int n);  
 /*An m x n matrix is said to have a saddle point if some entry a[i][j] is  
 smallest value in row i and the largest value of column j */  
 void main()  
 { int **a,**b,**c,m1,n1,m2,n2,m3,n3;  
 int a1[10][10],b1[10][10],c1[10][10];  
 int opt;  
 clrscr();  
 do  
 { printf("\n1)Transpose of the Matrix:(transpose stored in the same matrix)");  
 printf("\n2)Add two matrices");  
 printf("\n3)Multiply two matrices");  
 printf("\n4)Saddle point in the matrix");  
 printf("\n5)Quit");  
 printf("\nEnter Your Choice : ");  
 scanf("%d",&opt);  
 switch(opt)  
 {  
 case 1: printf("\n Enter the size of the matrix :");  
 scanf("%d%d",&m1,&n1);  
 a=create(m1,n1);  
 if(m1==n1)  
 {  
 transpose(a,m1,n1);  
 printf("\nResult=\n");  
 print(a,m1,n1);  
 }  
 else  
 printf("\Not a square matrix :");  
 break;  
 case 2: printf("\n Enter the size of the 1st matrix :");  
 scanf("%d%d",&m1,&n1);  
 a=create(m1,n1);  
 printf("\n Enter the size of the 2nd matrix :");  
 scanf("%d%d",&m2,&n2);  
 b=create(m2,n2);  
 if(m1==m2 & n1==n2)  
 { c=addmat(a,m1,n1,b,m2,n2);  
 printf("\nResult=\n");  
 print(c,m1,n1);  
 }  
 else  
 printf("\n Can not be added ");  
 break;  
 case 3: printf("\n Enter the size of the 1st matrix :");  
 scanf("%d%d",&m1,&n1);  
 create1(a1,m1,n1);  
 printf("\n Enter the size of the 2nd matrix :");  
 scanf("%d%d",&m2,&n2);  
 create1(b1,m2,n2);  
 if(n1==m2)  
 { multmat(a1,m1,n1,b1,m2,n2,c1);  
 printf("\nResult=\n");  
 print1(c1,m1,n2);  
 }  
 else  
 printf("\n Can not multiply");  
 break;  
 case 4: printf("\n Enter the size of the 1st matrix :");  
 scanf("%d%d",&m1,&n1);  
 create1(a1,m1,n1);  
 saddle(a1,m1,n1);  
 break;  
 }  
 }while(opt!=5);  
 }  
 int ** create(int m ,int n)  
 { int i,j; int **a;  
 /*creating a matrix */  
 a=(int**)malloc(m*sizeof(int*));  
 for(i=0;i /*creating a matrix */  
 printf("\n Enter the data:");  
 for(i=0;i/* find saddle point row wise */  
 { small=a[i][0];  
 col_of_small=0;  
 for(j=1;j< small)  
 {  
 small=a[i][j];  
 col_of_small=j;  
 }  
 /* find the largest element in "col_of_small"*/  
 large= a[0][col_of_small];  
 row_of_large=0;  
 for(j=1;jlarge)  
 { large=a[j][col_of_small];  
 row_of_large=j;  
 }  
 if(i==row_of_large)  
 {  
 printf("\n Saddle point exist at (%d,%d) with value as %d",i,col_of_small  
 ,a[i][col_of_small]);  
 return(1);  
 }  
 }  
 printf("\nSaddle point does not exist ");  
 return(0);  
 }  

Output

1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 1

Enter the size of the matrix :3 3

Enter the data:1 2 3
4 5 6
7 8 9

Result=

1 4 7
2 5 8
3 6 9
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit

Enter Your Choice : 2

Enter the size of the 1st matrix :3 3

Enter the data:1 2 3
4 5 6
7 8 9

Enter the size of the 2nd matrix :3 3

Enter the data:9 8 7
6 5 4
3 2 1

Result=

10 10 10
10 10 10
10 10 10
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 3

Enter the size of the 1st matrix :2 2

Enter the data:6 8
3 5

Enter the size of the 2nd matrix :2 2

Enter the data:7 9
2 4

Result=

58 86
31 47
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 4

Enter the size of the 1st matrix :2 2

Enter the data:5 6
1 2

Saddle point exist at (0,0) with value as 5
1)Transpose of the Matrix:(transpose stored in the same matrix)
2)Add two matrices
3)Multiply two matrices
4)Saddle point in the matrix
5)Quit
Enter Your Choice : 5

Other Projects to Try:

  1. Operations on matrices like addition, multiplication, saddle point, magic square ,inverse & transpose
  2. Matrix Operations in C Language
  3. String Operations with Pointers
  4. Database Operations without pointers source code
  5. Matrix operations in c language

Filed Under: Datastructure and Files Tagged With: Datastructure Assignments

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

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

  • « Go to Previous Page
  • Page 1
  • Page 2
  • Page 3
  • Page 4
  • Page 5
  • 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