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

    #include<iostream>
    using namespace std;
    struct LinkQueueNode
    {
         LinkQueueNode* nextIn;
         int value;
    };
    
    struct LinkQueue
    {
        LinkQueueNode* front;
        LinkQueueNode* rear;
        bool isEmpty;
        int cnt;
    };
     
    LinkQueue* createLinkQueue()
    {
        LinkQueueNode* head = (LinkQueueNode* )malloc(sizeof(LinkQueueNode));
        LinkQueue* link =(LinkQueue*)malloc(sizeof(LinkQueue));
        link->front=head;
        link->rear=head;
        link->isEmpty=true;
        link->cnt=0;
        return link;
    }
    void InQueue(LinkQueue* q, int value)
    {
        LinkQueueNode* newIn = (LinkQueueNode* )malloc(sizeof(LinkQueueNode));
        newIn->nextIn=NULL;
        newIn->value=value;
        q->rear->nextIn=newIn;
        q->rear=newIn;
        q->isEmpty=((++q->cnt)==0);
    }
    bool Dequeue(LinkQueue* q, int* value)
    {
        if(!q->isEmpty)
        {
            LinkQueueNode* tep = q->front->nextIn;
            *value = tep->value;
            q->front->nextIn=tep->nextIn;
            q->isEmpty=((--q->cnt)==0);
            if(q->isEmpty)
                q->rear=q->front;///当清空的时候,要重新设置rear
            delete tep;
            return 1;
        }
        return 0;
    }
    bool front(LinkQueue* q, int* value)
    {
        if(!q->isEmpty)
        {
            LinkQueueNode* tep = q->front->nextIn;
            *value = tep->value;
            return 1;
        }
        return 0;
    }
    void outPut(LinkQueue* q)
    {
        LinkQueueNode* p = q->front->nextIn;
        while(p!=NULL)
        {
            cout<<p->value<<" ";
            p=p->nextIn;
        }
        cout<<endl;
    }
    void clearLinkQueue(LinkQueue* q)
    {
        int x;
        while(!q->isEmpty)
        {
             Dequeue(q,&x);
             cout<<x<<" ";
        }
        cout<<endl;
    }
    void main()
    {
        int len=10;
        LinkQueue* q = createLinkQueue();
        int v;
        for(int i=0;i<len;i++)
        {
             v = rand() % 100;
             cout<<v<<" ";
             InQueue(q,v);
    
        }
        cout<<endl;
        outPut(q);
        clearLinkQueue(q);
    
        for(int i=0;i<len;i++)
        {
             v = rand() % 100;
             cout<<v<<" ";
             InQueue(q,v);
    
        }
        cout<<endl;
        outPut(q);
        
        Dequeue(q,&v);
        cout<<v<<endl;
        outPut(q);
    
        front(q,&v);
        cout<<v<<endl;
        outPut(q);
    
        Dequeue(q,&v);
        cout<<v<<endl;
        outPut(q);
    
        Dequeue(q,&v);
        cout<<v<<endl;
        outPut(q);
    
        for(int i=0;i<5;i++)
        {
             v = rand() % 100;
             cout<<v<<" ";
             InQueue(q,v);
    
        }
        cout<<endl;
        outPut(q);
        if(!front(q,&v))
            cout<<"fail"<<endl;
        else outPut(q);
    
        clearLinkQueue(q);
    
        if(!front(q,&v))
            cout<<"fail"<<endl;
        else outPut(q);
    
        if(!front(q,&v))
            cout<<"fail"<<endl;
        else outPut(q);
    
        cin>>len;
    }
  • 相关阅读:
    小程序动态添加input(一)
    vue样式穿透
    小程序判断用户是否授权位置信息
    【超详细】MySQL学习笔记汇总(四)之排序查询
    【超详细】MySQL学习笔记汇总(三)之进阶1、2测试
    【超详细】MySQL学习笔记汇总(二)之条件查询
    【超详细】MySQL学习笔记汇总(一)之基础查询
    【超详细】MakeDown(Typora)+PicGo+Gitee实现图床
    JavaDOC生成文档
    学习Hive遇到的问题
  • 原文地址:https://www.cnblogs.com/kbyd/p/3991802.html
Copyright © 2011-2022 走看看