Let's say I had a priority queue, Q, I'm trying to write an alogorithm that will implement the enqueue operation.
here is my enqueue...
Code:
void queue::enqueue(int number, int priority) {
bool placedElement = false; // determine whether or not the element was placed
qNode tempNode1;
qNode tempNode2;
int temp = 0;
/* if the queue is empty, just add the number to the front of the queue */
if (isEmpty()) {
q[temp].num = number;
q[temp].priority = priority;
size++;
placedElement = true;
}
else {
while (!placedElement) {
if(q[temp].priority > priority) {
if(q[temp].priority != DUMMY_PRIORITY) {
tempNode1 = q[temp];
q[temp].num = number;
q[temp].priority = priority;
size++;
for(int i = temp + 1 ; i <= size ; i++) { // move the elements up
tempNode2 = q[i];
q[i] = tempNode1;
tempNode1 = tempNode2;
}
}
else {
q[temp].num = number;
q[temp].priority = priority;
size++;
}
placedElement = true;
}
else {
temp++;
}
}
}
}
Also if I used Asymptotic notation, what would be the worst case time complextiy of this operation? Any ideas..?
Thanks