/* Merge Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 10, 1, 9, 11, 46};
int y[5]= { 20, 15, 0, 72, 2};
int z[10];
int i,j,k,temp;
clrscr();
printf(" Merge Sort .\n");
printf("\n First Array :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
printf("\n\n Second Array :\n");
for(i=0;i<=4;i++)
printf(" %d\t",y[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(x[i] > x[j])
{
temp=x[i];
x[i]=x[j];
x[j]=temp;
}
if(y[i] > y[j])
{
temp=y[i];
y[i]=y[j];
y[j]=temp;
}
}
}
for(i=j=k=0;i<=9;)
{
if(x[j] <= y[k])
z[i++] = x[j++];
else
z[i++] = y[k++];
if(j==5 || k==5)
break;
}
for(;j<=4;)
z[i++] = x[j++];
for(;k<=4;)
z[i++] = y[k++];
printf("\n\n Array after Sorting :\n");
for(i=0;i<=9;i++)
printf(" %d\t",z[i]);
getch();
}
Thursday, October 28, 2010
C Program to demonstrate QUICK SORT
/* Quick Sort */
#include<stdio.h>
#include<conio.h>
int split(int*,int,int);
void main()
{
int arr[10]={ 10, 1, 9, 11,46, 20, 15, 0, 72, 2};
int i;
void quicksort(int *,int,int);
clrscr();
printf(" Quick Sort. \n");
printf("\n Array before sorting : \n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,9);
printf("\n Array after sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
getch();
}
void quicksort(int z[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(z,lower,upper);
quicksort(z,lower,i-1);
quicksort(z,i+1,upper);
}
}
int split(int z[],int lower,int upper)
{
int i,a,b,t;
a=lower+1;
b=upper;
i=z[lower];
while(b>=a)
{
while(z[a]<i)
a++;
while(z[b]>i)
b--;
if(b>a)
{
t=z[a];
z[a]=z[b];
z[b]=t;
}
}
t=z[lower];
z[lower]=z[b];
z[b]=t;
return b;
}
#include<stdio.h>
#include<conio.h>
int split(int*,int,int);
void main()
{
int arr[10]={ 10, 1, 9, 11,46, 20, 15, 0, 72, 2};
int i;
void quicksort(int *,int,int);
clrscr();
printf(" Quick Sort. \n");
printf("\n Array before sorting : \n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
quicksort(arr,0,9);
printf("\n Array after sorting :\n");
for(i=0;i<=9;i++)
printf("%d\t",arr[i]);
getch();
}
void quicksort(int z[],int lower,int upper)
{
int i;
if(upper>lower)
{
i=split(z,lower,upper);
quicksort(z,lower,i-1);
quicksort(z,i+1,upper);
}
}
int split(int z[],int lower,int upper)
{
int i,a,b,t;
a=lower+1;
b=upper;
i=z[lower];
while(b>=a)
{
while(z[a]<i)
a++;
while(z[b]>i)
b--;
if(b>a)
{
t=z[a];
z[a]=z[b];
z[b]=t;
}
}
t=z[lower];
z[lower]=z[b];
z[b]=t;
return b;
}
C Program to demontrate INSERTION SORT
/* Insertion Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 23, 15, 29, 11, 1};
int i,j,k,temp;
clrscr();
printf(" Insertion Sort. \n");
printf(" \n Array prior to sorting :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
if(x[j] > x[i])
{
temp=x[j];
x[j]=x[i];
for(k=i;k>j;k--)
x[k]=x[k-1];
x[k+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int x[5]= { 23, 15, 29, 11, 1};
int i,j,k,temp;
clrscr();
printf(" Insertion Sort. \n");
printf(" \n Array prior to sorting :\n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
for(i=1;i<=4;i++)
{
for(j=0;j<i;j++)
{
if(x[j] > x[i])
{
temp=x[j];
x[j]=x[i];
for(k=i;k>j;k--)
x[k]=x[k-1];
x[k+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",x[i]);
getch();
}
C Program to demontrate Selection Sort
/* Selection Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={23, 15, 29, 11, 1};
int i,j,temp;
clrscr();
printf(" Selection Sort ");
printf("\n Array before Sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\n Array after Sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={23, 15, 29, 11, 1};
int i,j,temp;
clrscr();
printf(" Selection Sort ");
printf("\n Array before Sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=i+1;j<=4;j++)
{
if(arr[i] > arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
printf("\n\n Array after Sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
getch();
}
C Program to demonstrate BUBBLE SORT
/* BUBBLE SORT */
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={ 23, 15, 29, 11, 1 };
int i,j,temp;
clrscr();
printf(" Bubble Sort. \n");
printf(" \nArray before sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=0;j<=3-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int arr[5]={ 23, 15, 29, 11, 1 };
int i,j,temp;
clrscr();
printf(" Bubble Sort. \n");
printf(" \nArray before sorting : \n");
for(i=0;i<=4;i++)
printf(" %d\t",arr[i]);
for(i=0;i<=3;i++)
{
for(j=0;j<=3-i;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
printf("\n\n Array after sorting : \n");
for(i=0;i<=4;i++)
printf("%d\t",arr[i]);
getch();
}
Tuesday, October 26, 2010
C Program to demostrate Binary Tree traversal using recursion
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct NODE
{
int data;
struct NODE *leftchild;
struct NODE *rightchild;
};
struct NODE *Binary_Tree(char *,int,int);
void display(struct NODE *,int);
void Pre_order(struct NODE *);
void In_order(struct NODE *);
void Post_order(struct NODE *);
/* Function to create a binary tree */
struct NODE * Binary_Tree(char *List,int Lower,int Upper)
{
struct NODE *Node;
int Mid=(Lower + Upper)/2;
Node=(struct NODE*)malloc(sizeof(struct NODE));
Node->data=List[Mid];
if(Lower>=Upper)
{
Node->leftchild=NULL;
Node->rightchild=NULL;
return(Node);
}
if(Lower <=Mid-1)
Node->leftchild=Binary_Tree(List,Lower,Mid-1);
else
Node->leftchild=NULL;
if(Mid+1<=Upper)
Node->rightchild=Binary_Tree(List,Mid+1,Upper);
else
Node->rightchild=NULL;
return(Node);
}
/* Output function */
void display(struct NODE *T,int Level)
{
int i;
if(T)
{
display(T->rightchild,Level+1);
printf("\n");
for(i=0;i<Level;i++)
printf(" ");
printf(" %d",T->data);
display(T->leftchild,Level+1);
}
}
/* Pre-order traversal */
void Pre_order(struct NODE *Node)
{
if(Node)
{
printf(" %d",Node->data);
Pre_order(Node->leftchild);
Pre_order(Node->rightchild);
}
}
/* In-order traversal */
void In_order(struct NODE *Node)
{
if(Node)
{
In_order(Node->leftchild);
printf(" %d",Node->data);
In_order(Node->rightchild);
}
}
/* Post-order traversal */
void Post_order(struct NODE *Node)
{
if(Node)
{
Post_order(Node->leftchild);
Post_order(Node->rightchild);
printf(" %d",Node->data);
}
}
/* Function Main */
void main()
{
char List[100];
int Number=0;
int info;
char choice;
struct NODE *T=(struct NODE*)malloc(sizeof(struct NODE));
T=NULL;
printf("\n Input Choice 'b' to break : ");
choice=getchar();
fflush(stdin);
while(choice != 'b')
{
printf("\n Input information of the node : ");
scanf("%d",&info);
List[Number++]=info;
fflush(stdin);
printf("\n Input choice 'b' to break : ");
choice=getchar();
fflush(stdin);
}
Number--;
printf("\n Number of elements in the list is %d ",Number);
T=Binary_Tree(List,0,Number);
display(T,1);
printf("\n Pre-order traversal \n");
Pre_order(T);
printf("\n In-order traversal \n");
In_order(T);
printf("\n Post-order traversal \n");
Post_order(T);
}
#include<conio.h>
#include<alloc.h>
struct NODE
{
int data;
struct NODE *leftchild;
struct NODE *rightchild;
};
struct NODE *Binary_Tree(char *,int,int);
void display(struct NODE *,int);
void Pre_order(struct NODE *);
void In_order(struct NODE *);
void Post_order(struct NODE *);
/* Function to create a binary tree */
struct NODE * Binary_Tree(char *List,int Lower,int Upper)
{
struct NODE *Node;
int Mid=(Lower + Upper)/2;
Node=(struct NODE*)malloc(sizeof(struct NODE));
Node->data=List[Mid];
if(Lower>=Upper)
{
Node->leftchild=NULL;
Node->rightchild=NULL;
return(Node);
}
if(Lower <=Mid-1)
Node->leftchild=Binary_Tree(List,Lower,Mid-1);
else
Node->leftchild=NULL;
if(Mid+1<=Upper)
Node->rightchild=Binary_Tree(List,Mid+1,Upper);
else
Node->rightchild=NULL;
return(Node);
}
/* Output function */
void display(struct NODE *T,int Level)
{
int i;
if(T)
{
display(T->rightchild,Level+1);
printf("\n");
for(i=0;i<Level;i++)
printf(" ");
printf(" %d",T->data);
display(T->leftchild,Level+1);
}
}
/* Pre-order traversal */
void Pre_order(struct NODE *Node)
{
if(Node)
{
printf(" %d",Node->data);
Pre_order(Node->leftchild);
Pre_order(Node->rightchild);
}
}
/* In-order traversal */
void In_order(struct NODE *Node)
{
if(Node)
{
In_order(Node->leftchild);
printf(" %d",Node->data);
In_order(Node->rightchild);
}
}
/* Post-order traversal */
void Post_order(struct NODE *Node)
{
if(Node)
{
Post_order(Node->leftchild);
Post_order(Node->rightchild);
printf(" %d",Node->data);
}
}
/* Function Main */
void main()
{
char List[100];
int Number=0;
int info;
char choice;
struct NODE *T=(struct NODE*)malloc(sizeof(struct NODE));
T=NULL;
printf("\n Input Choice 'b' to break : ");
choice=getchar();
fflush(stdin);
while(choice != 'b')
{
printf("\n Input information of the node : ");
scanf("%d",&info);
List[Number++]=info;
fflush(stdin);
printf("\n Input choice 'b' to break : ");
choice=getchar();
fflush(stdin);
}
Number--;
printf("\n Number of elements in the list is %d ",Number);
T=Binary_Tree(List,0,Number);
display(T,1);
printf("\n Pre-order traversal \n");
Pre_order(T);
printf("\n In-order traversal \n");
In_order(T);
printf("\n Post-order traversal \n");
Post_order(T);
}
C Program to insert and delete a node from the binary search tree
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#define TRUE 1
#define FALSE 0
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert(struct btreenode **,int);
void del(struct btreenode **,int);
void search(struct btreenode **,int,struct btreenode **,struct btreenode **,int *);
void inorder(struct btreenode *);
void main()
{
struct btreenode *bt;
int req;
int i=0,num,a[]={10,7,11,5,8,12,16,15,6};
bt=NULL; /* Empty Tree */
clrscr();
while(i<=8)
{
insert(&bt,a[i]);
i++;
}
clrscr();
printf(" Binary tree before deletion :\n");
inorder(bt);
del(&bt,11);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,10);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,6);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,16);
printf("\n Binary tree after deletion :\n");
inorder(bt);
getch();
}
/* inserts a new node in a binary search tree */
void insert(struct btreenode **sr,int num)
{
if(*sr==NULL)
{
(*sr)=malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /* Search the node to whilch new node will be attatched */
{
/* If new data is less, traverse to left*/
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
/* Else traverse to right */
insert(&((*sr)->rightchild),num);
}
}
/* Deletes a node from the binary search tree */
void del(struct btreenode **root,int num)
{
int found;
struct btreenode *parent,*x,*xsucc;
/* If tree is empty */
if(*root == NULL)
{
printf("\n Tree is Empty ");
return;
}
parent=x=NULL;
/* Call to search function to find the node to be deleted */
search(root,num,&parent,&x,&found);
/* If the node to be deleted is not found */
if(found == FALSE)
{
printf("\n Data to be deleted , not found ");
return;
}
/* If the node to be deleted has two children */
if(x->leftchild !=NULL && x->rightchild!=NULL)
{
parent=x;
xsucc=x->rightchild;
while(xsucc->leftchild != NULL)
{
parent=xsucc;
xsucc=xsucc->leftchild;
}
x->data=xsucc->data;
x=xsucc;
}
/* If the node to be deleted has no child */
if(x->leftchild==NULL && x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->rightchild=NULL;
free(x);
return;
}
/* If the node to be deleted has only right child */
if(x->leftchild==NULL && x->rightchild !=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
/* If the node to be deleted has only left child */
if(x->leftchild != NULL && x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
}
/* Returns the address of the node to be deleted ,address of its parent and whether the node is found or not */
void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{
struct btreenode *q;
q=*root;
*found=FALSE;
*par=NULL;
while(q!=NULL)
{
/* If the node to be deleted is found */
if(q->data == num)
{
*found=TRUE;
*x=q;
return;
}
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data > num)
q=q->leftchild;
else
q=q->rightchild;
}
}
/* Traverse a binary search tree in an LDR(Left-Data-Right) fashion */
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
/* Print the data of the node whose leftchild is NULL or the path has already been traversed */
printf("%d\t",sr->data);
inorder(sr->rightchild);
}
}
#include<conio.h>
#include<alloc.h>
#define TRUE 1
#define FALSE 0
struct btreenode
{
struct btreenode *leftchild;
int data;
struct btreenode *rightchild;
};
void insert(struct btreenode **,int);
void del(struct btreenode **,int);
void search(struct btreenode **,int,struct btreenode **,struct btreenode **,int *);
void inorder(struct btreenode *);
void main()
{
struct btreenode *bt;
int req;
int i=0,num,a[]={10,7,11,5,8,12,16,15,6};
bt=NULL; /* Empty Tree */
clrscr();
while(i<=8)
{
insert(&bt,a[i]);
i++;
}
clrscr();
printf(" Binary tree before deletion :\n");
inorder(bt);
del(&bt,11);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,10);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,6);
printf("\n Binary tree after deletion :\n");
inorder(bt);
del(&bt,16);
printf("\n Binary tree after deletion :\n");
inorder(bt);
getch();
}
/* inserts a new node in a binary search tree */
void insert(struct btreenode **sr,int num)
{
if(*sr==NULL)
{
(*sr)=malloc(sizeof(struct btreenode));
(*sr)->leftchild=NULL;
(*sr)->data=num;
(*sr)->rightchild=NULL;
}
else /* Search the node to whilch new node will be attatched */
{
/* If new data is less, traverse to left*/
if(num<(*sr)->data)
insert(&((*sr)->leftchild),num);
else
/* Else traverse to right */
insert(&((*sr)->rightchild),num);
}
}
/* Deletes a node from the binary search tree */
void del(struct btreenode **root,int num)
{
int found;
struct btreenode *parent,*x,*xsucc;
/* If tree is empty */
if(*root == NULL)
{
printf("\n Tree is Empty ");
return;
}
parent=x=NULL;
/* Call to search function to find the node to be deleted */
search(root,num,&parent,&x,&found);
/* If the node to be deleted is not found */
if(found == FALSE)
{
printf("\n Data to be deleted , not found ");
return;
}
/* If the node to be deleted has two children */
if(x->leftchild !=NULL && x->rightchild!=NULL)
{
parent=x;
xsucc=x->rightchild;
while(xsucc->leftchild != NULL)
{
parent=xsucc;
xsucc=xsucc->leftchild;
}
x->data=xsucc->data;
x=xsucc;
}
/* If the node to be deleted has no child */
if(x->leftchild==NULL && x->rightchild==NULL)
{
if(parent->rightchild==x)
parent->rightchild=NULL;
else
parent->rightchild=NULL;
free(x);
return;
}
/* If the node to be deleted has only right child */
if(x->leftchild==NULL && x->rightchild !=NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->rightchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
/* If the node to be deleted has only left child */
if(x->leftchild != NULL && x->rightchild==NULL)
{
if(parent->leftchild==x)
parent->leftchild=x->leftchild;
else
parent->rightchild=x->rightchild;
free(x);
return;
}
}
/* Returns the address of the node to be deleted ,address of its parent and whether the node is found or not */
void search(struct btreenode **root,int num,struct btreenode **par,struct btreenode **x,int *found)
{
struct btreenode *q;
q=*root;
*found=FALSE;
*par=NULL;
while(q!=NULL)
{
/* If the node to be deleted is found */
if(q->data == num)
{
*found=TRUE;
*x=q;
return;
}
if(q->data==num)
{
*found=TRUE;
*x=q;
return;
}
*par=q;
if(q->data > num)
q=q->leftchild;
else
q=q->rightchild;
}
}
/* Traverse a binary search tree in an LDR(Left-Data-Right) fashion */
void inorder(struct btreenode *sr)
{
if(sr!=NULL)
{
inorder(sr->leftchild);
/* Print the data of the node whose leftchild is NULL or the path has already been traversed */
printf("%d\t",sr->data);
inorder(sr->rightchild);
}
}
C Program to demonstrate Binary Tree through Array
#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#define MAX 10
// #define LEFTCHILD(K) 2K-1
// #define RIGHTCHILD(K) 2K
struct btree
{
int data;
int k;
}tree[100];
void initialize(struct btree tree[],int maxlength)
{
int i;
for(i=0;i<=maxlength;i++)
{
tree[i].data=-1;
tree[i].k=0;
}
}
void create(struct btree tree[],int num)
{
int i;
if(tree[0].data==-1)
{
tree[0].data=num;
tree[0].k=1;
}
else
{
i=tree[0].k;
while(tree[i-1].data!=-1 && i<=MAX)
{
if(tree[i-1].data>num)
{
i=2*tree[i-1].k;
}
else
{
i=2*tree[i-1].k+1;
}
}
tree[i-1].k=i;
tree[i-1].data=num;
}
}
void display(void)
{
int row=0,col=0,root=0;
int i=0;
for(i=0;i<=MAX;i++)
{
printf("\n %d",tree[i].data);
}
//getch();
}
void main()
{
int length=10;
char ch='y';
int choice=0;
int key=0;
clrscr();
do
{
clrscr();
printf("\n\t <<< ARRAY REPRESENTATION OF BINARY TREE >>>\n");
printf("\n 1. Initialize. ");
printf("\n 2. Insert. ");
printf("\n 3. Delete. ");
printf("\n 4. Display. ");
printf("\n 5. EXIT. \n");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
// INITIALIZE
initialize(tree,length);
printf("\n Binary Tree Initialized. \n");
break;
case 2:
// INSERT
printf("\n Enter The Key : ");
scanf("%d",&key);
create(tree,key);
break;
case 3:
// DELETE
break;
case 4:
// DISPLAY
display();
break;
case 5:
// EXIT
exit(1);
default:
printf("\n Invalid Choice . Try Again...");
}
fflush(stdin);
printf("\n Do you Wish to continue [y/n] :");
ch=getch();
}
while(ch=='y');
getch();
}
#include<conio.h>
#include<graphics.h>
#include<dos.h>
#include<process.h>
#define MAX 10
// #define LEFTCHILD(K) 2K-1
// #define RIGHTCHILD(K) 2K
struct btree
{
int data;
int k;
}tree[100];
void initialize(struct btree tree[],int maxlength)
{
int i;
for(i=0;i<=maxlength;i++)
{
tree[i].data=-1;
tree[i].k=0;
}
}
void create(struct btree tree[],int num)
{
int i;
if(tree[0].data==-1)
{
tree[0].data=num;
tree[0].k=1;
}
else
{
i=tree[0].k;
while(tree[i-1].data!=-1 && i<=MAX)
{
if(tree[i-1].data>num)
{
i=2*tree[i-1].k;
}
else
{
i=2*tree[i-1].k+1;
}
}
tree[i-1].k=i;
tree[i-1].data=num;
}
}
void display(void)
{
int row=0,col=0,root=0;
int i=0;
for(i=0;i<=MAX;i++)
{
printf("\n %d",tree[i].data);
}
//getch();
}
void main()
{
int length=10;
char ch='y';
int choice=0;
int key=0;
clrscr();
do
{
clrscr();
printf("\n\t <<< ARRAY REPRESENTATION OF BINARY TREE >>>\n");
printf("\n 1. Initialize. ");
printf("\n 2. Insert. ");
printf("\n 3. Delete. ");
printf("\n 4. Display. ");
printf("\n 5. EXIT. \n");
printf("\n Enter Your Choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
// INITIALIZE
initialize(tree,length);
printf("\n Binary Tree Initialized. \n");
break;
case 2:
// INSERT
printf("\n Enter The Key : ");
scanf("%d",&key);
create(tree,key);
break;
case 3:
// DELETE
break;
case 4:
// DISPLAY
display();
break;
case 5:
// EXIT
exit(1);
default:
printf("\n Invalid Choice . Try Again...");
}
fflush(stdin);
printf("\n Do you Wish to continue [y/n] :");
ch=getch();
}
while(ch=='y');
getch();
}
C Program to multiply two polynomials using array
#include<stdio.h>
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly mulpoly(struct poly,struct poly);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=mulpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
/* Multiplies two polynomials p1 and p2 */
struct poly mulpoly(struct poly p1,struct poly p2)
{
int coeff,exp;
struct poly temp,p3;
initpoly(&temp);
initpoly(&p3);
if(p1.totalterms != 0 && p2.totalterms != 0)
{
int i;
for(i=0;i<p1.totalterms;i++)
{
int j;
struct poly p;
initpoly(&p);
for(j=0;j<p2.totalterms;j++)
{
coeff=p1.t[i].coeff * p2.t[j].coeff;
exp= p1.t[i].exp +p2.t[j].exp;
polycreate(&p,coeff,exp);
}
if(i !=0)
{
p3=addpoly(temp,p);
temp=p3;
}
else
temp=p;
}
}
return p3;
}
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly mulpoly(struct poly,struct poly);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=mulpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
/* Multiplies two polynomials p1 and p2 */
struct poly mulpoly(struct poly p1,struct poly p2)
{
int coeff,exp;
struct poly temp,p3;
initpoly(&temp);
initpoly(&p3);
if(p1.totalterms != 0 && p2.totalterms != 0)
{
int i;
for(i=0;i<p1.totalterms;i++)
{
int j;
struct poly p;
initpoly(&p);
for(j=0;j<p2.totalterms;j++)
{
coeff=p1.t[i].coeff * p2.t[j].coeff;
exp= p1.t[i].exp +p2.t[j].exp;
polycreate(&p,coeff,exp);
}
if(i !=0)
{
p3=addpoly(temp,p);
temp=p3;
}
else
temp=p;
}
}
return p3;
}
C Program to add two polynomials using array
#include<stdio.h>
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=addpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
#include<conio.h>
#define MAX 10
struct term
{
int coeff;
int exp;
};
struct poly
{
struct term t[10];
int totalterms;
};
void initpoly(struct poly*);
void polycreate(struct poly*,int c,int e);
struct poly addpoly(struct poly,struct poly);
void display(struct poly);
void main()
{
struct poly p1,p2,p3;
clrscr();
initpoly(&p1);
initpoly(&p2);
initpoly(&p3);
polycreate(&p1,1,7);
polycreate(&p1,2,6);
polycreate(&p1,3,5);
polycreate(&p1,4,4);
polycreate(&p1,5,2);
polycreate(&p2,1,4);
polycreate(&p2,1,3);
polycreate(&p2,1,2);
polycreate(&p2,1,1);
polycreate(&p2,2,0);
p3=addpoly(p1,p2);
printf("\n First polynomial :\n ");
display(p1);
printf("\n Second polynomial :\n ");
display(p2);
printf("\n\n Resultant Polynomial :\n");
display(p3);
getch();
}
/* Initializes elements of struct poly */
void initpoly(struct poly *p)
{
int i;
p->totalterms=0;
for(i=0;i<MAX;i++)
{
p->t[i].coeff=0;
p->t[i].exp=0;
}
}
/* Add the term of polynomial to the array t */
void polycreate(struct poly *p,int c,int e)
{
p->t[p->totalterms].coeff=c;
p->t[p->totalterms].exp=e;
(p->totalterms)++;
}
/* DISPLAY the polynomial equation */
void display(struct poly p)
{
int flag=0,i;
for(i=0;i<p.totalterms;i++)
{
if(p.t[i].exp != 0)
printf("%d x^%d + ",p.t[i].coeff,p.t[i].exp);
else
{
printf("%d",p.t[i].coeff);
flag=1;
}
}
if(!flag)
printf("\b\b");
}
/* ADD two polynomials p1 and p2 */
struct poly addpoly(struct poly p1,struct poly p2)
{
int i,j,c;
struct poly p3;
initpoly(&p3);
if(p1.totalterms>p2.totalterms)
c=p1.totalterms;
else
c=p2.totalterms;
for(i=0,j=0;i<=c;p3.totalterms++)
{
if(p1.t[i].coeff==0 && p2.t[j].coeff==0)
break;
if(p1.t[i].exp>=p2.t[j].exp)
{
if(p1.t[i].exp==p2.t[j].exp)
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff + p2.t[j].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
j++;
}
else
{
p3.t[p3.totalterms].coeff=p1.t[i].coeff;
p3.t[p3.totalterms].exp=p1.t[i].exp;
i++;
}
}
else
{
p3.t[p3.totalterms].coeff=p2.t[j].coeff;
p3.t[p3.totalterms].exp=p2.t[j].exp;
j++;
}
}
return p3;
}
Friday, October 8, 2010
C Program to create,display and add polynomials
/* Program to create,display and add polynomials */
#include
#include
#include
struct polynode
{
float coeff;
int exp;
struct polynode *next;
};
void create_poly(struct polynode **,float,int);
void display(struct polynode *);
void add_poly(struct polynode *,struct polynode *,struct polynode **);
void main()
{
struct polynode *first,*second,*total;
int i=0;
first=second=total=NULL; /* Empty linked lists */
create_poly(&first,1.4,5);
create_poly(&first,1.5,4);
create_poly(&first,1.7,2);
create_poly(&first,1.8,1);
create_poly(&first,1.9,0);
clrscr();
display(first);
create_poly(&second,1.5,6);
create_poly(&second,2.5,5);
create_poly(&second,-3.5,4);
create_poly(&second,4.5,3);
create_poly(&second,6.5,1);
printf("\n\n");
display(second);
/* Draws a dashed horizontal line */
printf("\n");
while(i++<79) printf("-"); printf("\n\n"); add_poly(first,second,&total); display(total); getch(); } /* ADDs a term to polynomial */ void create_poly(struct polynode **q,float x,int y) { struct polynode *temp; temp=*q; /* Creates a new node if the list is empty */ if(*q==NULL) { (*q)=malloc(sizeof(struct polynode)); temp=*q; } else { /* Traverse the entire Linked List */ while(temp->next != NULL)
temp=temp->next;
/* Create new node at intermediate stages */
temp->next=malloc(sizeof(struct polynode));
temp=temp->next;
}
/* Assign coefficient and exponent */
temp->coeff=x;
temp->exp=y;
temp->next=NULL;
}
/* Displays the contents of linked list representing a polynomial */
void display(struct polynode *q)
{
/* Traverse till the end of the linked list */
while(q!= NULL)
{
printf("%2.1f x^ %d : ",q->coeff,q->exp);
q=q->next;
}
printf("\b\b\b"); /* Erases the last colon */
}
/* Add two polynomials */
void add_poly(struct polynode *x,struct polynode *y,struct polynode **s)
{
struct polynode *z;
/* If both linked lists are empty */
if(x==NULL && y==NULL)
return;
/*Traverse till one of the list ends */
while(x!=NULL && y!=NULL)
{
/*Create a new node if the list is empty */
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
/* Create new nodes at intermediate stages */
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Store a term of larger degree polynomial */
if(x->exp < y->exp)
{
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next; /* GO to next node */
}
else
{
if(x->exp > y->exp)
{
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next;
}
else
{
/* Add the coefficients when exponents ae equal */
if(x->exp == y->exp)
{
/* Assigning the added coefficients */
z->coeff=x->coeff+y->coeff;
z->exp=x->exp;
/* Go to next node */
x=x->next;
y=y->next;
}
}
}
}
/* Assigning remainining terms of the first polynomial to the rssult */
while(x!=NULL)
{
if(*s == NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next; /* Go to next node */
/* Assign remainning terms of the second polynomial to the result */
while(y!=NULL)
{
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next;
}
z->next=NULL; /* Assign NULLL at the end of the resultoing linked list */
} }
#include
#include
#include
struct polynode
{
float coeff;
int exp;
struct polynode *next;
};
void create_poly(struct polynode **,float,int);
void display(struct polynode *);
void add_poly(struct polynode *,struct polynode *,struct polynode **);
void main()
{
struct polynode *first,*second,*total;
int i=0;
first=second=total=NULL; /* Empty linked lists */
create_poly(&first,1.4,5);
create_poly(&first,1.5,4);
create_poly(&first,1.7,2);
create_poly(&first,1.8,1);
create_poly(&first,1.9,0);
clrscr();
display(first);
create_poly(&second,1.5,6);
create_poly(&second,2.5,5);
create_poly(&second,-3.5,4);
create_poly(&second,4.5,3);
create_poly(&second,6.5,1);
printf("\n\n");
display(second);
/* Draws a dashed horizontal line */
printf("\n");
while(i++<79) printf("-"); printf("\n\n"); add_poly(first,second,&total); display(total); getch(); } /* ADDs a term to polynomial */ void create_poly(struct polynode **q,float x,int y) { struct polynode *temp; temp=*q; /* Creates a new node if the list is empty */ if(*q==NULL) { (*q)=malloc(sizeof(struct polynode)); temp=*q; } else { /* Traverse the entire Linked List */ while(temp->next != NULL)
temp=temp->next;
/* Create new node at intermediate stages */
temp->next=malloc(sizeof(struct polynode));
temp=temp->next;
}
/* Assign coefficient and exponent */
temp->coeff=x;
temp->exp=y;
temp->next=NULL;
}
/* Displays the contents of linked list representing a polynomial */
void display(struct polynode *q)
{
/* Traverse till the end of the linked list */
while(q!= NULL)
{
printf("%2.1f x^ %d : ",q->coeff,q->exp);
q=q->next;
}
printf("\b\b\b"); /* Erases the last colon */
}
/* Add two polynomials */
void add_poly(struct polynode *x,struct polynode *y,struct polynode **s)
{
struct polynode *z;
/* If both linked lists are empty */
if(x==NULL && y==NULL)
return;
/*Traverse till one of the list ends */
while(x!=NULL && y!=NULL)
{
/*Create a new node if the list is empty */
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
/* Create new nodes at intermediate stages */
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Store a term of larger degree polynomial */
if(x->exp < y->exp)
{
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next; /* GO to next node */
}
else
{
if(x->exp > y->exp)
{
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next;
}
else
{
/* Add the coefficients when exponents ae equal */
if(x->exp == y->exp)
{
/* Assigning the added coefficients */
z->coeff=x->coeff+y->coeff;
z->exp=x->exp;
/* Go to next node */
x=x->next;
y=y->next;
}
}
}
}
/* Assigning remainining terms of the first polynomial to the rssult */
while(x!=NULL)
{
if(*s == NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=x->coeff;
z->exp=x->exp;
x=x->next; /* Go to next node */
/* Assign remainning terms of the second polynomial to the result */
while(y!=NULL)
{
if(*s==NULL)
{
*s=malloc(sizeof(struct polynode));
z=*s;
}
else
{
z->next=malloc(sizeof(struct polynode));
z=z->next;
}
/* Assign coefficient and exponent */
z->coeff=y->coeff;
z->exp=y->exp;
y=y->next;
}
z->next=NULL; /* Assign NULLL at the end of the resultoing linked list */
} }
Subscribe to:
Posts (Atom)