// P_Que.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Node{
public:
int data;
int prio;
Node *next;
Node *previous;
Node(){
data=0;
prio=0;
next=NULL;
previous=NULL;
}
};
class P_Que{
public:
Node *first;
Node* last;
Node *current;
P_Que(){
first=NULL;
last=NULL;
current=NULL;
}
void enque(int n,int p){
if(first==NULL){
Node *temp= new Node;
temp->data=n;
temp->prio=p;
temp->next=NULL;
temp->previous=NULL;
first=temp;
last=temp;
}
else{
current=first;
Node *temp =new Node;
temp->data=n;
temp->prio=p;
while(current->next!=NULL){
if(temp->prio>current->prio){
temp->next=current;
temp->previous=current->previous;
current=current->next;
}
}
}
}
void display(){
current=first;
while(current->next!=NULL){
cout<<current->data<<endl;
current=current->next;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
P_Que q;
int option,n,p;
cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;
while(option!='e'){
switch(option){
case 1:
cout<<"\nEnter the data you want to enque: ";
cin>>n;
cout<<"\nEnter it's priority: ";
cin>> p;
q.enque(n,p);
break;
case 2:
q.display();
break;
}
cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;
}
return 0;
}
//
#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
class Node{
public:
int data;
int prio;
Node *next;
Node *previous;
Node(){
data=0;
prio=0;
next=NULL;
previous=NULL;
}
};
class P_Que{
public:
Node *first;
Node* last;
Node *current;
P_Que(){
first=NULL;
last=NULL;
current=NULL;
}
void enque(int n,int p){
if(first==NULL){
Node *temp= new Node;
temp->data=n;
temp->prio=p;
temp->next=NULL;
temp->previous=NULL;
first=temp;
last=temp;
}
else{
current=first;
Node *temp =new Node;
temp->data=n;
temp->prio=p;
while(current->next!=NULL){
if(temp->prio>current->prio){
temp->next=current;
temp->previous=current->previous;
current=current->next;
}
}
}
}
void display(){
current=first;
while(current->next!=NULL){
cout<<current->data<<endl;
current=current->next;
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
P_Que q;
int option,n,p;
cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;
while(option!='e'){
switch(option){
case 1:
cout<<"\nEnter the data you want to enque: ";
cin>>n;
cout<<"\nEnter it's priority: ";
cin>> p;
q.enque(n,p);
break;
case 2:
q.display();
break;
}
cout<<"\nSelect an option:\n1: Enque()\n2:Display Que\nEnter 'e' to exit.\n";
cin>> option;
}
return 0;
}
No comments:
Post a Comment