zoukankan      html  css  js  c++  java
  • c++实现队列

        参加“去哪儿”面试的一道题,面试官很严格,很注意细节,而我昨天从进门开始就犯迷糊,代码也写得一塌糊涂,回来后查了资料看了几遍,自己又写了几遍。这次面试得到的教训是:自己会的东西一定要懂得彻底,不能被问倒,尤其是基础的问题,更不能出现这种大概知道,细节实现不好的问题。

    LinkQueue.h

    #include<iostream>
    using namespace std;
    
    template <typename T>
    struct Node{
        Node(T &d){
            data=d;
            next=NULL;
        }
        T data;
        Node *next;
    };
    
    template <typename T>
    class LinkQueue{
        private:
            int length;
            Node<T> *front;
            Node<T> *rear;
        public:
            LinkQueue(T &n){
                Node <T> *p=new Node<T>(n);
                length=0;
                front=rear=p;
            }
            bool Queuelength()
            {
                cout<<"当前队列长度:"<<length<<endl;
                return true;
            }
            bool IsEmpty(){
                return length==0;
            }
            void EnQueue(T n)
            {
                Node<T> *p=new Node<T>(n);
                rear->next=p;
                rear=p;
                length++;
            }
            bool DelQueue(){
                if(front==rear)
                  return false;
                Node<T> *p=front->next;
                front->next=p->next;
                if(front->next==NULL)
                    rear=front;
                delete p;
                length--;
                return true;
            }
            
            void Tranverse()
            {
                Node<T> *p=front->next;
                
                cout<<"遍历队列:"<<endl;
                while(p!=NULL)
                {
                    cout<<p->data<<" ";
                    p=p->next;
                }
                cout<<endl;
            }
    };

    LinkQueue.cpp

    #include "LinkQueue.h"
    
    int main(){
        int head=0;
        LinkQueue<int> *lq=new LinkQueue<int>(head);
        lq->EnQueue(2);
        lq->EnQueue(4);
        lq->EnQueue(6);
        cout<<"当前队列"<<endl;
        lq->Tranverse();
        if(lq->DelQueue()){
            cout<<"出队成功"<<endl; 
        }
        lq-> Queuelength();
        lq->Tranverse();
            lq->EnQueue(16);
        lq->EnQueue(18);
        lq->EnQueue(19);
        if(lq->DelQueue()){
            cout<<"出队成功"<<endl; 
        }
        lq-> Queuelength();
        lq->Tranverse();
        if(lq->DelQueue()){
            cout<<"出队成功"<<endl; 
        }
        lq-> Queuelength();
        lq->Tranverse();
        return 0;
    }
  • 相关阅读:
    nio非阻塞缓存流
    java高级---->Thread之ScheduledExecutorService的使用
    ScheduledExecutorService-spring-定时周期执行指定的任务
    常用数据库连接池 (DBCP、c3p0、Druid) 配置说明
    Linux定时任务Crontab命令详解
    mybatis xml查询语句大全
    mysql索引研究
    关于HttpClient重试策略的研究
    对于org.apache.commons.dbcp.BasicDataSource的配置认知
    Linux dos2unix命令
  • 原文地址:https://www.cnblogs.com/lanying/p/4889399.html
Copyright © 2011-2022 走看看