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

    队列的链式实现:

    在这个队列里面:r 为低, f 为顶

    //队列(链式)
    
    #include <iostream>
    using namespace std;
    
    typedef int DataType;
    struct QNode
    {
        DataType data;
        struct QNode *link;
    };
    typedef struct QNode *PNode;
    
    //r为低 f为顶
    struct LinkQueue
    {
        PNode f;
        PNode r;
    };
    typedef struct LinkQueue * PLinkQueue;
    
    PLinkQueue createEmptyQueue()
    {
        PLinkQueue plqueue = (PLinkQueue) malloc (sizeof(struct LinkQueue));
        if(plqueue != NULL)
        {
            plqueue ->f = NULL;
            plqueue ->r =NULL;
        }
        else
            printf("Out of space
    ");
        return plqueue;
    }
    
    int isEmptyQueue(PLinkQueue plqueue)
    {
        return (plqueue ->f == NULL);
    }
    
    void enQueue( PLinkQueue plqueue, DataType x)
    {
        PNode p = (PNode) malloc (sizeof(struct QNode));
        if(p!= NULL)
        {
            p->data = x;
            p->link = NULL;
    
            if(plqueue ->f == NULL)
                plqueue->f = p;
            else
                plqueue ->r -> link = p;
    
            plqueue->r =p;
        }
        else
            printf("Out of space
    ");
    
    }
    
    void deQueue(PLinkQueue plqueue)
    {
        PNode p = (PNode) malloc (sizeof(struct QNode));
        if(plqueue->f == NULL)
            printf("Empty Queue
    ");
        else
        {
            p = plqueue ->f;
            plqueue -> f = p ->link;
            free(p);
        }
    
    }
    
    DataType getFront(PLinkQueue plqueue)
    {
        if(plqueue ->f == NULL)
            printf("Empty Queue
    ");
        else
            return (plqueue ->f ->data);
    }
    int main()
    {
        PLinkQueue lqueue = createEmptyQueue();
        cout<<"创建一个n元素的队列
    输入n"<<endl;
        int n,t;
        cin>>n;
        t = n;
        while(n)
        {
            int data;
            cin>>data;
            enQueue(lqueue,data);
            n--;
        }
        cout<<"取队头并出队"<<endl;
        while(t)
        {
            cout<<getFront(lqueue)<<" ";
            deQueue(lqueue);
            t--;
        }
        cout<<endl;
        system("pause");
        return 0;
    }
  • 相关阅读:
    MySQL表之间的关系概述
    网路通信简介
    多态与鸭子类型
    组合与类继承
    类与对象的属性与方法以及封装
    对象与类的初始
    2018.12.12
    2018.12.9浮动布局,盒子显隐,定位,z-index,流式布局,小米开头
    2018.12.8浮动布局,display总结,overflow,清浮动
    2018.12.7边界圆角redius,背景图设置,平铺,精灵图,盒子伪类索引
  • 原文地址:https://www.cnblogs.com/tianjintou/p/4605153.html
Copyright © 2011-2022 走看看