Would these functions be considered O(1) (in terms of Big-O notation)?
1 2 3 4 5 6 7 8 9 10 11 12 13
void Stack::pop() throw(StackException)
{
if (isEmpty())
throw StackException("StackException: stack empty on pop");
else
{ // stack is not empty; delete top
StackNode *temp = topPtr;
topPtr = topPtr->next;
// return deleted node to system
temp->next = NULL; // safeguard
delete temp;
} // end if
} // end pop
1 2 3 4 5 6 7 8 9 10 11 12 13
void Queue::enqueue(const QueueItemType& newItem)
throw(QueueException)
{
if (count == MAX_QUEUE)
throw QueueException(
"QueueException: queue full on enqueue");
else
{ // queue is not full; insert item
back = (back+1) % MAX_QUEUE;
items[back] = newItem;
++count;
} // end if
} // end enqueue
If so, why? My understanding is that it would because it takes the same amount of time regardless of the size of the problem.
First is O(1)
Second is O(1) in case 'items' as an array. Of course, it 'items' is a class object with operator[] defined the complexity can be different