#include "stdio.h"
#include "conio.h"
struct barbie
{
int coff;
int pow;
struct barbie *link;
}*ptr,*start1,*node,*start2,*start3,*ptr1,*ptr2;
typedef struct barbie bar;
int temp1,temp2;
void main()
{
void create(void);
void prnt(void);
void suml(void);
void sort(void);
clrscr();
printf("Enrter the elements of the first poly :");
node = (bar *) malloc(sizeof (bar));
start1=node;
if (start1==NULL)
{
printf("Unable to create memory.");
getch();
exit();
}
create();
printf("
Enrter the elements of the second poly :");
node = (bar *) malloc(sizeof (bar));
start2=node;
if (start2==NULL)
{
printf("Unable to create memory.");
getch();
exit();
}
create();
clrscr();
//printing the elements of the lists
printf("The elements of the poly first are :");
ptr=start1;
prnt();
printf("The elements of the poly second are :");
ptr=start2;
prnt();
printf("The first sorted list is :");
ptr=start1;
sort();
ptr=start1;
prnt();
printf("
The second sorted list is :");
ptr=start2;
sort();
ptr=start2;
prnt();
printf("The sum of the two lists are :");
suml();
ptr=start3;
prnt();
getch();
}
/*-----------------------------------------------------------------------------*/
void create()
{
char ch;
while(1)
{
printf("Enter the coff and pow :");
scanf("%d%d",&node->coff,&node->pow);
if (node->pow==0 )
{
ptr=node;
node=(bar *)malloc(sizeof(bar));
node=NULL;
ptr->link=node;
break;
}
printf("
Do u want enter more coff ?(y/n)");
fflush(stdin);
scanf("%c",&ch);
if (ch=='n' )
{
ptr=node;
node=(bar *)malloc(sizeof(bar));
node=NULL;
ptr->link=node;
break;
}
ptr=node;
node=(bar *)malloc(sizeof(bar));
ptr->link=node;
}
}
/*-------------------------------------------------------------------------*/
void prnt()
{ int i=1;
while(ptr!=NULL )
{
if(i!=1)
printf("+ ");
printf(" %dx^%d ",ptr->coff,ptr->pow);
ptr=ptr->link;
i++;
}
//printf(" %d^%d",ptr->coff,ptr->pow);
}
/*---------------------------------------------------------------------------*/
void sort()
{
for(;ptr->coff!=NULL;ptr=ptr->link)
for(ptr2=ptr->link;ptr2->coff!=NULL;ptr2=ptr2->link)
{
if(ptr->pow>ptr2->pow)
{
temp1=ptr->coff;
temp2=ptr->pow;
ptr->coff=ptr2->coff;
ptr->pow=ptr2->pow;
ptr2->coff=temp1;
ptr2->pow=temp2;
}
}
}
/*---------------------------------------------------------------------------*/
void suml()
{
node=(bar *)malloc (sizeof(bar));
start3=node;
ptr1=start1;
ptr2=start2;
while(ptr1!=NULL && ptr2!=NULL)
{
ptr=node;
if (ptr1->pow > ptr2->pow )
{
node->coff=ptr2->coff;
node->pow=ptr2->pow;
ptr2=ptr2->link; //update ptr list B
}
else if ( ptr1->pow < ptr2->pow )
{
node->coff=ptr1->coff;
node->pow=ptr1->pow;
ptr1=ptr1->link; //update ptr list A
}
else
{
node->coff=ptr2->coff+ptr1->coff;
node->pow=ptr2->pow;
ptr1=ptr1->link; //update ptr list A
ptr2=ptr2->link; //update ptr list B
}
node=(bar *)malloc (sizeof(bar));
ptr->link=node; //update ptr list C
}//end of while
if (ptr1==NULL) //end of list A
{
while(ptr2!=NULL)
{
node->coff=ptr2->coff;
node->pow=ptr2->pow;
ptr2=ptr2->link; //update ptr list B
ptr=node;
node=(bar *)malloc (sizeof(bar));
ptr->link=node; //update ptr list C
}
}
else if (ptr2==NULL) //end of list B
{
while(ptr1!=NULL)
{
node->coff=ptr1->coff;
node->pow=ptr1->pow;
ptr1=ptr1->link; //update ptr list B
ptr=node;
node=(bar *)malloc (sizeof(bar));
ptr->link=node; //update ptr list C
}
}
node=NULL;
ptr->link=node;
}
Implementation of Circular linked List.
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
#include "alloc.h"
#define null 0
struct node
{
int info;
struct node *link;
}*start;
void main()
{
int ch,n,m,position,i;
last=null;
while(1)
{
printf("1.create
2.addat
3.addbt
4.del
5.disp
6.exit
);
printf("er ur ch");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("er no of itc");
scanf("%d",&n);
for(i=0;i {
printf("er the element");
scanf("%d",&m);
create(m);
}break;
case 2:
printf("er the element");
scanf("%d",&m);
addat(m);
break;
case 3:
printf("er the element");
scanf("%d",&m);
printf("er the position");
scanf("%d",&position);
addbt(m,position);
break;
case 4:
if(last==null)
{
printf("list is empty");
continue;
}
printf("er the element for delete");
scanf("%d",&m);
del(m);
break;
case 5:
disp();
break;
case 6:
exit(0);
break;
default:
printf("wrong choice");
}
}
}
create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=null;
if(last==null)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}}
addat(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
addbt(int data,int pos)
{
struct node *tmp,*q;
int i;
q=last->link;;
for(i=0;i {
q=q->link;
if(q==last->link)
{
printf("there r lessthan %d elements",pos);
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
if(q==last)
last=tmp;
}
del(int data)
{
struct node *tmp,*q;
if(last->link==last&&last->info==data)
{
tmp=last;
last=null;
free(tmp);
return;
}
q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
return;
}
while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
printf("element %d is deleted",data);
}
if(q->link->info=data)
{
tmp=q->link;
q->link=last->link;
free(tmp);
last=q;
return;}
printf("element%d is not found",data);
}
disp()
{
struct node *q;
if(last==null)
{
printf("list isdempty");
return;
}q=last->link;
while(q!=last)
{
printf("%d",q->info);
q=q->link;
}
printf("%d",last->info);
}
#include "conio.h"
#include "stdlib.h"
#include "alloc.h"
#define null 0
struct node
{
int info;
struct node *link;
}*start;
void main()
{
int ch,n,m,position,i;
last=null;
while(1)
{
printf("1.create
2.addat
3.addbt
4.del
5.disp
6.exit
);
printf("er ur ch");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("er no of itc");
scanf("%d",&n);
for(i=0;i
printf("er the element");
scanf("%d",&m);
create(m);
}break;
case 2:
printf("er the element");
scanf("%d",&m);
addat(m);
break;
case 3:
printf("er the element");
scanf("%d",&m);
printf("er the position");
scanf("%d",&position);
addbt(m,position);
break;
case 4:
if(last==null)
{
printf("list is empty");
continue;
}
printf("er the element for delete");
scanf("%d",&m);
del(m);
break;
case 5:
disp();
break;
case 6:
exit(0);
break;
default:
printf("wrong choice");
}
}
}
create(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=null;
if(last==null)
{
last=tmp;
tmp->link=last;
}
else
{
tmp->link=last->link;
last->link=tmp;
last=tmp;
}}
addat(int data)
{
struct node *q,*tmp;
tmp=(struct node *)malloc(sizeof(struct node));
tmp->info=data;
tmp->link=last->link;
last->link=tmp;
}
addbt(int data,int pos)
{
struct node *tmp,*q;
int i;
q=last->link;;
for(i=0;i
q=q->link;
if(q==last->link)
{
printf("there r lessthan %d elements",pos);
return;
}
}
tmp=(struct node *)malloc(sizeof(struct node));
tmp->link=q->link;
tmp->info=data;
q->link=tmp;
if(q==last)
last=tmp;
}
del(int data)
{
struct node *tmp,*q;
if(last->link==last&&last->info==data)
{
tmp=last;
last=null;
free(tmp);
return;
}
q=last->link;
if(q->info==data)
{
tmp=q;
last->link=q->link;
free(tmp);
return;
}
while(q->link!=last)
{
if(q->link->info==data)
{
tmp=q->link;
q->link=tmp->link;
free(tmp);
printf("element %d is deleted",data);
}
if(q->link->info=data)
{
tmp=q->link;
q->link=last->link;
free(tmp);
last=q;
return;}
printf("element%d is not found",data);
}
disp()
{
struct node *q;
if(last==null)
{
printf("list isdempty");
return;
}q=last->link;
while(q!=last)
{
printf("%d",q->info);
q=q->link;
}
printf("%d",last->info);
}
Program to search an element in Linked list.
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
struct linlst
{
int info;
struct link *next;
}
start, *node;
int search(int);
void main()
{
int no,i,item,pos;
clrscr();
start.next=NULL;
node=&start;
printf("How many nodes, you want in linked list? ");
scanf("%d",&no);
printf("
);
for(i=0;i {
node->next=(struct linlst *)malloc(sizeof(struct linlst));
printf("Enter element in node %d: ",i+1);
scanf("%d",&node->info);
node=node->next;
}
node->next=NULL;
printf("Linked list(only with info field) is:
);
node=&start;
while(node->next!=NULL)
{
printf("%d ",node->info);
node=node->next;
}
printf("Enter item to be searched : ");
scanf("%d",&item);
pos=search(item);
if(pos<=no)
printf("Your item is at node %d",pos);
else
printf("Sorry! item is no in linked list.a");
getch();
}
int search(int item)
{
int n=1;
node=&start;
while(node->next!=NULL)
{
if(node->info==item)
break;
else
n++;
node=node->next;
}
return n;
}
#include "conio.h"
#include "malloc.h"
struct linlst
{
int info;
struct link *next;
}
start, *node;
int search(int);
void main()
{
int no,i,item,pos;
clrscr();
start.next=NULL;
node=&start;
printf("How many nodes, you want in linked list? ");
scanf("%d",&no);
printf("
);
for(i=0;i
node->next=(struct linlst *)malloc(sizeof(struct linlst));
printf("Enter element in node %d: ",i+1);
scanf("%d",&node->info);
node=node->next;
}
node->next=NULL;
printf("Linked list(only with info field) is:
);
node=&start;
while(node->next!=NULL)
{
printf("%d ",node->info);
node=node->next;
}
printf("Enter item to be searched : ");
scanf("%d",&item);
pos=search(item);
if(pos<=no)
printf("Your item is at node %d",pos);
else
printf("Sorry! item is no in linked list.a");
getch();
}
int search(int item)
{
int n=1;
node=&start;
while(node->next!=NULL)
{
if(node->info==item)
break;
else
n++;
node=node->next;
}
return n;
}
TO SORT ELEMENTS OF THE ARRAY USING QUICK SORT ALGORITHM
#include "stdio.h"
#include "conio.h"
#define max 15
int beg,end,top,i,n,loc,left,right;
int array[max+1]; //contains the various elements.
int upper[max-1],lower[max-1];
//two stacks to store two ends of the list.
void main()
{
void enter(void);
void quick(void);
void prnt(void);
clrscr();
enter(); //entering elements in the array
top=i-1; //set top to stack
if (top==0)
{
printf("UNDERFLOW CONDITION ");
getch();
exit();
}
top=0;
if(n>1)
{
top++;
lower[top]=1;upper[top]=n;
while ( top!=NULL )
{
beg=lower[top];
end=upper[top];
top--;
left=beg; right=end; loc=beg;
quick();
if ( beg {
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1 {
top++;
lower[top]=loc+1;
upper[top]=end;
}
} //end of while
} //end of if statement
printf("Sorted elements of the array are :");
prnt(); //to print the sorted array
getch();
} //end of main
void enter(void)
{
printf("Enter the no of elements in the array:");
scanf("%d",&n);
printf("Enter the elements of the array :");
for(i=1;i<=n;i++)
{
printf("Enter the %d element :",i);
scanf("%d",&array[i]);
}
}
void prnt(void)
{
for(i=1;i<=n;i++)
{
printf("The %d element is : %d",i,array[i]);
}
}
void quick()
{
int temp;
void tr_fr_right(void);
while( array[loc]<=array[right] && loc!=right)
{
right--;
}
if(loc==right)
return ;
if(array[loc]>array[right])
{
temp=array[loc];
array[loc]=array[right];
array[right]=temp;
loc=right;
tr_fr_right();
}
return ;
}
void tr_fr_right()
{
int temp;
while( array[loc] > array[left] && loc!=left)
{
left++;
}
if(loc==left)
return ;
if(array[loc] < array[left])
{
temp=array[loc];
array[loc]=array[left];
array[left]=temp;
loc=left;
quick();
}
return ;
}
#include "conio.h"
#define max 15
int beg,end,top,i,n,loc,left,right;
int array[max+1]; //contains the various elements.
int upper[max-1],lower[max-1];
//two stacks to store two ends of the list.
void main()
{
void enter(void);
void quick(void);
void prnt(void);
clrscr();
enter(); //entering elements in the array
top=i-1; //set top to stack
if (top==0)
{
printf("UNDERFLOW CONDITION ");
getch();
exit();
}
top=0;
if(n>1)
{
top++;
lower[top]=1;upper[top]=n;
while ( top!=NULL )
{
beg=lower[top];
end=upper[top];
top--;
left=beg; right=end; loc=beg;
quick();
if ( beg
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1
top++;
lower[top]=loc+1;
upper[top]=end;
}
} //end of while
} //end of if statement
printf("Sorted elements of the array are :");
prnt(); //to print the sorted array
getch();
} //end of main
void enter(void)
{
printf("Enter the no of elements in the array:");
scanf("%d",&n);
printf("Enter the elements of the array :");
for(i=1;i<=n;i++)
{
printf("Enter the %d element :",i);
scanf("%d",&array[i]);
}
}
void prnt(void)
{
for(i=1;i<=n;i++)
{
printf("The %d element is : %d",i,array[i]);
}
}
void quick()
{
int temp;
void tr_fr_right(void);
while( array[loc]<=array[right] && loc!=right)
{
right--;
}
if(loc==right)
return ;
if(array[loc]>array[right])
{
temp=array[loc];
array[loc]=array[right];
array[right]=temp;
loc=right;
tr_fr_right();
}
return ;
}
void tr_fr_right()
{
int temp;
while( array[loc] > array[left] && loc!=left)
{
left++;
}
if(loc==left)
return ;
if(array[loc] < array[left])
{
temp=array[loc];
array[loc]=array[left];
array[left]=temp;
loc=left;
quick();
}
return ;
}
TO SORT ELEMENTS OF THE ARRAY USING QUICK SORT ALGORITHM
#include "stdio.h"
#include "conio.h"
#define max 15
int beg,end,top,i,n,loc,left,right;
int array[max+1]; //contains the various elements.
int upper[max-1],lower[max-1];
//two stacks to store two ends of the list.
void main()
{
void enter(void);
void quick(void);
void prnt(void);
clrscr();
enter(); //entering elements in the array
top=i-1; //set top to stack
if (top==0)
{
printf("UNDERFLOW CONDITION ");
getch();
exit();
}
top=0;
if(n>1)
{
top++;
lower[top]=1;upper[top]=n;
while ( top!=NULL )
{
beg=lower[top];
end=upper[top];
top--;
left=beg; right=end; loc=beg;
quick();
if ( beg {
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1 {
top++;
lower[top]=loc+1;
upper[top]=end;
}
} //end of while
} //end of if statement
printf("Sorted elements of the array are :");
prnt(); //to print the sorted array
getch();
} //end of main
void enter(void)
{
printf("Enter the no of elements in the array:");
scanf("%d",&n);
printf("Enter the elements of the array :");
for(i=1;i<=n;i++)
{
printf("Enter the %d element :",i);
scanf("%d",&array[i]);
}
}
void prnt(void)
{
for(i=1;i<=n;i++)
{
printf("The %d element is : %d",i,array[i]);
}
}
void quick()
{
int temp;
void tr_fr_right(void);
while( array[loc]<=array[right] && loc!=right)
{
right--;
}
if(loc==right)
return ;
if(array[loc]>array[right])
{
temp=array[loc];
array[loc]=array[right];
array[right]=temp;
loc=right;
tr_fr_right();
}
return ;
}
void tr_fr_right()
{
int temp;
while( array[loc] > array[left] && loc!=left)
{
left++;
}
if(loc==left)
return ;
if(array[loc] < array[left])
{
temp=array[loc];
array[loc]=array[left];
array[left]=temp;
loc=left;
quick();
}
return ;
}
#include "conio.h"
#define max 15
int beg,end,top,i,n,loc,left,right;
int array[max+1]; //contains the various elements.
int upper[max-1],lower[max-1];
//two stacks to store two ends of the list.
void main()
{
void enter(void);
void quick(void);
void prnt(void);
clrscr();
enter(); //entering elements in the array
top=i-1; //set top to stack
if (top==0)
{
printf("UNDERFLOW CONDITION ");
getch();
exit();
}
top=0;
if(n>1)
{
top++;
lower[top]=1;upper[top]=n;
while ( top!=NULL )
{
beg=lower[top];
end=upper[top];
top--;
left=beg; right=end; loc=beg;
quick();
if ( beg
top++;
lower[top]=beg;
upper[top]=loc-1;
}
if(loc+1
top++;
lower[top]=loc+1;
upper[top]=end;
}
} //end of while
} //end of if statement
printf("Sorted elements of the array are :");
prnt(); //to print the sorted array
getch();
} //end of main
void enter(void)
{
printf("Enter the no of elements in the array:");
scanf("%d",&n);
printf("Enter the elements of the array :");
for(i=1;i<=n;i++)
{
printf("Enter the %d element :",i);
scanf("%d",&array[i]);
}
}
void prnt(void)
{
for(i=1;i<=n;i++)
{
printf("The %d element is : %d",i,array[i]);
}
}
void quick()
{
int temp;
void tr_fr_right(void);
while( array[loc]<=array[right] && loc!=right)
{
right--;
}
if(loc==right)
return ;
if(array[loc]>array[right])
{
temp=array[loc];
array[loc]=array[right];
array[right]=temp;
loc=right;
tr_fr_right();
}
return ;
}
void tr_fr_right()
{
int temp;
while( array[loc] > array[left] && loc!=left)
{
left++;
}
if(loc==left)
return ;
if(array[loc] < array[left])
{
temp=array[loc];
array[loc]=array[left];
array[left]=temp;
loc=left;
quick();
}
return ;
}
PROGRAM TO SORT NAMES ALPHABETICALLY USING LINKED LIST
#include "stdio.h"
#include "conio.h"
struct name
{
char info[20];
struct name *link;
}*ptr,*start,*node;
typedef struct name list;
void main()
{
void create(void);
void disp(void);
void sort(void);
clrscr();
printf("Program to enter the names and sort them using linked list.");
create(); //fun to enter the names
printf("The contents of the list were :
);
disp(); //fun to display the data
sort(); //fun to sort the data
printf("After sorting :");
printf("The contents of the list are :
);
disp();
getch();
}
//-------------------------------------------
void create(void)
{
char ch='y';
node=(list *)malloc(sizeof(list));
start=node;
while(ch=='y')
{
printf("Enter the name :");
gets(node->info);
ptr=node;
node=(list *)malloc(sizeof(list));
ptr->link=node;
printf("Want to continue?(y/n)");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n')
break;
ch='y';
}
ptr->link=NULL;
}
//-------------------------------------------
void disp(void)
{
ptr=start;
while(ptr!=NULL)
{
printf(" %s",ptr->info);
ptr=ptr->link;
}
}
//-------------------------------------------
void sort(void)
{
int comp(char [],char []);
int i;
char temp[20],s1[20],s2[20];
list *ptr2;
for(ptr=start;ptr!=NULL;ptr=ptr->link)
{
for(ptr2=ptr->link;ptr2!=NULL;ptr2=ptr2->link)
{
strcpy(s1,ptr->info);
strcpy(s2,ptr2->info);
i=comp(s1,s2);
if(i==1)
{
strcpy(temp,ptr->info);
strcpy(ptr->info,ptr2->info);
strcpy(ptr2->info,temp);
}
}
}
}
//-------------------------------------------
int comp(char s1[],char s2[])
{
int i=0,j,k;
while(s1[i]!='
#include "conio.h"
struct name
{
char info[20];
struct name *link;
}*ptr,*start,*node;
typedef struct name list;
void main()
{
void create(void);
void disp(void);
void sort(void);
clrscr();
printf("Program to enter the names and sort them using linked list.");
create(); //fun to enter the names
printf("The contents of the list were :
);
disp(); //fun to display the data
sort(); //fun to sort the data
printf("After sorting :");
printf("The contents of the list are :
);
disp();
getch();
}
//-------------------------------------------
void create(void)
{
char ch='y';
node=(list *)malloc(sizeof(list));
start=node;
while(ch=='y')
{
printf("Enter the name :");
gets(node->info);
ptr=node;
node=(list *)malloc(sizeof(list));
ptr->link=node;
printf("Want to continue?(y/n)");
fflush(stdin);
scanf("%c",&ch);
if(ch=='n')
break;
ch='y';
}
ptr->link=NULL;
}
//-------------------------------------------
void disp(void)
{
ptr=start;
while(ptr!=NULL)
{
printf(" %s",ptr->info);
ptr=ptr->link;
}
}
//-------------------------------------------
void sort(void)
{
int comp(char [],char []);
int i;
char temp[20],s1[20],s2[20];
list *ptr2;
for(ptr=start;ptr!=NULL;ptr=ptr->link)
{
for(ptr2=ptr->link;ptr2!=NULL;ptr2=ptr2->link)
{
strcpy(s1,ptr->info);
strcpy(s2,ptr2->info);
i=comp(s1,s2);
if(i==1)
{
strcpy(temp,ptr->info);
strcpy(ptr->info,ptr2->info);
strcpy(ptr2->info,temp);
}
}
}
}
//-------------------------------------------
int comp(char s1[],char s2[])
{
int i=0,j,k;
while(s1[i]!='
CPU scheduling algorithm
#include "stdio.h"
#include "conio.h"
void main()
{
int padd[10],msize[10],madd[10],psize[10],rpsize[10];
int nom,n,i,j,k,t_madd[10],t_msize[10];
clrscr();
printf("\n\n\t\t * * * BEST FIT * * *\n\n");
// Getting input
printf("\nNo. of partitions in Memory: \t");
scanf("%d",&nom);
for(i=0;i
printf("\nAddress of partition:\t");
scanf("%d",&madd[i]);
printf("\nEnter the SIZE :\t");
scanf("%d",&msize[i]);
}
printf("\nProcess SIZE:\t");
scanf("%d",&psize[0]);
printf("\n\nBefore allocation:-\n");
printf("\nADDRESS \tAVAIL. SIZE\t\n");
for(i=0;i
printf("\n%d\t\t%d\n",madd[i],msize[i]);
}
for(k=0;k
for(j=k;j
if(msize[k]>msize[j])
{
t_madd[j]=madd[j];
madd[j]=madd[k];
madd[k]=t_madd[j];
t_msize[j]=msize[j];
msize[j]=msize[k];
msize[k]=t_msize[j];
}
}
}
for(i=0;i
if(psize[0]<=msize[i])
{
printf("\n partition");
printf(" %d",madd[i]);
printf(" was filled\n");
madd[i]+=psize[0];
msize[i]-=psize[0];
break;
}
}
//display memory block
printf("\nAfter allocation:-\n\n");
printf("\nADDRESS \tFREE SPACE\t\n");
for(i=0;i
printf("\n%d\t\t%d\n",madd[i],msize[i]);
}
getch();
}
Linked List implementation
#include"m_list.h"
void main()
{
list *first=NULL,*second=NULL,*third=NULL;
int choice,i;
char ch='y';
while(1)
{
clrscr();
printf(" case 1: Create list");
printf(" case 2: Add in the list");
printf(" case 3: Delete in the list");
printf(" case 4: Append two list");
printf(" case 5: show list");
printf(" case 6: Exit");
printf(" Enter your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: //create list
while(ch!='n')
{
printf(" Enter element : ");
scanf("%d",&i);
create(&first,i);
printf("Enter element (y/n) : ");
fflush(stdin);
scanf("%c",&ch);
}
break;
case 2: //add in the list
int c;
clrscr();
printf("case 1: Add in Beginning");
printf("case 2: Add in End");
printf("Case 3: Add After a given element");
printf("case 4: Return to main menu");
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1: add_at_beg(&first);
break;
case 2: add_at_end(&first);
break;
case 3: add_after_given_element(&first);
break;
case 4: break;
}
break;
case 3:
clrscr();
printf(" case 1: Delete in Beginning");
printf(" case 2: Delete in End");
printf("case 3: Delete a specified element");
printf("case 4: Return to main menu");
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1: del_at_beg(&first);
break;
case 2: del_at_end(&first);
break;
case 3: del_specified_element(&first);
break;
case 4: break;
}
break;
case 4:
char ch='y';
printf("Enter element in second list : ");
while(ch!='n')
{
printf(" Enter element : ");
scanf("%d",&i);
create(&second,i);
printf(" Enter element (y/n) : ");
fflush(stdin);
scanf("%c",&ch);
}
append(&third,first,second);
break;
case 5: //show list
clrscr();
printf("
case 1: List 1");
printf("
case 2: List 2");
printf("
case 3: List 3");
printf("
Enter choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1: show(first);break;
case 2: show(second);break;
case 3: show(third);break;
}
break;
case 6: exit(0);
}
}
}
*********************************
#include
#include
#include
#include
typedef struct list
{
int info;
struct list *next;
};
//.................Function Declaration ...........
void create(struct list **p,int i)
{
struct list *temp,*q=*p;
temp=(struct list*)malloc(sizeof(struct list));
temp->info=i;
temp->next=NULL;
if(*p==NULL)
*p=temp;
else
{
while(q->next!=NULL)
q=q->next;
q->next=temp;
}
}
int append(struct list **t,struct list *f,struct list *s)
{
struct list *temp=*t;
if(f==NULL && s==NULL)
return 0;
while(f)
{
create(t,f->info);
f=f->next;
}
while(s)
{
create(t,s->info);
s=s->next;
}
return 0;
}
void show(struct list *p)
{
if(p==NULL)
printf(" List is Empty");
else
while(p)
{
printf("%d ",p->info);
p=p->next;
}
getch();
}
void add_at_beg(struct list **l)
{
struct list *temp=(struct list *)malloc(sizeof(struct list));
printf(" Enter element : ");
scanf("%d",&temp->info);
temp->next=NULL;
if(*l==NULL)
*l=temp;
else
{
temp->next=*l;
*l=temp;
}
}
void del_at_beg(struct list **l)
{
list *temp;
if(*l==NULL)
{
printf("
List is empty");
getch();
}
else
{
temp=*l;
*l=(*l)->next;
free(temp);
}
}
void add_at_end(struct list **l)
{
list *temp,*p;
temp=(struct list *)malloc(sizeof(struct list));
printf("Enter element : ");
scanf("%d",&temp->info);
temp->next=NULL;
if(*l==NULL)
*l=temp;
else
{
p=*l;
while(p->next!=NULL)
p=p->next;
p->next=temp;
}
}
void del_at_end(struct list **l)
{
list *temp,*p;
if(*l==NULL)
{
printf("
List is Empty");
getch();
}
else if((*l)->next==NULL)
{
temp=*l;
*l=NULL;
free(temp);
}
else
{
p=*l;
while(p->next->next!=NULL)
p=p->next;
temp=p->next->next;
p->next=NULL;
free(temp);
}
}
void add_after_given_element(list **l)
{
list *temp,*p;
int m;
temp=(struct list *)malloc(sizeof(struct list));
printf("
Enter element : ");
scanf("%d",&temp->info);
printf("
Enter position after which element inserted : ");
scanf("%d",&m);
temp->next=NULL;
if(*l==NULL)
*l=temp;
else
{
p=*l;
while(p->next!=NULL)
if(p->info==m)
break;
else
p=p->next;
temp->next=p->next;
p->next=temp;
}
}
void del_specified_element(list **l)
{
list *temp,*p,*q;
int m;
printf("
Enter element which is deleted : ");
scanf("%d",&m);
if(*l==NULL)
{
printf("
List is Empty");
getch();
}
else if((*l)->next!=NULL && (*l)->info==m)
{
temp=*l;
*l=(*l)->next;
free(temp);
}
else if((*l)->next==NULL && (*l)->info==m)
{
temp=*l;
*l=NULL;
free(temp);
}
else
{
p=*l;
while(p!=NULL)
if(p->info==m)
break;
else
{
q=p;
p=p->next;
}
temp=p;
q->next=p->next;
free(temp);
}
}
IMPLEMENTATION OF STACKS USING ARRAY & USING LINKED LIST
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#define MAX 10
struct st_a
{
int topmost;
int stack[MAX] ;
};
struct st_a s;
struct stl
{
int data;
struct stl*next ;
}*top,*p,*q;
void stack_array();
void stack_llist();
void main()
{
int i,ch,ch1;
char c;
clrscr();
printf(" PROGRAM TO IMPLEMENT STACKS USING ARRAY & USING LINKED
LIST");
do
{
printf("Select method to implement stacks...
1.Array
2.linked list
3.Exit
your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
stack_array();
break;
case 2:
stack_llist();
break;
case 3:
exit();
default:
printf("Invalid choice");
break;
}
printf("Return to main menu?y/n:");
scanf("%s",&c);
} while(c=='y');
getch();
}
void stack_array()
{
void push_array();
void pop_array();
void display_array();
int i,ch2 ;
char c1;
printf("IMPLEMENTAION OF STACKS USING ARRAY);
do
{
printf("*******MENU*******");
printf("
1.Push
2.Pop
3.Display
4.Exit
your choice:");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
push_array();
break;
case 2:
pop_array();
break;
case 3:
display_array();
break;
case 4:
exit();
default:
printf("
Invalid choice");
break;
}
printf("Want to perform another operation?y/n:");
scanf("%s",&c1);
} while(c1=='y');
}
int full()
{
if(s.topmost==MAX)
return 1;
else
return 0;
}
int empty()
{
if(s.topmost==0)
return 1;
else
return 0;
}
void push_array()
{
int full();
char ch3;
s.topmost=0;
do{
if(full()==1)
{
printf("The Stack is full!!");
break;
}
else
{
s.topmost++;
printf("Enter the Element to be pushed : ");
scanf("%d",&s.stack[s.topmost]);
printf("Push another element?y/n?:");
//flushall();
scanf("%s",&ch3);
}
} while(ch3=='y' || ch3=='Y');
}
void pop_array()
{
int empty();
char ch;
do{
if(empty()==1)
{
printf("The Stack is Empty!!");
break;
}
else
{
printf("%d has been POPPED!",s.stack[s.topmost]);
s.stack[s.topmost--]=0;
printf("Pop other element?y/n:");
//flushall();
scanf("%s",&ch);
}
} while(ch=='y' || ch=='Y');
}
void display_array()
{
int i;
if(empty()==1)
printf("No Records!!!!!!");
else
{
printf("The contents of the stack are....
);
for(i=s.topmost ;i>=1; i--)
printf("
%d",s.stack[i]);
}
}
/* Implementation of stacks using linked list */
void stack_llist()
{
int* push_llist(struct stl *,struct stl *);
int* pop_llist(struct stl *,struct stl *);
void display_llist(struct stl *);
int i,ch5 ;
char c5;
printf("IMPLEMENTAION OF STACKS USING LINKED LIST);
do
{
printf("*****MENU*******");
printf("
1.Push
2.Pop
3.Display
4.Exit
your choice:");
scanf("%d",&ch5);
switch(ch5)
{
case 1:
top=push_llist(top,p);
break;
case 2:
top=pop_llist(top,q);
break;
case 3:
display_llist(top);
break;
case 4:
exit();
default:
printf("Invalid choice");
break;
}
printf("Want to perform another operation?y/n:");
scanf("%s",&c5);
}while(c5=='y');
}
int* push_llist(struct stl *top,struct stl *p)
{
void display_llist(struct stl *);
int num;
char c7;
do{
printf("Enter the Element to be pushed : ");
scanf("%d",&num);
p=malloc(sizeof(struct stl));
p->data=num;
p->next=NULL;
if(top==NULL)
top=p;
else
{
p->next=top;
top=p;
}
printf("Push another element?y/n?:");
scanf("%s",&c7);
} while(c7=='y' || c7=='Y');
return(top);
}
int* pop_llist(struct stl *top,struct stl *q)
{
void display_llist(struct stl *);
char c8;
do{
if(top==NULL)
{
printf("List is empty!");
break;
}
else
{
q=top;
top=top->next;
q->next=NULL;
free(q);
}
printf("Pop other element?y/n:");
scanf("%s",&c8);
} while(c8=='y' || c8=='Y');
return(top);
}
void display_llist(struct stl *top)
{
if(top==NULL)
printf("stack is empty!!");
else
{
q=top;
printf ( "contents of stack are:" ;
while ( q!= NULL ) /* traverse the entire linked list */
{
printf ( "
%d ", q -> data ) ;
q = q -> next ;
}
}
}
#include "conio.h"
#include "malloc.h"
#define MAX 10
struct st_a
{
int topmost;
int stack[MAX] ;
};
struct st_a s;
struct stl
{
int data;
struct stl*next ;
}*top,*p,*q;
void stack_array();
void stack_llist();
void main()
{
int i,ch,ch1;
char c;
clrscr();
printf(" PROGRAM TO IMPLEMENT STACKS USING ARRAY & USING LINKED
LIST");
do
{
printf("Select method to implement stacks...
1.Array
2.linked list
3.Exit
your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
stack_array();
break;
case 2:
stack_llist();
break;
case 3:
exit();
default:
printf("Invalid choice");
break;
}
printf("Return to main menu?y/n:");
scanf("%s",&c);
} while(c=='y');
getch();
}
void stack_array()
{
void push_array();
void pop_array();
void display_array();
int i,ch2 ;
char c1;
printf("IMPLEMENTAION OF STACKS USING ARRAY);
do
{
printf("*******MENU*******");
printf("
1.Push
2.Pop
3.Display
4.Exit
your choice:");
scanf("%d",&ch2);
switch(ch2)
{
case 1:
push_array();
break;
case 2:
pop_array();
break;
case 3:
display_array();
break;
case 4:
exit();
default:
printf("
Invalid choice");
break;
}
printf("Want to perform another operation?y/n:");
scanf("%s",&c1);
} while(c1=='y');
}
int full()
{
if(s.topmost==MAX)
return 1;
else
return 0;
}
int empty()
{
if(s.topmost==0)
return 1;
else
return 0;
}
void push_array()
{
int full();
char ch3;
s.topmost=0;
do{
if(full()==1)
{
printf("The Stack is full!!");
break;
}
else
{
s.topmost++;
printf("Enter the Element to be pushed : ");
scanf("%d",&s.stack[s.topmost]);
printf("Push another element?y/n?:");
//flushall();
scanf("%s",&ch3);
}
} while(ch3=='y' || ch3=='Y');
}
void pop_array()
{
int empty();
char ch;
do{
if(empty()==1)
{
printf("The Stack is Empty!!");
break;
}
else
{
printf("%d has been POPPED!",s.stack[s.topmost]);
s.stack[s.topmost--]=0;
printf("Pop other element?y/n:");
//flushall();
scanf("%s",&ch);
}
} while(ch=='y' || ch=='Y');
}
void display_array()
{
int i;
if(empty()==1)
printf("No Records!!!!!!");
else
{
printf("The contents of the stack are....
);
for(i=s.topmost ;i>=1; i--)
printf("
%d",s.stack[i]);
}
}
/* Implementation of stacks using linked list */
void stack_llist()
{
int* push_llist(struct stl *,struct stl *);
int* pop_llist(struct stl *,struct stl *);
void display_llist(struct stl *);
int i,ch5 ;
char c5;
printf("IMPLEMENTAION OF STACKS USING LINKED LIST);
do
{
printf("*****MENU*******");
printf("
1.Push
2.Pop
3.Display
4.Exit
your choice:");
scanf("%d",&ch5);
switch(ch5)
{
case 1:
top=push_llist(top,p);
break;
case 2:
top=pop_llist(top,q);
break;
case 3:
display_llist(top);
break;
case 4:
exit();
default:
printf("Invalid choice");
break;
}
printf("Want to perform another operation?y/n:");
scanf("%s",&c5);
}while(c5=='y');
}
int* push_llist(struct stl *top,struct stl *p)
{
void display_llist(struct stl *);
int num;
char c7;
do{
printf("Enter the Element to be pushed : ");
scanf("%d",&num);
p=malloc(sizeof(struct stl));
p->data=num;
p->next=NULL;
if(top==NULL)
top=p;
else
{
p->next=top;
top=p;
}
printf("Push another element?y/n?:");
scanf("%s",&c7);
} while(c7=='y' || c7=='Y');
return(top);
}
int* pop_llist(struct stl *top,struct stl *q)
{
void display_llist(struct stl *);
char c8;
do{
if(top==NULL)
{
printf("List is empty!");
break;
}
else
{
q=top;
top=top->next;
q->next=NULL;
free(q);
}
printf("Pop other element?y/n:");
scanf("%s",&c8);
} while(c8=='y' || c8=='Y');
return(top);
}
void display_llist(struct stl *top)
{
if(top==NULL)
printf("stack is empty!!");
else
{
q=top;
printf ( "contents of stack are:" ;
while ( q!= NULL ) /* traverse the entire linked list */
{
printf ( "
%d ", q -> data ) ;
q = q -> next ;
}
}
}
Implentation of queue using arrays
# include "stdio.h"
# include "conio.h"
# define SIZE 10
int arr[SIZE], front = -1, rear = -1, i ;
void enqueue() ;
void dequeue() ;
void display() ;
void main()
{
int ch ;
clrscr() ;
do
{
printf("
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
) ;
printf("Enter your choice [1-4] : ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
enqueue() ;
break ;
case 2 :
dequeue() ;
break ;
case 3 :
display() ;
break ;
case 4 :
break ;
default :
printf("
Invalid option
) ;
getch() ;
}
} while(ch != 4) ;
getch() ;
}
void enqueue()
{
if(rear == SIZE - 1)
{
printf(" Queue is full (overflow)
) ;
getch() ;
return ;
}
rear++ ;
printf("Enter the element to ENQUEUE : ") ;
scanf("%d", &arr[rear]) ;
if(front == -1)
front++ ;
}
void dequeue()
{
if(front == -1)
{
printf("Queue is empty (underflow)
) ;
getch() ;
return ;
}
printf("The DEQUEUE element is : %d
, arr[front]) ;
getch() ;
if(front == rear)
front = rear = -1 ;
else
front++ ;
}
void display()
{
if(front == -1)
{
printf("Queue is empty (underflow)
) ;
getch() ;
return ;
}
printf(" The elements in queue are : FRONT ->") ;
for(i = front ; i <= rear ; i++)
printf(" ... %d", arr[i]) ;
printf(" ... <- REAR
) ;
getch() ;
}
# include "conio.h"
# define SIZE 10
int arr[SIZE], front = -1, rear = -1, i ;
void enqueue() ;
void dequeue() ;
void display() ;
void main()
{
int ch ;
clrscr() ;
do
{
printf("
[1].ENQUEUE [2].DEQUEUE [3].Display [4].Exit
) ;
printf("Enter your choice [1-4] : ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
enqueue() ;
break ;
case 2 :
dequeue() ;
break ;
case 3 :
display() ;
break ;
case 4 :
break ;
default :
printf("
Invalid option
) ;
getch() ;
}
} while(ch != 4) ;
getch() ;
}
void enqueue()
{
if(rear == SIZE - 1)
{
printf(" Queue is full (overflow)
) ;
getch() ;
return ;
}
rear++ ;
printf("Enter the element to ENQUEUE : ") ;
scanf("%d", &arr[rear]) ;
if(front == -1)
front++ ;
}
void dequeue()
{
if(front == -1)
{
printf("Queue is empty (underflow)
) ;
getch() ;
return ;
}
printf("The DEQUEUE element is : %d
, arr[front]) ;
getch() ;
if(front == rear)
front = rear = -1 ;
else
front++ ;
}
void display()
{
if(front == -1)
{
printf("Queue is empty (underflow)
) ;
getch() ;
return ;
}
printf(" The elements in queue are : FRONT ->") ;
for(i = front ; i <= rear ; i++)
printf(" ... %d", arr[i]) ;
printf(" ... <- REAR
) ;
getch() ;
}
# include "stdio.h"
# include "conio.h"
# define SIZE 10
int arr[SIZE], top = -1, i ;
void push() ;
void pop() ;
void display() ;
void main()
{
int ch ;
clrscr() ;
do
{
printf("[1].PUSH [2].POP [3].Display [4].Exit) ;
printf("Enter your choice [1-4] : ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
push() ;
break ;
case 2 :
pop() ;
break ;
case 3 :
display() ;
break ;
case 4 :
break ;
default :
printf("
Invalid option
) ;
getch() ;
}
} while(ch != 4) ;
getch() ;
}
void push()
{
if(top == SIZE - 1)
{
printf("Stack is full (overflow)) ;
getch() ;
return ;
}
top++ ;
printf("Enter the element to PUSH : ") ;
scanf("%d", &arr[top]) ;
}
void pop()
{
if(top == -1)
{
printf("Stack is empty (underflow)) ;
getch() ;
return ;
}
printf("The POP element is : %d, arr[top]) ;
getch() ;
top-- ;
}
void display()
{
if(top == -1)
{
printf("
Stack is empty (underflow)
) ;
getch() ;
return ;
}
printf("The elements in stack are :TOP") ;
for(i = top ; i >= 0 ; i--)
printf(" -> %d", arr[i]) ;
printf("") ;
getch() ;
}
# include "conio.h"
# define SIZE 10
int arr[SIZE], top = -1, i ;
void push() ;
void pop() ;
void display() ;
void main()
{
int ch ;
clrscr() ;
do
{
printf("[1].PUSH [2].POP [3].Display [4].Exit) ;
printf("Enter your choice [1-4] : ") ;
scanf("%d", &ch) ;
switch(ch)
{
case 1 :
push() ;
break ;
case 2 :
pop() ;
break ;
case 3 :
display() ;
break ;
case 4 :
break ;
default :
printf("
Invalid option
) ;
getch() ;
}
} while(ch != 4) ;
getch() ;
}
void push()
{
if(top == SIZE - 1)
{
printf("Stack is full (overflow)) ;
getch() ;
return ;
}
top++ ;
printf("Enter the element to PUSH : ") ;
scanf("%d", &arr[top]) ;
}
void pop()
{
if(top == -1)
{
printf("Stack is empty (underflow)) ;
getch() ;
return ;
}
printf("The POP element is : %d, arr[top]) ;
getch() ;
top-- ;
}
void display()
{
if(top == -1)
{
printf("
Stack is empty (underflow)
) ;
getch() ;
return ;
}
printf("The elements in stack are :TOP") ;
for(i = top ; i >= 0 ; i--)
printf(" -> %d", arr[i]) ;
printf("") ;
getch() ;
}
#include "stdio.h"
#include "conio.h"
#include "alloc.h"
#include "stdlib.h"
struct btree
{
int n;
struct btree *left;
struct btree *right;
}
typedef btree,node;
btree *root,*ptr,*prev,*bfor,*x,*y;
int num;
char ch;
void main()
{
int c;
void create(node *);
void print(node *);
void search(node *);
void insert(node *);
void modify(node *);
void delet(node *);
while(c!=7)
{
clrscr();
printf("1. CREATE
);
printf("2. PRINT
);
printf("3. SEARCH
);
printf("4. INSERT
);
printf("5. MODIFY
);
printf("6. DELETE
);
printf("7. EXIT
);
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1 :
{ root=(node *)malloc(sizeof(node));
printf("Enter number : ");
scanf("%d",&root->n);
ptr=root;
root->left=root->right=NULL;
printf("Enter more(y/n)? ");
ch=getch();
create(root);
break;
}
case 2 :
{
print(root);
getch();
break;
}
case 3 :
{
printf("
);
search(root);
getch();
break;
}
case 4 :
{
printf("
);
insert(root);
getch();
break;
}
case 5 :
{
printf("
);
modify(root);
getch();
break;
}
case 6 :
{
printf("
);
delet(root);
getch();
break;
}
case 7 :
exit(0);
default:
{
printf("Invalid choice");
getch();
break;
}
}
}
getch();
}
void create(node *ptr)
{
while(ch == 'y')
{
printf("
);
ptr=prev=root;
printf("Enter number : ");
scanf("%d",&num);
do
{
if(num < ptr->n)
{
prev=ptr;
ptr=ptr->left;
}
else if(num > ptr->n)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=NULL;
break;
}
}while(ptr);
if(prev)
{
ptr=(node *)malloc(sizeof(node));
ptr->n=num;
ptr->left=ptr->right=NULL;
if(ptr->n < prev->n)
{
prev->left=ptr;
if(prev == root)
root=prev;
}
if(ptr->n > prev->n)
{
prev->right=ptr;
ptr->left=ptr->right=NULL;
if(prev == root)
root=prev;
}
}
else
printf("'%d' is already present..
,num);
printf("Enter more(y/n)? ");
ch=getch();
}
}
void print(node *ptr)
{
void inprint(node *);
void preprint(node *);
void postprint(node *);
if(!ptr)
{
printf("Tree is empty...");
return;
}
printf("
Root is '%d'
,root->n);
printf("
INORDER : ");
inprint(root);
printf("
PREORDER : ");
preprint(root);
printf("
POSTORDER : ");
postprint(root);
}
void inprint(node *ptr)
{
if(!ptr)
return;
inprint(ptr->left);
printf("%2d ",ptr->n);
inprint(ptr->right);
return;
}
void preprint(node *ptr)
{
if(!ptr)
return;
printf("%2d ",ptr->n);
preprint(ptr->left);
preprint(ptr->right);
return;
}
void postprint(node *ptr)
{
if(!ptr)
return;
postprint(ptr->left);
postprint(ptr->right);
printf("%2d ",ptr->n);
return;
}
void search(node *ptr)
{
if(!ptr)
{
if(ptr == root)
{
printf("Tree is empty... You can't search anything...");
return;
}
}
printf("Enter the number to search : ");
scanf("%d",&num);
while(ptr)
{
if(ptr->n == num)
{
printf("Success... You found the number...");
return;
}
else if(ptr->n < num)
ptr=ptr->right;
else
ptr=ptr->left;
}
printf("Tree don't contain '%d'...",num);
}
void insert(node *ptr)
{
if(!ptr)
{
printf("Tree is empty...First create & then insert... ");
return;
}
printf("
);
ptr=prev=root;
printf("Enter number to be inserted : ");
scanf("%d",&num);
do{
if(num < ptr->n)
{
prev=ptr;
ptr=ptr->left;
}
else if(num > ptr->n)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=NULL;
break;
}
}while(ptr);
if(prev)
{
ptr=(node *)malloc(sizeof(node));
ptr->n=num;
ptr->left=ptr->right=NULL;
if(ptr->n < prev->n)
{
prev->left=ptr;
if(prev == root)
root=prev;
}
if(ptr->n > prev->n)
{
prev->right=ptr;
ptr->left=ptr->right=NULL;
if(prev == root)
root=prev;
}
printf("'%d' is inserted...",num);
}
else
printf("'%d' is already present...",num);
return;
}
void modify(node *ptr)
{
int mod;
if(!ptr)
{
if(ptr == root)
{
printf("Tree is empty... You can't modify anything...");
return;
}
}
printf("Modification of particular number can't create a binary
tree...");
getch();
printf("Enter the number to get modified : ");
scanf("%d",&num);
prev=ptr;
while(ptr)
{
if(ptr->n == num)
{
x=ptr;
mod=x->n;
printf("Then enter new number : ");
scanf("%d",&num);
bfor=ptr=root;
while(ptr)
{
if(ptr->n == num)
{
printf("'%d' already present...Modification denied...,",num);
return;
}
else if(ptr->n < num)
{
bfor=ptr;
ptr=ptr->right;
}
else
{
bfor=ptr;
ptr=ptr->left;
}
}
if(x==root)
{
y=x->right;
root=y;
while(y->left)
y=y->left;
y->left=x->left;
free(x);
}
else if(!(x->left) && !(x->right))
{
if(prev->left == x)
{
prev->left=NULL;
free(x);
}
else if(prev->right == x)
{
prev->right=NULL;
free(x);
}
}
else if(!(x->left))
{
if(prev->left == x)
{
prev->left=x->right;
free(x);
}
else if(prev->right == x)
{
prev->right=x->right;
free(x);
}
}
else if(!(x->right))
{
if(prev->left == x)
{
prev->left=x->left;
free(x);
}
else if(prev->right == x)
{
prev->right=x->left;
free(x);
}
}
else
{
y=x->right;
while(y->left)
y=y->left;
y->left=x->left;
if(prev->left == x)
prev->left=y;
else if(prev->right == x)
prev->right=y;
free(x);
}
ptr=(node *)malloc(sizeof(node));
ptr->n=num;
ptr->left=ptr->right=NULL;
if(ptr->n < bfor->n)
{
bfor->left=ptr;
if(bfor == root)
root=bfor;
}
if(ptr->n > bfor->n)
{
bfor->right=ptr;
ptr->left=ptr->right=NULL;
if(bfor == root)
root=bfor;
}
printf("'%d' is modified by '%d'",mod,ptr->n);
return;
}
else if(ptr->n < num)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=ptr;
ptr=ptr->left;
}
}
printf("Tree don't contain '%d'...",num);
}
void delet(node *ptr)
{
if(!ptr)
{
if(ptr == root)
{
printf("Tree is empty... You can't delete anything...");
return;
}
}
printf("Enter the number to get deleted : ");
scanf("%d",&num);
prev=ptr;
while(ptr)
{
if(ptr->n == num)
{
if(ptr==root)
{
x=ptr->right;
root=x;
while(x->left)
x=x->left;
x->left=ptr->left;
free(ptr);
printf("'%d' is deleted...",num);
return;
}
else if(!(ptr->left) && !(ptr->right))
{
if(prev->left == ptr)
prev->left=NULL;
else
prev->right=NULL;
free(ptr);
printf("'%d' is deleted...",num);
return;
}
else if(!(ptr->left))
{
if(prev->left == ptr)
{
prev->left=ptr->right;
free(ptr);
}
else if(prev->right == ptr)
{
prev->right=ptr->right;
free(ptr);
}
printf("'%d' is deleted...",num);
return;
}
else if(!(ptr->right))
{
if(prev->left == ptr)
{
prev->left=ptr->left;
free(ptr);
}
else if(prev->right == ptr)
{
prev->right=ptr->left;
free(ptr);
}
printf("'%d' is deleted...",num);
return;
}
else
{
x=ptr->right;
while(x->left)
x=x->left;
x->left=ptr->left;
if(prev->left == ptr)
prev->left=ptr->right;
else if(prev->right == ptr)
prev->right=ptr->right;
free(ptr);
printf("'%d' is deleted...",num);
return;
}
}
else if(ptr->n < num)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=ptr;
ptr=ptr->left;
}
}
printf("Tree don't contain '%d'...",num);
}
#include "conio.h"
#include "alloc.h"
#include "stdlib.h"
struct btree
{
int n;
struct btree *left;
struct btree *right;
}
typedef btree,node;
btree *root,*ptr,*prev,*bfor,*x,*y;
int num;
char ch;
void main()
{
int c;
void create(node *);
void print(node *);
void search(node *);
void insert(node *);
void modify(node *);
void delet(node *);
while(c!=7)
{
clrscr();
printf("1. CREATE
);
printf("2. PRINT
);
printf("3. SEARCH
);
printf("4. INSERT
);
printf("5. MODIFY
);
printf("6. DELETE
);
printf("7. EXIT
);
printf("Enter your choice : ");
scanf("%d",&c);
switch(c)
{
case 1 :
{ root=(node *)malloc(sizeof(node));
printf("Enter number : ");
scanf("%d",&root->n);
ptr=root;
root->left=root->right=NULL;
printf("Enter more(y/n)? ");
ch=getch();
create(root);
break;
}
case 2 :
{
print(root);
getch();
break;
}
case 3 :
{
printf("
);
search(root);
getch();
break;
}
case 4 :
{
printf("
);
insert(root);
getch();
break;
}
case 5 :
{
printf("
);
modify(root);
getch();
break;
}
case 6 :
{
printf("
);
delet(root);
getch();
break;
}
case 7 :
exit(0);
default:
{
printf("Invalid choice");
getch();
break;
}
}
}
getch();
}
void create(node *ptr)
{
while(ch == 'y')
{
printf("
);
ptr=prev=root;
printf("Enter number : ");
scanf("%d",&num);
do
{
if(num < ptr->n)
{
prev=ptr;
ptr=ptr->left;
}
else if(num > ptr->n)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=NULL;
break;
}
}while(ptr);
if(prev)
{
ptr=(node *)malloc(sizeof(node));
ptr->n=num;
ptr->left=ptr->right=NULL;
if(ptr->n < prev->n)
{
prev->left=ptr;
if(prev == root)
root=prev;
}
if(ptr->n > prev->n)
{
prev->right=ptr;
ptr->left=ptr->right=NULL;
if(prev == root)
root=prev;
}
}
else
printf("'%d' is already present..
,num);
printf("Enter more(y/n)? ");
ch=getch();
}
}
void print(node *ptr)
{
void inprint(node *);
void preprint(node *);
void postprint(node *);
if(!ptr)
{
printf("Tree is empty...");
return;
}
printf("
Root is '%d'
,root->n);
printf("
INORDER : ");
inprint(root);
printf("
PREORDER : ");
preprint(root);
printf("
POSTORDER : ");
postprint(root);
}
void inprint(node *ptr)
{
if(!ptr)
return;
inprint(ptr->left);
printf("%2d ",ptr->n);
inprint(ptr->right);
return;
}
void preprint(node *ptr)
{
if(!ptr)
return;
printf("%2d ",ptr->n);
preprint(ptr->left);
preprint(ptr->right);
return;
}
void postprint(node *ptr)
{
if(!ptr)
return;
postprint(ptr->left);
postprint(ptr->right);
printf("%2d ",ptr->n);
return;
}
void search(node *ptr)
{
if(!ptr)
{
if(ptr == root)
{
printf("Tree is empty... You can't search anything...");
return;
}
}
printf("Enter the number to search : ");
scanf("%d",&num);
while(ptr)
{
if(ptr->n == num)
{
printf("Success... You found the number...");
return;
}
else if(ptr->n < num)
ptr=ptr->right;
else
ptr=ptr->left;
}
printf("Tree don't contain '%d'...",num);
}
void insert(node *ptr)
{
if(!ptr)
{
printf("Tree is empty...First create & then insert... ");
return;
}
printf("
);
ptr=prev=root;
printf("Enter number to be inserted : ");
scanf("%d",&num);
do{
if(num < ptr->n)
{
prev=ptr;
ptr=ptr->left;
}
else if(num > ptr->n)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=NULL;
break;
}
}while(ptr);
if(prev)
{
ptr=(node *)malloc(sizeof(node));
ptr->n=num;
ptr->left=ptr->right=NULL;
if(ptr->n < prev->n)
{
prev->left=ptr;
if(prev == root)
root=prev;
}
if(ptr->n > prev->n)
{
prev->right=ptr;
ptr->left=ptr->right=NULL;
if(prev == root)
root=prev;
}
printf("'%d' is inserted...",num);
}
else
printf("'%d' is already present...",num);
return;
}
void modify(node *ptr)
{
int mod;
if(!ptr)
{
if(ptr == root)
{
printf("Tree is empty... You can't modify anything...");
return;
}
}
printf("Modification of particular number can't create a binary
tree...");
getch();
printf("Enter the number to get modified : ");
scanf("%d",&num);
prev=ptr;
while(ptr)
{
if(ptr->n == num)
{
x=ptr;
mod=x->n;
printf("Then enter new number : ");
scanf("%d",&num);
bfor=ptr=root;
while(ptr)
{
if(ptr->n == num)
{
printf("'%d' already present...Modification denied...,",num);
return;
}
else if(ptr->n < num)
{
bfor=ptr;
ptr=ptr->right;
}
else
{
bfor=ptr;
ptr=ptr->left;
}
}
if(x==root)
{
y=x->right;
root=y;
while(y->left)
y=y->left;
y->left=x->left;
free(x);
}
else if(!(x->left) && !(x->right))
{
if(prev->left == x)
{
prev->left=NULL;
free(x);
}
else if(prev->right == x)
{
prev->right=NULL;
free(x);
}
}
else if(!(x->left))
{
if(prev->left == x)
{
prev->left=x->right;
free(x);
}
else if(prev->right == x)
{
prev->right=x->right;
free(x);
}
}
else if(!(x->right))
{
if(prev->left == x)
{
prev->left=x->left;
free(x);
}
else if(prev->right == x)
{
prev->right=x->left;
free(x);
}
}
else
{
y=x->right;
while(y->left)
y=y->left;
y->left=x->left;
if(prev->left == x)
prev->left=y;
else if(prev->right == x)
prev->right=y;
free(x);
}
ptr=(node *)malloc(sizeof(node));
ptr->n=num;
ptr->left=ptr->right=NULL;
if(ptr->n < bfor->n)
{
bfor->left=ptr;
if(bfor == root)
root=bfor;
}
if(ptr->n > bfor->n)
{
bfor->right=ptr;
ptr->left=ptr->right=NULL;
if(bfor == root)
root=bfor;
}
printf("'%d' is modified by '%d'",mod,ptr->n);
return;
}
else if(ptr->n < num)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=ptr;
ptr=ptr->left;
}
}
printf("Tree don't contain '%d'...",num);
}
void delet(node *ptr)
{
if(!ptr)
{
if(ptr == root)
{
printf("Tree is empty... You can't delete anything...");
return;
}
}
printf("Enter the number to get deleted : ");
scanf("%d",&num);
prev=ptr;
while(ptr)
{
if(ptr->n == num)
{
if(ptr==root)
{
x=ptr->right;
root=x;
while(x->left)
x=x->left;
x->left=ptr->left;
free(ptr);
printf("'%d' is deleted...",num);
return;
}
else if(!(ptr->left) && !(ptr->right))
{
if(prev->left == ptr)
prev->left=NULL;
else
prev->right=NULL;
free(ptr);
printf("'%d' is deleted...",num);
return;
}
else if(!(ptr->left))
{
if(prev->left == ptr)
{
prev->left=ptr->right;
free(ptr);
}
else if(prev->right == ptr)
{
prev->right=ptr->right;
free(ptr);
}
printf("'%d' is deleted...",num);
return;
}
else if(!(ptr->right))
{
if(prev->left == ptr)
{
prev->left=ptr->left;
free(ptr);
}
else if(prev->right == ptr)
{
prev->right=ptr->left;
free(ptr);
}
printf("'%d' is deleted...",num);
return;
}
else
{
x=ptr->right;
while(x->left)
x=x->left;
x->left=ptr->left;
if(prev->left == ptr)
prev->left=ptr->right;
else if(prev->right == ptr)
prev->right=ptr->right;
free(ptr);
printf("'%d' is deleted...",num);
return;
}
}
else if(ptr->n < num)
{
prev=ptr;
ptr=ptr->right;
}
else
{
prev=ptr;
ptr=ptr->left;
}
}
printf("Tree don't contain '%d'...",num);
}
This program is to find the expression tree.
#include "stdio.h"
#include "conio.h"
#include "process.h"
#include "string.h"
#include "stdlib.h"
struct treenode
{
char c;
treenode * rlink;
treenode * llink;
}*stc[30],*temp,*root;
char prefix[20],ch;
int topt=-1,max=50,len; //global declaration
void pusht(struct treenode * p);
struct treenode* popt();
void tredis(struct treenode *ptr,int level);
void exptree();
void post(struct treenode* p);
void main()
{
clrscr();
printf("
Enter a prefix expression :");
scanf("%s",prefix);
exptree();
tredis(root,1);
printf("
The postfix expression is :");
post(root);
getch();
}
void post(struct treenode* p)
{
if(p!=NULL)
{
post(p->llink);
post(p->rlink);
printf("%c",p->c);
}
}
void exptree()
{
len=strlen(prefix);
int i=len-1;
while(i>=0)
{
switch(prefix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
temp=(struct treenode*)malloc(sizeof(struct treenode));
temp->c=prefix[i];
temp->llink=popt();
temp->rlink=popt();
pusht(temp);
break;
default :
temp=(struct treenode*)malloc(sizeof(struct treenode));
temp->c=prefix[i];
temp->rlink=NULL;
temp->llink=NULL;
pusht(temp);
}
i--;
}
root=stc[topt];
}
void pusht(struct treenode * p)
{
if(topt==max)
{
printf("
/**Beyond Capacity**/");
}
else
{
stc[++topt]=p;
}
}
struct treenode* popt()
{
if(topt==-1)
printf("
/**No Expression**/");
else
return(stc[topt--]);
}
void tredis(struct treenode *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
tredis(ptr->rlink, level+1);
printf("
);
for (i = 0; i <>c);
tredis(ptr->llink, level+1);
}
}
#include "conio.h"
#include "process.h"
#include "string.h"
#include "stdlib.h"
struct treenode
{
char c;
treenode * rlink;
treenode * llink;
}*stc[30],*temp,*root;
char prefix[20],ch;
int topt=-1,max=50,len; //global declaration
void pusht(struct treenode * p);
struct treenode* popt();
void tredis(struct treenode *ptr,int level);
void exptree();
void post(struct treenode* p);
void main()
{
clrscr();
printf("
Enter a prefix expression :");
scanf("%s",prefix);
exptree();
tredis(root,1);
printf("
The postfix expression is :");
post(root);
getch();
}
void post(struct treenode* p)
{
if(p!=NULL)
{
post(p->llink);
post(p->rlink);
printf("%c",p->c);
}
}
void exptree()
{
len=strlen(prefix);
int i=len-1;
while(i>=0)
{
switch(prefix[i])
{
case '+':
case '-':
case '*':
case '/':
case '^':
temp=(struct treenode*)malloc(sizeof(struct treenode));
temp->c=prefix[i];
temp->llink=popt();
temp->rlink=popt();
pusht(temp);
break;
default :
temp=(struct treenode*)malloc(sizeof(struct treenode));
temp->c=prefix[i];
temp->rlink=NULL;
temp->llink=NULL;
pusht(temp);
}
i--;
}
root=stc[topt];
}
void pusht(struct treenode * p)
{
if(topt==max)
{
printf("
/**Beyond Capacity**/");
}
else
{
stc[++topt]=p;
}
}
struct treenode* popt()
{
if(topt==-1)
printf("
/**No Expression**/");
else
return(stc[topt--]);
}
void tredis(struct treenode *ptr,int level)
{
int i;
if ( ptr!=NULL )
{
tredis(ptr->rlink, level+1);
printf("
);
for (i = 0; i <>c);
tredis(ptr->llink, level+1);
}
}
Bubble Sort By Visual Method
#include "stdio.h"
#include "dos.h"
#include "conio.h"
#include "graphics.h"
void object(int,int,int);
void bubble(int);
void flow(int,int);
void mixing(int,int);
int el[10];
void main()
{
int n,i,d=0,m=0;
clrscr();
printf("Enter the No of Elements : " );
scanf("%d",&n);
for(i=0;iel[j+1])
{
flow(i,j+1);
temp=el[i];
el[i]=el[j+1];
el[j+1]=temp;
}
}
void object(int x,int y,int no)
{
char s[8];
sprintf(s,"%d",no);
circle(x,y,15);
settextstyle(2,0,6);
outtextxy(x-3,y-10,s);
}
void flow(int f,int s)
{
int i;
for(i=0;i<50;i++) i="0;i<50;i++)" i="50;i">0;i--)
{
setcolor(WHITE);
object(100+f*50,150+i*4,el[s]);
delay(10);
setcolor(0);
object(100+f*50,150+i*4,el[s]);
}
setcolor(WHITE);
object(100+f*50,150+i*4,el[s]);
for(i=50;i>0;i--)
{
setcolor(WHITE);
object(100+s*50,150+i*4,el[f]);
delay(15);
setcolor(0);
object(100+s*50,150+i*4,el[f]);
}
setcolor(WHITE);
object(100+s*50,150+i*4,el[f]);
}
void mixing(int f,int s)
{
int i;
for(i=0;i<(s-f)*50;i++)
{
setcolor(WHITE);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
delay(20);
setcolor(0);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
} setcolor(WHITE);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
}
#include "dos.h"
#include "conio.h"
#include "graphics.h"
void object(int,int,int);
void bubble(int);
void flow(int,int);
void mixing(int,int);
int el[10];
void main()
{
int n,i,d=0,m=0;
clrscr();
printf("Enter the No of Elements : " );
scanf("%d",&n);
for(i=0;i
el[i]=el[j+1];
el[j+1]=temp;
{
setcolor(WHITE);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
delay(20);
setcolor(0);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
} setcolor(WHITE);
object(100+f*50+i,350,el[f]);
object(100+s*50-i,350,el[s]);
}
Program to calculate the determinant of a matrix of any order using a recursive function.
#include "stdio.h"
#include "conio.h"
#include "math.h"
// A square matrix struct
typedef struct
{
int order;
int **array;
} SQRMATRIX;
// Function declarations
int CreateMatrix( SQRMATRIX *p, int order );
void DisplayMatrix( SQRMATRIX *p );
void InputMatrix( SQRMATRIX *p );
int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, int row, int col );
int CalcDeterminant( SQRMATRIX *p );
void DestroyMatrix( SQRMATRIX *p );
// main( )
int main( )
{
SQRMATRIX p;
int order;
printf("Enter order of matrix: ");
scanf("%d", &order );
if( !CreateMatrix( &p, order ))
{
printf("Matrix couldn't be created.");
return 0;
}
printf("Matrix created.);
InputMatrix( &p );
printf("The matrix is:);
DisplayMatrix( &p );
printf("The determinant of the matrix is: %d", CalcDeterminant( &p
));
getch( );
return 0;
}
// Create matrix of specified order
int CreateMatrix( SQRMATRIX *p, int order )
{
int i;
if( order < 1 ) return 0;
p->order = order;
p->array = (int**)
malloc( order * sizeof( int* )); // Allocate space for each row
if( !p->array )
return 0;
for( i=0; i < order; i++ )
{
p->array[i] = (int*) malloc( order* sizeof( int ));
// Allocate spacefor each column
if( !p->array )
return 0;
}
return 1;
}
// Print matrix in proper format
void DisplayMatrix( SQRMATRIX *p )
{
int i,j;
if( p->order < i =" 0;">order; i++ )
{
for( j = 0; j <>order; j++ )
printf("%5d ", p->array[i][j] );
printf(");
}
}
// Input matrix from user
void InputMatrix( SQRMATRIX *p )
{
int i,j;
for( i = 0; i <>order; i++ )
for( j = 0; j <>order; j++ )
{
printf("Enter element at ( %d, %d ): ", i+1, j+1 );
scanf("%d", &p->array[i][j] );
}
}
/* Calculate the 'minor' of the given matrix at given position.
The minor is the matrix formed by deleting the specified row
and column from the matrix.
*/
int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, int row, int col )
{
int i,j,a,b;
if( p->order <= 1 ) return 0;
if( row >= p->order || col >= p->order )
return 0;
if( !CreateMatrix( minor, p->order-1 ))
return 0;
a = b = 0;
for( i = 0; i <>order; i++ )
{
if( i != row )
{
b = 0;
for( j = 0; j <>order; j++ )
{
if( j != col )
{
minor->array[a][b] = p->array[i][j];
b++; // Increase column-count of minor
}
}
a++; // Increase row-count of minor
}
}
return 1;
}
/* Calculate the determinant recursively.
The recursive definition is :
det( m ) = Summation( i = 0 to order ) [ (-1)^i * m[0][i] * det(
minor( m[0][i] ))]
*/
int CalcDeterminant( SQRMATRIX *p )
{
int i, result = 0, temp;
SQRMATRIX minor;
if( p->order < 1 )
{
printf("CalcDeterminant( ) : Invalid matrix.");
return 0;
} // The 'stopping' condition
if( p->order == 1 )
return p->array[0][0];
for( i = 0; i <>order; i++ )
{
if( !CalcMinor( p, &minor, 0, i ))
{
printf("CalcDeterminant( ) : Memory allocation failed.");
return 0;
}
result += ( pow( -1, i ) * p->array[0][i] * CalcDeterminant( &minor
));
DestroyMatrix( &minor );
}
return result;
}
// Release allocated memory
void DestroyMatrix( SQRMATRIX *p )
{
int i;
if( p->order < 1 )
return;
for( i = 0; i <>order; i++ )
free( p->array[i] ); // free each columns
free( p->array ); // free each row
p->order = 0;
}
#include "conio.h"
#include "math.h"
// A square matrix struct
typedef struct
{
int order;
int **array;
} SQRMATRIX;
// Function declarations
int CreateMatrix( SQRMATRIX *p, int order );
void DisplayMatrix( SQRMATRIX *p );
void InputMatrix( SQRMATRIX *p );
int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, int row, int col );
int CalcDeterminant( SQRMATRIX *p );
void DestroyMatrix( SQRMATRIX *p );
// main( )
int main( )
{
SQRMATRIX p;
int order;
printf("Enter order of matrix: ");
scanf("%d", &order );
if( !CreateMatrix( &p, order ))
{
printf("Matrix couldn't be created.");
return 0;
}
printf("Matrix created.);
InputMatrix( &p );
printf("The matrix is:);
DisplayMatrix( &p );
printf("The determinant of the matrix is: %d", CalcDeterminant( &p
));
getch( );
return 0;
}
// Create matrix of specified order
int CreateMatrix( SQRMATRIX *p, int order )
{
int i;
if( order < 1 ) return 0;
p->order = order;
p->array = (int**)
malloc( order * sizeof( int* )); // Allocate space for each row
if( !p->array )
return 0;
for( i=0; i < order; i++ )
{
p->array[i] = (int*) malloc( order* sizeof( int ));
// Allocate spacefor each column
if( !p->array )
return 0;
}
return 1;
}
// Print matrix in proper format
void DisplayMatrix( SQRMATRIX *p )
{
int i,j;
if( p->order < i =" 0;">order; i++ )
{
for( j = 0; j <>order; j++ )
printf("%5d ", p->array[i][j] );
printf(");
}
}
// Input matrix from user
void InputMatrix( SQRMATRIX *p )
{
int i,j;
for( i = 0; i <>order; i++ )
for( j = 0; j <>order; j++ )
{
printf("Enter element at ( %d, %d ): ", i+1, j+1 );
scanf("%d", &p->array[i][j] );
}
}
/* Calculate the 'minor' of the given matrix at given position.
The minor is the matrix formed by deleting the specified row
and column from the matrix.
*/
int CalcMinor( SQRMATRIX *p, SQRMATRIX *minor, int row, int col )
{
int i,j,a,b;
if( p->order <= 1 ) return 0;
if( row >= p->order || col >= p->order )
return 0;
if( !CreateMatrix( minor, p->order-1 ))
return 0;
a = b = 0;
for( i = 0; i <>order; i++ )
{
if( i != row )
{
b = 0;
for( j = 0; j <>order; j++ )
{
if( j != col )
{
minor->array[a][b] = p->array[i][j];
b++; // Increase column-count of minor
}
}
a++; // Increase row-count of minor
}
}
return 1;
}
/* Calculate the determinant recursively.
The recursive definition is :
det( m ) = Summation( i = 0 to order ) [ (-1)^i * m[0][i] * det(
minor( m[0][i] ))]
*/
int CalcDeterminant( SQRMATRIX *p )
{
int i, result = 0, temp;
SQRMATRIX minor;
if( p->order < 1 )
{
printf("CalcDeterminant( ) : Invalid matrix.");
return 0;
} // The 'stopping' condition
if( p->order == 1 )
return p->array[0][0];
for( i = 0; i <>order; i++ )
{
if( !CalcMinor( p, &minor, 0, i ))
{
printf("CalcDeterminant( ) : Memory allocation failed.");
return 0;
}
result += ( pow( -1, i ) * p->array[0][i] * CalcDeterminant( &minor
));
DestroyMatrix( &minor );
}
return result;
}
// Release allocated memory
void DestroyMatrix( SQRMATRIX *p )
{
int i;
if( p->order < 1 )
return;
for( i = 0; i <>order; i++ )
free( p->array[i] ); // free each columns
free( p->array ); // free each row
p->order = 0;
}
To generate cos series
# include "stdio.h"
# include "conio.h"
main()
{
int i,p,n;
clrscr();
printf("enter the number in term of series");
scanf("%d",&n);
printf("xcos(x)=1");
for(i=1;i<=n;i++)
{
p=2*i;
if(i%2==0)
printf("+");
else
printf("-");
printf(x^%d/%d!",p,p);
getch();
}
}
# include "conio.h"
main()
{
int i,p,n;
clrscr();
printf("enter the number in term of series");
scanf("%d",&n);
printf("xcos(x)=1");
for(i=1;i<=n;i++)
{
p=2*i;
if(i%2==0)
printf("+");
else
printf("-");
printf(x^%d/%d!",p,p);
getch();
}
}
Taylor's series-e^x
#include "stdio.h"
#include "conio.h"
#include "math.h"
long int factorial(int n);
void main()
{
int x,i;
float s,r;
char c;
clrscr();
printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!
..x^x/x!");
printf("To which term you want its sum? ");
scanf("%d",&x);
s=0;
for(i=1;i<=x;i++)
{ s=s+((float)pow(x,i)/(float)factorial(i));
}
printf("The sum of %d terms is %f",x,1+s);
fflush(stdin);
getch();
}
long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}
#include "conio.h"
#include "math.h"
long int factorial(int n);
void main()
{
int x,i;
float s,r;
char c;
clrscr();
printf("You have this series:-1+x/1! + x^2/2! + x^3/3! + x^4/4!
..x^x/x!");
printf("To which term you want its sum? ");
scanf("%d",&x);
s=0;
for(i=1;i<=x;i++)
{ s=s+((float)pow(x,i)/(float)factorial(i));
}
printf("The sum of %d terms is %f",x,1+s);
fflush(stdin);
getch();
}
long int factorial(int n)
{
if (n<=1)
return(1);
else
n=n*factorial(n-1);
return(n);
}
PROGRAM TO FIND SQUARE ROOT OF A NUMBER
#include "stdio.h"
#include "conio.h"
main()
{
float a,b,e=0.00001,p,k;
clrscr();
textcolor(GREEN);
do {
printf(" ************************************************
);
printf(" PROGRAM TO FIND SQUARE ROOT OF A);
printf(" ************************************************
);
cprintf("ENTER A NUMBER(-1 to Quit) :");
scanf("%f",&k);
a=k;p=a*a;
while(p-k>=e)
{
b=(a+(k/a))/2;
a=b;
p=a*a;
}
printf("SQUARE ROOT IS = %f,a);
getch();
clrscr();
}
while(k!=-1);
getch();
}
#include "conio.h"
main()
{
float a,b,e=0.00001,p,k;
clrscr();
textcolor(GREEN);
do {
printf(" ************************************************
);
printf(" PROGRAM TO FIND SQUARE ROOT OF A);
printf(" ************************************************
);
cprintf("ENTER A NUMBER(-1 to Quit) :");
scanf("%f",&k);
a=k;p=a*a;
while(p-k>=e)
{
b=(a+(k/a))/2;
a=b;
p=a*a;
}
printf("SQUARE ROOT IS = %f,a);
getch();
clrscr();
}
while(k!=-1);
getch();
}
PROGRAM TO CALCULATE AREA,VOLUME,PERIMETER OF A SHAPE
#include "stdio.h"
#include "conio.h"
#include "math.h"
#define PI 3.14159
char ch;
main()
{
clrscr();
textcolor(4);
intro();
getch();
textcolor(7);
clrscr();
do
{
ch=menu();
switch(ch)
{
case 'a':
case 'A':
clrscr();
square();
getch();
break;
case 'b':
case 'B':
clrscr();
rect();
getch();
break;
case 'c':
case 'C':
clrscr();
circl();
getch();
break;
case 'd':
case 'D':
clrscr();
tri();
getch();
break;
case 'e':
case 'E':
clrscr();
rom();
getch();
break;
case 'f':
case 'F':
clrscr();
para();
getch();
break;
case 'g':
case 'G':
clrscr();
tra();
getch();
break;
case 'h':
case 'H':
clrscr();
qua();
getch();
break;
case 'i':
case 'I':
clrscr();
semicir();
getch();
break;
case 'j':
case 'J':
clrscr();
msector();
getch();
break;
case 'k':
case 'K':
clrscr();
sphere();
getch();
break;
case 'l':
case 'L':
clrscr();
cone();
getch();
break;
case 'm':
case 'M':
clrscr();
cyll();
getch();
break;
case 'n':
case 'N':
clrscr();
cube();
getch();
break;
case 'o':
case 'O':
clrscr();
cuboid();
getch();
break;
case 'p':
case 'P':
clrscr();
hemisphe();
getch();
break;
case 'q':
case 'Q':
exit(1);
}
} while(ch!='Q'||ch!='q');
getch();
}
intro()
{
int i;
clrscr();
printf(");
textcolor(2);
cprintf("#################################################################
###############");
textcolor(4);
printf("PROGRAM TO CALCULATE AREAS , VOLUMES ,CIRCUMFERENCES ");
printf("=====================================================
);
printf("OF VARIOUS GEOMETRIC SHAPES");
printf("===========================);
textcolor(2);
cprintf("#################################################################
###############");
getch();
printf("Program developed and designed by...");
printf("www.lernc.blogspot.com");
}
menu()
{
clrscr();
textcolor(7);
printf(" MENU Two Dimensional Shapes.
-----------------------
A.SQUARE
B.RECTANGLE
C.CIRCLE
D.TRIANGLE
E.RHOMBUS
F.PARALLELOGRAM
G.TRAPEZIUM
H.QUADRILATERAL.
I.SEMICERCLE
J.SECTOR
);
printf("Three Dimensional Shapes.
-------------------------
K.SPHERE
L.CONE
M.CYLLINDER
N.CUBE
O.CUBOID
P.HEMISPHERE
Q.QUIT
Enter Your Choice :");
scanf("%c",&ch);
return(ch);
}
/***** SUB FUNCTIONS *****/
/***** 2 D SHAPES *****/
square()
{
float s,a,p;int i,j;
printf("Enter side of square:");
scanf("%f",&s);
a=s*s;
p=4*s;
printf("Perimeter of square : %.3f units",p);
printf("Area of square : %.3f sq.units",a);
printf("Square is ...");
for(i=1;i<=s;i++)
{
textcolor(10);
for(j=1;j<=s;j++)
cprintf("ÛÛ");
printf(" ");
} return(0);
} rect()
{
float a,p,l,b;
int i,j;
printf(" Enter length and breadth of rectangle: Length:"); scanf("%f",&l);
printf(" Breadth:");
scanf("%f",&b);
a=l*b;
p=2*(l+b);
printf(" Perimeter of rectangle : %.3f units",p);
printf(" Area of rectangle : %.3f sq.units",a);
printf(" Rectangle is... ");
for(i=1;i<=b;i++)
{
textcolor(4);
for(j=1;j<=l;j++)
cprintf("ÛÛ");
printf(" ");
}
(0);
}
tri()
{
float area,p;
float a,b,c,s;
printf(" Enter three sides of triangle:");
scanf( "%f%f%f",&a,&b,&c);
p=a+b+c;
s=p/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(" Perimeter of triangle : %.3f units",p);
printf(" Area of a triangle : %.3f sq.units",area);
} rom()
{
float s,d1,d2,a,p;
printf(" Enter side and diagonals of a rhombus: Side:"); scanf("%f",&s);
printf(" Diagonal :");
scanf("%f",&d1);
printf(" Diagonal :");
scanf("%f",&d2);
a=0.5*d1*d2;
p=4*s;
printf(" Perimeter of rhombus :%.3f units",p);
printf(" Area of rhombus :%.3f sq.units",a);
}
circl()
{
float r,a,p;
printf("Enter radius of circle:");
scanf("%f",&r);
a=PI * r * r;
p=2 * PI * r;
printf(" Circumference of circle : %.3f units",p);
printf(" Area of circle : %.3f sq.units",a);
}
para()
{
float a,p,base,h,l,b;
printf("Enter height,length,breadth of parallalogram :);
printf("Height :");
scanf("%f",&h);
printf("Base or Length :");
scanf("%f",&l);
printf("Breadth :");
scanf("%f",&b);
base=l;
a=base*h;
p=2 * ( l + b );
printf("Perimeter of parallalogram :%.3f units",p);
printf("Area of parallogram :%.3f sq.units",a);
}
tra()
{
float a,b,d,are;
printf("Enter height and lengths of two parallel sides:Height :");
scanf("%f",&d);
printf("Side:");
scanf("%f",&a);
printf("Side:");
scanf("%f",&b);
are=0.5 * d * (a+b);
printf("Area of trapezium : %.3f sq.units",are);
}
qua()
{
float a,b,area,d;
printf("Enter diagonal and perpendicular distances from opposite
vertices:
);
printf("Diagonal :");
scanf("%f",&d);
printf("Distance :");
scanf("%f",&a);
printf("Distance :");
scanf("%f",&b);
area= 0.5 * d * (a + b);
printf("Area of quadrilateral : %.3f sq.units", area);
}
semicir()
{
float a,p,r;
printf("Enter radius of semicircle:");
scanf("%f",&r);
a=0.5* PI * r * r;
p= (PI * r ) + (2 * r);
printf("Circumference of semicircle : %.3f units",p);
printf("Area of semicircle : %.3f sq.units",a);
}
msector()
{
float x,r,temp,a,p;
printf("Enter radius and angle of sector:");
printf("Radius :");
scanf("%f",&r);
printf("Angle(in degrees) :");
scanf("%f",&x);
temp= x/360;
a= temp * (PI * r * r);
p= temp * (2 * PI * r);
printf("Circumference of sector : %.3f units",p);
printf("Area of sector : %.3f sq.units",a);
}
/******** 3 DIMENSIONAL SHAPES *********/
sphere()
{
float lsa,tsa,v,r;
printf("Enter radius of sphere :");
scanf("%f",&r);
tsa=4*PI*r*r;
v=(4.0/3.0)*PI*r*r*r;
printf("Total surface area of sphere :%.3f sq.units",tsa);
printf("Volume of sphere :%.3f cu.units",v);
}
cone()
{
float h,r,s ,v,tsa,lsa;
printf("Enter base radius ,height, slant height of cone :");
printf("Radius :");
scanf("%f",&r);
printf("Height :");
scanf("%f",&h);
printf("Slant height :");
scanf("%f",&s);
tsa=PI * r *(s+r);
lsa=PI * r * s;
v=(PI * r * r * h)/3;
printf("Total surface area of cone :%.3f sq.units",tsa);
printf("Lateral surface area of cone :%.3f sq.units",lsa);
printf("Volume of cone :%.3f cu.units",v);
}
cyll()
{
float lsa,tsa,v,r,h;
printf("Enter height and radius of cyllinder");
printf("Height :");
scanf("%f",&h);
printf("Radius :");
scanf("%f",&r);
lsa=2*PI*r*h;
tsa=2*PI*r*(h+r);
v=PI*r*r*h;
printf("Total surface area of cyllinder :%.3f sq.units",tsa);
printf("Curved surface area of cyllinder :%.3f sq.units",lsa);
printf("Volume of cyllinder :%.3f cu.units",v);
}
cube()
{
float lsa,tsa,v,s,d;
printf("Enter side of cube :");
scanf("%f",&s);
d=s*sqrt(3);
lsa=4 * s * s;
tsa=6 * s * s;
v= s * s * s;
printf("Diagonal of cube :%.3f units",d);
printf("Total surface area of cube :%.3f sq.units",tsa);
printf("Lateral surface area of cube :%.3f sq.units",lsa);
printf("Volume of cube :%.3f cu.units",v);
}
cuboid()
{
float lsa,tsa,v,l,b,d,h;
printf("Enter length,breadth,height of cuboid :");
printf("Length :");
scanf("%f",&l);
printf("Breadth :");
scanf("%f",&b);
printf("Height :");
scanf("%f",&h);
d=sqrt(l*l + b*b + h*h );
lsa =2 * h *( l+b );
tsa = lsa + 2 * l * b;
v=l*b*h;
printf("Diagonal of cuboid :%.3f units",d);
printf("Total surface area of cuboid :%.3f sq.units",tsa);
printf("Lateral surface area of cuboid :%.3f sq.units",lsa);
printf("Volume of cuboid :%.3f cu.units",v);
}
hemisphe()
{
float lsa,tsa,v,r;
printf("Enter radius of hemisphere :");
scanf("%f",&r);
tsa=3*PI*r*r;
lsa=2*PI*r*r;
v=(2.0/3.0)*PI*r*r*r;
printf("Total surface area of hemisphere :%.3f sq.units",tsa);
printf("Lateral surface area of hemisphere :%.3f sq.units",lsa);
printf("Volume of hemisphere :%.3f cu.units",v);
}
#include "conio.h"
#include "math.h"
#define PI 3.14159
char ch;
main()
{
clrscr();
textcolor(4);
intro();
getch();
textcolor(7);
clrscr();
do
{
ch=menu();
switch(ch)
{
case 'a':
case 'A':
clrscr();
square();
getch();
break;
case 'b':
case 'B':
clrscr();
rect();
getch();
break;
case 'c':
case 'C':
clrscr();
circl();
getch();
break;
case 'd':
case 'D':
clrscr();
tri();
getch();
break;
case 'e':
case 'E':
clrscr();
rom();
getch();
break;
case 'f':
case 'F':
clrscr();
para();
getch();
break;
case 'g':
case 'G':
clrscr();
tra();
getch();
break;
case 'h':
case 'H':
clrscr();
qua();
getch();
break;
case 'i':
case 'I':
clrscr();
semicir();
getch();
break;
case 'j':
case 'J':
clrscr();
msector();
getch();
break;
case 'k':
case 'K':
clrscr();
sphere();
getch();
break;
case 'l':
case 'L':
clrscr();
cone();
getch();
break;
case 'm':
case 'M':
clrscr();
cyll();
getch();
break;
case 'n':
case 'N':
clrscr();
cube();
getch();
break;
case 'o':
case 'O':
clrscr();
cuboid();
getch();
break;
case 'p':
case 'P':
clrscr();
hemisphe();
getch();
break;
case 'q':
case 'Q':
exit(1);
}
} while(ch!='Q'||ch!='q');
getch();
}
intro()
{
int i;
clrscr();
printf(");
textcolor(2);
cprintf("#################################################################
###############");
textcolor(4);
printf("PROGRAM TO CALCULATE AREAS , VOLUMES ,CIRCUMFERENCES ");
printf("=====================================================
);
printf("OF VARIOUS GEOMETRIC SHAPES");
printf("===========================);
textcolor(2);
cprintf("#################################################################
###############");
getch();
printf("Program developed and designed by...");
printf("www.lernc.blogspot.com");
}
menu()
{
clrscr();
textcolor(7);
printf(" MENU Two Dimensional Shapes.
-----------------------
A.SQUARE
B.RECTANGLE
C.CIRCLE
D.TRIANGLE
E.RHOMBUS
F.PARALLELOGRAM
G.TRAPEZIUM
H.QUADRILATERAL.
I.SEMICERCLE
J.SECTOR
);
printf("Three Dimensional Shapes.
-------------------------
K.SPHERE
L.CONE
M.CYLLINDER
N.CUBE
O.CUBOID
P.HEMISPHERE
Q.QUIT
Enter Your Choice :");
scanf("%c",&ch);
return(ch);
}
/***** SUB FUNCTIONS *****/
/***** 2 D SHAPES *****/
square()
{
float s,a,p;int i,j;
printf("Enter side of square:");
scanf("%f",&s);
a=s*s;
p=4*s;
printf("Perimeter of square : %.3f units",p);
printf("Area of square : %.3f sq.units",a);
printf("Square is ...");
for(i=1;i<=s;i++)
{
textcolor(10);
for(j=1;j<=s;j++)
cprintf("ÛÛ");
printf(" ");
} return(0);
} rect()
{
float a,p,l,b;
int i,j;
printf(" Enter length and breadth of rectangle: Length:"); scanf("%f",&l);
printf(" Breadth:");
scanf("%f",&b);
a=l*b;
p=2*(l+b);
printf(" Perimeter of rectangle : %.3f units",p);
printf(" Area of rectangle : %.3f sq.units",a);
printf(" Rectangle is... ");
for(i=1;i<=b;i++)
{
textcolor(4);
for(j=1;j<=l;j++)
cprintf("ÛÛ");
printf(" ");
}
(0);
}
tri()
{
float area,p;
float a,b,c,s;
printf(" Enter three sides of triangle:");
scanf( "%f%f%f",&a,&b,&c);
p=a+b+c;
s=p/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
printf(" Perimeter of triangle : %.3f units",p);
printf(" Area of a triangle : %.3f sq.units",area);
} rom()
{
float s,d1,d2,a,p;
printf(" Enter side and diagonals of a rhombus: Side:"); scanf("%f",&s);
printf(" Diagonal :");
scanf("%f",&d1);
printf(" Diagonal :");
scanf("%f",&d2);
a=0.5*d1*d2;
p=4*s;
printf(" Perimeter of rhombus :%.3f units",p);
printf(" Area of rhombus :%.3f sq.units",a);
}
circl()
{
float r,a,p;
printf("Enter radius of circle:");
scanf("%f",&r);
a=PI * r * r;
p=2 * PI * r;
printf(" Circumference of circle : %.3f units",p);
printf(" Area of circle : %.3f sq.units",a);
}
para()
{
float a,p,base,h,l,b;
printf("Enter height,length,breadth of parallalogram :);
printf("Height :");
scanf("%f",&h);
printf("Base or Length :");
scanf("%f",&l);
printf("Breadth :");
scanf("%f",&b);
base=l;
a=base*h;
p=2 * ( l + b );
printf("Perimeter of parallalogram :%.3f units",p);
printf("Area of parallogram :%.3f sq.units",a);
}
tra()
{
float a,b,d,are;
printf("Enter height and lengths of two parallel sides:Height :");
scanf("%f",&d);
printf("Side:");
scanf("%f",&a);
printf("Side:");
scanf("%f",&b);
are=0.5 * d * (a+b);
printf("Area of trapezium : %.3f sq.units",are);
}
qua()
{
float a,b,area,d;
printf("Enter diagonal and perpendicular distances from opposite
vertices:
);
printf("Diagonal :");
scanf("%f",&d);
printf("Distance :");
scanf("%f",&a);
printf("Distance :");
scanf("%f",&b);
area= 0.5 * d * (a + b);
printf("Area of quadrilateral : %.3f sq.units", area);
}
semicir()
{
float a,p,r;
printf("Enter radius of semicircle:");
scanf("%f",&r);
a=0.5* PI * r * r;
p= (PI * r ) + (2 * r);
printf("Circumference of semicircle : %.3f units",p);
printf("Area of semicircle : %.3f sq.units",a);
}
msector()
{
float x,r,temp,a,p;
printf("Enter radius and angle of sector:");
printf("Radius :");
scanf("%f",&r);
printf("Angle(in degrees) :");
scanf("%f",&x);
temp= x/360;
a= temp * (PI * r * r);
p= temp * (2 * PI * r);
printf("Circumference of sector : %.3f units",p);
printf("Area of sector : %.3f sq.units",a);
}
/******** 3 DIMENSIONAL SHAPES *********/
sphere()
{
float lsa,tsa,v,r;
printf("Enter radius of sphere :");
scanf("%f",&r);
tsa=4*PI*r*r;
v=(4.0/3.0)*PI*r*r*r;
printf("Total surface area of sphere :%.3f sq.units",tsa);
printf("Volume of sphere :%.3f cu.units",v);
}
cone()
{
float h,r,s ,v,tsa,lsa;
printf("Enter base radius ,height, slant height of cone :");
printf("Radius :");
scanf("%f",&r);
printf("Height :");
scanf("%f",&h);
printf("Slant height :");
scanf("%f",&s);
tsa=PI * r *(s+r);
lsa=PI * r * s;
v=(PI * r * r * h)/3;
printf("Total surface area of cone :%.3f sq.units",tsa);
printf("Lateral surface area of cone :%.3f sq.units",lsa);
printf("Volume of cone :%.3f cu.units",v);
}
cyll()
{
float lsa,tsa,v,r,h;
printf("Enter height and radius of cyllinder");
printf("Height :");
scanf("%f",&h);
printf("Radius :");
scanf("%f",&r);
lsa=2*PI*r*h;
tsa=2*PI*r*(h+r);
v=PI*r*r*h;
printf("Total surface area of cyllinder :%.3f sq.units",tsa);
printf("Curved surface area of cyllinder :%.3f sq.units",lsa);
printf("Volume of cyllinder :%.3f cu.units",v);
}
cube()
{
float lsa,tsa,v,s,d;
printf("Enter side of cube :");
scanf("%f",&s);
d=s*sqrt(3);
lsa=4 * s * s;
tsa=6 * s * s;
v= s * s * s;
printf("Diagonal of cube :%.3f units",d);
printf("Total surface area of cube :%.3f sq.units",tsa);
printf("Lateral surface area of cube :%.3f sq.units",lsa);
printf("Volume of cube :%.3f cu.units",v);
}
cuboid()
{
float lsa,tsa,v,l,b,d,h;
printf("Enter length,breadth,height of cuboid :");
printf("Length :");
scanf("%f",&l);
printf("Breadth :");
scanf("%f",&b);
printf("Height :");
scanf("%f",&h);
d=sqrt(l*l + b*b + h*h );
lsa =2 * h *( l+b );
tsa = lsa + 2 * l * b;
v=l*b*h;
printf("Diagonal of cuboid :%.3f units",d);
printf("Total surface area of cuboid :%.3f sq.units",tsa);
printf("Lateral surface area of cuboid :%.3f sq.units",lsa);
printf("Volume of cuboid :%.3f cu.units",v);
}
hemisphe()
{
float lsa,tsa,v,r;
printf("Enter radius of hemisphere :");
scanf("%f",&r);
tsa=3*PI*r*r;
lsa=2*PI*r*r;
v=(2.0/3.0)*PI*r*r*r;
printf("Total surface area of hemisphere :%.3f sq.units",tsa);
printf("Lateral surface area of hemisphere :%.3f sq.units",lsa);
printf("Volume of hemisphere :%.3f cu.units",v);
}