gpt4 book ai didi

c - 如何确定这些操作的复杂度?

转载 作者:行者123 更新时间:2023-11-30 15:28:24 25 4
gpt4 key购买 nike

我已经使用这些日期结构实现了堆栈:

typedef struct node{
StackEntry entry;
struct node *next;
}Node;


typedef struct stack{
Node *top;
}Stack;

如何找到下面每个操作的 BigO?

  • StackEmpty(堆栈*s)

  • 堆栈大小(堆栈*s)

  • 推(节点 *e,堆栈 *s)

  • 弹出(节点*e,堆栈*s)

最佳答案

时间复杂度

  • StackEmpty(Stack *s) = O(1)//我们只需要检查 top== null或不

  • StackSize(Stack *s) = O(n)//我们需要弹出 n 次 count++如果堆栈包含 n 个元素,则直到 top== null

  • Push(Node *e, Stack *s) = O(1)//只需要一个操作,我认为不需要对此进行解释

  • Pop(Node *e, Stack *s) = O(1)//只需要一个操作,我认为不需要对此进行解释

<小时/>
Boolean StackEmpty(Stack *s)

{
if(s.Next==null&&s.value==null)
return true; //or 1
else
return false; //or 0
}

int StackSize(Stack *s)
{
if(StackEmpty!=true)
{
count = 0
while(StackEmpty!=true)
{
pop();
Count++;
}
}
return Count;
}

关于c - 如何确定这些操作的复杂度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26598346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com