avatar
a*r
1
For questions 1-4, assume that an array stores integers and has a maximum
size of 100. The array is used to store two distinct data structures, BOTH
a queue and a stack. The answer should be written in C or C++ and should
not use any existing data structure libraries.
1.Write a function that pushes an integer into the stack.
2.Write a function that pops an integer from the stack.
3.Write a function that enqueues an integer into the queue.
4.Write a function that dequeues an integer from the queue.
5.For the given C function, below, please answer the 3 questions below:
int Function()
{
int intArray[N];
int arraySum = 0;
for (int i = 0;i< N;i++)
{
intArray[i] = i;
For(int J = 1;j{
intArray[i] = J * intArray[
i]
}
}
For(int k = 0;k{
arraySum += intArray[k];
}
return arraySum;
}
a)Give the O(n) runtime complexity for the function.
b)Give an optimized version (if one exists) for the function.
c) Give the O(n) runtime complexity for the optimized version.
avatar
l*8
2
1-4说是要在一个数组上同时实现stack和queue吧?

BOTH

【在 a********r 的大作中提到】
: For questions 1-4, assume that an array stores integers and has a maximum
: size of 100. The array is used to store two distinct data structures, BOTH
: a queue and a stack. The answer should be written in C or C++ and should
: not use any existing data structure libraries.
: 1.Write a function that pushes an integer into the stack.
: 2.Write a function that pops an integer from the stack.
: 3.Write a function that enqueues an integer into the queue.
: 4.Write a function that dequeues an integer from the queue.
: 5.For the given C function, below, please answer the 3 questions below:
: int Function()

avatar
l*8
3
5. 原来程序O(N^2)
改成O(N),
int Function(int N)
{
int Factorial=1;
for (int j=1; jFactorial *= j;
return N*(N-1)/2 * Factorial ;
}
小心溢出
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。