Skip to content

Instantly share code, notes, and snippets.

Created June 20, 2015 14:42
Show Gist options
  • Save anonymous/aa85cb2804c934ab0625 to your computer and use it in GitHub Desktop.
Save anonymous/aa85cb2804c934ab0625 to your computer and use it in GitHub Desktop.
#include <string.h>
#include <string>
using namespace std;
template <class T>
class PQ
{
private:
T value;
int Priority;
static int number_of_objects_in_Q;
PQ* start;
public:
static void inc_number_of_objects(){ number_of_objects_in_Q += 1; }
static int get_number_of_objects(){ return number_of_objects_in_Q; }
PQ();// ctor
PQ(T, int); //ctor2
// preferably to not call the ctor directly but to use enqueue.
enqueue(T value_wanted, int P);
};
template <class T>
PQ<T>::number_of_objects_in_Q(0); //default start
template <class T>
PQ<T>::PQ()
{
//CTOR
value = 0;
Priority = 0;
//number_of_objects_in_Q = 0;
//PQNode * pointer = new PQNode;
//start = new PQNode;
//start->next = NULL;
//start->prev = NULL;
inc_number_of_objects();
}
template <class T>
PQ<T>::PQ(T value_wanted, int P)
{
//CTOR 2
value = value_wanted;
Priority = P;
//start = new PQNode;
//start->next = NULL;
//start->prev = NULL;
inc_number_of_objects();
}
template <class T>
PQ<T>::enqueue(T value_wanted, int P)
{
value = value_wanted;
Priority = P;
inc_number_of_objects();
{
if (!get_number_of_objects()) //incase number of objects is zero
{
//value = value_wanted;
//Priority = P;
start = new PQNode;
start->next = NULL;
start->prev = NULL;
start->current = this;
//inc_number_of_objects();
}
// else
PQ * temp = start;
while ( temp.Priority<P ) //stops when temp->next is zero/NULL or when the priorty of current item is less then the next one
{
if (temp->next == NULL) break;
temp = temp->next;
}
//P=Priority determine where to insert the new item in Queue. bigger P means higher level of priority.
if ( temp->next != NULL) // inserting in the middle of the Queue
{
new_node = new PQNode;
new_node->current = this;
//PQ * remember = temp->prev->next;
temp->perv->next= new_node;
new_node->next = temp;
new_node->prev = temp->prev;
temp->prev = new_node;
}
else //inserting in the end of the Queue,and Queue has more then one item in.
{
new_node = new PQNode;
temp->next = new_node;
new_node->prev = temp;
}
}
}
template <class T>
struct PQNode
{
PQ * next;
PQ * prev;
PQ * current;
};
void main()
{
PQ <string>.enqueue(string("I need dollar , dollar is what I need"), 1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment