我的作业要求我确定以下算法的最坏情况运行时间。入队很容易。那只是恒定的(我希望,否则我会感到愚蠢)
第二个很混乱。我昨天问了一个类似的问题,得到了很多非常有用的答案。我会尽力而为。
Algorithm enqueue(o)
in stack.push(o)
Algorithm dequeue()
while (! in stack.isEmpty()) do // this just checks for an empty stack, so O(1)
out stack.push(in stack.pop()) // this do loop runs for as many times as values in the stack, so it is O(N)
if (out stack.isEmpty()) then
throw a QueueEmptyException // this is just an exception, so I assume it is O(1)
return_obj ← out stack.pop() // I think the rest of the program is linear.
// Although without actually coding it out, I'm not 100% sure I even understand what it does.
while (! out stack.isEmpty()) do
in stack.push(out stack.pop())
return return_obj;