Sunday, April 19, 2009

binary searching

//* wap a program of binary searching *//

#include
#include
int BinarySearch(int Array[], int Value)
{
int First,Last,Mid;
First=0;
Last=9;
while(First<=Last)
{
Mid=(First+Last)/2;
if(Value==Array[Mid])
return(Mid);
else
if(Value Last=Mid-1;
else
if(Value>Array[Mid])
First=Mid+1;
}
return(-1);
}
void main()
{
int Array[10]={19,32,131,223,287,363,372,373,447,452};
int a,Value,pos;
clrscr();
printf("\n\n Ur List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);

printf("\n\n\n Which Value U want To Find : ");
scanf("%d",&Value);

pos=BinarySearch(Array,Value);

if(pos==-1)
printf("\n\n\n Value Does Not Exist");
else
printf("\n\n\n Value Found At Position %d",pos+1);

getch();
}

linear searching

//* wap a program of linear searching *//

#include
#include
int LinearSearch(int Array[], int Value)
{
int a;
for(a=0;a<10;a++)
if(Value==Array[a])
return(a);

return(-1);
}
void main()
{
int Array[10]={19,32,31,23,87,63,72,73,7,52};
int a,Value,pos;
clrscr();
printf("\n\n Ur List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);

printf("\n\n\n Which Value U want To Find : ");
scanf("%d",&Value);

pos=LinearSearch(Array,Value);

if(pos==-1)
printf("\n\n\n Value Does Not Exist");
else
printf("\n\n\n Value Found At Position %d",pos+1);

getch();
}

merge sorting

//* wap a program of merge sorting *//

#include
#include
void Sort(int Array[],int First,int Last)
{
int a,b,t;
for(a=First;a {
for(b=a+1; b<=Last;b++)
{
if(Array[a] > Array[b])
{
t=Array[a];
Array[a]=Array[b];
Array[b]=t;
}
}
}
}

void main()
{
int Array[10]={89,43,63,43,94,5,34,63,12,3};
int PairSize,Size=9,a;
clrscr();
printf("\n Ur Unsorted List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);
//-------------------------------------
PairSize=1;
do
{
PairSize=PairSize*2;
for(a=0;a {
if(a+PairSize<=Size)
Sort(Array,a,a+PairSize);
else
Sort(Array,a,Size);
}
}while(PairSize<=Size);


//-------------------------------------
printf("\n\n\n\n Ur Sorted List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);

getch();
}




insertion sorting

//* wap a program of insertion sorting *//

#include
#include
void main()
{
int Array[10]={89,43,63,43,94,5,34,63,12,3};
int a,b,t;
clrscr();
printf("\n Ur Unsorted List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);
//-------------------------------------

for(a=0;a<9;a++)
{
for(b=1+a; b>=1;b--)
{
if(Array[b] < Array[b-1])
{
t=Array[b];
Array[b]=Array[b-1];
Array[b-1]=t;
}
}
}

//-------------------------------------
printf("\n\n\n\n Ur Sorted List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);

getch();
}

bubble sorting

//* wap a program of bubble sorting *//

#include
#include
void main()
{
int Array[10]={89,43,63,43,94,5,34,63,12,3};
int a,b,t;
clrscr();
printf("\n Ur Unsorted List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);
//-------------------------------------

for(a=0;a<9;a++)
{
for(b=0; b<9-a;b++)
{
if(Array[b] > Array[b+1])
{
t=Array[b];
Array[b]=Array[b+1];
Array[b+1]=t;
}
}
}

//-------------------------------------
printf("\n\n\n\n Ur Sorted List : ");
for(a=0;a<10;a++)
printf(" %d",Array[a]);

getch();
}

selection sorting

//* wap a program of selection sort *//

#include
#include
void main()
{
int array[20];
int i,j,n,temp;
clrscr();
printf("enter size of an array ");
scanf("%d",&n);
for(i=0;i {
scanf("%d",&array[i]);
}
for(i=0;i {
for(j=i+1;j {
if(array[i]>=array[j])
{
temp=array[i];
array[i]=array[j];
array[j]=temp;
}
}
}
printf("\n\nsorted array is ........");
for(i=0;i {
printf("\n\n %d",array[i]);
}

getch();
}




push and pop operation using stack

//* wap a program of push and pop using stack * //


#include
#include
#include
void push(int x[],int *loc);
void pop(int x[],int *loc);
void peep(int x[],int *loc);
void main()
{
int x[5],loc=-1;
int choice;
clrscr();
while(1)
{
textmode(C40);
textcolor(6);
cprintf("\r\n\n 1 push ");
cprintf("\r\n 2 pop ");
cprintf("\r\n 3 peep ");
cprintf("\r\n 4 exit ");
textcolor(4);
cprintf("\r\n what option you want to do --> ");
scanf("%d",&choice);
switch(choice)
{

case 1: push(x,&loc);
break;

case 2: pop(x,&loc);
break;

case 3: peep(x,&loc);
break;

case 4: exit(0);

}
getch();
clrscr();
}
}
void push(int x[],int *loc)
{
if(*loc==4)
{
printf("\n stack is overflow...........");
return;
}
*loc=*loc+1;
textcolor(5);
cprintf("\n please enter a value --> ");
scanf("%d",&x[*loc]);
}
void pop(int x[],int *loc)
{
if(*loc==-1)
{
printf("\n stack is underflow...........");
return;
}
textcolor(BLUE);
cprintf("\n poped value is --> %d",x[*loc]);
*loc=*loc-1;
}
void peep(int x[],int *loc)
{
int a;
if(*loc==-1)
{
printf("\n stack is underflow...........");
return;
}
textcolor(7);
cprintf("\n your stack list is --> ");
for(a=0;a<=*loc;a++)
printf(" %d",x[a]);
}
//* wap a program of push and pop using stack * //


#include
#include
#include
void push(int x[],int *loc);
void pop(int x[],int *loc);
void peep(int x[],int *loc);
void main()
{
int x[5],loc=-1;
int choice;
clrscr();
while(1)
{
textmode(C40);
textcolor(6);
cprintf("\r\n\n 1 push ");
cprintf("\r\n 2 pop ");
cprintf("\r\n 3 peep ");
cprintf("\r\n 4 exit ");
textcolor(4);
cprintf("\r\n what option you want to do --> ");
scanf("%d",&choice);
switch(choice)
{

case 1: push(x,&loc);
break;

case 2: pop(x,&loc);
break;

case 3: peep(x,&loc);
break;

case 4: exit(0);

}
getch();
clrscr();
}
}
void push(int x[],int *loc)
{
if(*loc==4)
{
printf("\n stack is overflow...........");
return;
}
*loc=*loc+1;
textcolor(5);
cprintf("\n please enter a value --> ");
scanf("%d",&x[*loc]);
}
void pop(int x[],int *loc)
{
if(*loc==-1)
{
printf("\n stack is underflow...........");
return;
}
textcolor(BLUE);
cprintf("\n poped value is --> %d",x[*loc]);
*loc=*loc-1;
}
void peep(int x[],int *loc)
{
int a;
if(*loc==-1)
{
printf("\n stack is underflow...........");
return;
}
textcolor(7);
cprintf("\n your stack list is --> ");
for(a=0;a<=*loc;a++)
printf(" %d",x[a]);
}
//* c-program of a linklist *//

#include
#include
#include
#include
struct Product
{
char Name[20];
char Color[20];
int Price;
struct Product *Next;
};
void Insert(struct Product *Node);
void Delete(struct Product *Node);
void Traverse(struct Product *Node);
void main()
{
struct Product Start;
Start.Next=NULL;
int choice;
clrscr();
while(1)
{
printf("\n [1] Insert");
printf("\n [2] Delete");
printf("\n [3] Traverse");
printf("\n [4] Exit");
printf("\n What Op. U want to Do : ");
scanf("%d",&choice);
switch(choice)
{
case 1: Insert(&Start);
break;
case 2: Delete(&Start);
break;
case 3: Traverse(&Start);
break;
case 4: exit(0);
}
getch();
clrscr();
}

}
void Insert(struct Product *Node)
{
struct Product *Newnode;
Newnode=(struct Product*)malloc(sizeof(struct Product));
printf("\n Product Name : ");
scanf("%s",&Newnode->Name);
printf("\n Product Color : ");
scanf("%s",&Newnode->Color);
printf("\n Product Price : ");
scanf("%d",&Newnode->Price);
Newnode->Next=NULL;
while(Node->Next!=NULL)
Node=Node->Next;

Node->Next=Newnode;
}
void Delete(struct Product *Node)
{
struct Product *SLast;
if(Node->Next==NULL)
{
printf("\n Stack is UNDER-FLOW");
return;
}
while(Node->Next!=NULL)
{
SLast=Node;
Node=Node->Next;
}
printf("\n PRODUCT LIST");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6s","NAME","COLOR","PRICE");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6d",Node->Name,Node->Color,Node->Price);
printf("\n-----------------------------------");


SLast->Next=NULL;
free(Node);
}
void Traverse(struct Product *Node)
{
if(Node->Next==NULL)
{
printf("\n Stack is UNDER-FLOW");
return;
}

printf("\n PRODUCT LIST");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6s","NAME","COLOR","PRICE");
printf("\n-----------------------------------");
while(Node->Next!=NULL)
{
Node=Node->Next;
printf("\n %-12s %-12s %6d",Node->Name,Node->Color,Node->Price);
}
printf("\n-----------------------------------");
}

ds-program

//*program of a circular queue *//


#include
#include
#include
#include
struct Product
{
char Name[20];
char Color[20];
int Price;
struct Product *Next;
};
void Push(struct Product *Node);
void Pop(struct Product *Node);
void Peep(struct Product *Node);
void main()
{
struct Product Start;
Start.Next=NULL;
int choice;
clrscr();
while(1)
{
printf("\n [1] Push");
printf("\n [2] Pop");
printf("\n [3] Peep");
printf("\n [4] Exit");
printf("\n What Op. U want to Do : ");
scanf("%d",&choice);
switch(choice)
{
case 1: Push(&Start);
break;
case 2: Pop(&Start);
break;
case 3: Peep(&Start);
break;
case 4: exit(0);
}
getch();
clrscr();
}

}
void Push(struct Product *Node)
{
struct Product *Newnode,*First;


Newnode=(struct Product*)malloc(sizeof(struct Product));
printf("\n Product Name : ");
scanf("%s",&Newnode->Name);
printf("\n Product Color : ");
scanf("%s",&Newnode->Color);
printf("\n Product Price : ");
scanf("%d",&Newnode->Price);
if(Node->Next==NULL)
{
Node->Next=Newnode;
Newnode->Next=Newnode;
}
else
{
First=Node->Next;
Node=Node->Next;
while(Node->Next!=First)
Node=Node->Next;

Node->Next=Newnode;
Newnode->Next=First;
}
}
void Pop(struct Product *Node)
{
struct Product *First,*Second,*Start;
First=Node->Next;
if(Node->Next==NULL)
{
printf("\n Stack is UNDER-FLOW");
return;
}
else
if(First->Next==First)
{
Node->Next=NULL;
printf("\n PRODUCT LIST");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6s","NAME","COLOR","PRICE");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6d",First->Name,First->Color,First->Price);
printf("\n-----------------------------------");
free(First);

}
else
{
Start=Node;
First=Node->Next;
Second=First->Next;

Node=Node->Next;
while(Node->Next!=First)
{
Node=Node->Next;
}

Start->Next=Second;
Node->Next=Second;

printf("\n PRODUCT LIST");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6s","NAME","COLOR","PRICE");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6d",First->Name,First->Color,First->Price);
printf("\n-----------------------------------");
free(First);
}
}
void Peep(struct Product *Node)
{
if(Node->Next==NULL)
{
printf("\n Stack is UNDER-FLOW");
return;
}

printf("\n PRODUCT LIST");
printf("\n-----------------------------------");
printf("\n %-12s %-12s %6s","NAME","COLOR","PRICE");
printf("\n-----------------------------------");
while(1)
{
Node=Node->Next;
printf("\n %-12s %-12s %6d",Node->Name,Node->Color,Node->Price);
if(getch()==32)
break;

}
printf("\n-----------------------------------");
}