Tuesday, May 19, 2009

Link List Sorting

#include
#include

struct node
{
int data;
struct node *next;
}*head, *p, *q;

struct node *create();
void display(struct node *head);
void sort(struct node *head);
struct node *merge(struct node *p, struct node *q);

void main()
{
int ch;
struct node *head1, *head2, *r, *head3;
clrscr();
do
{
printf("\n\nWhat operation u have to do?");
printf("\n1.Create\n2.Sort\n3.Merge\n4.Exit\nEnter:");
scanf("%d", &ch);
switch(ch)
{
case 1:
p = create();
printf("\nList is:");
display(head);
break;

case 2:
create();
sort(head);
printf("\nSorted list is:");
display(head);
break;

case 3:
printf("\n\nEnter 1st list:");
head1 = create();
printf("\n1st List is:");
display(head1);
sort(head1);
printf("\n1st Sorted list is:");
display(head1);
printf("\n\nEnter 2nd list:");
head2 = create();
printf("\n\n2nd List is:");
display(head2);
sort(head2);
printf("\n2nd Sorted list is:");
display(head2);
head3 = merge(head1,head2);
printf("\nMerged list is:");
display(head3);

case 4:
exit();
break;
}
}while(ch!=4);
getch();
}

struct node *create()
{
int x;
char ins;
head = NULL;
p = (struct node*) malloc(sizeof(struct node));
printf("\nEnter 1st data:");
scanf("%d",&x);
p->next = NULL;
p->data = x;
head = p;
printf("\nDo u wanna insert more data?(y:yes/n:no)");
scanf("%s",&ins);
p = head;
while(ins =='y')
{
q = (struct node*) malloc(sizeof(struct node));
printf("\nEnter data:");
scanf("%d",&x);
q->data = x;
p->next = q;
p = q;
q->next = NULL;
printf("\nDo u wanna insert more data?(y:yes/n:no)");
scanf("%s",&ins);
}
return(head);
}

void display(struct node *head)
{
if(head == NULL)
printf("\nSorry!!!!\nNo data available in list!!!!");
p = head;
while(p!=NULL)
{
printf(" %d",p->data);
p = p->next;
}
}

void sort(struct node *head)
{
int temp;
if(head == NULL)
printf("\nSorry no data available!!!!!");
q = (struct node*) malloc(sizeof(struct node));
p = head;

for(p=head; p!=NULL; p=p->next)
{
for(q=p->next; q!=NULL; q=q->next)
{
if(p->data > q->data)
{
temp = p->data;
p->data = q->data;
q->data = temp;
}
}
}
}

struct node *merge(struct node *p, struct node *q)
{
struct node *head4, *head3, *r;
head4 = r;

while(p!=NULL && q!=NULL)
{
if(p->data == q->data)
{
r->data = p->data;
r = r->next;
p = p->next;
q = q->next;
}
else if(p->data <>data)
{
r->data = p->data;
r = r->next;
p = p->next;
}
else if(p->data > q->data)
{
r->data = q->data;
r = r->next;
q = q->next;
}
}
while(p!=NULL)
{
r->data = p->data;
r = r->next;
p = p->next;
}
while(q!=NULL)
{
r->data = q->data;
r = r->next;
q = q->next;
}

return(head4);
}

Sorting Techniques

/* Title: Sorting Of Array Elements

Objective:
1. To understand the different sorting methods like
Bubble, Inserion, Quick, Bucket.
2. To understand why & how to use sorting.

Problem Statement:
Write a program to implement Bubble, Insertion, Quick,
Bucket and display result.

*/
#include
#include

void bucket();
void selection();
void sort();
void insert();
void bubble();
void quicksort(int items[],int left,int right,int n);
int max;
int a[10], b[10], c[10], d[10];
int i, j, k, n, temp, ch;

void main()
{
int i=0,n,ch;
clrscr();
do
{
printf("\n\nWhich sorting you have to do?");
printf("\n1.Insertion sort\n2.Bubble sort\n3.Quick sort");
printf("\n4.Bucket sort\n5.Exit\n6.Selection Sort\nEnter:");
scanf("%d", &ch);
switch(ch)
{
case 1: //insertion sort
insert();
break;

case 2: // bubble sort
bubble();
break;

case 4:
bucket();
break;

case 5:
exit(5);
break;

case 3:
printf("\n\nEnter the size of array:");
scanf("%d",&n);
max=n;
printf("Enter Array Elements:");
for(i=0;ipivot) && (j>left) ) j--;
if(i<=j) { temp=a[i]; a[i]=a[j]; a[j]=temp; i++; j--; } } while(i<=j); for(i=0;ii) quicksort(a,i,right,n);
}
}

void insert()
{
printf("\n\nEnter the no of elements in array: ");
scanf("%d", &n);
printf("Enter the 1st element:");
scanf(" %d",&a[0]) ;

printf("Enter other elements:");
for(i=1; ia[j+1])
{
temp = a[j+1];
a[j+1] = a[j];
a[j] = temp;
}
}
}
printf("Sorted elements are :");
for(j=0; j0)
{
printf(" %d ",i);
freq[i]--;
}
}

void selection()
{
printf("\nEnter no. of elements:");
scanf("%d", &n);
printf("\nEnter array elements:");
for(i=0; ia[j])
k = j;
if(k != i)
{
temp = a[i];
a[i] = a[k];
a[k] = temp;
}
}
printf("Sorted elements are :");
for(j=0; j printf(" %d", a[j]);
}

String Operations

/*
Title : String Operations
Write a program to perform following string operations:
1. Length
2. Reverse
3. Compare (Case Sensitive)
4. Concatenation
5. Sub String
6. Palendrome
7. Copy
(With and Without pointer to array.)
*/

#include
#include
#include

void length(char *p,char *q);
void reverse(char *p,char *q);
void compare(char *p,char *q);
void conctenate(char *p,char *q,char *r);
void substring(char *p,char *q);
void Palendrome(char *p,char *r);
void copy(char *p,char *q,char *r);

main()
{
char str[10], str1[10], str2[25];
int i, j, k, ch, ch1, flag=0, d;


clrscr();
flushall();


printf("\n Enter 1st string: ");
scanf("%s", str);
printf("\n Enter 2nd string: ");
scanf("%s", str1);
printf("\n Menu 1:-");
printf("\n 1. Without Pointer");
printf("\n 2. With Pointer\n Enter:");
scanf("%d",&ch);
do
{
switch(ch)
{
case 1: //Without Pointer

printf("\n\n Menu:-\n\n");
printf(" 1. LENGTH \n");
printf(" 2. REVERSE \n");
printf(" 3. COMPARE \n");
printf(" 4. CONCATENATE\n");
printf(" 5. SUBSTRING\n");
printf(" 6. PALENDROME \n");
printf(" 7. COPY \n");
printf(" 8. EXIT\n");
printf("\n\nEnter the choice:");
scanf("%d",&ch1);

switch(ch1)
{
case 1: //Length
for(i=0;str[i]!='\0';i++);
printf("\n The Length of 1st string is: %d", i);
for(j=0;str1[j]!='\0';j++);
printf("\n The Length of 2nd string is: %d", j);
break;

case 2: // Reverse
printf(" \n The Reverse of 1st string is:");
for(i=0;str[i]!='\0';i++);
for(j=i-1;j>=0;j--)
printf("%c", str[j]);
printf(" \n The Reverse of 2nd string is:");
for(i=0;str1[i]!='\0';i++);
for(j=i-1;j>=0;j--)
printf("%c", str1[j]);

break;

case 3: //Compare
for(i=0;str[i]!='\0';i++);
for(j=0;str1[j]!='\0';j++);
if(i!=j)
{
printf("\n Strings are not equal");
}
else
{
for (k=0; k!=i; k++);
if (toupper(str[k])==toupper(str1[k]))
{
flag=1;
}
else
{
flag=0;
break;
}
}
if (flag==1)
{
printf("\n Strings are Equal");
}
if (flag==0)
{
printf("\n Strings are not Equal");
}
break;

case 4: // Conctenate
d=0;
for(i=0;str[i]!='\0';i++);
for(j=0;str1[j]!='\0';j++);
k=i+j;
for(i=0;str[i]!='\0';i++)
str2[i]=str[i];
for(j=i;j<=k;j++) { str2[j]=str1[d]; d++; } puts(str2); getch(); break; case 5: //Sub String flag=0; j=0; for(i=0;str[i]!='\0' && str1[j]!='\0'; i++) { if(str[i]==str1[j]) { j++; flag=1; } else { flag=0; j=0; } } if (flag==1) { printf("it is a substring"); } if (flag==0) { printf("it is not a substring"); } break; case 6: //Palendrome for(i=0; str[i]!='\0'; i++); k=0; { for(j=i-1;j>=0;j--)
{
str2[k]=str[j];
k++;
}
str2[k]='\0';
for(k=0;k!=i;k++)
{
if (str2[k]==str[k])
{
flag=0;
}
else
{
flag=1;
break;
}
}
}
if(flag==0)
printf("\n It is a palindrome");
else
printf("\n It is not a palindrome");

break;

case 7: // Copy
for(i=0;str[i]!='\0';i++)
str2[i]=str[i];
str2[i]='\0';
for(i=0;str1[i]!='\0';i++)
str[i]=str1[i];
str[i]='\0';
printf("\nThe original string is: %s",str2);
printf("\nThe 1st copied string is: %s",str);
printf("\nThe 2nd copied string is: %s",str1);

break;

case 8:
exit(0);
}
break;

case 2: //With Pointer

printf("\n\n Menu:-\n\n");
printf(" 1. LENGTH \n");
printf(" 2. REVERSE \n");
printf(" 3. COMPARE \n");
printf(" 4. CONCATENATE \n");
printf(" 5. SUBSTRING\n");
printf(" 6. PALENDROME\n");
printf(" 7. COPY \n");
printf(" 8. EXIT\n");
printf("\nEnter the choice: ");
scanf("%d",&ch1);

switch(ch1)
{
case 1: //Length
length(str,str1);
break;

case 2: // Reverse
reverse(str,str1);
break;

case 3: //Compare
compare(str,str1);
break;

case 4: // Conctenate
conctenate(str,str1,str2);
break;

case 5: //Sub String
substring(str,str1);
break;

case 6: //Palendrome
Palendrome(str,str2);
break;

case 7: // Copy
copy(str,str1,str2);
break;

case 8:
exit(0);

}
}

}
while(ch1!=8);


}

void length(char *p,char *q)
{
int i,j;
for(i=0;*(p+i)!='\0';i++);
printf("\n The Length of 1st string is: %d", i);
for(j=0;*(q+j)!='\0';j++);
for(j=0;*(q+j)!='\0';j++);
printf("\n The Length of 2nd string is: %d", j);

}

void reverse(char *p,char *q)
{
int i,j;
printf(" \nThe Reverse of 1st string is:");
for(i=0;*(p+i)!='\0';i++);
for(j=i-1;j>=0;j--)
printf("%c", *(p+j));
printf(" \nThe Reverse of 2nd string is:");
for(i=0;*(q+i)!='\0';i++);
for(j=i-1;j>=0;j--)
printf("%c", *(q+j));

}
void compare(char *p,char *q)
{
int i,j,k,flag;
for(i=0;*(p+i)!='\0';i++);
for(j=0;*(q+j)!='\0';j++);
if(i!=j)
{
printf("\n Strings are not equal");
}
else
{
for (k=0; k!=i; k++);
if (toupper(*(p+k))==toupper(*(q+k)))
{
flag=1;
}
else
{
flag=0;
}
}
if (flag==1)
{
printf("\n Strings are Equal");
}
if(flag==0)
{
printf("\n Strings are not Equal");
}
}

void conctenate(char *p,char *q,char *r)
{
int d,i,j,k;
d=0;
for(i=0;*(p+i)!='\0';i++);
for(j=0;*(q+j)!='\0';j++);
k=i+j;

for(i=0;*(p+i)!='\0';i++)
*(r+i)=*(p+i);

for(j=i;j<=k;j++) { *(r+j)=*(q+d); d++; } for(i=0;*(r+i)!='\0';i++) printf("%c",*(r+i)); } void substring(char *p,char *q) { int i,j; int flag=0; j=0; for(i=0;*(p+i)!='\0' && *(q+j)!='\0'; i++) { if (*(p+i)==*(q+j)) { j++; flag=1; } else { flag=0; j=0; } } if (flag==1) { printf("it is a substring"); } if (flag==0) { printf("it is not a substring"); } } void Palendrome(char *p,char *r) { int i,j,k,flag; for(i=0;*(p+i)!='\0';i++); k=0; { for(j=i-1;j>=0;j--)
{
*(r+k)=*(p+j);
k++;
}
for(k=0;k!=i;k++)
{
if (*(r+k)==*(p+k))
{
flag=0;
}
else
{
flag=1;
}
}
}
if(flag==0)
printf("\n It is a palindrome");
else
printf("\n It is not a palindrome");
}
void copy(char *p,char *q,char *r)
{
int i;
for(i=0;*(p+i)!='\0';i++)
*(r+i)=*(p+i);
*(r+i)='\0';
for(i=0;*(q+i)!='\0';i++)
*(p+i)=*(q+i);
*(p+i)='\0';
printf("\nThe original string is:%s",r);
printf("\nThe 1st copied string is:%s",p);
printf("\nThe 2nd copied string is:%s",q);
}

Sparse Matrix

/*
Title : Sparse Matrices

Problem Definition: To represent sparse matrix using arrays and perform
following operations:
1.Addition of 2 sparse matrices
2.Simple transpose
3.Fast transpose of a sparse matrix
*/


#include
#include
#include

void sparse(int [10][10],int [10][10],int,int,int);
void accept(int [10][10],int,int);
void display(int [10][10],int,int);
int sparsecount(int [10][10],int,int);
void addsparse(int [10][10],int [10][10],int [10][10]);
void simptrans(int [10][10],int);
void fasttrans(int [10][10]);

void main()
{
int a[10][10]={0},b[10][10]={0};
int m,n,p,q;
int k,t,ch;
int s[10][10]={0},r[10][10]={0},pd[10][10]={0};
clrscr();

do
{
printf("\n Menu: ");
printf("\n 0.Enter the matrix");
printf("\n 1.Addition");
printf("\n 2.Simple transpose");
printf("\n 3.Fast transpose");
printf("\n 4.Exit");
printf("\n Enter your choice:");
scanf(" %d",&ch);
switch(ch)
{
case 0:
printf("\n Enter the number of rows and columns of a:");
scanf(" %d %d",&m,&n);
printf("\n Enter the number of rows and columns of b:");
scanf(" %d %d",&p,&q);
accept(a,m,n);
accept(b,p,q);
k=sparsecount(a,m,n);
printf("\n The count is %d",k);
t=sparsecount(b,p,q);
printf("\n The count is %d",t);
sparse(s,a,m,n,k);
sparse(r,b,p,q,t);
break;

case 1:
addsparse(s,r,pd);
break;

case 2:
simptrans(s,n);
simptrans(r,q);
break;

case 3:
fasttrans(s);
fasttrans(r);
break;

case 4:
exit(0);
}
}
while(ch!=4);
getch();
}


void accept(int x[10][10],int m,int n)
{
int i,j;
printf("\nEnter the elements : ");
for(i=0;i for(j=0;j {
scanf(" %d",&x[i][j]);
}
}


void display(int x[10][10],int m,int n)
{
int i,j;
printf("\nThe matrix : ");
printf("\n");
for(i=0;i {
for(j=0;j {
printf("\t%d",x[i][j]);
}
printf("\n");
}
}


int sparsecount(int x[10][10],int m,int n)
{
int i,j,count=0;
for(i=0;i for(j=0;j if(x[i][j]!=0)
count++;
return(count);
}


void sparse(int q[10][10],int x[10][10],int m,int n,int z)
{
int i,j,k=1;
q[0][0]=m;
q[0][1]=n;
for(i=0;i for(j=0;j if(x[i][j]!=0)
{
q[k][0]=i;
q[k][1]=j;
q[k][2]=x[i][j];
k++;
}
q[0][2]=z;
display(q,k,3);
}

void addsparse(int a[10][10],int b[10][10],int c[10][10])
{
int ca,cb,cc,sum,ta,tb;

ta=a[0][2];
tb=b[0][2];
ca=cb=cc=1;

if((a[0][0] != b[0][0]) (a[0][1] != b[0][1]))
{
printf("\nAddition not possible");
return ;
}
c[0][0]=a[0][0];
c[0][1]=a[0][1];
while(ca<=ta && cb<=tb)
{
if(a[ca][0] {
c[cc][0]=a[ca][0];
c[cc][1]=a[ca][1];
c[cc][2]=a[ca][2];
ca++;
cc++;
}
else if(a[ca][0]==b[cb][0])
{
if(a[ca][1]==b[cb][1])
{
sum=a[ca][2]+b[cb][2];
if(sum!=0)
{
c[cc][2]=sum;
c[cc][1]=a[ca][1];
c[cc][0]=a[ca][0];
cc++;
}
ca++;
cb++;
}
else if(a[ca][1] {
c[cc][0]=a[ca][0];
c[cc][1]=a[ca][1];
c[cc][2]=a[ca][2];
ca++;
cc++;
}
else
{
c[cc][0]=b[cb][0];
c[cc][1]=b[cb][1];
c[cc][2]=b[cb][2];
cb++;
cc++;
}
}
else
{
c[cc][0]=b[cb][0];
c[cc][1]=b[cb][1];
c[cc][2]=b[cb][2];
cb++;
cc++;
}
}
while(ca<=ta)
{
c[cc][0]=a[ca][0];
c[cc][1]=a[ca][1];
c[cc][2]=a[ca][2];
ca++;
cc++;
}
while(cb {
c[cc][0]=b[cb][0];
c[cc][1]=b[cb][1];
c[cc][2]=b[cb][2];
cb++;
cc++;
}
c[0][2]=cc-1;
display(c,cc,3);
}


void simptrans(int x[10][10],int n)
{
int i,j,k=0;
int y[10][10]={0};
for(i=0;i<=n;i++)
{
for(j=0;j<=x[0][2];j++)
{
if(x[j][1]==i)
{
k++;
y[k][0]=x[j][1];
y[k][1]=x[j][0];
y[k][2]=x[j][2];
}
}
}
y[0][0]=x[0][1];
y[0][1]=x[0][0];
y[0][2]=x[0][2];
display(y,k,3);
}


void fasttrans(int x[10][10])
{
int i,j;
int s[10]={0},p[10]={0},b[10][10]={0};
for(i=0;i s[i]=0;
for(i=1;i<=x[0][2];i++)
s[x[i][1]]=s[x[i][1]]+1;
p[0]=1;
for(i=1;i p[i]=p[i-1]+s[i-1];
for(i=1;i<=x[0][2];i++)
{
j=p[x[i][1]];
b[j][0]=x[i][1];
b[j][1]=x[i][0];
b[j][2]=x[i][2];
p[x[i][1]]=j+1;
}
b[0][0]=x[0][1];
b[0][1]=x[0][0];
b[0][2]=x[0][2];
display(b,i,3);
}

Set Operations

// TITLE : SET OPERATION //
/* PROBLEM STATEMENT : WRITE A C PROGRAM TO PERFORM FOLLOWING OPERATIONS
ON CHARACTER ARRAY
1-UNION
2-INTERSECTION
3-DIFFERENCE
4-SYMMETRIC DIFFERENCE

*/

#include
#include
#include

void main()
{
char a[10], b [10], c[20];
int i,j,k,m,n,ch;
char h;
clrscr();
printf("Enter length of set 1:");
scanf("%d", &n);
printf("Enter length of set 2:");
scanf("%d", &m);
printf("Enter elements of set 1:");

for(i=0;i {
flushall();
scanf("%c",&a[i]);
}

printf("Enter elements of set 2:");
for(j=0;j {
flushall();
scanf("%c",&b[j]);
}

do
{
printf("\n\nWhich operation you want to perform?");
printf("\n1.Union");
printf("\n2.Intersection");
printf("\n3.Difference");
printf("\n4.Symmetric difference");
printf("\n\nEnter the choice:\t");
scanf("%d",&ch);
switch(ch)
{
case 0:
exit(0);
break;

case 1: //UNION
k=0;
for(i=0;i {
c[k]=a[i];
}
for(i=0;i {
for(j=0;j {
if(a[j]==b[i])
break;
}
if(j==n)
{
c[k]=b[i];
k++;
}
}
for(i=0;i {
printf("%c",c[i]);
}
getch();
break;
/*---------------------------------------------------------------------------*/

case 2: //INTERSECTION
k=0;
for(i=0;i {
for(j=0;j {
if(a[j]==b[i])
break;
}
if(j!=n)
{
c[k]=b[i];
k++;
}
}
for(i=0;i {
printf("%c",c[i]);
}
getch();
break;
/*---------------------------------------------------------------------------*/

case 3: //DIFFERENCE
k=0;
for(i=0;i {
for(j=0;j {
if(a[j]==b[i])
break;
}
if(j==m)
{
c[k]=a[i];
k++;
}
}
for(i=0;i {
printf("%c",c[i]);
}
getch();
break;
/*-----------------------------------------------------------------------------*/
case 4: //SYMMETRIC DIFFERENCE
k=0;
for(i=0;i {
for(j=0;j {
if(a[j]==b[i])
break;
}
if(j==m)
{
c[k]=a[i];
k++;
}
}
for(i=0;i {
for(j=0;j {
if(a[j]==b[i])
break;
}
if(j==n)
{
c[k]=b[i];
k++;
}
}

for(i=0;i printf("%c",c[i]);
break;

}
} while(ch!=0);
getch();
}
/*-------------------------------------------------------------------------*/

Searching Methods

/* Title: Searching Method
Problem Statement:
Write a program to implement binary search
*/

#include

int i, n, key, a[20], b[20], flag =1, ch;

void bin();
void seq();
void main()

{
clrscr();
printf("\nEnter no. of elements in array : ");
scanf("%d", &n);
printf("\nEnter array elements: \n");
for(i=0; ia[j])
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}

printf("\nSorted elements are :");
for(j=0; jkey)
upp = mid - 1;
else if(key == a[mid])
{
flag = 1;
break;
}

}
if(flag == 1)
printf("\nElement found at location %d" , mid+1);
else
printf("\nElement not found");

}

Addition Of Polynomials

#include
#include
struct node
{
int power;
float coeff;
struct node *next;
}*head,*p,*q;
struct node *create();
void display(struct node *head);
struct node *add(struct node *head1,struct node *head2);
struct node *mult(struct node *head1,struct node *head2);
void main()
{
struct node *head1, *head2, *head3;
int ch;
clrscr();
do
{
printf("\n\n\nWhat u have to do?\n1.Create & Display only");
printf("\n2.Add\n3.Multiply\n4.Exit\nEnter:");
scanf("%d", &ch);
switch(ch)
{
case 1:
create();
display(head);
break;
case 2:
printf("\n\n1st:");
head1 = create();
display(head1);
printf("\n\n2nd");
head2 = create();
display(head2);
printf("\n\nAddition:");
head3 = add(head1, head2);
display(head3);
break;
case 3:
printf("\n\n1st:");
head1 = create();
display(head1);
printf("\n\n2nd");
head2 = create();
display(head2);
printf("\n\nMultiplication:");
head3 = mult(head1, head2);
display(head3);
break;
case 4:
exit();
break;
}
}while(ch != 4);
getch();
}
struct node *insert(struct node *head,int power, float coeff)
{
p = (struct node *)malloc(sizeof(struct node));
p->power = power;
p->coeff = coeff;
p->next = NULL;
if(head == NULL)
return(p);
else
{
if(powerpower)
{
p->next = head;
return(p);
}
else
{
q = head;
while(q->next != NULL && power>=q->next->power)
q = q->next;
p->next = q->next;
q->next = p;
if(q->power == p->power)
{
q->coeff = q->coeff + p->coeff;
q->next = p->next;
free(p);
}
return(head);
}
}

}
struct node *create()
{
int n, power, i;
float coeff;
head = NULL;
printf("\nEnter no. of terms:");
scanf("%d",&n);
printf("\nEnter polynomial:");
printf("\nEnter terms as tuple of (power, coefficient):\n");
for(i=0;i{
printf("\nTerm:%d\n", i+1);
scanf("%d%f",&power, &coeff);
head = insert(head, power, coeff);
}
return(head);
}
void display(struct node *head)
{
if(head == NULL)
printf("\nError!!!!");
while(head!=NULL)
{
printf("%6.2fX^%d",head->coeff,head->power);
if(head->next!=NULL)
printf(" +");
else
printf(" = 0");
head = head->next;
}
}
struct node *add(struct node *head1,struct node *head2)
{
int power;
float coeff;
head = NULL;
while(head1 != NULL && head2 != NULL)
{
if(head1->power <>power)
{
head = insert(head, head1->power, head1->coeff);
head1 = head1->next;
continue;
}
else if(head2->power <>power)
{
head = insert(head, head2->power, head2->coeff);
head2 = head2->next;
continue;
}
head = insert(head, head1->power, head1->coeff+head2->coeff);
head1 = head1->next;
head2 = head2->next;
}
while(head1!=NULL)
{
head = insert(head, head1->power, head1->coeff);
head1 = head1->next;
}
while(head2!=NULL)
{
head = insert(head, head2->power, head2->coeff);
head2 = head2->next;
}
return(head);
}
struct node *mult(struct node *head1,struct node *head2)
{
head = NULL;
while(head2 != NULL)
{
for(p=head1; p!=NULL; p=p->next)
head = insert(head, p->power+head2->power, p->coeff+head2->coeff);
head2 = head2->next;
}
return(head);
}

Matrix Manipulations

//Title:Matrix Manipulation

#include
#include

int i, j, k;
int a[10][10], b[10][10], c[10][10];

void accept(int mat[10][10], int r, int c);
void display(int mat[10][10], int r, int c);
void addition(int mat1[10][10],int mat2[10][10],int r1,int c1,int r2,int c2);
void subtract(int mat1[10][10],int mat2[10][10],int r1,int c1,int r2,int c2);
void multiply(int mat1[10][10],int mat2[10][10],int r1,int c1,int r2,int c2);
void magic(int n);
void saddle(int a[10][10],int r,int c);

void main()
{
int res[10][10];
int ch, r1, c1, r2, c2, n;
clrscr();
do
{
printf("\n\n\nWhich operation u have to do?\n1.Add matrices");
printf("\n2.Subtract matrices\n3.Multiply\n4.Find Saddle point");
printf("\n5.Magic square\n6.Exit\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
printf("\nEnter no. of rows and columns of matrix:");
scanf("%d %d", &r1,&c1);
printf("\nEnter the matrix:");
accept(a,r1,c1);
printf("\nEnter no. of rows and columns of matrix:");
scanf("%d %d", &r2,&c2);
printf("\nEnter the matrix:");
accept(b,r2,c2);
addition(a,b,r1,c1,r2,c2);
break;

case 2:
printf("\nEnter no. of rows and columns of matrix:");
scanf("%d %d", &r1,&c1);
printf("\nEnter the matrix:");
accept(a,r1,c1);
printf("\nEnter no. of rows and columns of matrix:");
scanf("%d %d", &r2,&c2);
printf("\nEnter the matrix:");
accept(b,r2,c2);
subtract(a,b,r1,c1,r2,c2);
break;

case 3:
printf("\nEnter no. of rows and columns of matrix:");
scanf("%d %d", &r1,&c1);
printf("\nEnter the matrix:");
accept(a,r1,c1);
printf("\nEnter no. of rows and columns of matrix:");
scanf("%d %d", &r2,&c2);
printf("\nEnter the matrix:");
accept(b,r2,c2);
multiply(a,b,r1,c1,r2,c2);
break;

case 4:
printf("\nEnter no. of rows and columns of matrix:");
scanf("%d %d", &r1,&c1);
printf("\nEnter the matrix:");
accept(a,r1,c1);
saddle(a,r1,c1);
break;

case 5:
printf("\nEnter order of matrix:");
scanf("%d", &n);
magic(n);
break;

case 6:
exit();
break;
}
}while(ch != 6);
getch();
}

void accept(int mat[10][10], int r, int c)
{
for(i=0;in-1)
j=0;
if(a[i][j]!=0)
{
i+=2;
j--;
}
if(i>n-1 && j<0) i="0;" i="0;" j="0;" min="p[0];">q[i])
q[i] = a[i][j];
}
}
for(k=0; kmax)
max = q[k];
}
if(min == max)
printf("\nSaddle point is present in matrix and it is %d!!!!!",min);

else
printf("\nSaddle point is not present!!!!!!");
}

Singly Link List

/* Title: Database creation using singly link list

Problem Statement: Write a program to create database using singly link list with options like create, insert, delete, modify, print reverse.
*/

#include
#include
#include

struct node
{
int data;
struct node *next;
}*head;

int create();
int insertloc(int a,int b);
int insertdata(int a,int b);
void del(int a);
void display();
void reverse();


void main()
{
int x,y,ch,s,ins;
head=NULL;
clrscr();
do
{
printf("\n\n-------Menu-------");
printf("\n1.Create database");
printf("\n2.Insert a node");
printf("\n3.Delete a node");
printf("\n4.Reverse");
printf("\n5.Exit");
printf("\nEnter your choice : ");
scanf("%d",&ch);
printf("\n");
switch(ch)
{
case 1:
s=create();
if(s != 0)
printf("\nError in creating list");
display();
break;

case 2:
printf("\n1:Insert by location");
printf("\n2:Insert by data");
printf("\nEnter your choice : ");
scanf("%d",&ins);
switch(ins)
{
case 1:
printf("\nEnter location at which node is to be inserted(from 1) : ");
scanf("%d",&x);
printf("\nEnter data to be inserted:");
scanf("%d",&y);
s=insertloc(x,y);
break;

case 2:
printf("\nEnter data after which node is to be inserted:");
scanf("%d",&x);
printf("\nEnter data to be inserted:");
scanf("%d",&y);
s=insertdata(x,y);
break;
}

if(s != 0)
{
printf("\nFailure to create or insert node");
display();
}
else
display();
break;

case 3:
printf("\nEnter data to be deleted:");
scanf("%d",&x);
del(x);
display();
break;

case 4:
reverse();
display();
break;
}
} while(ch != 5);
}

int create()
{
int i,size,r=0,ch;
struct node *p,*q;
p=(struct node*)malloc(sizeof(struct node));
if(!p)
r=1;
printf("\nEnter first data in link list:");
scanf("%d",&p->data);
head=p;
printf("\n\nDo you want to insert more data?(1:yes/0:no):");
scanf("%d",&ch);
p=head;
while(ch != 0)
{
q=(struct node*)malloc(sizeof(struct node));
if(!q)
r=1;
printf("\n\nEnter the data to be entered : ");
scanf("%d",&q->data);
p->next=q;
q->next=NULL;
p=q;
printf("\n\nDo you want to insert more data?(1:yes/0:no):");
scanf("%d",&ch);
}
return r;
}

int insertloc(int l,int d)
{
struct node *p,*q;
int r=0,i,n;
if(head==NULL)
return 1;
p=(struct node*)malloc(sizeof(struct node));
if(!p)
return 1;
p->data=d;
if(l==1)
{
p->next=head;
head=p;
return r;
}
q=head;
n=l-2;
while(q && n>0)
{
q=q->next;
n--;
}
p->next=q->next;
q->next=p;
return r;
}

int insertdata(int data,int d)
{
struct node *p,*q;
int r=0,i,n;
if(head==NULL)
return 1;
p=(struct node*)malloc(sizeof(struct node));
if(!p)
return 1;
p->data=d;
q=head;
while(q && q->data != data)
{
q=q->next;
}
if(q == NULL)
return 1;
p->next=q->next;
q->next=p;
return r;
}

void del(int d)
{
struct node *p,*q;
if(head->data== d)
{
p=head;
head=head->next;
free(p);
return ;
}
p=head;
while(p->next->data != d && p->next)
{
p=p->next;
}
if(p->next->data == d)
{
q=p->next;
p->next=p->next->next;
free(q);
}
}


void display()
{
struct node *p;
if(head== NULL)
return;
p=head;
printf("\nThe list is:");
while(p)
{
printf("\t%d",p->data);
p=p->next;
}
}

void reverse()
{
struct node *p,*q,*r;
if(head==NULL)
return;
if(head->next == NULL)
return;
p=head;
q=p->next;
if(q->next==NULL)
{
q->next=p;
p->next=NULL;
head=q;
return;
}
r=q->next;
while(r)
{
q->next=p;
p=q;
q=r;
r=q->next;
}
q->next=p;
head->next=NULL;
head=q;
}

Structure Manipulatons

/*
Title: Structure Manipulatons
Problem Statement: Write a program to perform structure manipulation.
*/

#include
#include

struct student
{
char name[20];
int roll;
int cls;
char section;
} *p[20];

int n;

void add (int i)
{
--i;
p[i] = (struct student*) malloc ( sizeof ( struct student ) );
clrscr ();
printf ("\nSerial Number: %d", i+1);
flushall();
printf ("\nName: ");
gets (p[i]->name);
flushall();
printf ("Roll Number: ");
scanf ("%d", &p[i]->roll);
flushall();
printf ("Class: ");
scanf ("%d", &p[i]->cls);
flushall();
printf ("Section: ");
scanf ("%c", &p[i]->section);
}

void modify (int i)
{
--i;
clrscr ();
printf ("\nSerial Number: %d", i+1);
flushall();
printf ("\nName: ");
gets (p[i]->name);
flushall();
printf ("Roll Number: ");
scanf ("%d", &p[i]->roll);
flushall();
printf ("Class: ");
scanf ("%d", &p[i]->cls);
flushall();
printf ("Section: ");
scanf ("%c", &p[i]->section);
}

void view (int i)
{
--i;
clrscr ();
printf ("\nSerial Number: %d", i+1);
printf ("\nName: %s", p[i]->name);
printf ("\nRoll Number: %d", p[i]->roll);
printf ("\nClass: %d", p[i]->cls);
printf ("\nSection: %c", p[i]->section);
}

void del (int i)
{
int ch;
--i;
view (i);
printf ("\n\nAre you sure you wish to delete this record?\n1)\tYes\n2)\tNo");
if (ch==1)
free (p[i]);
p[i]=NULL;
}


int valid (int i)
{
--i;
if (i>=n)
return 0;
else if (p[i]==NULL)
return 0;
else
return 1;
}


void main ()
{
int ctr, ch=1;
int ch1=1;

for (ctr=0; ctr<20; ctr="1;">=20)
{
printf ("Database is full");
break;
}
else
{
add(n);
n++;
}
break;

case 2:
printf ("\nEnter the serial number of record to edit: ");
scanf ("%d", &ch);

if (valid (ch))
modify (ch);
else
printf ("\n\nRECORD DOES NOT EXIST");
break;

case 3:
printf ("\nEnter the serial number of record to delete: ");
scanf ("%d", &ch);

if (valid (ch))
{
del (ch);
n--;
}
else
printf ("\n\nRECORD DOES NOT EXIST");
break;

case 4:
printf ("\n1)\tDisplay all\n2)\tDisplay specific\n");
scanf ("%d", &ch);
switch (ch)
{
case 1:
for (ctr=1; ctr<=n; ctr++)
{
if (valid (ctr))
view (ctr);
printf ("\nPress any key to continue");
getch();
}
break;

case 2:
printf ("\nEnter the serial number of record to view: ");
scanf ("%d", &ch);

if (valid (ch))
view (ch);
else
printf ("\n\nRECORD DOES NOT EXIST");
break;
}
break;

case 5:
ch1=0;
}
getch ();
}
}

Command Line Argument

/* Title: Command Line Argument
Problem Statement:
Write a program to stimulate copycon, copy & type command using command line argument.


*/

#include
#include
#include

char ch;
FILE *FP1, *FP2;
void main(int argc, char *argv[])
{
clrscr();

if(argc!=3)
{
printf("\nWrong no. of arguments.\n");
exit(1);
}

// Copycon

printf("\nEnter data & Enter $ to terminate:");
FP1=fopen(argv[1],"w");
scanf("%c", &ch);
while(ch != '$')
{
putc(ch, FP1);
scanf("%c", &ch);
}
fclose(FP1);
printf("\n%s File contains:",argv[1]);
FP1=fopen(argv[1],"r");
while( ! feof(FP1))
{
ch=getc(FP1);
printf("%c", ch);
}

printf("\n%s File contains:",argv[2]);
FP2=fopen(argv[2],"r");
while( ! feof(FP2))
{
ch=getc(FP2);
printf("%c", ch);
}
printf("\n\n\n copied file is ");

//Copy
FP1=fopen(argv[1],"r");
if(FP1 == NULL)
{
printf("File not opened");
exit(1);
}

FP2=fopen(argv[2],"w");
if(FP2 == NULL)
{
printf("File not opened");
exit(1);
}


while(!feof(FP1))
{
ch = getc(FP1);
putc(ch,FP2);
}

fcloseall();

// Type
printf("\n\n%s File contains:",argv[2]);
FP2=fopen(argv[2],"r");
while( ! feof(FP2))
{
ch=getc(FP2);
printf("%c", ch);
}

getch();
}

Circular Tree

//Circular Tree
#include
#include

#define SIZE 5
int a[SIZE];
int rear, front;
void insert();
void delete1();
void display();

void main()
{
int ch;
clrscr();

rear = front = 0;
do
{
printf("\n\nWhat u have to do?\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter:");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert();
break;

case 2:
delete1();
break;

case 3:
display();
break;

case 4:
exit();
break;
}
}while(ch!=4);
getch();
}

int full()
{
if(front == rear+1 (rear == SIZE -1 && front == 0))
return 1;
else
return 0;
}

int empty()
{
if(front == rear)
return 1;
else
return 0;
}

void insert()
{
int key;
if(full()==1)
printf("\nQueue full!!!!!\nCant insert!!!!!");
else
{
printf("\nEnter element to be inserted:");
scanf("%d",&key);
a[rear] = key;
if(rear == SIZE - 1)
rear = 0;
else
rear++;
}
}

void delete1()
{
int key;
if(empty()==1)
printf("\nQueue empty!!!!!\nCant delete!!!!!!");
else
{
key = a[front];
printf("\nElement deleted is %d", key);
if(front == SIZE-1)
front = 0;
else
front++;
}
}

void display()
{
int i;
printf("\nThe queue is:");
for(i=front; i!=rear; i++)
{
printf(" %d", a[i]);
if(i==4)
{
i = -1;
}
}
}

Addition of Binary Numbers

//Program To Add Binary Numbers

#include


struct dnode *binary(int n);
void print(struct dnode *head);
void add(struct dnode *p, struct dnode *q);


struct dnode
{
int data;
struct dnode *next;
struct dnode *prev;
}*head;


void main()
{
struct dnode *head1, *head2, *head3;
int x, n;
clrscr();

printf("\nEnter no of bits in binary number:");
scanf("%d", &n);
printf("\n\nEnter 1st binary number:");
head1 = binary(n);
printf("\n 1st binary number:");
print(head1);
printf("\n\nEnter 2nd binary number:");
head2 = binary(n);
printf("\n2nd binary number:");
print(head2);
printf("\n\nAddition of binary numbers:");
add(head1, head2);

getch();
}

struct dnode *binary(int n)
{
struct dnode *p, *q;
int i, x;
head = NULL;

for(i=0; i {
scanf("%d", &x);
if(x==1 x==0)
{
q = (struct dnode *)malloc(sizeof(struct dnode));
q->data = x;
q->next = q->prev = NULL;
if(head == NULL)
p = head = q;
else
{
p->next = q;
q->prev = p;
p = q;
}
}
else
{
printf("\nEnter either 0 0r 1!!!!");
i--;
}
}
return(head);
}

void print(struct dnode *head)
{
if(head == NULL)
printf("\nError!!!!!!");
else
{
while(head!=NULL)
{
printf(" %d",head->data);
head = head->next;
}
}
}

void add(struct dnode *p, struct dnode *q)
{
struct dnode *r;
int sum, carry = 0;
r = (struct dnode *)malloc(sizeof(struct dnode));
r->next = r->prev = NULL;
r = head;

if(p!=NULL)
while(p->next!=NULL)
p=p->next;
if(q!=NULL)
while(q->next!=NULL)
q=q->next;
if(r!=NULL)
while(r->next!=NULL)
r=r->next;

while(p q)
{
sum = p->data + q->data + carry;

if(sum == 0)
{
r->data = 0;
carry = 0;
}
else if(sum == 1)
{
r->data = 1;
carry = 0;
}
else if(sum == 2)
{
r->data = 0;
carry = 1;
}
else if(sum == 3)
{
r->data = 1;
carry = 1;
}
p = p->prev;
q = q->prev;
r = r->prev;
}
r = head;
printf("\n %d",carry);
while(head!=NULL)
{
printf(" %d",head->data);
head = head->next;
}
}