在C ++中实现优化版队列的代码是什么? [英] What would be the code for implement an optimized version of queue in C++?

查看:88
本文介绍了在C ++中实现优化版队列的代码是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个问题应该通过c ++解决,如果你编写代码会有所帮助。我也写了代码。但它没有运行。

This problem should be solved by c++ and it will be helpful if you write the code. i have also written the code. but it is not running.

#include <iostream>
#include <cassert>
#define MAX 5

using namespace std;

class queue{
public:
    queue();
    void Enqueue(int);
    int Dequeue();
    int size() const;
    bool isEmpty() const;

private:
    int arr[MAX];
    int sz;
    int front_idx,back_idx;
};

queue :: queue(){
    sz = 0;
    front_idx = 0;
    back_idx = 0;
}

void queue :: Enqueue(int val){
    if(sz == 5){
        //Queue Overflow
        assert("");
    }
    else{
        arr[back_idx] = val;
        sz++;
        back_idx = (back_idx + 1) % MAX;
    }
}

int queue :: Dequeue(){
    if(sz == 0){
        //Queue Underflow.
        assert("");
    }
    else{
        int ret_val = arr[front_idx];
        front_idx = (front_idx+1) % MAX;
        sz--;
        return ret_val;
    }
}

int queue :: size() const{
    return sz;
}

bool queue :: isEmpty() const{
    return (sz == 0);
}

int main()
{
    queue q;
    q.Enqueue(5);
    q.Enqueue(3);
    q.Enqueue(2);

    printf("%d\n",q.Dequeue());
    printf("%d\n",q.Dequeue());
    q.Enqueue(1);
    printf("%d\n",q.Dequeue());
    printf("%d\n",q.Dequeue());

}





我的尝试:



i试图排队,出队和显示但是代码没有运行。



What I have tried:

i have tried to enqueue , dequeue and for display also but the code is not running.

推荐答案

你可以从两个中选择一个选项:

1)我们不做你的功课:这是有原因的。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!



2)它不是那样的工作。

我们这样做不要为你工作。

如果你想让别人写你的代码,你必须付钱 - 我建议你去Freelancer.com并在那里问。



但要注意:你得到你付出的代价。支付花生,买猴子。



发展的概念就像这句话所暗示的那样:系统地运用科学和技术知识来满足特定的目标或要求。 BusinessDictionary.com [ ^ ]

这与有一个不一样快速谷歌并放弃,如果我找不到完全正确的代码。

所以要么付钱给别人去做,要么学会如何自己写。我们不是为你做的。
You can pick one from two options:
1) We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!

2) It doesn't quite work like that.
We do not do your work for you.
If you want someone to write your code, you have to pay - I suggest you go to Freelancer.com and ask there.

But be aware: you get what you pay for. Pay peanuts, get monkeys.

The idea of "development" is as the word suggests: "The systematic use of scientific and technical knowledge to meet specific objectives or requirements." BusinessDictionary.com[^]
That's not the same thing as "have a quick google and give up if I can't find exactly the right code".
So either pay someone to do it, or learn how to write it yourself. We aren't here to do it for you.


引用:

我也写了代码。但它没有运行。

i have also written the code. but it is not running.



首先,忘记优化并找到代码中的错误。

一个正确的慢代码更好而不是一个不正确的快速代码。

另有说法:在加速之前做好准备。

-----

Debuging :


First of all, forget about optimization and find what is wrong in your code.
A slow code that is correct is better than a fast code that is not correct.
Said otherwise: Make it right before making it fast.
-----
Debuging:

引用:

但它没有运行。



I在你的代码中看不到任何明显的东西,只是你对 const 的使用看起来很奇怪。



你的代码不按你期望的方式运行,你不明白为什么!



有一个几乎通用的解决方案:逐步在调试器上运行代码,检查变量。

调试器在这里向您展示您的代码正在做什么,您的任务是与它应该做什么进行比较。

调试器中没有魔法,它不知道你应该做什么,它没有发现错误,它只是帮助你通过向您展示正在发生的事情。当代码没有达到预期的效果时,你就接近了一个错误。

要查看你的代码在做什么:只需设置断点并查看代码是否正常运行,调试器允许你执行第1行第1行并在执行时检查变量。



此解决方案的缺点:

- 这是一个DIY,你是跟踪问题并找到根源的那个,这导致了解决方案。

这个解决方案的优点:

- 它也是一个很好的学习工具,因为它告诉你现实,你可以看到哪种期望与现实相符。



次要效果

- 你会为自己找到虫子感到自豪。

- 你的学习技巧会提高。



你应该很快就会发现什么是错的。



调试器 - 维基百科,免费的百科全书 [ ^ ]



掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]

使用Visual Studio 2010进行基本调试 - YouTube [ ^ ]

这里的调试器只显示你的代码正在做什么,你的任务是与它应该做什么进行比较。

-----

优化:

队列非常简单,几乎没有优化的地方。

当你考虑优化时,你需要学习您想要优化的算法和其他替代方案。良好的算法知识是一个很好的帮助。

队列(抽象数据类型) - 维基百科 [ ^ ]

有一个工具可以帮助您优化代码,它的名称是 profiler ,它可以帮助您找到您的代码花费时间的位置。它可以帮助您查看优化是否有效。

分析(计算机编程) - 维基百科 [ ^ ]

我看到的唯一优化是


I don't see anything obvious on your code, just your usage of const look weird to me.

Your code do not behave the way you expect, and you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

The downside of this solution:
- It is a DIY, you are the one tracking the problem and finding its roots, which lead to the solution.
The upside of this solution:
- It is also a great learning tool because it show you reality and you can see which expectation match reality.

secondary effects
- Your will be proud of finding bugs yourself.
- Your learning skills will improve.

You should find pretty quickly what is wrong.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]
The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Optimization:
A queue is so simple that there is very little place for optimization.
When you think about optimization, you need to study the algorithm you want to optimize and irs alternatives. A good knowledge of algorithms is a great help.
Queue (abstract data type) - Wikipedia[^]
there is a tool that can help you to optimize code, its name is profiler, it help you to spot where your code spend time. It help you to see if an optimization is efficient or not.
Profiling (computer programming) - Wikipedia[^]
The only optimization I see is on

back_idx = (back_idx + 1) % MAX;



%成本与分部相同。测试back_idx是否达到MAX更有效率,在这种情况下将其重置为零。

此优化非常小,不会在运行时更改任何内容。


The % cost as much as a division. It is more efficient to test if back_idx reached MAX and in this case reset it to zero.
This optimization is very small and will not change anything in its runtime.


这篇关于在C ++中实现优化版队列的代码是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆