Sunday, 5 October 2014

BINARY SEARCH

/*binary search*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int ar[10],value,size,i,flag,lower,middle,upper;
char choice;
cout<<"Enter the size ";
cin>>size;
cout<<"Enter the array\n";
for(i=1;i<=size;i++)
cin>>ar[i];
do{
flag=0;
cout<<"\nEnter search element ";
cin>>value;
lower=1;
upper=size;
while(lower<upper)
{
middle=(lower+upper)/2;
if(value>ar[middle])
lower=middle;
if(value<ar[middle])
upper=middle;

if(value==ar[middle])
{
cout<<"\nElement found ";
cout<<"Element postion "<<middle;
flag=1;
break;
}
 /*search start*/
}

LINEAR SEARCH

/*linear seacrh*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int ar[10],value,size,i,flag;
char choice;
cout<<"Enter the size ";
cin>>size;
cout<<"Enter the array\n";
for(i=1;i<=size;i++)
cin>>ar[i];
do{
flag=0;
cout<<"\nEnter search element ";
cin>>value;
 /*search start*/
for(i=1;i<=size;i++)
{
if(value==ar[i])
{
cout<<"\nElement found";
cout<<"\nElement postion ";
cout<<i;
flag=1;
}
 }
if(flag==0)
{
cout<<"\nElement not found ";     /*search end*/
}

cout<<"\nWant to continue press (1)  ";
cin>>choice;
}while(choice=='1');
}

MENU DRIVEN STACK

/*push and pop in stack*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int top=-1,size,value,stack[100],i,option;  /*variable initiation*/
char choice;
cout<<"\n Enter stack size ";
cin>>size;
do
{
cout<<"\n press 1 for push ";
cout<<"\n       2 for pop ";
cin>>option;
if(option==1)
{   if(top==size-1)
{ cout<<"\noverflow";
exit(0);
}
else
{        cout<<"\n Enter value ";
cin>>value;
top=top+1;
stack[top]=value;
cout<<"stack..................\n";
for(i=top;i>=0;i--)
cout<<stack[i]<<endl;
}
}
if(option==2)
{  if(top==-1)                                                                                           /*no element for push*/
{ cout<<"\nunderflow";
exit(0);
}
if(top==0)
{
cout<<"\n element deleted";
top=top-1;
cout<<"stack..................";
for(i=top;i>=0;i--)
cout<<stack[top]<<endl;
}

{
}

}
cout<<"\n\n\nDo u want to continue press 1 ";
cout<<"\nEnter your choice ";
cin>>choice;
}while(choice=='1');
}

PUSH IN STACK

/*push in stack*/
#include<iostream.h>
#include<conio.h>                    /*for clrscr*/
#include<stdlib.h>     /*for exit*/
void main()
{
     clrscr();
     int stack[10],top=-1,size,value;
     char choice;
     cout<<"Enter the size if element ";
     cin>>size;
do
{
  if(top==(size-1))
{  cout<<"overflow";
                   exit(0);
}
 cout<<"\nEnter the value ";
 cin>>value;
 ++top;
 stack[top]=value;
 for(choice=top;choice>=0;choice--)
 cout<<"\n\n\Your stack........... "<<stack[choice];
 cout<<endl;

cout<<"\n\n Do you want to continue press(1) ";
cin>>choice;

}while(choice=='1');
}

POP IN STACK

/*pop in stack*/
#include<iostream.h>
#include<conio.h>                    /*for clrscr*/
#include<stdlib.h>     /*for exit*/
void main()
{
clrscr();
int stack[]={1,2,3,4,5,6},top=5;
char choice;
        for(choice=top;choice>=0;choice--)
cout<<"\n\n\Your stack........... "<<stack[choice];
do
{
if(top==-1)
{  cout<<"underflow";
exit(0);
}
cout<<"\n D u want to pop element ";
cout<<"\nelement deleted ";
cout<<stack[top];
--top;
for(choice=top;choice>=0;choice--)
cout<<"\n\n\Your stack........... "<<stack[choice];
cout<<endl;
cout<<"\n\n Do you want to continue press(1) ";
cin>>choice;

}while(choice=='1');
}

MENU DRIVEN QUEUE

/*Menu driven queue*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hadder file*/
#include<stdlib.h>
void main()
{
clrscr();
int rear=-1,front=-1,size,value,queue[100],i,option;  /*variable initiation*/
char choice;
cout<<"\n Enter queue size ";
cin>>size;
do
{
cout<<"\n press 1 for push ";
cout<<"\n       2 for pop "<<"\nYour option is ";
cin>>option;
if(option==1)
{      if(rear==size-1)
{ cout<<"\n overflow";
exit(0);
}

if(rear==-1&&front==-1)
{ cout<<"\n Enter value ";
cin>>value;
++rear;
++front;
queue[rear]=value;
cout<<"queue..................";
for(i=rear;i>=front;i--)
cout<<queue[i];
}
else
{cout<<"\n Enter value ";
cin>>value;
++rear;
queue[rear]=value;
cout<<"queue..................";
for(i=rear;i>=front;i--)
cout<<queue[i];
}
}
if(option==2)
{  if(front==-1)
{        cout<<"\n Underflow";
exit(0);
}
if(front==rear)
front=rear=-1;
else
{
cout<<"\n element deleted ";
cout<<queue[front];
++front;
cout<<"queue..................";
for(i=rear;i>=front;i--)
cout<<queue[i];
}




}
cout<<"\n\n\nDo u want to continue press 1 ";
cout<<"\nEnter your choice ";
cin>>choice;
}while(choice=='1');
}

INSERTION IN QUEUE

/*Insertion in queue*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hedder file*/
#include<stdlib.h>
void main()
{
clrscr();
int rear=-1,front=-1,size,value,queue[100];  /*variable initiation*/
char choice;
cout<<"\n Enter queue size ";
cin>>size;
do
{

if(rear==(size-1))
 { cout<<"\n overflow";
exit(0);
}
if((front==-1)&&(rear==-1))
 {   front=rear=0;
cout<<"\n Enter value ";
cin>>value;
queue[rear]=value;
cout<<"queue..................\n";
for(choice=rear;choice>=0;choice--)
cout<<queue[choice]<<endl;
 }
else
{ ++rear;
cout<<"\n Enter value ";
cin>>value;
queue[rear]=value;
cout<<"queue..................\n";
for(choice=rear;choice>=0;choice--)
cout<<queue[choice]<<endl;
}

DELETION IN QUEUE

/*Deletion in queue*/
#include<iostream.h>
#include<conio.h>                 /*for clrscr hedder file*/
#include<stdlib.h>
void main()
{
clrscr();
int queue[]={1,2,3,4,5,6,7,8},rear=7,front=-1;  /*variable initiation*/
char choice;
do
{
if(front==-1&&rear==-1)
 { cout<<"\n underflow";
exit(0);
}
if(front==rear)
{          front=rear=-1;

 }
else
{    front=front+1;
cout<<"\n\n Elememt deleted...";
cout<<queue[front];
cout<<"\n\n Queue..................\n";
for(choice=rear;choice>=front;choice--)
cout<<queue[choice]<<endl;
}


cout<<"\n\n\nDo u want to continue press 1 ";
cout<<"\nEnter your choice ";
cin>>choice;
}while(choice=='1');
}