zoukankan      html  css  js  c++  java
  • 链队列的C/C++实现

    #include <iostream>
    using namespace std;
    
    const int N = 10;
    typedef int ELEMTYPE;
    typedef struct Node
    {
        ELEMTYPE data;
        struct Node * next;
    }LNode;
    
    typedef struct queue
    {
        LNode *head;
        LNode *tail;
    }Queue;
    
    
    void initQueur(Queue &Q);
    int isQueueEmpty(Queue &Q);
    int  enQueue(Queue &Q, ELEMTYPE e);
    int  deQueue(Queue &Q, ELEMTYPE &e);
    void printQueue(Queue &Q);
    
    int main()
    {
        Queue Q;
        initQueur(Q);
        for(int i=1;i<=N;++i)
        {
    
            enQueue(Q,i);
        }
        printQueue(Q);
        return 0;
    }
    
    
    void initQueur(Queue &Q)
    {
    
        Q.head = new LNode;
        Q.head->next = NULL;
        Q.tail = Q.head;
    }
    
    int isQueueEmpty(Queue &Q)
    {
    
        return Q.head == Q.tail;
    }
    
    
    int  enQueue(Queue &Q, ELEMTYPE e)
    {
        LNode *p = new LNode;
        p->data = e;
        p->next = NULL;
    
        Q.tail->next = p;
        Q.tail = p;
    
        return 1;
    }
    int  deQueue(Queue &Q, ELEMTYPE &e)
    {
    
        if(isQueueEmpty(Q)) return 0;
        LNode *p = Q.head->next;
        if(p == Q.tail)
        {
            Q.tail = Q.head;
        }
        else
        {
            Q.head->next = p->next;
        }
    
         e = p->data;
        delete p;
        return 1;
    
    }
    void printQueue(Queue &Q)
    {
    
        ELEMTYPE e;
        while(isQueueEmpty(Q)==0)
        {
            deQueue(Q,e);
            cout<<e<<"	";
        }
    }
    
  • 相关阅读:
    ArcGIS10.3.1于2015年6月发布
    jS数组
    正则表达式
    JS中prototype属性-JS原型模式
    URI, URL, and URN
    JS中的Call和apply
    北京获得2022冬奥会举办权
    JQuery.on()事件绑定
    JavaScript模块化-require.js
    SpringBoot中DataSourceAutoConfiguration注解
  • 原文地址:https://www.cnblogs.com/yldf/p/11900171.html
Copyright © 2011-2022 走看看