zoukankan      html  css  js  c++  java
  • 数据结构(3) -- 队列

    1、线性存储

    //Queue.h
    #ifndef QUEUE_H_
    #define QUEUE_H_
    #define ElemType int 
    #define MAXSIZE 100
    typedef struct _Node
    {
        ElemType data[MAXSIZE];
        int front;
        int rear;
        
    }Node;
    
    class Queue
    {
    private:
        Node data;
    public:
        Queue();
        ~Queue();
        void EnQueue(ElemType e);
        ElemType DeQueue();
        int IsFull();
        int IsEmpty();
    };
    
    #endif
    
    
    //Queue.cpp
    #include "Queue.h"
    #include <iostream>
    
    Queue::Queue()
    {
        data.front = 0;
        data.rear = 0;
    }
    
    Queue::~Queue(){}
    
    void Queue::EnQueue(ElemType e)
    {
        if (IsFull())
        {
            std::cout << "队列已满" << std::endl;
            return;
        }
        data.rear = (data.rear + 1) % MAXSIZE;
        data.data[data.rear] = e;
    }
    
    ElemType Queue::DeQueue()
    {
        if (IsEmpty())
        {
            std::cout << "队列已空" << std::endl;
            return 0;
        }
        data.front = (data.front + 1) % MAXSIZE;
        return data.data[data.front];
    }
    
    int Queue::IsFull()
    {
        if (data.front % MAXSIZE == (data.rear + 1) % MAXSIZE)
            return 1;
        else
            return 0;
    }
    
    int Queue::IsEmpty()
    {
        if (data.front == data.rear)
            return 1;
        else
            return 0;
    }

    2、链式存储

    //PQueue.h
    #ifndef PQUEUE_H_
    #define PQUEUE_H_
    #define ElemType int 
    #define MAXSIZE 100
    
    typedef struct _PNode
    {
        ElemType data;
        _PNode *next;
        
    }PNode;
    
    class PQueue
    {
    private:
        PNode *front;
        PNode *rear;
    
    public:
        PQueue();
        ~PQueue();
        void EnQueue(ElemType e);
        ElemType DeQueue();
        int IsFull();
        int IsEmpty();
    };
    
    #endif
    
    
    //PQueue.cpp
    #include "PQueue.h"
    #include <iostream>
    
    PQueue::PQueue()
    {
        front = new PNode();
        rear = front;
        front->next = NULL;
    }
    
    PQueue::~PQueue()
    {
        delete front;
        delete rear;
    }
    
    void PQueue::EnQueue(ElemType e)
    {
        PNode *s = new PNode();
        s->data = e;
        rear->next = s;
        s->next = NULL;
        rear = s;
    }
    
    ElemType PQueue::DeQueue()
    {
        if (IsEmpty())
        {
            std::cout << "队列已空" << std::endl;
            return 0;
        }
        PNode *s;
        s = front;
        ElemType t = s->next->data;
        front = front->next;
        delete s;
        return t;
    }
    
    int PQueue::IsEmpty()
    {
        if (rear == front)
            return 1;
        else
            return 0;
    }

    3、测试用例

    #include <iostream>
    #include "PQueue.h"
    #include <string>
    using namespace std;
    
    
    
    int main()
    {
        PQueue q;
        for (int i = 0; i < 6; i++)
            q.EnQueue(i);
        for (int i = 0; i < 6; i++)
            cout << q.DeQueue();
        system("pause");
        return 0;
    }
  • 相关阅读:
    一天一个 Linux 命令(44):ifstat 命令
    Java集合框架示意图
    Java中String类常见问题汇总
    一天一个 Linux 命令(43):netstat 命令
    Windows系统下,如何设置maven字符编码
    Java文件操作编程
    Java 注解(Annotation)
    Linux Centos7.4 更新Java jdk版本
    Java基础(6)Java数据类型扩展
    Windows系统下Elasticsearch7.15.2单服务器配置多节点
  • 原文地址:https://www.cnblogs.com/yongssu/p/4395206.html
Copyright © 2011-2022 走看看