Thursday 27 September 2012

Program of Circular Queue using c++

//PROGRAM FOR CIRCULAR QUEUE

#include<iostream.h>
#include<conio.h>
#include<process.h>
#define MAX 10
void main()
{
clrscr();
char ch;
int front=-1,rear=-1,temp,n,q[MAX],i,num;
do
{
cout<<"1.to insert value in queue";
cout<<"\n2.to delete value from queue";
cout<<"\n3.to traverse elements of queue";
cout<<"\n4.exit";
cout<<"\nenter your choice...";
cin>>n;
switch(n)
{
case 1:
{
cout<<"\n enter number you want to insert";
cin>>num;
if(front==-1)
{
front=rear=0;
q[rear]=num;
}
else if((front==0)&&(rear==MAX)||(front==rear+1))
{
cout<<"\nqueue is full or overflow";
break;
}
else if((rear==MAX)&&(front!=0))
{
rear=0;
q[rear]=num;
}
else
{
rear++;
q[rear]=num;
}
}
break;
case 2:
{
if(front==-1)
{
cout<<"\n no element or underflow"  ;
break;
}
else if(front==rear)
{
temp=q[front];
cout<<"\n number deleted is="<<temp ;
front=rear=-1;
}
else if(front==MAX)
{
temp=q[front];
cout<<"\n number deleted is="<<temp;
//q[front]=null;
front=0;
}
else
{
temp=q[front];
cout<<"\n number deleted is="<<temp;

front++;
}
}
break;
case 3:
{
if(front<=rear)
{
for(i=front;i<=rear;i++)
{
cout<<"\t"<<q[i];
}
}
else
{
for(i=front;i<=MAX;i++)
{
cout<<"\t"<<q[i];
}
for(i=0;i<=rear;i++)
{
cout<<"\t"<<q[i];
}
}
}
break;
case 4:
{
cout<<    "\n ";
for(long int r=50000;r>0;r--)
{
clrscr();
cout<<"it will take 5 seconds"<<"\t";
if(r==1)
{
exit(1);
}
}
}
break;
}
cout<<"\n press y to play more with queue otherwise n===";
cin>>ch;
}while(ch=='y');
 getch();
}

Wednesday 26 September 2012

MERGING TWO LINKED LIST WITH PRINT EVEN LOCATION OF LINKED LIST

#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int i,n,d;
struct node *ptr1,*temp,*ptr,*start=NULL,*start1=NULL;
temp=new node;
temp->data=10;
temp->link=NULL;
start=temp;
cout<<"How many node do u want in 1st lined list:-";
cin>>n;
cout<<"Enter data in first linked list"<<endl;
for(i=1;i<=n;i++)
{
cin>>d;
 temp=new node;
temp->data=d;
temp->link=start;
start=temp;
}

cout<<"data is:-"<<endl;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<" "<<temp->data<<" ";
}
//-----------------------------------------------

temp=new node;
temp->data=10;
temp->link=NULL;
start1=temp;
cout<<endl<<"How many node do u want in 2nd linked list:-";
cin>>n;
cout<<"Enter data in first linked list"<<endl;
for(i=1;i<=n;i++)
{
cin>>d;
 temp=new node;
temp->data=d;
temp->link=start1;
start1=temp;
}

cout<<"data is:-"<<endl;
for(temp=start1;temp!=NULL;temp=temp->link)
{
cout<<" "<<temp->data<<" ";
}


cout<<endl<<"After merging of two linked list"<<endl;
        ptr1=start;
while(ptr1->link!=NULL)
{
ptr1=ptr1->link;
}
ptr1->link=start1;
ptr1=start;
while(ptr1!=NULL)
{
cout<<" "<<ptr1->data<<" ";
ptr1=ptr1->link;
}


cout<<endl<<"data print at even node "<<endl;


temp=start;
while(temp!=NULL)
{
for(i=1;i<=2;i++)
{
ptr=temp;
temp=temp->link;
}
cout<<" "<<ptr->data<<" ";
}

getch();
}

Print data at only even location in single linked list

#include<conio.h>
#include<iostream.h>
struct node
{
int data;
struct node *link;
};
void main()
{
int i,n,d;
struct node *temp,*ptr,*start=NULL;
temp=new node;
temp->data=10;
temp->link=NULL;
start=temp;
cout<<"How many node do u want:-";
cin>>n;
for(i=1;i<=n;i++)
{
cin>>d;
 temp=new node;
temp->data=d;
temp->link=start;
start=temp;
}

cout<<"data is:-"<<endl;
for(temp=start;temp!=NULL;temp=temp->link)
{
cout<<" "<<temp->data<<" ";
}

cout<<endl<<"data print at only  even location "<<endl;


temp=start;
while(temp!=NULL)
{
for(i=1;i<=2;i++)
{
ptr=temp;
temp=temp->link;
}
cout<<" "<<ptr->data<<" ";
}

getch();
}

Tuesday 25 September 2012

PUSH, POP and Peek operation in stack using array


#include<iostream.h>
#include<conio.h>
#include<process.h>
#define max 20
int top;
class stack
{
               int a[max],n,item;
               public:
               stack()
                {
                top=0;
                }
               void initial_stack();
               void push(int);
               int pop();
               void display();
};
               void stack::initial_stack()
               {
               char c;
               cout<<"\nDo you want initialy elements inserted in the stack(y/n):-";
               cin>>c;
                               if(c=='y')
                               {
                               cout<<"How many elements you want initialy in the stack"<<endl;
                               cin>>n;
                               cout<<"enter elements\n";
                                              for(int i=1;i<=n;i++)
                                              {
                                              top=top+1;
                                              cin>>item;
                                              a[top]=item;
                                              }
                               }
                }

               void stack::push(int m)
                {
                               if(top==max)
                               {
                               cout<<"\nCONDITION OF OVERFLOW";
                               getch();
                               exit(1);
                               }
                               else
                               {
                               top=top+1;
                               a[top]=m;
                               }
                }
               int stack::pop()
                {
                                if(top==0)
                                {
                                cout<<"\nCONDITION OF UNDERFLOW";
                                getch();
                                exit(1);
                                }
                               else
                                {
                                item=a[top];
                                top=top-1;
                                }
                               return item;
                }
               void stack::display()
                {
                               cout<<"\nTop ="<<top;
                               cout<<"\nstack["<<top<<"]="<<a[top];
                }
void main()
{
clrscr();
               stack obj;
               char ch='y';
               int choice,element;
               obj.initial_stack();
               while(ch=='y')
                {
               cout<<"What you want to do :-Enter ur choice"<<endl;
               cout<<"1.Push in stack\n2.Pop from stack\n";
               cout<<"3.Display(peek) the TOP of stack\n4.exit";
               cin>>choice;
               switch(choice)
               {
               case 1:
                               cout<<"enter the element to push"<<endl;
                               cin>>element;
                               obj.push(element);
                               cout<<"After push:-"<<endl;
                               obj.display();
               break;
               case 2:
                               element=obj.pop();
                               cout<<"After pop of"<<element<<"from"<<top+1<<"location:-"<<endl;
                               obj.display();
               break;
               case 3:
                               obj.display();
               break;
               default:
                               exit(1);

               }
cout<<endl<<"Do  you  want to continue(y/n)";
cin>>ch;
                }
getch();

}

wap to search element from array using Binary Search


#include<iostream.h>
#include<conio.h>
 void main()
 {
 int n,a[20],i,item;
 clrscr();
 cout<<"Enter the size of array :-";
 cin>>n;
 int beg=1,end=n,mid=(beg+end)/2;
 cout<<"Enter the elements of the array in sorted form :-";
 for(i=1;i<=n;i++)
            cin>>a[i];
 cout<<"\nEnter the element you  want to search:-"<<endl;
 cin>>item;
 while(a[mid]!=item && beg!=end)
             {
              if(a[mid]<item)
                        beg=mid+1;
             else
              end=mid-1;

            mid=(beg+end)/2;
             }
 if(a[mid]==item)
 cout<<"\n"<<item<<" is found at the location:- "<<mid ;
 else
 cout<<"item does not found in list";
getch();
}