Saturday, December 13, 2014

Shell Sort code in C++

 Other Type Of Sorting : Click here :: most important ::


#include<iostream.h>
#include<conio.h>
 void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t"; }
 cout<<endl;
}

void shellsort(int *arr,int n)
{
 int gap,i,j,temp;
 for(gap=n/2;gap>0;gap/=2)
 {
 for(i=gap;i<=n;i++)
 {
 for(j=i-gap;j>=0 && arr[j]>arr[j+gap];j-=gap)
 {
 display(arr,n);
 temp=arr[j];
 arr[j]=arr[j+gap];
 arr[j+gap]=temp;
 }
 }
 }
}

void main()
{
int x[]={12,7,3,8,1,5,2};
clrscr();
display(x,6);
shellsort(x,6);
display(x,6);
getch();
}

Quick Sort code in C++

 Other Type Of Sorting : Click here :: most important ::


#include <iostream.h>
#include <stdlib.h>
#include <conio.h>
void display(int *x,int s)
{
cout<<endl;
for(int i=0;i<s;i++)
{
cout<<x[i]<<"  ";
}}


int partition(int* A, int p, int r)
{
 int pivot = r;
 int low = p;
 int high = r-1;
 while(low<high)
 {              display(A,9);
while(low<high && A[low]<A[pivot])
{
low++;
}
while(low<high && A[high]>A[pivot])
{
high--;
}
if(A[high]<A[low]){
int n=A[low];
A[low]=A[high];
A[high]=n;        }



 }   if(A[low]>A[pivot]) {
 int temp=A[low];
 A[low]=A[pivot];
 A[pivot]=temp;     }
 return low;
}
void quickSort(int* A, int p, int r)
{
if( p < r )
{
int q = partition(A, p, r);

quickSort(A, p, q-1);
quickSort(A, q+1, r);
}
}
void main()
{
clrscr();
int A[]={3,1,2,8,7,4,5,6,9};
quickSort(A, 0,8);

display(A,9);
getch();
}
---------------------------------
Another : -
----------------------------------
#include <iostream.h>
#include <conio.h>
void swap(int *a,int b,int c)
{
int temp=a[b];
a[b]=a[c];
a[c]=temp;
}
void display(int *a,int s)
{
cout<<endl;
for(int i=0;i<=s;i++){
cout<<a[i]<<"  ";  }
cout<<endl;
}

void quickSort(int *a,int,int);

int partition(int *, int,int);

void main()
{
    int A[] = {6,10,13,5,8,3,2,25,4,11};
    int p=0;
    int q=10;
    clrscr();
    cout<<"======Original======="<<endl;
    display(A,9);
    quickSort(A,p,q);
    cout<<"======Sorted======="<<endl;
    display(A,9);
    getch();
}


void quickSort(int  *A, int p,int q)
{
    int r;
    if(p<q)
    {   display(A,9);   getch();
    r=partition(A, p,q);
    quickSort(A,p,r);
    display(A,9);          getch();
    quickSort(A,r+1,q);
    display(A,9);
    }
}
int partition(int *A, int p,int q)
{
    int x= A[p];
    int i=p;
    int j;
    for(j=p+1; j<q; j++)
    {   display(A,9);  getch();
    if(A[j]<=x)
    {
        i=i+1;
        swap(A,i,j);
    }
    }
    swap(A,i,p);
    return i;
}

Selection Sort code in C++


 Other Type Of Sorting : Click here :: most important ::



#include<iostream.h>
#include<conio.h>
void display(int *arr,int size)
{
 cout<<endl;
 for(int i=0;i<=size;i++){
 cout<<arr[i]<<"\t";     }
}

int find_min_index(int *arr,int start,int n)
{
 int posmin=start;
 int index;
 for(index=start;index<=n;index++){
 display(arr,n);
 if(arr[index]<arr[posmin]){
 posmin=index;
 cout<<"\n Min index = "<<posmin<<"\t Value = "<<arr[posmin]<<"\n";
 }
 }
return posmin;
}

void selection_sort(int *arr,int n)
{
 int posmin,count,temp;
 for(count=0;count<=n;count++)
 {
 posmin=find_min_index(arr,count,n);
 temp=arr[posmin];
 arr[posmin]=arr[count];
 arr[count]=temp;
 }
}

void main()
{
int x[]={12,5,7,1,9,4,8};
clrscr();
selection_sort(x,6);
display(x,6);
getch();
}

Insertion Sort Code in C++


 Other Type Of Sorting : Click here :: most important ::



#include<iostream.h>
#include<conio.h>

void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t";  }
}

void insertion_sort(int *arr,int n)
{
 int pos,count,val;
 for(count=1;count<=n;count++)
 {
 val=arr[count];
 for(pos=count-1;pos>=0;pos--)
 {
 if(arr[pos]>val)     {
 cout<<"\n Swapped arr["<<pos+1<<"] with arr["<<pos<<"]\n";
 arr[pos+1]=arr[pos]; }
 else { break; };
 }
 arr[pos+1]=val;
 display(arr,6);
 }
}

void main()
{
int x[]={12,4,7,2,9,5,1};
clrscr();
display(x,6);
insertion_sort(x,6);
getch();
}


Bubble Sort Code in C++


 Other Type Of Sorting : Click here :: most important ::



#include<iostream.h>
#include<conio.h>

void display(int *arr,int n)
{
 cout<<endl;
 for(int i=0;i<=n;i++){
 cout<<arr[i]<<"\t";  }
}

void bubblesort(int *arr,int n)
{
 int i,temp,bound=n-1;
 int swapped=1;
 while(swapped>0)
 {
 swapped=0;
 for(i=0;i<=bound;i++){ display(arr,6);
 if(arr[i]>arr[i+1]){
  cout<<endl<<"Swaped arr["<<i<<"] with arr["<<i+1<<"]\n";
 temp=arr[i];
 arr[i]=arr[i+1];
 arr[i+1]=temp;
 swapped=i;          }}
 bound=swapped;
 }
}

void main()
{
int x[]={12,7,5,4,11,9,8};
clrscr();
bubblesort(x,6);
display(x,6);
getch();
}

Tuesday, December 2, 2014

Removing in Bineary Tree


Removing In Bineary Tree All 3 cases: 
1st case having no child (leaf node)
2nd case having one child (non leaf node)
3rd case having two childs :)
==========================================

#include<iostream.h>
#include<conio.h>
class treenode{
public:
int info;
treenode *left,*right;
treenode()
{ this->info=NULL;
  this->left=NULL;
  this->right=NULL;
}
treenode(int info)
{ this->info=info;
  this->left=NULL;
  this->right=NULL;
}
void setinfo(int info)
{ this->info=info; }
int getinfo()
{ return info; }
void setleft(treenode *left)
{  this->left=left; }
void setright(treenode *right)
{   this->right=right; }
treenode *getleft()
{ return left; }
treenode *getright()
{  return right; }

void insert(treenode *root,int info)
{
treenode *node=new treenode(info);
treenode *p,*q;
p=q=root;
while(info!=p->getinfo() && q!=NULL)
{
p=q;
if(info<p->getinfo())
{ q=p->getleft(); }
else
{  q=p->getright(); }
}
if(info==p->getinfo())
{ cout<<" \n Attempt to insert duplicate "<<info<<endl;
delete node;
}
else if(info<p->getinfo())
{ p->setleft(node); }
else
{p->setright(node); }
}

void preorder(treenode *root)
{
if(root!=NULL)
{ cout<<"\t"<<root->getinfo();
preorder(root->getleft());
preorder(root->getright());
}}
void postorder(treenode *root)
{
if(root!=NULL)
{
postorder(root->getleft());
postorder(root->getright());
cout<<"\t"<<root->getinfo();
}}
void inorder(treenode *root)
{
if(root!=NULL)
{
inorder(root->getleft());
cout<<"\t"<<root->getinfo();
inorder(root->getright());
}}

treenode *remove(treenode *tree,int info)
{
treenode *t;
int cmp=info-tree->getinfo();
if(cmp<0){
t=remove(tree->getleft(),info);
tree->setleft(t);
}
else if(cmp>0){
t=remove(tree->getright(),info);
tree->setright(t);
}
else if(tree->getleft()!=NULL && tree->getright()!=NULL)
{
treenode *minnode;
minnode=findmin(tree->getright());
tree->setinfo(minnode->getinfo());
t=remove(tree->getright(),minnode->getinfo());
tree->setright(t);
}
else
{ treenode *nodetodelete=tree;
  if(tree->getleft()==NULL)
  tree=tree->getright();
  else if(tree->getright()==NULL)
  tree=tree->getright();
  else{
  tree=NULL;
  delete nodetodelete;}
}
return tree;
}
treenode *findmin(treenode *tree)
{ if(tree==NULL)
 return NULL;
 if(tree->getleft()==NULL)
 return tree;
 return findmin(tree->getleft());
}

};
void main()
{
clrscr();
int x[]={14,15,4,8,7,3,5,2,1,9,6,-1};
treenode *root=new treenode();
root->setinfo(x[0]);
for(int i=1;x[i]>0;i++)
{ root->insert(root,x[i]);}
cout<<"\n\nDispaly ::Pre Order\n\n";
root->preorder(root);
cout<<"\n\n Display ::In Order\n\n";
root->inorder(root);
cout<<"\n\n Display ::Post Order\n\n";
root->postorder(root);
getch();
clrscr();
root->preorder(root);
cout<<"\n\n Removing leaf node     :: 1st case\n\n ";
root->remove(root,x[10]);
root->preorder(root);
cout<<"\n\n Removing Non leaf node :: 2nd case\n\n";
root->remove(root,x[6]);
root->preorder(root);
cout<<"\n\n Removing parent node [having two child] ::case 3\n\n";
root->remove(root,x[2]);
root->preorder(root);
getch();
}


Sunday, November 30, 2014

Heap Sort (min heap)

Sorting Using min heap and arrange in decending order 


 Other Type Of Sorting : Click here :: most important ::


#include<iostream.h>
#include<conio.h>
#define max 7
class array{
int arr[max];
int count;
public:
array()
{
count=0;
for(int i=0;i<max;i++)
{ arr[i]=0; }
}
void add(int num){
if(count<max){
arr[count]=num; count++;}
else
{cout<<"\n Array is full"<<endl;}
}
void heapsort()
{
for(int i=count-1;i>0;i--)
{int ivalue=arr[i];
arr[i]=arr[0];
arr[0]=ivalue;
makeheap(i);}
}
void display()
{
for(int i=0;i<count;i++)
{ cout<<arr[i]<<"\t";}
}
void makeheap(int c)
{
for(int i=0;i<c;i++)
{
 int val=arr[i];
int s=i;
int f=(s-1)/2;
while(s>0 && arr[f]>val)
{ arr[s]=arr[f];
s=f;
f=(s-1)/2;}
arr[s]=val;
}
}
};
void main()
{
clrscr();
array a;
a.add(15);
a.add(19);
a.add(18);
a.add(7);
a.add(17);
a.add(16);
a.add(8);
cout<<"\n\n Values you insert in min order heap\n \n";
a.display();
a.makeheap(7);
cout<<"\n\n Values inserted in Heap tree\n\n";
a.display();
a.heapsort();
cout<<"\n\n Values after heap sort (min order)( desecnding )\n\n";
a.display();
getch();
}



Tuesday, November 25, 2014

Trees in C++

Implementation of Binary Tree .... 
In-Order Traversal
Pre-Order Traversal
Post-Order Traversal



#include<iostream.h>
#include<conio.h>
class treenode{
public:
int info;
treenode *left,*right;
treenode()
{ this->info=NULL;
  this->left=NULL;
  this->right=NULL;
}
treenode(int info)
{ this->info=info;
  this->left=NULL;
  this->right=NULL;
}
void setinfo(int info)
{ this->info=info; }
int getinfo()
{ return info; }
void setleft(treenode *left)
{  this->left=left; }
void setright(treenode *right)
{   this->right=right; }
treenode *getleft()
{ return left; }
treenode *getright()
{  return right; }

void insert(treenode *root,int info)
{
treenode *node=new treenode(info);
treenode *p,*q;
p=q=root;
while(info!=p->getinfo() && q!=NULL)
{
p=q;
if(info<p->getinfo())
{ q=p->getleft(); }
else
{  q=p->getright(); }
}
if(info==p->getinfo())
{ cout<<" \n Attempt to insert duplicate "<<info<<endl;
delete node;
}
else if(info<p->getinfo())
{ p->setleft(node); }
else
{p->setright(node); }
}

void preorder(treenode *root)
{
if(root!=NULL)
{ cout<<"\t"<<root->getinfo();
preorder(root->getleft());
preorder(root->getright());
}}
void postorder(treenode *root)
{
if(root!=NULL)
{
postorder(root->getleft());
postorder(root->getright());
cout<<"\t"<<root->getinfo();
}}
void inorder(treenode *root)
{
if(root!=NULL)
{
inorder(root->getleft());
cout<<"\t"<<root->getinfo();
inorder(root->getright());
}}
};
void main()
{
clrscr();
int x[]={14,15,4,8,7,3,5,2,1,9,6,-1};
treenode *root=new treenode();
root->setinfo(x[0]);
for(int i=1;x[i]>0;i++)
{ root->insert(root,x[i]);}
cout<<"\n\nDispaly ::Pre Order\n\n";
root->preorder(root);
cout<<"\n\n Display ::In Order\n\n";
root->inorder(root);
cout<<"\n\n Display ::Post Order\n\n";
root->postorder(root);
getch();

}


Wednesday, November 5, 2014

Circular D - Queue Using Arrays

Implementation of Circular D-Queue (Double-Ended-Queue) in C++ Using Classes and Arrays :
-----------------------------------------------------
Assignment Question ☺ ☺


See Also:  Linear Double Ended Queue


#include<iostream.h>
#include<conio.h>
class d_queue{
private:
int rfront,rrear,lfront,lrear,max;
int *queue;
public:
d_queue(){ clrscr();
    lfront=lrear=-1;
   cout<<"\n\n\tEnter the size of Queue: ";
   cin>>max;
   for(int z=0;z<=max;z++){queue[z]=0;}
   rfront=max;rrear=max+1;
   }

void insert_left(int n)
{
   if(lfront==-1 && lrear==-1)
   { lfront=lrear=0;
     queue[lrear]=n;
   }
   else if( queue[lrear+1]==0  &&  lrear+1!=rrear)
   {  lrear=lrear+1;
      queue[lrear]=n;
   }
   else if(lrear+1==lfront)
   {cout<<"\n\n\n\t\t\a Error:\a\a Left side is Full \n";}
    else if(queue[lrear+1]!=0 && lfront==0)
   {   cout<<"\n\n\n\t\t\a\a\aError:  Left side is Full \n";  }
   else if( lfront!=0  && queue[lrear+1]!=0 || lrear+1==rrear)
   {   lrear=0;
       queue[lrear]=n;
    }

    else
    {   lrear=lrear+1;
        queue[lrear]=n;
    }

}
void insert_right(int n)
{

 if( queue[rrear-1]==0 && rrear-1!=lrear)
 {  rrear=rrear-1;
     queue[rrear]=n;
 }
 else if(rrear==rfront+1)
 { cout<<"\n\n\n\t\t\a\aError:  Right side is Full \n";}
 else if( queue[rrear-1]!=0/* || rrear-1==lrear*/ && rfront==max)
 {  cout<<"\n\n\n\t\t\a\a\aError: 1 Right side is Full \n ";  }

 else if(  queue[rrear-1]!=0 ||  rrear-1==lrear && rfront!=max)
 {  rrear=max;     cout<<"1";
    queue[rrear]=n;
 }

  else
  {
  rrear=rrear-1;
   queue[rrear]=n;
  }
  }
void delete_left()
{
  if(lfront==-1 && lrear==-1 && rrear==max)
  {  cout<<"\n\n\t\a\a Queue is Empty";  }
  else if(lrear==lfront)
  {queue[lfront]=0;
  lfront=lrear=-1;    }
  else if(lfront==rrear)
  {  lfront=0;       }
  else{
       cout<<"\n\t Item Deleted: "<<queue[lfront];
       queue[lfront]=0;
       lfront=lfront+1;
       }
}
void delete_right()
{
  if(rrear==max+1 && rfront==max+1 && lrear==-1)
  {  cout<<"\n\n\t\a Queue is Empty";   }
  else if(rrear==rfront)
  { queue[rfront]=0;
  rrear=rfront=max+1;  }
  else if( rfront==lrear)
  { rfront=max;   }
  else{
       cout<<"\n\t Item Deleted: "<<queue[rfront+1];
       queue[rfront]=0;
       rfront=rfront-1;
 }
}
void display()
{   {if(lrear+1==rrear && lfront==0 && rfront==max){gotoxy(5,8);cout<<"WARNING: \a\a\a  Dont try to insert any other value without deletion";}}

    for(int j=1;j<=max+1;j++){
 gotoxy(j*5,10);
 cout<<"   "<<j-1;
 gotoxy(j*5,11);
 cout<<"|````|";
 gotoxy(j*5,12);
 cout<<"|    |";
 gotoxy(j*5,13);
 cout<<"``````";
   }
for(int p=0,z=5;p<=max;p++,z=z+5){
  gotoxy(z+1,12);
  if(queue[p]==0){cout<<" ";}else{ cout<<queue[p];}
  if(p==rrear){gotoxy(z+1,14);cout<<"RR";}
  if(p==rfront){gotoxy(z+1,14);cout<<"RF";}
  if(p==lrear){gotoxy(z+1,14);cout<<"LR";}
  if(p==lfront){gotoxy(z+1,14);cout<<"LF";}
  }//for
  gotoxy(5,15);
  cout<<"\n\nRight Rear:  "<<rrear<<"\t\tLeft Rear:   "<<lrear;
  cout<<"\n\nRight Front: "<<rfront<<"\t\tLeft Front:  "<<lfront;

}  ~d_queue(){}
};
void main()
{
int a,value;
d_queue obj;
while(getch()!=27)
{
 cout<<"\n\n\t\t\tSelect  An  Option\n\n\n\t|`````````````````````````````|\n\n\t| 1  `   Insertion at Right   |\n\n\t| 2  `   Insertion at Left    |\n\n\t| 3  `   Deleletion at Right  |\n\n\t| 4  `   Deletion at Left     |\n\n\t| 5  `   Display              |\n\n\t```````````````````````````````";
 cin>>a;
   clrscr();
 switch(a)
 {
  case 1: {
  cout<<"\nRight Insertion`` Enter the Value : ";
  cin>>value;
  obj.insert_right(value); obj.display();
  break;
  }
  case 2: {
   cout<<"\nLeft Insertion`` Enter the value : ";
   cin>>value;
   obj.insert_left(value);
   obj.display();
   break;
   }
 case 3: {
   obj.delete_right();  obj.display();      break;
   }
 case 4: {
   obj.delete_left();    obj.display();     break;
   }
 case 5:{
  obj.display();             break;
  }
  }   //switch
  cout<<"\n\n\n\n\t Any Key=Continue  || Escap = Exit ";
 }   //while
}





|`````|`````|````|````|````|`````|`````|``````|`````|
|      |      |     |     |     |       |      |       |       |
`````````````````````````````````````````````````
  ^                 ^    ^                           ^
LF              LR  RR                        RF

Sunday, November 2, 2014

Create Foreign key in Sql

How To create Foreign key in sql table :

You have a code(for table)like this:

create table person( name     varchar2(5),
                               Id_no       number(3),
                              Address    number(10) );

and you have another table of place:

create table place(name   varchar2(5)primary key,
                             location  varchar2(5),
                             owner_name  varchar2(5) );

Now you want to make owner_name  as Foreign key which relate name of person to the owner name of place table. so there are two methods to make a column foreign key one in table order and other one is column order.
===================================================
Column  Order:
create table place(name              varchar2(5)    primary key,
                             location           varchar2(5),
                             owner_name   varchar2(5)   references   person(name) );
==================================================
Table Order:
create table place(name              varchar2(5)   primary key,
                             location           varchar2(5),
                             owner_name   varchar2(5),
                            constraint   n_pk    foreign key (owner_name)  references   person(name) );

Note:All the bold words are reserved word in Sql.

See Also :How to create Primary Key in data base table using Sql Command.


Create Primary Key in Sql table

How To create primary key in sql table :

You have a code(for table)like this:

create table person( name     varchar2(5),
                               Id_no       number(3),
                              Address    number(10));

Now you want to make Id_no as Primary key: so there are two methods to make a column primary key one in table order and other one is column order.
===================================================
Column  Order:
create table person( name    varchar2(5),
                               Id_no     number(3) primary key,
                              Address  number(10));
==================================================
Table Order:
create table person( name varchar2(5),
                               Id_no number(3),
                              Address number(10),
                             primary key(Id_no));

Create Table in Sql

How to create table in sql:-

The create table statement is used to create a new table.
Example:
create table employee
(first varchar(15),
 last varchar(20),
 age number(3),
 address varchar(30),
 city varchar(20),
 state varchar(20));
 
To create a new table, enter the keywords create table followed by the table name, followed by an open parenthesis, followed by the first column name, followed by the data type for that column, followed by any optional constraints, and followed by a closing parenthesis. It is important to make sure you use an open parenthesis before the beginning table, and a closing parenthesis after the end of the last column definition. Make sure you seperate each column definition with a comma. All SQL statements should end with a ";".

Here are the most common Data types:
char(size)Fixed-length character string. Size is specified in parenthesis. Max 255 bytes.
varchar(size)Variable-length character string. Max size is specified in parenthesis.
number(size)Number value with a max number of column digits specified in parenthesis.
dateDate value
number(size,d)Number value with a maximum number of digits of "size" total, with a maximum number of "d" digits to the right of the decimal.


See Also:   How To Create Primary Key in Sql table


See Also:  How to create Foreign Key Using Sql Command

Double-ended Queue in C++ Using Array

Implementation of Linear Double Ended Queue in C++ Using Arrays and Classes
Assignment Question ☺☺ 

See Also: Circular Double Ended Queue (circular D-Queue)
#include<iostream.h>
#include<conio.h>
#define Max 5
class D_Queue{
private:
int front,rfront,rrear,rear,max;
int d_queueary[Max];
public:
D_Queue()
{
clrscr();
front=-1,rfront=0;rear=-1,rrear=0;
//cout<<"\nEnter the size of D-Queue: ";
//cin>>max;
//d_queueary[max];
}
void insert_left(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
rear=rear+1;
d_queueary[rear]=n;
}
void delete_left()
{
if(rear==-1)
cout<<"\n Queue is Empty\n";
else
cout<<"\nDeleted Value = "<<d_queueary[rear];
rear=rear-1;
}
void insert_right(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
{
for(rrear=rear;rrear>=0;rrear--)
   {
   d_queueary[rrear+1]=d_queueary[rrear];
   }
d_queueary[0]=n;
rear=rear+1;
}
}
void delete_right()
{
if(rear==-1)
cout<<"\n Queue is Empty \n ";
else
{       cout<<"\nDeleted Value = "<<d_queueary[0];
for(rfront=0;rfront<=rear;rfront++)
 {
 d_queueary[rfront]=d_queueary[rfront+1];
 }
rear=rear-1;
}
}
void display()
{
cout<<"\npcphunt.blogspot.com\n";
for(int i=0;i<=rear;i++)
{
 cout<<"\nValue is = "<<d_queueary[i];
}
}
};
void main()
{
int value,a;
D_Queue obj;
while(getch()!=27)
{
cout<<"\n\n\t\t\tSelect  An  Option\n\n\n\t|`````````````````````````````|\n\n\t| 1  `   Insertion at Right   |\n\n\t| 2  `   Insertion at Left    |\n\n\t| 3  `   Deleletion at Right  |\n\n\t| 4  `   Deletion at Left     |\n\n\t| 5  `   Display              |\n\n\t```````````````````````````````";
cin>>a;
 clrscr();
switch(a)
{
case 1: {
cout<<"\nRight Insertion`` Enter the Value : ";
cin>>value;
obj.insert_right(value);
break;
}
case 2: {
cout<<"\nLeft Insertion`` Enter the value : ";
cin>>value;
obj.insert_left(value);
break;
}
case 3: {
obj.delete_right();        break;
}
case 4: {
obj.delete_left();         break;
}
case 5:{
obj.display();             break;
}
}   //switch
cout<<"\n Any Key=Continue  || Escap = Exit ";
}   //while

}

Copyright◘2014@pcphunt.blogspot.com






Here its the same code as in above but in this code i put some efforts to show how this circular queue works by shewing boxes and values in them . For batter understanding you must run this code. ☺


#include<iostream.h>
#include<conio.h>
//#define Max 5
class D_Queue{
private:
int front,rfront,rrear,rear,max;
int *d_queueary;
public:
D_Queue()
{
clrscr();
front=-1,rfront=0;rear=-1,rrear=0;
cout<<"\nEnter the size of D-Queue: ";
cin>>max;
//d_queueary[max];
}
void insert_right(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
rear=rear+1;
d_queueary[rear]=n;
}
void delete_right()
{
if(rear==-1)
cout<<"\n Queue is Empty\n";
else
cout<<"\nDeleted Value = "<<d_queueary[rear];
rear=rear-1;
}
void insert_left(int n)
{
if(rear==max)
cout<<"\n Sorry Queue is Full \n";
else
{
for(rrear=rear;rrear>=0;rrear--)
   {
   d_queueary[rrear+1]=d_queueary[rrear];
   }
d_queueary[0]=n;
rear=rear+1;
}
}
void delete_left()
{
if(rear==-1)
cout<<"\n Queue is Empty \n ";
else
{       cout<<"\nDeleted Value = "<<d_queueary[0];
for(rfront=0;rfront<=rear;rfront++)
 {
 d_queueary[rfront]=d_queueary[rfront+1];
 }
rear=rear-1;
}
}
void display()
{       for(int j=1;j<=max;j++){
gotoxy(j*5,11);
cout<<"|````|";
gotoxy(j*5,12);
cout<<"|    |";
gotoxy(j*5,13);
cout<<"``````";
 }
for(int i=0,z=5;i<=rear;i++)
{ gotoxy(z+1,12);
 z=z+5;
 cout<<d_queueary[i];
}
}
};
void main()
{
int value,a;
D_Queue obj;
while(getch()!=27)
{
cout<<"\n\n\t\t\tSelect  An  Option\n\n\n\t|`````````````````````````````|\n\n\t| 1  `   Insertion at Right   |\n\n\t| 2  `   Insertion at Left    |\n\n\t| 3  `   Deleletion at Right  |\n\n\t| 4  `   Deletion at Left     |\n\n\t| 5  `   Display              |\n\n\t```````````````````````````````";
cin>>a;
 clrscr();
switch(a)
{
case 1: {
cout<<"\nRight Insertion`` Enter the Value : ";
cin>>value;
obj.insert_right(value); obj.display();
break;
}
case 2: {
cout<<"\nLeft Insertion`` Enter the value : ";
cin>>value;
obj.insert_left(value);
obj.display();
break;
}
case 3: {
obj.delete_right();  obj.display();      break;
}
case 4: {
obj.delete_left();    obj.display();     break;
}
case 5:{
obj.display();             break;
}
}   //switch
cout<<"\n\n Any Key=Continue  || Escap = Exit ";
}   //while

}



Saturday, November 1, 2014

Priority Queue in C++

Implementation of  Priority Queue on the bases of Acceding Order in C++ Using Arrays and Classes 
Assignment Question: ☺☺☺

// copyright@2014 ☻ usman siddique 
//pcphunt.blogsopt.com

#include<iostream.h>
#include<conio.h>
class queue{
private:
int front,rear,max,i,temp;
int *queuearr;
public:
queue()
{
cout<<"\nEnter the size of Queue: ";
cin>>max;
i=temp=0;
front=0,rear=-1;
int *Queue=new int[max];
}
void insert(int n)
{
if(rear==max)
cout<<"\nQueue is full\n";
else
  {
   rear=rear+1;
   queuearr[rear]=n;
if(queuearr[rear]<queuearr[rear-1])       //This part of code check it
      {                                            //and arreange in accending order
for(i=rear;i!=0;i--)
{
if(queuearr[i]<queuearr[i-1])  {
temp=queuearr[i-1];
queuearr[i-1]=queuearr[i];
queuearr[i]=temp;              }  //2nd if
} //for
      }   //if
  }        //else
}
void Delete()
{
if(front==-1 && rear==-1)
cout<<"\nQueue is empty\n";
else
cout<<"\nThe Queue element "<<queuearr[front]<<" is Deleted\n";
for(front=0;front<rear;front++)    {
queuearr[front]=queuearr[front+1]; }rear=rear-1;
}
void display()
{
cout<<"\n\npcphunt.blogsopt.com\n\n";

for(int i=0;i<=rear;i++)
cout<<"\nvalue is :"<<queuearr[i];
}
};
void main()
{
clrscr();
int a=0,value=0;
queue obj;
while(getch()!=27)     //check while escap key press :)
{
cout<<"\n \n\n\t\tSelect an option :\n 1   for Insert\n 2   for Delete\n 3   for Display ";
cin>>a;
clrscr();
switch(a)
{
case 1:
{
cout<<"\nEnter value for index: ";
cin>>value;
obj.insert(value);
break;
}
case 2:
obj.Delete();
break;
case 3:
obj.display();
break;
}
cout<<"\n\n\t   || Press Esc Key to exit || Any Key For next Operation  ||";
}
}


Implementation of linear Queue in C++

Implementation of single[two ended],(simple),linear Queue in C++ using Arrays ans classes "

#include<iostream.h>
#include<conio.h>
class queue{
private:
int front,rear,max;
int *queuearr;
public:
queue()
{
cout<<"\nEnter the size of Queue: ";
cin>>max;
front=0,rear=-1;
int *Queue=new int[max];
}
void push(int n)
{
if(rear==max)
cout<<"\nQueue is full\n";
else
rear=rear+1;
queuearr[rear]=n;
}
void pop()
{
if(front==-1 && rear==-1)
cout<<"\nQueue is empty\n";
else
cout<<"\nThe Queue element "<<queuearr[front]<<" is poped\n";
for(front=0;front<rear;front++)    {
queuearr[front]=queuearr[front+1]; }rear=rear-1;
}
void display()
{

for(int i=0;i<=rear;i++)
cout<<"\nvalue is :"<<queuearr[i];
}
};
void main()
{
clrscr();
int a=0,value=0;
queue obj;
while(getch()!=27)
{
cout<<"\n \n\n\t\tSelect an option:\n 1   for Insert\n 2   for Delete\n 3   for Display ";
cin>>a;
clrscr();
switch(a)
{
case 1:
{
cout<<"\nEnter value for index: ";
cin>>value;
obj.push(value);
break;
}
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
}
cout<<"\n\n\t   || Press Esc Key to exit || Any Key For next Operation  ||";
}

}



Friday, October 31, 2014

Implementation Of Circular Queue in C++

Implementation Of Circular Queue in C++ Using Array 



#include<iostream.h>
#include<conio.h>
#define max 5
class queue{
private:
int front,rear;
int *queue;
public:
queue()
{   cout<<"\nThe size of Queue is Already = 5 or put 5 ";
     cin>>max;

  front=rear=-1;
}
void insert_rear(int n)
{
 if((front==0 && rear==max)||(front==(rear+1)))
 { cout<<"\nQueue is Full\n"; }
 else if(front==-1 && rear==-1)
    {  front=rear=0;
      queue[rear]=n;
    }
    else if ( rear==max && front>0)
    {  rear=0;
       queue[rear]=n;
    }
    else
    {  rear=rear+1;
       queue[rear]=n;
    }

}
void delete_front()
{
 if(front==-1 && rear==-1)
 { cout<<"\n Queue is Empty\n"; }
 else if(front==rear)
 {  front=rear=-1;  }
 else if(front==max)
 {  front=0;    }
 else
 {  front=front+1;
    cout<<"\nItem Deleted  = "<<queue[front-1];
 }
}
void display()
{
 int p;
 if(front==-1 && rear==-1)
 {  cout<<"\nQueue is Empty\n"; }
 else if(front<=max && rear<=front)
   {
 for(p=0;p<=rear;p++)  {
 cout<<queue[p]<<"   ";}
 for(p=front;p<=max;p++){
 cout<<queue[p]<<"   ";        }
 }
 else{
 for(p=front;p<=rear;p++)
 cout<<queue[p]<<"   ";         }
}
};
void main()
{
clrscr();
int a,b;
queue s;
while(getch()!=27)
{
cout<<"\n\t\t\"Select an Option\"\n\n\t||  1  |    Insert  ||\n\t||  2  |    Delete  ||\n\t||  3  |    Display ||";
cin>>a;
clrscr();
switch(a)
{
 case 1:{
cout<<"\nEnter value to be inserted:  ";
cin>>b;
s.insert_rear(b);    break;
}
 case 2:{
       s.delete_front();      break;
}
 case 3:{
       s.display();           break;
}
  }  //switch
   cout<<"\n\n\tAny key to Contenue || Esc to Exit\n";
 }  //while
}  //main



Here its the same code as in above but in this code i put some efforts to show how this circular queue works by shewing boxes and values in them . For batter understanding you must run this code. ☺


#include<iostream.h>
#include<conio.h>
class queue{
private:
int front,rear;
int *queue,max;
public:
queue()
{   cout<<"\nEnter the size of Queue =";
     cin>>max;

  front=rear=-1;
}
void insert_rear(int n)
{
 if((front==0 && rear==max)||(front==(rear+1)))
 { cout<<"\nQueue is Full\n"; }
 else if(front==-1 && rear==-1)
    {  front=rear=0;
      queue[rear]=n;
    }
    else if ( rear==max && front>0)
    {  rear=0;
       queue[rear]=n;
    }
    else
    {  rear=rear+1;
       queue[rear]=n;
    }

}
void delete_front()
{
 if(front==-1 && rear==-1)
 { cout<<"\n Queue is Empty\n"; }
 else if(front==rear)
 {  front=rear=-1;  }
 else if(front==max)
 {  front=0;    }
 else
 {  front=front+1;
    cout<<"\nItem Deleted  = "<<queue[front-1];
 }
}
void display()
{
 int p,z;
 for(int j=1;j<=max+1;j++){
gotoxy(j*5,11);cout<<"|````|";gotoxy(j*5,12);cout<<"|    |";
gotoxy(j*5,13);cout<<"``````";}
 if(front==-1 && rear==-1)
 {  cout<<"\nQueue is Empty\n"; }
 else if(front<=max && rear<=front)
   { z=5;
 for(p=0;p<=rear;p++,z+=5)  {
 gotoxy(z+1,12);cout<<queue[p];}
 z=(front+1)*5;for(p=front;p<=max;p++,z+=5){
 gotoxy(z+1,12);cout<<queue[p];        }
 }
 else{ z=(front+1)*5;
 for(p=front;p<=rear;p++,z+=5){
 gotoxy(z+1,12);cout<<queue[p];        } }
}
};
void main()
{
clrscr();
int a,b;
queue s;
while(getch()!=27)
{
cout<<"\n\t\t\"Select an Option\"\n\n\t||  1  |    Insert  ||\n\t||  2  |    Delete  ||\n\t||  3  |    Display ||";
cin>>a;
clrscr();
switch(a)
{
 case 1:{
cout<<"\nEnter value to be inserted:  ";
cin>>b;
s.insert_rear(b); s.display();   break;
}
 case 2:{
       s.delete_front();  s.display();    break;
}
 case 3:{
       s.display();       s.display();    break;
}
  }  //switch
   cout<<"\n\n\tAny key to Contenue || Esc to Exit\n";
 }  //while
}  //main