- 
            
            #include<iostream> using namespace std; class Myqueue { public: Myqueue(int capcaity);//初始化队列 ~Myqueue(); int get_length(); //获取队列元素个数 bool judge_empty(); //判断队列是否为空 bool judge_full(); //判断队列是否为满 bool in_ele(int x); //入栈 bool out_ele(int &x); //出栈 void tra_ele(); //遍历 void clear_queue(); //清空 private: int m_length; //队列元素个数 int m_capacity; // 队列元素容量 int m_head; //队列首部元素编号 int m_tail; // 队列尾部元素编号 int *my_queue; //定义队列指针 }; Myqueue::Myqueue(int capacity) { m_capacity = capacity; my_queue = new int[m_capacity]; if (my_queue == NULL) { cout << "the applicatiao ofstorage for my_queue is failed" << endl; } m_head = 0; m_tail = 0; m_length = 0; } Myqueue::~Myqueue() { delete[]my_queue; my_queue = NULL; } int Myqueue::get_length() { return m_length; } bool Myqueue::judge_empty() { if (m_length == 0) { cout << "the queue is empty" << endl; return true; } return false; } bool Myqueue::judge_full() { if (m_length == m_capacity) { cout << "the queue is full"<<endl; return true; } return false; } bool Myqueue::in_ele(int x) { if (judge_full()) { return false; } my_queue[m_tail] = x; m_tail++; m_length++; if (m_tail == m_capacity) m_tail = 0; return true; } bool Myqueue::out_ele(int &x) { if (judge_empty()) { return false; } x = my_queue[m_head]; m_head++; m_length=m_length-1; if (m_head == m_capacity) m_head = 0; return true; } void Myqueue::tra_ele() { int num = m_head; for (int i = m_head; i<m_head+m_length; i++) { if (num == m_capacity) { num = 0; } cout << my_queue[num] << endl; num++; } } void Myqueue::clear_queue() { m_head = 0; m_tail = 0; m_length = 0; } int main() { int out_ele; Myqueue *lis =new Myqueue(5); lis->in_ele(11); lis->in_ele(12); lis->in_ele(14); lis->in_ele(8); lis->in_ele(20); lis->tra_ele(); cout << endl; lis->out_ele(out_ele); lis->tra_ele(); cout << endl; lis->out_ele(out_ele); lis->tra_ele(); cout << endl; lis->in_ele(14); lis->in_ele(8); lis->tra_ele(); cout << endl; lis->in_ele(20); return 0; } 查看全部
- 
            
            1.每次队列插入一个新元素,队列长度应该+1。m_iQueueLen++; 每次队列取出一个元素 ,队列长度应该-1。m_iQueueLne--; 2.对于环形队列,经过一圈后又从0开始(因此要求 模%): 入队:m_ ihead =m_ ihead %m_ iQueuecapacity; 出队:m_itail = m_i tail %m_iQueuecapacity; 遍历队列时,是从队头开始遍历的,而有时候队头不是指向位置0,指向其他位置 。另外,对于环形队列,当遍历完一圈后又从0开始。 for(int i = 0; i < m_iQueueLen; ++i) cout << m_pQueue[(i+m_iHead)%m_iQueueCapacity] << endl; 查看全部
- 
            
            插入元素:每次插入之前需要判断队列是否已满。插入元素从队尾开始 插入。每插入一个,队尾指向下一个位置。 取出元素:每次取出之前需要判断队列是否为空。取出元素从队头开始 取出,每取出一个,队头指向下一个位置; 队列中空出的位置,队列为又可以插入新的元素 查看全部
- 
            
            05:47 初始化 申请内存 7:20 析构函数 销毁队列 内存回收 查看全部
- 
            
            数据结构是指相互之间存在一种或多种特定关系的数据元素的集合。 查看全部
- 
            
            队列: FIFO:first in first out 先进先出 查看全部
- 
            
            队列:先入先出FIFO,分为普通队列和环形队列。普通队列的空间利用没有环形队列充分,环形队列的复杂度比普通队列复杂。 查看全部
- 
            
            FIFO查看全部
- 
            
            环形队列的声明 查看全部
- 
            
            先入先出 普通队列:浪费内存(不移动),处理速度慢(各位都前移一位) 环形队列:充分利用内存空间 查看全部
- 
            
            环形队列类声明 查看全部
- 
            
            普通队列示例 查看全部
- 
            
            队列:先进先出 FIFO :first in first out 查看全部
- 
            
            MyQueue.cpp2 查看全部
- 
            
            MyQueue.cpp1 查看全部
举报
 
             
         
                 
                 
                 
                 
                 
                 
                 
                 
                 
                 
                