Linux C++岗位笔记题目之二

  1. 软件设计
    (1) 基础编程
    采用泛型编程实现一个队列管理器,要求支持 uint8_t, uint16_t, uint32_t 等类
    型,维护空、满队列,实现数据的交换。
 1 #ifndef _QUEUE_H_
  2 #define _QUEUE_H_
  3 #include <iostream>
  4 using namespace std;
  5 
  6 template<typename T>
  7 class Node
  8 {   
  9     public:
 10         T data; 
 11         Node<T> *next;
 12 };
 13 
 14 template<typename T>
 15 class Queue
 16 {
 17     public:
 18         Queue()
 19         {   
 20             front = rear = nullptr;
 21             size = 0;
 22         }
 23 
 24         ~Queue()
 25         {   
 26             while (!IsEmpty())
 27             {   
 28                 Pop();
 29             }
 30         }
 31 
 32         void Push(const T& data)
 33         {   
 34             Node<T>* temp = new Node<T>;
 35             temp->data = data;
 36             temp->next = nullptr;
 37             if (IsEmpty())
 38             {
 39                 front = rear = temp;
 40             }
 41             else
 42             {
 43                 rear->next = temp;
 44                 rear = temp;
 45             }
 46             ++size;
 47         }
 48 
 49         void Pop()
 50         {
 51             if (IsEmpty())
 52             {
 53                 return;
 54             }
 55             Node<T>* temp = front;
 56             front = front->next;
 57             delete temp;
 58             --size;
 59         }
 60 
 61         void Swap(const T& data1, const T& data2)
 62         {
 63           cout << "enter function swap " << data1 << " " << data2<<std::endl;
 64           if(IsEmpty())
 65           {
 66             return;
 67           }
 68           Node<T>* tempFront = front;
 69           while(tempFront)
 70           {
71                cout<<tempFront->data<<std::endl;
 72                if(tempFront->data == data1)
 73                {
 74                   tempFront->data = data2;
 75                }
 76                tempFront = tempFront->next;
 77           }
 78           cout << "exit function swap" <<std::endl;
 79         }
 80 
 81         bool IsEmpty() const
 82         {
 83             return size == 0;
 84         }
 85 
 86         T Top()
 87         {
 88             return front->data;
 89         }
 90 
 91         T Botton()
 92         {
 93             return rear->data;
 94         }
 95 
 96     private:
 97         Node<T>* front;
 98         Node<T>* rear;
 99         int size;
100 };
101 
102 #endif

测试程序:

 1 #include "Queue.h"
  2 #include <iostream>
  3 using namespace std;
  4 int main()
  5 {
  6     Queue<int> queue;
  7     int x;
  8     int y = 0;
  9     while (cin >> x)
 10     {
 11         queue.Push(x);
 12         if(y++ > 5)
 13         {
 14            break;
 15         }
 16     }
 17     int a = 2;
 18     int b = 100;
 19     queue.swap(a, b);
 20     while (!queue.IsEmpty())
 21     {
 22         cout << queue.Top() << " ";
 23         queue.Pop();
 24     }
 25     cout << endl;
 26     //cin >> x;
 27     return 0;
 28 }
~                                                                                                                                                                                              
~                                                                                                                                                                                              
~                                                                                                                                                                                              
~                                                                                                                                                                                              
~                                                                                                                                                                                              
~                                                                                                                                                                                              
~                                                                                                                                                                                              
~                                                                                                                                                                                              
"main1.cpp" 28L, 386C                 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容