• 队列的链式实现


    队列的链式实现:

    在这个队列里面: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;
    }
  • 相关阅读:
    水波模拟算法
    火车调度问题
    讨论范式
    字符串编码传输
    意识的物质,物质的意识
    需求分析——项目日志管理系统
    委托揭秘
    [9]OCP:开放封闭原则
    NULL OBJECT 模式
    由《通用权限设计》而引发的随想
  • 原文地址:https://www.cnblogs.com/tianjintou/p/4605153.html
Copyright © 2020-2023  润新知