• 队列的链表实现 题目(难度1/10)


    C++数据结构与算法 目录

    队列介绍

    队列这种容器,就像大家排队上公交车一样。

    第一个来到的人排在最前面;

    最后来的排在最后面;

    第一个先上车(离开队列);

    队列的接口

    队列是有如下接口的容器:

    1. class Queue
    2. {
    3. public:
    4. const int& first(void) const;//队列的对头(第一个元素),最先进队列的元素
    5. inline bool empty(void) const;//判断队列里是否没有元素(空队列)
    6. inline size_t size(void) const;//返回队列元素数量
    7. void enqueue(const int& item);//将元素item进队列
    8. void dequeue(void);//将对头(队列的第一个元素)出队列(从队列中删除)
    9. void clear(void);//清空队列中的所有元素

    实现思路

    由于双链表的接口覆盖了队列的接口,所以可以使用双链表来实现一个队列。

    这样就可以在双链表外面封装出一个队列的类。

    在实现的时候,队列的接口只需要调用双链表的接口即可。

    题目如下

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. //------下面的代码是用来测试你的代码有没有问题的辅助代码,你无需关注------
    6. #include
    7. #include
    8. #include
    9. #include
    10. #include
    11. using namespace std;
    12. struct Record { Record(void* ptr1, size_t count1, const char* location1, int line1, bool is) :ptr(ptr1), count(count1), line(line1), is_array(is) { int i = 0; while ((location[i] = location1[i]) && i < 100) { ++i; } }void* ptr; size_t count; char location[100] = { 0 }; int line; bool is_array = false; bool not_use_right_delete = false; }; bool operator==(const Record& lhs, const Record& rhs) { return lhs.ptr == rhs.ptr; }std::vector myAllocStatistic; void* newFunctionImpl(std::size_t sz, char const* file, int line, bool is) { void* ptr = std::malloc(sz); myAllocStatistic.push_back({ ptr,sz, file, line , is }); return ptr; }void* operator new(std::size_t sz, char const* file, int line) { return newFunctionImpl(sz, file, line, false); }void* operator new [](std::size_t sz, char const* file, int line)
    13. {
    14. return newFunctionImpl(sz, file, line, true);
    15. }void operator delete(void* ptr) noexcept { Record item{ ptr, 0, "", 0, false }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); } }void operator delete[](void* ptr) noexcept { Record item{ ptr, 0, "", 0, true }; auto itr = std::find(myAllocStatistic.begin(), myAllocStatistic.end(), item); if (itr != myAllocStatistic.end()) { auto ind = std::distance(myAllocStatistic.begin(), itr); myAllocStatistic[ind].ptr = nullptr; if (!itr->is_array) { myAllocStatistic[ind].not_use_right_delete = true; } else { myAllocStatistic[ind].count = 0; }std::free(ptr); } }
    16. #define new new(__FILE__, __LINE__)
    17. struct MyStruct { void ReportMemoryLeak() { std::cout << "Memory leak report: " << std::endl; bool leak = false; for (auto& i : myAllocStatistic) { if (i.count != 0) { leak = true; std::cout << "leak count " << i.count << " Byte" << ", file " << i.location << ", line " << i.line; if (i.not_use_right_delete) { cout << ", not use right delete. "; } cout << std::endl; } }if (!leak) { cout << "No memory leak." << endl; } }~MyStruct() { ReportMemoryLeak(); } }; static MyStruct my; void check_do(bool b, int line = __LINE__) { if (b) { cout << "line:" << line << " Pass" << endl; } else { cout << "line:" << line << " Ohh! not passed!!!!!!!!!!!!!!!!!!!!!!!!!!!" << " " << endl; exit(0); } }
    18. #define check(msg) check_do(msg, __LINE__);
    19. //------上面的代码是用来测试你的代码有没有问题的辅助代码,你无需关注------
    20. class Queue
    21. {
    22. public:
    23. inline const int& first(void) const;
    24. inline bool empty(void) const;
    25. inline size_t size(void) const;
    26. void enqueue(const int& _item);
    27. void dequeue(void);
    28. void clear(void);
    29. private:
    30. //使用双链表实现队列
    31. std::list<int> m_queue;
    32. };
    33. inline const int& Queue::first(void) const
    34. {
    35. int a = 0;
    36. return a;
    37. }
    38. bool Queue::empty(void) const
    39. {
    40. return false;
    41. }
    42. void Queue::clear(void)
    43. {
    44. }
    45. size_t Queue::size(void) const
    46. {
    47. return -1;
    48. }
    49. void Queue::enqueue(const int& _item)
    50. {
    51. }
    52. void Queue::dequeue(void)
    53. {
    54. }
    55. int main(int argc, char** argv)
    56. {
    57. //test clear
    58. {
    59. Queue s;
    60. check(s.size() == 0);
    61. check(s.empty());
    62. s.enqueue(1);
    63. check(s.size() == 1);
    64. check(s.empty() == false);
    65. s.clear();
    66. check(s.size() == 0);
    67. check(s.empty());
    68. }
    69. //test first
    70. {
    71. Queue q;
    72. check(q.size() == 0);
    73. q.enqueue(1);
    74. check(q.size() == 1);
    75. auto q2 = q;
    76. check(q2.size() == 1);
    77. q = q2;
    78. q.enqueue(2);
    79. auto first = q2.first();
    80. check(first == 1);
    81. check(q.size() == 2);
    82. check(q.first() == 1);
    83. q.clear();
    84. check(q.size() == 0 && q.empty());
    85. }
    86. //test enqueue dequeue
    87. {
    88. Queue q;
    89. for (size_t i = 0; i < 10; i++)
    90. {
    91. q.enqueue(i);
    92. }
    93. int i = 0;
    94. while (!q.empty())
    95. {
    96. check(q.first() == i++)
    97. q.dequeue();
    98. }
    99. check(q.size() == 0 && q.empty());
    100. }
    101. }

    预期输出如下

    1. line:71 Pass
    2. line:72 Pass
    3. line:74 Pass
    4. line:75 Pass
    5. line:77 Pass
    6. line:78 Pass
    7. line:83 Pass
    8. line:85 Pass
    9. line:87 Pass
    10. line:91 Pass
    11. line:92 Pass
    12. line:93 Pass
    13. line:95 Pass
    14. line:107 Pass
    15. line:107 Pass
    16. line:107 Pass
    17. line:107 Pass
    18. line:107 Pass
    19. line:107 Pass
    20. line:107 Pass
    21. line:107 Pass
    22. line:107 Pass
    23. line:107 Pass
    24. line:110 Pass
    25. Memory leak report:
    26. No memory leak.
  • 相关阅读:
    HDFS HA 高可用集群搭建详细图文教程
    C#面试题: 寻找中间值
    深度学习入门(三十三)卷积神经网络——ResNet
    常见凭证获取方法
    Python-中北大学人工智能OpenCV人脸识别(根据图片训练数据,根据训练好的数据识别人脸)
    008:安装Docker
    AQS详解
    自动化测试和测试自动化你分的清楚吗?
    JS遍历对象的七种方法
    K8s安全一
  • 原文地址:https://blog.csdn.net/ClamReason/article/details/132600600