Showing posts with label Queue. Show all posts
Showing posts with label Queue. Show all posts

Thursday, June 11, 2015

Breadth First Search (BFS) in Graphs Algorithm Code in C++

#include<iostream.h>
#include<conio.h>
int cost[10][10],i,j,k,n;
int que[10],front,rare,v,visit[10],visited[10];
void main()
{
clrscr();
int m;
cout <<"Enter no of vertices\ Nodes: ";
cin >> n;
cout <<"\nEnter no of Edges: ";
cin >> m;
cout <<"\n\tEnter All EDGES by source & destination\n";
for(k=1;k<=m;k++)
{
cout<<"\nEnter Source: ";
cin >>i;
cout<<"\nEnter Destination: ";
cin>>j;
cost[i][j]=1;
cout<<"\n-----------------------\n";
}
cout <<"\nEnter initial vertex: ";
cin >>v;
cout <<"Visitied vertices:\n";
cout << v<<" ";
visited[v]=1;
k=1;
while(k<n)
{
for(j=1;j<=n;j++)
if(cost[v][j]!=0 && visited[j]!=1 && visit[j]!=1)
{
visit[j]=1;
que[rare++]=j;
}
v=que[front++];
cout<<v << " ";
k++;
visit[v]=0;
visited[v]=1;
}
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

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


Saturday, September 20, 2014

Data Structure Programs

Read Also:  


"All Data Structure Programs up-till now"

To get code Click on that program heading :) ☺ ☻ 

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

-------------------

`Queues 

----------------

`Single Link-List:

*  How to create single Link-list in C++

*  Insertion at any point in link-list in C++

*  Concatenate or Merge two Link-lists in C++

Deletion At any point in Circular Link List in C++
------------------------------------

`Double Link-list:

=================

Stacks Data Structure:


Decimal to binary conversion using stack in c++
============ 

How to add two matrix size is define by use






Program to print counting 10 to 1 using Recursion

--------------------------------------------------