badge

C++ program to implement PRIORITY QUEUE PROGRAM

Program in c++ for PRIORITY QUEUE...


DESCRIPTION:- 

A priority queue is an abstract data type that captures the idea of a container whose elements have priorities attached to them.  An element of highest priority always appears at the front of the queue.   If that element  is removed, the next  highest priority  element  advances to the front. 


OUTPUT OF THE PROGRAM:-




SOURCE CODE:-

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

using namespace std;
int arr1[20];
char arr[20];
int front=-1,rear=-1;
int i,j;
class operator1
{
public:
int pre;
char ch;
};
operator1 opt;
void insert(int p,char num)
{
if(front==-1 && rear==-1)
{
rear++;
front++;
arr[rear]=num;
arr1[rear]=p;
}
else if(arr1[rear]>p)
{
for(i=front;i<=rear;i++)
{

if(arr1[i]>p)
{

break;
}
}
for(j=rear;j>=i;j--)
{
arr[j+1]=arr[j];
arr1[j+1]=arr1[j];
}
arr1[i]=p;
arr[i]=num;
rear++;
}
else
{
rear++;
arr[rear]=num;
arr1[rear]=p;

    }
}

void delete1()
{
front++;
}

void display()
{
int i;
cout<<"\nElements\tPrecedency\n";
for(i=front;i<=rear;i++)
{
cout<<arr[i]<<"\t\t"<<arr1[i]<<"\n";
}
}
int main()
{
int p,x,z,y=0;
char c;
cout<<"How many elements you want to insert";
cin>>x;
while(y<x)
{
cout<<"Enter Precedency";
cin>>opt.pre;
cout<<"Enter Character";
cin>>opt.ch;
p=opt.pre;
c=opt.ch;
insert(p,c);
display();
y++;
}
y=0;
cout<<"How many Character you want to delete";
cin>>z;
while(y<z)
{
delete1();
y++;
}
display();
}




/* IF U FOUND ANY PROBLEM IN THIS PROGRAM  OR IF YOU HAVE ANY QUERY RELATED THIS PROGRAM THEN PLEASE COMMENT IN THIS POST OR YOU CAN DIRECTLY MAIL ME ON piyushkhandelwal029@gmail.com . */

THANKU.



SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments: